Skip to content

Commit

Permalink
Binary Subarrays With Sum
Browse files Browse the repository at this point in the history
  • Loading branch information
hikjik committed Mar 14, 2024
1 parent aa9251b commit e19ab50
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 1 deletion.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ add_task(binary-prefix-divisible-by-5)
add_task(binary-search)
add_task(binary-search-tree-iterator)
add_task(binary-search-tree-to-greater-sum-tree)
add_task(binary-subarrays-with-sum)
add_task(binary-tree-cameras)
add_task(binary-tree-inorder-traversal)
add_task(binary-tree-level-order-traversal)
Expand Down
1 change: 1 addition & 0 deletions PROBLEM_LIST.md
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,7 @@
| 922. | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [C++](./solutions/sort-array-by-parity-ii/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/Sorting-333b7f?style=flat-square'/> | |
| 925. | [Long Pressed Name](https://leetcode.com/problems/long-pressed-name/) | [C++](./solutions/long-pressed-name/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Two Pointers-7f6633?style=flat-square'/> <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> | |
| 929. | [Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/) | [C++](./solutions/unique-email-addresses/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/String-7f334c?style=flat-square'/> | |
| 930. | [Binary Subarrays With Sum](https://leetcode.com/problems/binary-subarrays-with-sum/) | [C++](./solutions/binary-subarrays-with-sum/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N) / O(1)| <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/Prefix Sum-777f33?style=flat-square'/> | |
| 931. | [Minimum Falling Path Sum](https://leetcode.com/problems/minimum-falling-path-sum/) | [C++](./solutions/minimum-falling-path-sum/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N<sup>2</sup>) / O(N)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Dynamic Programming-37337f?style=flat-square'/> <img src='https://img.shields.io/badge/Matrix-5e337f?style=flat-square'/> | |
| 933. | [Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/) | [C++](./solutions/number-of-recent-calls/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Design-51337f?style=flat-square'/> <img src='https://img.shields.io/badge/Queue-6b337f?style=flat-square'/> <img src='https://img.shields.io/badge/Data Stream-4a7f33?style=flat-square'/> | |
| 934. | [Shortest Bridge](https://leetcode.com/problems/shortest-bridge/) | [C++](./solutions/shortest-bridge/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/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/Matrix-5e337f?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 @@ -801,6 +801,7 @@ Due to [restrictions](https://github.com/orgs/community/discussions/23920) on th
| 922. | [Sort Array By Parity II](https://leetcode.com/problems/sort-array-by-parity-ii/) | [C++](./solutions/sort-array-by-parity-ii/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/Sorting-333b7f?style=flat-square'/> | |
| 925. | [Long Pressed Name](https://leetcode.com/problems/long-pressed-name/) | [C++](./solutions/long-pressed-name/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Two Pointers-7f6633?style=flat-square'/> <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> | |
| 929. | [Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/) | [C++](./solutions/unique-email-addresses/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/String-7f334c?style=flat-square'/> | |
| 930. | [Binary Subarrays With Sum](https://leetcode.com/problems/binary-subarrays-with-sum/) | [C++](./solutions/binary-subarrays-with-sum/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N) / O(1)| <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/Prefix Sum-777f33?style=flat-square'/> | |
| 931. | [Minimum Falling Path Sum](https://leetcode.com/problems/minimum-falling-path-sum/) | [C++](./solutions/minimum-falling-path-sum/solution.hpp) | <img src='https://img.shields.io/badge/Medium-darkorange?style=flat-square'/> | O(N<sup>2</sup>) / O(N)| <img src='https://img.shields.io/badge/Array-57337f?style=flat-square'/> <img src='https://img.shields.io/badge/Dynamic Programming-37337f?style=flat-square'/> <img src='https://img.shields.io/badge/Matrix-5e337f?style=flat-square'/> | |
| 933. | [Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/) | [C++](./solutions/number-of-recent-calls/solution.hpp) | <img src='https://img.shields.io/badge/Easy-darkgreen?style=flat-square'/> | | <img src='https://img.shields.io/badge/Design-51337f?style=flat-square'/> <img src='https://img.shields.io/badge/Queue-6b337f?style=flat-square'/> <img src='https://img.shields.io/badge/Data Stream-4a7f33?style=flat-square'/> | |
| 934. | [Shortest Bridge](https://leetcode.com/problems/shortest-bridge/) | [C++](./solutions/shortest-bridge/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/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/Matrix-5e337f?style=flat-square'/> | |
Expand Down Expand Up @@ -1095,4 +1096,3 @@ Due to [restrictions](https://github.com/orgs/community/discussions/23920) on th
| 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) |
| 1389. | [Create Target Array in the Given Order](https://leetcode.com/problems/create-target-array-in-the-given-order/) | [C++](./solutions/create-target-array-in-the-given-order/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/Simulation-7e7f33?style=flat-square'/> | |
| 1392. | [Longest Happy Prefix](https://leetcode.com/problems/longest-happy-prefix/) | [C++](./solutions/longest-happy-prefix/solution.hpp) | <img src='https://img.shields.io/badge/Hard-darkred?style=flat-square'/> | O(N) / O(1)| <img src='https://img.shields.io/badge/String-7f334c?style=flat-square'/> <img src='https://img.shields.io/badge/Rolling Hash-6b7f33?style=flat-square'/> <img src='https://img.shields.io/badge/String Matching-7f3346?style=flat-square'/> <img src='https://img.shields.io/badge/Hash Function-647f33?style=flat-square'/> | |
1 change: 1 addition & 0 deletions solutions/binary-subarrays-with-sum/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_catch(test_binary_subarrays_with_sum test.cpp)
54 changes: 54 additions & 0 deletions solutions/binary-subarrays-with-sum/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#pragma once

#include <unordered_map>
#include <vector>

// Time: O(N)
// Space: O(1)

namespace ps {

// Time: O(N)
// Space: O(N)
class Solution {
public:
static int numSubarraysWithSum(const std::vector<int> &nums, int goal) {
int ans = 0;
std::unordered_map<int, int> map{{0, 1}};
for (int sum = 0; auto num : nums) {
sum += num;
if (auto it = map.find(sum - goal); it != map.end()) {
ans += it->second;
}
++map[sum];
}
return ans;
}
};

} // namespace ps

namespace sw {

// Time: O(N)
// Space: O(1)
class Solution {
public:
static int numSubarraysWithSum(const std::vector<int> &nums, int goal) {
int ans = 0;
int sum = 0, zeros = 0;
for (int l = 0, r = 0; r < std::ssize(nums); ++r) {
sum += nums[r];
while (l < r && (sum > goal || nums[l] == 0)) {
zeros = nums[l] ? 0 : zeros + 1;
sum -= nums[l++];
}
if (sum == goal) {
ans += 1 + zeros;
}
}
return ans;
}
};

} // namespace sw
38 changes: 38 additions & 0 deletions solutions/binary-subarrays-with-sum/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <catch.hpp>

#include <solution.hpp>

TEST_CASE("Simple") {
struct TestCase {
std::vector<int> nums;
int goal;
int expected;
};

std::vector<TestCase> test_cases{
{
.nums{1, 0, 1, 0, 1},
.goal = 2,
.expected = 4,
},
{
.nums{0, 0, 0, 0, 0},
.goal = 0,
.expected = 15,
},
};

SECTION("Prefix Sum") {
for (const auto &[nums, goal, expected] : test_cases) {
const auto actual = ps::Solution::numSubarraysWithSum(nums, goal);
REQUIRE(expected == actual);
}
}

SECTION("Sliding Window") {
for (const auto &[nums, goal, expected] : test_cases) {
const auto actual = sw::Solution::numSubarraysWithSum(nums, goal);
REQUIRE(expected == actual);
}
}
}

0 comments on commit e19ab50

Please sign in to comment.