Skip to content

Commit 3d25df3

Browse files
authored
Create 2391-minimum-amount-of-time-to-collect-garbage.js
1 parent f31510c commit 3d25df3

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* @param {string[]} garbage
3+
* @param {number[]} travel
4+
* @return {number}
5+
*/
6+
const garbageCollection = function(garbage, travel) {
7+
let res1 = 0, res2 = 0, res3 = 0
8+
const n = garbage.length
9+
// P
10+
res1 = helper('P')
11+
res2 = helper('M')
12+
res3 = helper('G')
13+
return res1 + res2 + res3
14+
15+
function helper(target) {
16+
const arr = []
17+
for(let i = 0; i < n; i++) {
18+
const str = garbage[i]
19+
for(const e of str) {
20+
if(e === target) arr.push(e)
21+
}
22+
if(i + 1 < n) arr.push(travel[i])
23+
}
24+
const idx = arr.indexOf(target)
25+
const lastIdx =arr.lastIndexOf(target)
26+
let tmp = 0
27+
// console.log(arr, idx, lastIdx)
28+
for(let i = 0; i >= 0 && i<=lastIdx; i++) {
29+
const e = arr[i]
30+
if(e === target) tmp += 1
31+
else tmp += e
32+
}
33+
return tmp
34+
}
35+
};

0 commit comments

Comments
 (0)