patternjavaModerate
Fish Food Chain of complexity O(N)
Viewed 0 times
foodfishcomplexitychain
Problem
I understand that if you don't know the trick, it's not easy create a solution with a complexity of \$O(N)\$. However, I would like to ask you how to solve this tricky question:
You are given two non-empty zero-indexed arrays A and B consisting of N integers. Arrays A and B represent N voracious fish in a river, ordered downstream along the flow of the river. The fish are numbered from 0 to N − 1, fish number P is represented by
If two fish move in opposite directions and there are no other (living) fish between them, they will eventually meet each other. Then only one fish can stay alive − the larger fish eats the smaller one. More precisely, we say that two fish P and Q meet each other when P number 1 meets fish number 2 and eats it, then it meets fish number 3 and eats it too. Finally, it meets fish number 4 and is eaten by it. The remaining two fish, numbers 0 and 4, never meet and therefore stay alive.
Write a function:
that, given two non-empty zero-indexed arrays A and B consisting of N integers, returns the number of fish that will stay alive.
For example, given the arrays shown above, the function should return 2, as explained above.
Assume that:
N is an integer within the range [1..100,000], where each element of array A is an integer within the range [0..1,000,000,000], where each element of array B is an integer that can have one of the following values: 0, 1, where the elements of A are all distinct.
Complexity:
expected worst-case time complexity is O(N);
expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
My solution:
```
import java.util.Stack;
clas
You are given two non-empty zero-indexed arrays A and B consisting of N integers. Arrays A and B represent N voracious fish in a river, ordered downstream along the flow of the river. The fish are numbered from 0 to N − 1, fish number P is represented by
A[P] and B[P], and if P unique. Array B contains the directions of the fish. It contains only 0s and/or 1s, where:0 represents a fish flowing upstream
1 represents a fish flowing downstreamIf two fish move in opposite directions and there are no other (living) fish between them, they will eventually meet each other. Then only one fish can stay alive − the larger fish eats the smaller one. More precisely, we say that two fish P and Q meet each other when P number 1 meets fish number 2 and eats it, then it meets fish number 3 and eats it too. Finally, it meets fish number 4 and is eaten by it. The remaining two fish, numbers 0 and 4, never meet and therefore stay alive.
Write a function:
class Solution { public int solution(int[] A, int[] B); }that, given two non-empty zero-indexed arrays A and B consisting of N integers, returns the number of fish that will stay alive.
For example, given the arrays shown above, the function should return 2, as explained above.
Assume that:
N is an integer within the range [1..100,000], where each element of array A is an integer within the range [0..1,000,000,000], where each element of array B is an integer that can have one of the following values: 0, 1, where the elements of A are all distinct.
Complexity:
expected worst-case time complexity is O(N);
expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
My solution:
```
import java.util.Stack;
clas
Solution
Unfortunately, both solutions are wrong as they don't pass the following JUnit tests:
The right answer with time and space complexity O(n) is shown below:
@Test
public void testSolution() {
FishSurvivor fs = new FishSurvivor();
int[] a = { 4, 3, 2, 1, 5 };
int[] b = { 0, 1, 0, 0, 0 };
assertEquals(2, fs.solution(a, b));
a = new int[] { 4, 3, 2, 1, 5 };
b = new int[] { 0, 1, 0, 1, 0 };
assertEquals(2, fs.solution(a, b));
a = new int[] { 4, 3, 2, 1, 5 };
b = new int[] { 0, 0, 0, 0, 0 };
assertEquals(5, fs.solution(a, b));
a = new int[] { 4, 3, 2, 1, 5 };
b = new int[] { 1, 1, 1, 1, 1 };
assertEquals(5, fs.solution(a, b));
a = new int[] { 4, 3, 2, 1, 5 };
b = new int[] { 0, 0, 0, 1, 1 };
assertEquals(5, fs.solution(a, b));
a = new int[] { 5, 3, 2, 1, 4 };
b = new int[] { 1, 0, 0, 0, 0 };
assertEquals(1, fs.solution(a, b));
a = new int[] { 1, 2, 3, 4, 5 };
b = new int[] { 1, 1, 1, 1, 0 };
assertEquals(1, fs.solution(a, b));
}The right answer with time and space complexity O(n) is shown below:
package com.atreceno.it.javanese.codility;
import java.util.Stack;
/**
* You are given two non-empty zero-indexed arrays A and B consisting of N
* integers. Arrays A and B represent N voracious fish in a river, ordered
* downstream along the flow of the river...
*
* @author atreceno
*
*/
public class FishSurvivor {
/**
* Given two non-empty zero-indexed arrays A and B consisting of N integers,
* this function returns the number of fish that will stay alive.
*
* @param a
* array representing the size.
* @param b
* array representing the direction.
* @return the number of fish that will stay alive.
*/
public int solution(int[] a, int[] b) {
int survivors = 0;
Stack ones = new Stack();
for (int i = 0; i ones.peek()) { // "One" dies
ones.pop();
} else { // "Zero" dies
break;
}
}
if (ones.empty()) { // "Zero" survives
survivors++;
}
}
} else {
ones.push(a[i]);
}
}
return survivors + ones.size();
}
}Code Snippets
@Test
public void testSolution() {
FishSurvivor fs = new FishSurvivor();
int[] a = { 4, 3, 2, 1, 5 };
int[] b = { 0, 1, 0, 0, 0 };
assertEquals(2, fs.solution(a, b));
a = new int[] { 4, 3, 2, 1, 5 };
b = new int[] { 0, 1, 0, 1, 0 };
assertEquals(2, fs.solution(a, b));
a = new int[] { 4, 3, 2, 1, 5 };
b = new int[] { 0, 0, 0, 0, 0 };
assertEquals(5, fs.solution(a, b));
a = new int[] { 4, 3, 2, 1, 5 };
b = new int[] { 1, 1, 1, 1, 1 };
assertEquals(5, fs.solution(a, b));
a = new int[] { 4, 3, 2, 1, 5 };
b = new int[] { 0, 0, 0, 1, 1 };
assertEquals(5, fs.solution(a, b));
a = new int[] { 5, 3, 2, 1, 4 };
b = new int[] { 1, 0, 0, 0, 0 };
assertEquals(1, fs.solution(a, b));
a = new int[] { 1, 2, 3, 4, 5 };
b = new int[] { 1, 1, 1, 1, 0 };
assertEquals(1, fs.solution(a, b));
}package com.atreceno.it.javanese.codility;
import java.util.Stack;
/**
* You are given two non-empty zero-indexed arrays A and B consisting of N
* integers. Arrays A and B represent N voracious fish in a river, ordered
* downstream along the flow of the river...
*
* @author atreceno
*
*/
public class FishSurvivor {
/**
* Given two non-empty zero-indexed arrays A and B consisting of N integers,
* this function returns the number of fish that will stay alive.
*
* @param a
* array representing the size.
* @param b
* array representing the direction.
* @return the number of fish that will stay alive.
*/
public int solution(int[] a, int[] b) {
int survivors = 0;
Stack<Integer> ones = new Stack<Integer>();
for (int i = 0; i < a.length; i++) {
if (b[i] == 0) {
if (ones.size() == 0) {
survivors++;
} else { // Duel
while (!ones.empty()) {
if (a[i] > ones.peek()) { // "One" dies
ones.pop();
} else { // "Zero" dies
break;
}
}
if (ones.empty()) { // "Zero" survives
survivors++;
}
}
} else {
ones.push(a[i]);
}
}
return survivors + ones.size();
}
}Context
StackExchange Code Review Q#33716, answer score: 10
Revisions (0)
No revisions yet.