728x90
https://school.programmers.co.kr/learn/courses/30/lessons/42747
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
🤔 문제분석
이분탐색으로 문제를 해결하였습니다.
- H의 개수가 많다 → H값을 늘린다. ( H값을 늘릴경우 카운팅되는 수가 적어 진다 )
- H의 개수가 적다 → H값을 감소시킨다. ( H값을 줄일경우 카운팅되는 수가 많아진다)
💻 코드
class Solution {
// 정렬된 데이터에서 H를 기준으로 이분탐색한다.
// 카운팅 한 개수가 h번 이상인 개수가 h이상이고, h번 이하 인용되었다면 증가시킨다.
// 카운팅 한 개수가 그렇지 않은경우 감소시킨다.
public static void main(String[] argv)
{
Solution solution = new Solution();
int[] citations = {3, 0, 6, 1, 5};
System.out.println(solution.solution(citations));
}
public int solution(int[] citations) {
int start = 0;
int end = citations.length;
int answer = 0;
while(end >= start)
{
int mid = (start + end)/2;
int h = 0;
for(int num : citations)
{
if(num >= mid)
{
h += 1;
}
}
if(h >= mid) // h를 더 중가시켜본다.
{
answer = Math.max(answer, mid);
start = mid + 1;
}
else { // h를 감소시켜본다.
end = mid - 1;
}
}
return answer;
}
}
728x90
'알고리즘 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 폰켓몬 (0) | 2024.04.30 |
---|---|
[프로그래머스] 다리를 지나는 트럭 (0) | 2024.04.20 |
[프로그래머스] 도둑질 (0) | 2024.04.14 |
[프로그래머스] 이중우선순위큐 (0) | 2024.04.13 |
[프로그래머스] 전력망을 둘로 나누기 (0) | 2024.04.13 |