-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
add_catch(test_subarrays_with_k_different_integers test.cpp) |
29 changes: 29 additions & 0 deletions
29
solutions/subarrays-with-k-different-integers/solution.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#pragma once | ||
|
||
#include <vector> | ||
|
||
// Time: O(N) | ||
// Space: O(N) | ||
|
||
class Solution { | ||
public: | ||
static int subarraysWithKDistinct(std::vector<int> &nums, int k) { | ||
return DistinctAtMostK(nums, k) - DistinctAtMostK(nums, k - 1); | ||
} | ||
|
||
private: | ||
static int DistinctAtMostK(const std::vector<int> &nums, int k) { | ||
std::vector<int> counter(nums.size() + 1); | ||
int distinct_count = 0; | ||
|
||
int ans = 0; | ||
for (int l = 0, r = 0; r < std::ssize(nums); ++r) { | ||
distinct_count += counter[nums[r]]++ == 0; | ||
while (distinct_count > k) { | ||
distinct_count -= counter[nums[l++]]-- == 1; | ||
} | ||
ans += r - l + 1; | ||
} | ||
return ans; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include <catch.hpp> | ||
|
||
#include <solution.hpp> | ||
|
||
TEST_CASE("Simple") { | ||
struct TestCase { | ||
std::vector<int> nums; | ||
int k; | ||
int expected; | ||
}; | ||
|
||
std::vector<TestCase> test_cases{ | ||
{ | ||
.nums{1, 2, 1, 2, 3}, | ||
.k = 2, | ||
.expected = 7, | ||
}, | ||
{ | ||
.nums{1, 2, 1, 3, 4}, | ||
.k = 3, | ||
.expected = 3, | ||
}, | ||
}; | ||
|
||
for (const auto &[nums, k, expected] : test_cases) { | ||
const auto actual = Solution::subarraysWithKDistinct(nums, k); | ||
REQUIRE(expected == actual); | ||
} | ||
} |