-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFish.js
More file actions
30 lines (26 loc) · 785 Bytes
/
Fish.js
File metadata and controls
30 lines (26 loc) · 785 Bytes
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
//Score: 100%
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A, B) {
// write your code in JavaScript (Node.js 8.9.4)
var fishDown = new Array();
var freeFishUp = 0;
for(var i = 0; i < A.length; i++){
var goingUp = B[i] == 0;
if(goingUp){
while((fishDown.length >= 1)){
var pop = fishDown.pop();
if(pop>A[i]){
fishDown.push(pop);
break;
}
}
if(fishDown.length<1)
freeFishUp++;
}else{
fishDown.push(A[i]);
}
}
var freeFishDown = fishDown.length;
return freeFishDown + freeFishUp;
}