File tree 2 files changed +32
-1
lines changed
2 files changed +32
-1
lines changed Original file line number Diff line number Diff line change 1
- # 1,057 LeetCode solutions in JavaScript
1
+ # 1,058 LeetCode solutions in JavaScript
2
2
3
3
[ https://leetcode.com/ ] ( https://leetcode.com/ )
4
4
782
782
971|[ Flip Binary Tree To Match Preorder Traversal] ( ./solutions/0971-flip-binary-tree-to-match-preorder-traversal.js ) |Medium|
783
783
972|[ Equal Rational Numbers] ( ./solutions/0972-equal-rational-numbers.js ) |Hard|
784
784
973|[ K Closest Points to Origin] ( ./solutions/0973-k-closest-points-to-origin.js ) |Medium|
785
+ 974|[ Subarray Sums Divisible by K] ( ./solutions/0974-subarray-sums-divisible-by-k.js ) |Medium|
785
786
976|[ Largest Perimeter Triangle] ( ./solutions/0976-largest-perimeter-triangle.js ) |Easy|
786
787
977|[ Squares of a Sorted Array] ( ./solutions/0977-squares-of-a-sorted-array.js ) |Easy|
787
788
985|[ Sum of Even Numbers After Queries] ( ./solutions/0985-sum-of-even-numbers-after-queries.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 974. Subarray Sums Divisible by K
3
+ * https://leetcode.com/problems/subarray-sums-divisible-by-k/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given an integer array nums and an integer k, return the number of non-empty subarrays
7
+ * that have a sum divisible by k.
8
+ *
9
+ * A subarray is a contiguous part of an array.
10
+ */
11
+
12
+ /**
13
+ * @param {number[] } nums
14
+ * @param {number } k
15
+ * @return {number }
16
+ */
17
+ var subarraysDivByK = function ( nums , k ) {
18
+ const remainderCount = new Map ( [ [ 0 , 1 ] ] ) ;
19
+ let sum = 0 ;
20
+ let result = 0 ;
21
+
22
+ for ( const num of nums ) {
23
+ sum += num ;
24
+ const remainder = ( ( sum % k ) + k ) % k ;
25
+ result += remainderCount . get ( remainder ) || 0 ;
26
+ remainderCount . set ( remainder , ( remainderCount . get ( remainder ) || 0 ) + 1 ) ;
27
+ }
28
+
29
+ return result ;
30
+ } ;
You can’t perform that action at this time.
0 commit comments