-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathProblem102.js
47 lines (38 loc) · 1.04 KB
/
Problem102.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Problem 102
//
// This problem was asked by Lyft.
//
// Given a list of integers and a number K, return which contiguous elements of the list sum to K.
//
// For example, if the list is [1, 2, 3, 4, 5] and K is 9, then it should return [2, 3, 4].
//
// O(N) Time complexity
// O(N) Space complexity
/**
* Returns contiguous elements of the list sum to K.
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
function contiguousSum(nums, k) {
if (k === 0) return [];
let currSum = 0;
const map = new Map();
// set the current sum and index at map
for (let i = 0; i < nums.length; i++) {
currSum += nums[i];
const difference = currSum - k;
if (difference === 0) {
return nums.slice(0, i + 1);
}
// when we find the difference in the map.
// this means we could remove 0 to index of difference
if (map.has(difference)) {
const startIndex = map.get(difference);
return nums.slice(startIndex + 1, i + 1);
}
map.set(currSum, i);
}
return [];
}
export default contiguousSum;