mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-10 11:09:43 +08:00
adding some sliding-window algorithms
This commit is contained in:
parent
af08253a95
commit
689fda613d
14
src/algorithms/sliding-window/fruits into basket/README.md
Normal file
14
src/algorithms/sliding-window/fruits into basket/README.md
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
# fruits into baskets (sliding window)
|
||||||
|
|
||||||
|
- Sliding window is an algorithm/technique used to solve
|
||||||
|
subarray/subsets problems.
|
||||||
|
- It has two types : "static sliding window(size of the window is known)
|
||||||
|
and dynamic sliding(size of the window is unknown).
|
||||||
|
- It used to remove nested loops and reduce algorithm complexity to O(n).
|
||||||
|
|
||||||
|
## link to the problem
|
||||||
|
https://leetcode.com/problems/fruit-into-baskets/
|
||||||
|
|
||||||
|
## References
|
||||||
|
|
||||||
|
- [Youtube](https://www.youtube.com/watch?v=yYtaV0G3mWQ)
|
@ -0,0 +1,33 @@
|
|||||||
|
var totalFruit = function (arr) {
|
||||||
|
let max = 0;
|
||||||
|
let hash = {};
|
||||||
|
let j = 0;
|
||||||
|
let objLength = 0;
|
||||||
|
|
||||||
|
for (let i = 0; i < arr.length; i++) {
|
||||||
|
if (!hash[arr[i]]) {
|
||||||
|
hash[arr[i]] = 1;
|
||||||
|
objLength++ ;
|
||||||
|
} else {
|
||||||
|
hash[arr[i]]++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (objLength <= 2) {
|
||||||
|
max = Math.max(max, i - j + 1);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
while (objLength > 2) {
|
||||||
|
hash[arr[j]]--;
|
||||||
|
if (hash[arr[j]] === 0) {
|
||||||
|
delete hash[arr[j]];
|
||||||
|
objLength-- ;
|
||||||
|
}
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return max;
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log(totalFruit([3, 3, 3, 1, 2, 1, 1, 2, 3, 3, 4]));
|
17
src/algorithms/sliding-window/maximum-subarray-sum/README.md
Normal file
17
src/algorithms/sliding-window/maximum-subarray-sum/README.md
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
# maximum subaray sum (sliding window)
|
||||||
|
|
||||||
|
- Sliding window is an algorithm/technique used to solve
|
||||||
|
subarray/subsets problems.
|
||||||
|
- It has two types : "static sliding window(size of the window is known)
|
||||||
|
and dynamic sliding(size of the window is unknown).
|
||||||
|
- It used to remove nested loops and reduce algorithm complexity to O(n).
|
||||||
|
|
||||||
|
|
||||||
|
## link to the problem
|
||||||
|
|
||||||
|
## References
|
||||||
|
|
||||||
|
- [Wikipedia](https://www.scaler.com/topics/sliding-window-algorithm/#:~:text=Sliding%20Window%20Algorithm%20is%20a,result%20of%20the%20next%20step.)
|
||||||
|
- [](https://leetcode.com/problems/subarray-sum-equals-k/)
|
||||||
|
- [continuous-subarray-sum](https://leetcode.com/problems/continuous-subarray-sum/)
|
||||||
|
-[Youtube](https://www.youtube.com/watch?v=MK-NZ4hN7rs&t=1121s)
|
@ -0,0 +1,23 @@
|
|||||||
|
//Problem : given an array of integers, return the maximum subarray sum of size k.
|
||||||
|
|
||||||
|
function maxSubArrSum(arr, size) {
|
||||||
|
let s = 0;
|
||||||
|
let max = -Infinity;
|
||||||
|
|
||||||
|
for (let i = 0; i < arr.length; i++) {
|
||||||
|
s += arr[i];
|
||||||
|
if (i >= size - 1) {
|
||||||
|
if (s > max) {
|
||||||
|
max = s;
|
||||||
|
}
|
||||||
|
s -= arr[i - (size-1)];
|
||||||
|
}
|
||||||
|
console.log(max);
|
||||||
|
}
|
||||||
|
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
|
||||||
|
let arr = [1, 5, 4, 8, 2, 3];
|
||||||
|
|
||||||
|
console.log(maxSubArrSum(arr, 3));
|
Loading…
Reference in New Issue
Block a user