From e19ab50fa81d0617d3fb3f6e33c6cd909169ef75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=BE=D1=80=D0=B5=D0=B2=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80?= Date: Thu, 14 Mar 2024 18:50:48 +0300 Subject: [PATCH] Binary Subarrays With Sum --- CMakeLists.txt | 1 + PROBLEM_LIST.md | 1 + README.md | 2 +- .../binary-subarrays-with-sum/CMakeLists.txt | 1 + .../binary-subarrays-with-sum/solution.hpp | 54 +++++++++++++++++++ solutions/binary-subarrays-with-sum/test.cpp | 38 +++++++++++++ 6 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 solutions/binary-subarrays-with-sum/CMakeLists.txt create mode 100644 solutions/binary-subarrays-with-sum/solution.hpp create mode 100644 solutions/binary-subarrays-with-sum/test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index ff0d3bcd..fecc1ffd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) diff --git a/PROBLEM_LIST.md b/PROBLEM_LIST.md index ba1d8f81..231063cf 100644 --- a/PROBLEM_LIST.md +++ b/PROBLEM_LIST.md @@ -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) | | | | | | 925. | [Long Pressed Name](https://leetcode.com/problems/long-pressed-name/) | [C++](./solutions/long-pressed-name/solution.hpp) | | | | | | 929. | [Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/) | [C++](./solutions/unique-email-addresses/solution.hpp) | | | | | +| 930. | [Binary Subarrays With Sum](https://leetcode.com/problems/binary-subarrays-with-sum/) | [C++](./solutions/binary-subarrays-with-sum/solution.hpp) | | O(N) / O(1)| | | | 931. | [Minimum Falling Path Sum](https://leetcode.com/problems/minimum-falling-path-sum/) | [C++](./solutions/minimum-falling-path-sum/solution.hpp) | | O(N2) / O(N)| | | | 933. | [Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/) | [C++](./solutions/number-of-recent-calls/solution.hpp) | | | | | | 934. | [Shortest Bridge](https://leetcode.com/problems/shortest-bridge/) | [C++](./solutions/shortest-bridge/solution.hpp) | | | | | diff --git a/README.md b/README.md index b08a948b..12e9fdc3 100644 --- a/README.md +++ b/README.md @@ -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) | | | | | | 925. | [Long Pressed Name](https://leetcode.com/problems/long-pressed-name/) | [C++](./solutions/long-pressed-name/solution.hpp) | | | | | | 929. | [Unique Email Addresses](https://leetcode.com/problems/unique-email-addresses/) | [C++](./solutions/unique-email-addresses/solution.hpp) | | | | | +| 930. | [Binary Subarrays With Sum](https://leetcode.com/problems/binary-subarrays-with-sum/) | [C++](./solutions/binary-subarrays-with-sum/solution.hpp) | | O(N) / O(1)| | | | 931. | [Minimum Falling Path Sum](https://leetcode.com/problems/minimum-falling-path-sum/) | [C++](./solutions/minimum-falling-path-sum/solution.hpp) | | O(N2) / O(N)| | | | 933. | [Number of Recent Calls](https://leetcode.com/problems/number-of-recent-calls/) | [C++](./solutions/number-of-recent-calls/solution.hpp) | | | | | | 934. | [Shortest Bridge](https://leetcode.com/problems/shortest-bridge/) | [C++](./solutions/shortest-bridge/solution.hpp) | | | | | @@ -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) | | | | | | 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) | | O(NlogN + NX) / O(N)| | [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) | | | | | -| 1392. | [Longest Happy Prefix](https://leetcode.com/problems/longest-happy-prefix/) | [C++](./solutions/longest-happy-prefix/solution.hpp) | | O(N) / O(1)| | | diff --git a/solutions/binary-subarrays-with-sum/CMakeLists.txt b/solutions/binary-subarrays-with-sum/CMakeLists.txt new file mode 100644 index 00000000..b512d46e --- /dev/null +++ b/solutions/binary-subarrays-with-sum/CMakeLists.txt @@ -0,0 +1 @@ +add_catch(test_binary_subarrays_with_sum test.cpp) diff --git a/solutions/binary-subarrays-with-sum/solution.hpp b/solutions/binary-subarrays-with-sum/solution.hpp new file mode 100644 index 00000000..d2f7281f --- /dev/null +++ b/solutions/binary-subarrays-with-sum/solution.hpp @@ -0,0 +1,54 @@ +#pragma once + +#include +#include + +// Time: O(N) +// Space: O(1) + +namespace ps { + +// Time: O(N) +// Space: O(N) +class Solution { +public: + static int numSubarraysWithSum(const std::vector &nums, int goal) { + int ans = 0; + std::unordered_map 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 &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 diff --git a/solutions/binary-subarrays-with-sum/test.cpp b/solutions/binary-subarrays-with-sum/test.cpp new file mode 100644 index 00000000..e4515d87 --- /dev/null +++ b/solutions/binary-subarrays-with-sum/test.cpp @@ -0,0 +1,38 @@ +#include + +#include + +TEST_CASE("Simple") { + struct TestCase { + std::vector nums; + int goal; + int expected; + }; + + std::vector 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); + } + } +}