File tree 2 files changed +30
-0
lines changed
2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 739
739
927|[ Three Equal Parts] ( ./solutions/0927-three-equal-parts.js ) |Hard|
740
740
928|[ Minimize Malware Spread II] ( ./solutions/0928-minimize-malware-spread-ii.js ) |Hard|
741
741
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|
742
743
933|[ Number of Recent Calls] ( ./solutions/0933-number-of-recent-calls.js ) |Easy|
743
744
937|[ Reorder Data in Log Files] ( ./solutions/0937-reorder-data-in-log-files.js ) |Medium|
744
745
966|[ Vowel Spellchecker] ( ./solutions/0966-vowel-spellchecker.js ) |Medium|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments