File tree Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Expand file tree Collapse file tree 2 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 313
313
322|[ Coin Change] ( ./solutions/0322-coin-change.js ) |Medium|
314
314
323|[ Number of Connected Components in an Undirected Graph] ( ./solutions/0323-number-of-connected-components-in-an-undirected-graph.js ) |Medium|
315
315
324|[ Wiggle Sort II] ( ./solutions/0324-wiggle-sort-ii.js ) |Medium|
316
+ 325|[ Maximum Size Subarray Sum Equals k] ( ./solutions/0325-maximum-size-subarray-sum-equals-k.js ) |Medium|
316
317
326|[ Power of Three] ( ./solutions/0326-power-of-three.js ) |Easy|
317
318
327|[ Count of Range Sum] ( ./solutions/0327-count-of-range-sum.js ) |Hard|
318
319
328|[ Odd Even Linked List] ( ./solutions/0328-odd-even-linked-list.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 325. Maximum Size Subarray Sum Equals k
3
+ * https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/
4
+ * Difficulty: Medium
5
+ *
6
+ * Given an integer array nums and an integer k, return the maximum length of a subarray that
7
+ * sums to k. If there is not one, return 0 instead.
8
+ */
9
+
10
+ /**
11
+ * @param {number[] } nums
12
+ * @param {number } k
13
+ * @return {number }
14
+ */
15
+ var maxSubArrayLen = function ( nums , k ) {
16
+ const sumIndex = new Map ( [ [ 0 , - 1 ] ] ) ;
17
+ let currentSum = 0 ;
18
+ let result = 0 ;
19
+
20
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
21
+ currentSum += nums [ i ] ;
22
+ if ( sumIndex . has ( currentSum - k ) ) {
23
+ result = Math . max ( result , i - sumIndex . get ( currentSum - k ) ) ;
24
+ }
25
+ if ( ! sumIndex . has ( currentSum ) ) {
26
+ sumIndex . set ( currentSum , i ) ;
27
+ }
28
+ }
29
+
30
+ return result ;
31
+ } ;
You can’t perform that action at this time.
0 commit comments