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.
----------------------------------- 풀이 ----------------------------------------
O(N*log(N)) or O(N)
test | time | result | |
---|---|---|---|
Example tests | |||
example example test | 1.488 s | OK | |
Correctness tests | |||
small_nondominator all different and all the same elements | 1.484 s | OK | |
small_half_positions half elements the same, and half + 1 elements the same | 1.472 s | OK | |
small small test | 1.472 s | OK | |
small_pyramid decreasing and plateau, small | 1.460 s | OK | |
extreme_empty_and_single_item empty and single element arrays | 1.464 s | OK | |
extreme_half1 array with exactly N/2 values 1, N even + [0,0,1,1,1] | 1.468 s | OK | |
extreme_half2 array with exactly floor(N/2) values 1, N odd + [0,0,1,1,1] | 1.472 s | OK | |
extreme_half3 array with exactly ceil(N/2) values 1 + [0,0,1,1,1] | 1.148 s | OK | |
Performance tests | |||
medium_pyramid decreasing and plateau, medium | 1.196 s | OK | |
large_pyramid decreasing and plateau, large | 1.384 s | OK | |
medium_random random test with dominator, N = 10,000 | 1.176 s | OK | |
large_random random test with dominator, N = 100,000 | 1.764 s | OK |