This commit is contained in:
Abidi Ramzi 2024-07-17 10:42:43 +09:00 committed by GitHub
commit 6434021fae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 87 additions and 0 deletions

View 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)

View File

@ -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]));

View 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)

View File

@ -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));