-
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
96 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_binary_subarrays_with_sum test.cpp) |
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,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 |
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,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); | ||
} | ||
} | ||
} |