Repository navigation
Expand file tree
/
Copy pathfish.js
More file actions
52 lines (42 loc) · 1.34 KB
/
Copy pathfish.js
File metadata and controls
52 lines (42 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function solution(A, B) {
const N = A.length;
const downstreamFish = [];
let aliveFishCount = 0;
for (let i = 0; i < N; i++) {
if (B[i] === 0) { // upstream fish
for (let j = downstreamFish.length - 1; j >= 0; j--) {
const downstreamFishSize = downstreamFish[j];
if (downstreamFishSize > A[i]) {
// The downstream fish eats the upstream fish
break; // Stop the loop as the upstream fish is eaten
}
// The upstream fish eats the downstream fish
downstreamFish.pop();
}
if (downstreamFish.length===0) {
// No downstream fish left, upstream fish survives
aliveFishCount++;
}
} else { // downstream fish
downstreamFish.push(A[i]);
}
}
aliveFishCount += downstreamFish.length;
return aliveFishCount;
}
// Example usage:
var A = [4, 3, 2, 1, 5];
var B = [0, 1, 0, 0, 0];
console.log(solution(A, B)); // Output: 2
A = [4,2];
B = [0, 1];
console.log(solution(A, B)); // Output: 2
A = [4,2,3,4,5];
B = [0,1,1,1,1];
console.log(solution(A, B)); // Output: 5
A = [4,2,3,4,5];
B = [0,0,0,0,0];
console.log(solution(A, B)); // Output: 5
A = [1,2,3,4,5];
B = [1,0,0,0,1];
console.log(solution(A, B)); // Output: 4