Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f8957e3

Browse files
committedMar 26, 2025
Add solution #930
1 parent b32d120 commit f8957e3

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,7 @@
739739
927|[Three Equal Parts](./solutions/0927-three-equal-parts.js)|Hard|
740740
928|[Minimize Malware Spread II](./solutions/0928-minimize-malware-spread-ii.js)|Hard|
741741
929|[Unique Email Addresses](./solutions/0929-unique-email-addresses.js)|Easy|
742+
930|[Binary Subarrays With Sum](./solutions/0930-binary-subarrays-with-sum.js)|Medium|
742743
933|[Number of Recent Calls](./solutions/0933-number-of-recent-calls.js)|Easy|
743744
937|[Reorder Data in Log Files](./solutions/0937-reorder-data-in-log-files.js)|Medium|
744745
966|[Vowel Spellchecker](./solutions/0966-vowel-spellchecker.js)|Medium|
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 930. Binary Subarrays With Sum
3+
* https://leetcode.com/problems/binary-subarrays-with-sum/
4+
* Difficulty: Medium
5+
*
6+
* Given a binary array nums and an integer goal, return the number of non-empty subarrays
7+
* with a sum goal.
8+
*
9+
* A subarray is a contiguous part of the array.
10+
*/
11+
12+
/**
13+
* @param {number[]} nums
14+
* @param {number} goal
15+
* @return {number}
16+
*/
17+
var numSubarraysWithSum = function(nums, goal) {
18+
const map = new Map([[0, 1]]);
19+
let result = 0;
20+
let sum = 0;
21+
22+
for (const num of nums) {
23+
sum += num;
24+
result += map.get(sum - goal) || 0;
25+
map.set(sum, (map.get(sum) || 0) + 1);
26+
}
27+
28+
return result;
29+
};

0 commit comments

Comments
 (0)
Please sign in to comment.