package com.company;
import java.util.Arrays;
class Solution {
public int solution(int[] citations) {
int answer = 0;
Arrays.sort(citations);
answer = binarySearch(citations);
return answer;
}
private int binarySearch(int[] citations) {
int answer = -1;
int left = 0;
int right = citations.length;
int totalLength = citations.length;
while(left <= right) {
int mid = (left + right) / 2;
int firstIndex = getFirstNumIndex(citations, mid);
if((totalLength - firstIndex) >= mid && firstIndex <= mid) {
answer = mid;
left = mid + 1;
}else{
right = mid - 1;
}
}
return answer;
}
public int getFirstNumIndex(int[] arr, int num) {
int left = 0;
int right = arr.length-1;
while (left < right) {
int mid = (left + right) / 2;
if (arr[mid] >= num) {
right = mid;
} else {
left = mid + 1;
}
}
return right;
}
public int getNextNumIndex(int[] arr, int num, int left, int right) {
while (left < right) {
int mid = (left + right) / 2;
if (arr[mid] > num) {
right = mid;
} else {
left = mid + 1;
}
}
return right;
}
}
다른 방법으로 풀이 : 규칙을 찾았으면 쉽게 풀수있게 되어있었다. ㅠㅠ
import java.util.*;
class Solution {
public int solution(int[] citations) {
Arrays.sort(citations);
int max = 0;
for(int i = citations.length-1; i > -1; i--){
int min = (int)Math.min(citations[i], citations.length - i);
if(max < min) max = min;
}
return max;
}
}