https://school.programmers.co.kr/learn/courses/30/lessons/42747
프로그래머스
SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
citations [3, 0, 6, 1, 5] return 3
h 를 구하는 문제
정렬 후 , 이분 탐색으로 구하기
left <= right : mid값이 정답이 될 수도 있으므로
import java.util.*;
class Solution {
public int solution(int[] citations) {
int answer = 0;
Arrays.sort(citations);
int left = 0, right = 10000; // h 편 설정
while(left <= right) {
int h = (right - left) / 2 + left;
if(isValid(citations, h)) {
answer = Math.max(answer, h);
left = h + 1;
} else {
right = h - 1;
}
}
return answer;
}
public boolean isValid(int[] citations, int h) {
int cnt = 0;
for(int i = 0; i < citations.length; i++) {
if(citations[i] >= h) {
cnt += 1;
}
}
return cnt >= h;
}
}
[프로그래머스] 기능 개발 (0) | 2024.12.16 |
---|---|
[프로그래머스] 연속 부분 수열 합의 개수 (0) | 2024.12.16 |
[프로그래머스] 같은 숫자는 싫어 (0) | 2024.12.12 |
[프로그래머스] [1차] 캐시 (0) | 2024.12.12 |
[프로그래머스] [1차] 비밀지도 (0) | 2024.12.10 |