본문 바로가기
Computer Engineering

Codility - Domains

by En.Lee 2014. 10. 26.
Task description

A zero-indexed array A consisting of N integers is given. Thedominator of array A is the value that occurs in more than half of the elements of A.

For example, consider array A such that

A[0] = 3    A[1] = 4    A[2] =  3
A[3] = 2    A[4] = 3    A[5] = -1
A[6] = 3    A[7] = 3

The dominator of A is 3 because it occurs in 5 out of 8 elements of A (namely in those with indices 0, 2, 4, 6 and 7) and 5 is more than a half of 8.

Write a function

class Solution { public int solution(int[] A); }

that, given a zero-indexed array A consisting of N integers, returns index of any element of array A in which the dominator of A occurs. The function should return −1 if array A does not have a dominator.

Assume that:

  • N is an integer within the range [0..100,000];
  • each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].

For example, given array A such that

A[0] = 3    A[1] = 4    A[2] =  3
A[3] = 2    A[4] = 3    A[5] = -1
A[6] = 3    A[7] = 3

the function may return 0, 2, 4, 6 or 7, as explained above.

Complexity:

  • expected worst-case time complexity is O(N);
  • expected worst-case space complexity is O(1), beyond input storage (not counting the storage required for input arguments).

Elements of input arrays can be modified.


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


Code: 07:24:13 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) {
        // write your code in Java SE 8
        int count = 0;
        int candidate_index = -1;
        int candidate = 0;
        for(int i=0 ; i < A.length; i++){
            if(count == 0){
                candidate = A[i];
                candidate_index = i;
                count++;
            }
            else{
                if(A[i] == candidate){
                    count++;
                }
                else{
                    count--;
                }
            }
        }
        int candidate_count =0;
        for(int i = 0; i < A.length; i++){
            if(A[i] == candidate){
                candidate_count++;
            }
        }
        
        if(candidate_count > A.length/2){
            return candidate_index;
        }
        
        return -1;
                    
            
            
            
    }
}
Analysis
Detected time complexity:
O(N*log(N)) or O(N)
testtimeresult
Example tests
example 
example test
1.488 sOK
Correctness tests
small_nondominator 
all different and all the same elements
1.484 sOK
small_half_positions 
half elements the same, and half + 1 elements the same
1.472 sOK
small 
small test
1.472 sOK
small_pyramid 
decreasing and plateau, small
1.460 sOK
extreme_empty_and_single_item 
empty and single element arrays
1.464 sOK
extreme_half1 
array with exactly N/2 values 1, N even + [0,0,1,1,1]
1.468 sOK
extreme_half2 
array with exactly floor(N/2) values 1, N odd + [0,0,1,1,1]
1.472 sOK
extreme_half3 
array with exactly ceil(N/2) values 1 + [0,0,1,1,1]
1.148 sOK
Performance tests
medium_pyramid 
decreasing and plateau, medium
1.196 sOK
large_pyramid 
decreasing and plateau, large
1.384 sOK
medium_random 
random test with dominator, N = 10,000
1.176 sOK
large_random 
random test with dominator, N = 100,000
1.764 sOK