본문 바로가기
Computer Engineering

Codility - Fish

by En.Lee 2014. 10. 26.

Task description

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. If P and Q are two fish and P < Q, then fish P is initially upstream of fish Q. Initially, each fish has a unique position.

Fish number P is represented by A[P] and B[P]. Array A contains the sizes of the fish. All its elements are 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 downstream.

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 < Q, B[P] = 1 and B[Q] = 0, and there are no living fish between them. After they meet:

  • If A[P] > A[Q] then P eats Q, and P will still be flowing downstream,
  • If A[Q] > A[P] then Q eats P, and Q will still be flowing upstream.

We assume that all the fish are flowing at the same speed. That is, fish moving in the same direction never meet. The goal is to calculate the number of fish that will stay alive.

For example, consider arrays A and B such that:

  A[0] = 4    B[0] = 0
  A[1] = 3    B[1] = 1
  A[2] = 2    B[2] = 0
  A[3] = 1    B[3] = 0
  A[4] = 5    B[4] = 0

Initially all the fish are alive and all except fish number 1 are moving upstream. Fish 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, number 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];
  • each element of array A is an integer within the range [0..1,000,000,000];
  • each element of array B is an integer that can have one of the following values: 0, 1;
  • 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).

Elements of input arrays can be modified.



--------------------------------- 풀이 -------------------------------------------

Code: 06:25:20 UTC, java, final, score: 100.00
// 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) {
        // write your code in Java SE 8
        /*
        문제:
        입력되는 배열 A의 index는 물기고번호, 값은 물고기 크기다.
        입력되는 배열 B의 index는 물고기번호, 값은 물고기가 향하는 방향이다.
        
        배열 B의 값이 0이면 상류로 가는 것, 
        배열 B의 값이 1이면 하류로 가는 것이다.
        
        index의 위치가 물고기가 처음 배치된 위치이다.
        그렇기 때문에 위쪽에 위치한 물고기는 아래에 있는 물고기를 만날수 없다.
        
        또한, 같은 방향으로 향하는 물고기도 만날 수 없다.
        다른 방향으로 향하는 물고기를 만나면 크기가 큰 물고기가 작은 물고리를 잡아 먹는다.
        
        방법:
        1. index가 작을수록(P < Q)일수록 상류에 위치한다.
        2. 하류에 위치한 물고기에 초점을 맞추고 자신보다 아래에서 올라오는 물고기와 비교한다.
        3. 살아남은 물고기 들은 스택에 저장한다.
        
        시나리오:
        0. 물고기가 하류로 향하면 스택에 넣는다.
        0-1. 다음 물고기가 상류로 향하는 물고기 일 경우 스택에서 빼와 비교한다.
        0-2. 상류 물고기의 크기가 크다면 비교 물고기를 먹는다(없엔다)
        0-3. 상류 물고기가 작다면 스택에 그대로 있다.
        
        1. 물고기가 상류로 향하고, 비교 물고기(하류로향하는 물고기)가 없으면 살아남은 물고기
        2. 물고기가 하류로 향하고, 비교 물고기가 있으면, 비교 물고기는 하류 스택에 넣는다.
        */
        
        int living_count = 0;
        Stack downstream = new Stack<Integer>();
        
        for(int i = 0; i<A.length; i++){
            int stream = B[i];
            if(B[i] == 0){ // 물고기가 상류로 향하면 
            
            // 1. 하류로 향하는 물고기가 있으면 비교, 없으면 살아서 올라감.
                if(!downstream.empty()){
                    int eat_count = 0;
                    while(!downstream.empty() && (int)downstream.peek() <A[i]){ // 하류보다 상류물고기가 더 크면 하류 물고기를 먹는다.
                        downstream.pop();
                        eat_count++;
                    }
                    if(downstream.empty()) // 상류 물고기가 하류 물고기 다 먹으면 살아서 올라감
                        living_count++;
                }
                else{ // 하류 물고기가 없을 경우
                    living_count++;
                }
            }
            else{ // 물고기가 하류로 향하면
            downstream.push(A[i]);
            }
        }
        
        living_count += downstream.size();
        
        return living_count;
    }
}
Analysis
Detected time complexity:
O(N)
testtimeresult
Example tests
example 
example test
1.496 sOK
Correctness tests
extreme_small 
1 or 2 fishes
1.584 sOK
simple1 
simple test
1.540 sOK
simple2 
simple test
1.588 sOK
small_random 
small random test, N = ~100
1.492 sOK
Performance tests
medium_random 
small medium test, N = ~5,000
1.524 sOK
large_random 
large random test, N = ~100,000
2.040 sOK
extreme_range1 
all except one fish flowing in the same direction
1.908 sOK
extreme_range2 
all fish flowing in the same direction
2.020 sOK