Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solveCurrentProblem(input) {
- let map = new Map();
- let remaining = new Map();
- for (let productInfo of input) {
- let [product, quantity] = productInfo.split(/\s+=>\s+/);
- if (quantity >= 1000 || (quantity + remaining.get(product) >= 1000) || (quantity + map.get(product) >= 1000)) {
- let left = remaining.get(product) !== undefined ? Number(remaining.get(product)) : 0;
- if(map.has(product) === false){
- map.set(product, 0);
- }
- map.set(product, map.get(product) + +quantity + left);
- remaining.delete(product);
- } else {
- if(remaining.has(product) === false){
- remaining.set(product, 0);
- }
- remaining.set(product, Number(remaining.get(product)) + +quantity);
- }
- }
- Array.from(map.keys()).forEach(fruit => {
- let bottles = Math.floor(map.get(fruit) / 1000);
- console.log(`${fruit} => ${bottles}`);
- })
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement