Skip to content

Commit

Permalink
Solve Daily Problem
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Mar 30, 2024
1 parent 74c98ac commit cef9ca0
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1477,6 +1477,7 @@ add_task(student-attendance-record-i)
add_task(subarray-product-less-than-k)
add_task(subarray-sum-equals-k)
add_task(subarrays-distinct-element-sum-of-squares-i)
add_task(subarrays-with-k-different-integers)
add_task(subdomain-visit-count)
add_task(subrectangle-queries)
add_task(subsets)
Expand Down
1 change: 1 addition & 0 deletions PROBLEM_LIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@
| 987. | [Vertical Order Traversal of a Binary Tree](https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/) | [C++](./solutions/vertical-order-traversal-of-a-binary-tree/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | O(NlogN) / O(N)| <img src='https://img.shields.io/badge/Hash Table-7f337a?style=flat-square'/> <img src='https://img.shields.io/badge/Tree-7f3373?style=flat-square'/> <img src='https://img.shields.io/badge/Depth First Search-337f35?style=flat-square'/> <img src='https://img.shields.io/badge/Breadth First Search-377f33?style=flat-square'/> <img src='https://img.shields.io/badge/Binary Tree-7f336d?style=flat-square'/> | |
| 989. | [Add to Array-Form of Integer](https://leetcode.com/problems/add-to-array-form-of-integer/) | [C++](./solutions/add-to-array-form-of-integer/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Math-7f5933?style=flat-square'/> | |
| 990. | [Satisfiability of Equality Equations](https://leetcode.com/problems/satisfiability-of-equality-equations/) | [C++](./solutions/satisfiability-of-equality-equations/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N) / O(A)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> <img src='https://img.shields.io/badge/Union Find-337f3b?style=flat-square'/> <img src='https://img.shields.io/badge/Graph-3d7f33?style=flat-square'/> | |
| 992. | [Subarrays with K Different Integers](https://leetcode.com/problems/subarrays-with-k-different-integers/) | [C++](./solutions/subarrays-with-k-different-integers/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | O(N) / O(N)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Hash Table-7f337a?style=flat-square'/> <img src='https://img.shields.io/badge/Sliding Window-7f6033?style=flat-square'/> <img src='https://img.shields.io/badge/Counting-7f7333?style=flat-square'/> | |
| 993. | [Cousins in Binary Tree](https://leetcode.com/problems/cousins-in-binary-tree/) | [C++](./solutions/cousins-in-binary-tree/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Tree-7f3373?style=flat-square'/> <img src='https://img.shields.io/badge/Depth First Search-337f35?style=flat-square'/> <img src='https://img.shields.io/badge/Breadth First Search-377f33?style=flat-square'/> <img src='https://img.shields.io/badge/Binary Tree-7f336d?style=flat-square'/> | |
| 994. | [Rotting Oranges](https://leetcode.com/problems/rotting-oranges/) | [C++](./solutions/rotting-oranges/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Breadth First Search-377f33?style=flat-square'/> <img src='https://img.shields.io/badge/Matrix-5e337f?style=flat-square'/> | |
| 997. | [Find the Town Judge](https://leetcode.com/problems/find-the-town-judge/) | [C++](./solutions/find-the-town-judge/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Hash Table-7f337a?style=flat-square'/> <img src='https://img.shields.io/badge/Graph-3d7f33?style=flat-square'/> | |
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,7 @@ Due to [restrictions](https://github.com/orgs/community/discussions/23920) on th
| 987. | [Vertical Order Traversal of a Binary Tree](https://leetcode.com/problems/vertical-order-traversal-of-a-binary-tree/) | [C++](./solutions/vertical-order-traversal-of-a-binary-tree/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | O(NlogN) / O(N)| <img src='https://img.shields.io/badge/Hash Table-7f337a?style=flat-square'/> <img src='https://img.shields.io/badge/Tree-7f3373?style=flat-square'/> <img src='https://img.shields.io/badge/Depth First Search-337f35?style=flat-square'/> <img src='https://img.shields.io/badge/Breadth First Search-377f33?style=flat-square'/> <img src='https://img.shields.io/badge/Binary Tree-7f336d?style=flat-square'/> | |
| 989. | [Add to Array-Form of Integer](https://leetcode.com/problems/add-to-array-form-of-integer/) | [C++](./solutions/add-to-array-form-of-integer/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Math-7f5933?style=flat-square'/> | |
| 990. | [Satisfiability of Equality Equations](https://leetcode.com/problems/satisfiability-of-equality-equations/) | [C++](./solutions/satisfiability-of-equality-equations/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N) / O(A)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> <img src='https://img.shields.io/badge/Union Find-337f3b?style=flat-square'/> <img src='https://img.shields.io/badge/Graph-3d7f33?style=flat-square'/> | |
| 992. | [Subarrays with K Different Integers](https://leetcode.com/problems/subarrays-with-k-different-integers/) | [C++](./solutions/subarrays-with-k-different-integers/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | O(N) / O(N)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Hash Table-7f337a?style=flat-square'/> <img src='https://img.shields.io/badge/Sliding Window-7f6033?style=flat-square'/> <img src='https://img.shields.io/badge/Counting-7f7333?style=flat-square'/> | |
| 993. | [Cousins in Binary Tree](https://leetcode.com/problems/cousins-in-binary-tree/) | [C++](./solutions/cousins-in-binary-tree/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Tree-7f3373?style=flat-square'/> <img src='https://img.shields.io/badge/Depth First Search-337f35?style=flat-square'/> <img src='https://img.shields.io/badge/Breadth First Search-377f33?style=flat-square'/> <img src='https://img.shields.io/badge/Binary Tree-7f336d?style=flat-square'/> | |
| 994. | [Rotting Oranges](https://leetcode.com/problems/rotting-oranges/) | [C++](./solutions/rotting-oranges/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Breadth First Search-377f33?style=flat-square'/> <img src='https://img.shields.io/badge/Matrix-5e337f?style=flat-square'/> | |
| 997. | [Find the Town Judge](https://leetcode.com/problems/find-the-town-judge/) | [C++](./solutions/find-the-town-judge/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Hash Table-7f337a?style=flat-square'/> <img src='https://img.shields.io/badge/Graph-3d7f33?style=flat-square'/> | |
Expand Down Expand Up @@ -1095,4 +1096,3 @@ Due to [restrictions](https://github.com/orgs/community/discussions/23920) on th
| 1382. | [Balance a Binary Search Tree](https://leetcode.com/problems/balance-a-binary-search-tree/) | [C++](./solutions/balance-a-binary-search-tree/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N) / O(N)| <img src='https://img.shields.io/badge/Divide and Conquer-717f33?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> <img src='https://img.shields.io/badge/Tree-7f3373?style=flat-square'/> <img src='https://img.shields.io/badge/Depth First Search-337f35?style=flat-square'/> <img src='https://img.shields.io/badge/Binary Search Tree-7f3366?style=flat-square'/> <img src='https://img.shields.io/badge/Binary Tree-7f336d?style=flat-square'/> | |
| 1383. | [Maximum Performance of a Team](https://leetcode.com/problems/maximum-performance-of-a-team/) | [C++](./solutions/maximum-performance-of-a-team/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Greedy-44337f?style=flat-square'/> <img src='https://img.shields.io/badge/Sorting-333b7f?style=flat-square'/> <img src='https://img.shields.io/badge/Heap (Priority Queue)-71337f?style=flat-square'/> | |
| 1385. | [Find the Distance Value Between Two Arrays](https://leetcode.com/problems/find-the-distance-value-between-two-arrays/) | [C++](./solutions/find-the-distance-value-between-two-arrays/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Two Pointers-7f6633?style=flat-square'/> <img src='https://img.shields.io/badge/Binary Search-33557f?style=flat-square'/> <img src='https://img.shields.io/badge/Sorting-333b7f?style=flat-square'/> | |
| 1387. | [Sort Integers by The Power Value](https://leetcode.com/problems/sort-integers-by-the-power-value/) | [C++](./solutions/sort-integers-by-the-power-value/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(NlogN&nbsp;+&nbsp;NX) / O(N)| <img src='https://img.shields.io/badge/Dynamic Programming-37337f?style=flat-square'/> <img src='https://img.shields.io/badge/Memoization-33357f?style=flat-square'/> <img src='https://img.shields.io/badge/Sorting-333b7f?style=flat-square'/> | [Collatz conjecture](https://w.wiki/329) |
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 solutions/subarrays-with-k-different-integers/solution.hpp
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;
}
};
29 changes: 29 additions & 0 deletions solutions/subarrays-with-k-different-integers/test.cpp
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);

Check failure on line 26 in solutions/subarrays-with-k-different-integers/test.cpp

View workflow job for this annotation

GitHub Actions / Unit Tests (RelWithDebInfo, ubuntu-22.04)

binding reference of type ‘std::vector<int>&’ to ‘const std::vector<int>’ discards qualifiers
REQUIRE(expected == actual);
}
}

0 comments on commit cef9ca0

Please sign in to comment.