Question1) BinaryGap
Find longest sequence of zeros in binary representation of an integer.
(출처 : https://codility.com/programmers/lessons/1-iterations/binary_gap/#disqus_thread)
A.)
import java.util.*;
class Solution {
public int solution(int[] A, int[] B, int[] C) {
int begin = 0;
int end = C.length - 1;
int res = -1;
while (begin <= end) {
int mid = (begin + end) / 2;
if (check(A, B, C, mid+1)) {
end = mid - 1;
res = mid+1;
} else {
begin = mid + 1;
}
}
return res;
}
private boolean check(int[] a, int[] b, int[] c, int num) {
int[] pNails = new int[2*c.length + 1];
Arrays.fill(pNails, 0);
for (int i = 0; i < num; ++i) {
++pNails[c[i]];
}
for (int i = 1; i < pNails.length; ++i) {
pNails[i] += pNails[i-1];
}
for (int i = 0; i < a.length; ++i) {
if (pNails[b[i]] <= pNails[a[i]-1]) return false;
}
return true;
}
}
(출처 : https://codility.com/programmers/lessons/14-binary_search_algorithm/ )
'Study & Edu > Algorithm' 카테고리의 다른 글
[Java] String to int, int to String 형변환 (1) | 2018.09.22 |
---|---|
[프로그래머스] 자연수 뒤집어 배열로 만들기 (3가지 방법) (0) | 2018.09.16 |
String to char/char[] 형변환, 혹은 그 반대로 형변환 (0) | 2018.09.11 |
[프로그래머스] 문자열 내 p와 y의 개수 (String to char array) (0) | 2018.09.11 |
JAVA로 10진수 2진수 변환 (decimal to binary in JAVA) (0) | 2017.10.05 |