-
Notifications
You must be signed in to change notification settings - Fork 0
/
sliding-window.js
63 lines (55 loc) · 1.58 KB
/
sliding-window.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/**
* - The problem input is a linear data structure such as a linked list, array, or string
* - You’re asked to find the longest/shortest substring, subarray, or a desired value
*
* Problems:
* - Maximum sum subarray of size ‘K’
* - Longest substring with ‘K’ distinct characters
* - String anagrams
*
* Leetcode problems: 3, 209, 424, 643, 1004, 1493
*/
/**
* Brute froce
* time = O(k*n)
* aux space = O(1)
*/
function maxSum(arr, k) {
const n = arr.length
if (n < k) return null
let maxSum = 0
for (let i = 0; i < n - k + 1; i++) {
let curSum = 0
for (let j = 0; j < k; j++) {
curSum += arr[i + j]
}
maxSum = curSum > maxSum ? curSum : maxSum
}
return maxSum
}
console.log(maxSum([100, 200, 300, 400], 2)) // => 700
console.log(maxSum([1, 4, 2, 10, 23, 3, 1, 0, 20], 4)) // => 39
console.log(maxSum([2, 3], 3)) // => null
/**
* Sliding window
* time = O(n)
* space = O(1)
*/
function maxSumSlidingWindow(arr, k) {
const n = arr.length
if (n < k) return null
let maxSum = 0
for (let i = 0; i < k; i++) {
maxSum += arr[i]
}
let windowSum = maxSum
for (let i = k; i < n; i++) {
windowSum = windowSum - arr[i - k] + arr[i]
maxSum = windowSum > maxSum ? windowSum : maxSum
}
return maxSum
}
console.log(maxSumSlidingWindow([100, 200, 300, 400], 2)) // => 700
console.log(maxSumSlidingWindow([1, 4, 2, 10, 23, 3, 1, 0, 20], 4)) // => 39
console.log(maxSumSlidingWindow([2, 3], 3)) // => null
console.log(maxSumSlidingWindow([0,4,0,3,2], 1)) // => 4