Skip to content

Commit

Permalink
add js
Browse files Browse the repository at this point in the history
  • Loading branch information
AkifhanIlgaz committed Feb 7, 2023
1 parent f508962 commit 5b4233b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions javascript/0904-fruit-into-baskets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @param {number[]} fruits
* @return {number}
*/
var totalFruit = function (fruits) {
let count = new Map();
let [left, total, res] = [0, 0, 0];

for (fruit of fruits) {
count.set(fruit, (count.get(fruit) || 0) + 1);
total++;

while (count.size > 2) {
let f = fruits[left];
count.set(f, count.get(f) - 1);
total -= 1;
left += 1;
if (!count.get(f)) {
count.delete(f);
}
}
res = Math.max(res, total);
}

return res;
};

0 comments on commit 5b4233b

Please sign in to comment.