Here is another codility problem solution from the codility lessons (Fish-N voracious fish are moving along a river. Calculate how many fish are alive.) due to the copy rights I can't copy the content of the problem here so to view the problem description click here.
// you can also use imports, for example:
import java.util.*;
// you can use System.out.println for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A, int[] B) {
int count = 0;
Stack < Integer > previousFishes = new Stack < Integer > ();
for (int i = 0; i < A.length; i++) {
int currentFish = A[i];
int currentFlow = B[i];
if (currentFlow == 1) {
previousFishes.push(currentFish);
}
if (!previousFishes.empty() && currentFlow == 0) {
while (!previousFishes.empty() && currentFish > previousFishes.peek()) {
int fish = previousFishes.pop();
}
}
if (previousFishes.empty() && currentFlow == 0) {
count++;
}
}
return count + previousFishes.size();
}
}
No comments:
Post a Comment