[프로그래머스][JAVA] H-Index (정렬, 이진탐색)

2022. 3. 31. 06:02· 알고리즘 풀이/프로그래머스
728x90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import java.util.*;
 
class Solution {
    public int solution(int[] citations) {
        int h = 0;
        Arrays.sort(citations);
        
        int len = citations.length;
        
        // System.out.println(Arrays.binarySearch(citations, 3));
        
        while(true){
            int idx = Arrays.binarySearch(citations, h);
            if(idx<0){
                idx = -(idx+1);
            }
            else{
                while(idx>=1 && citations[idx]==citations[idx-1]){
                    idx--;
                }
            }
            
            if(h<=len-idx){
                h++;    
            }
            else{
                h--;
                break;
            }
        }
        
        // while(i>=0){
        //     int curr = citations[i];
        //     while(i>=0 && citations[i] == curr){
        //         i--;
        //     }
        //     int num = len-i;
        //     if(curr<=num){
        //         answer = curr;
        //         break;
        //     }
        // }
        
        
        return h;
    }
}
Colored by Color Scripter
cs

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.*;
 
class Solution {
    public int solution(int[] citationsInput) {
        int answer = 0;
        
        ArrayList<Integer> citations = new ArrayList<>();
        for(int c : citationsInput){
            citations.add(c);  
        }
        
        Collections.sort(citations);
        Collections.reverse(citations);
        
        int accum = 0;
        for(int c : citations){
            if(accum<c) accum++;
            else{
                break;
            }
        }
        
        
        return accum;
    }
}
Colored by Color Scripter
cs
저작자표시 (새창열림)

'알고리즘 풀이 > 프로그래머스' 카테고리의 다른 글

[프로그래머스][JAVA] 큰 수 만들기 (스택, 그리디)  (0) 2022.03.31
[프로그래머스][JAVA] 카펫 (완전탐색)  (0) 2022.03.31
[프로그래머스][JAVA] 다리를 지나는 트럭 (큐)  (0) 2022.03.31
[프로그래머스][JAVA] 위장 (해시, 집합)  (0) 2022.03.27
[프로그래머스][JAVA] 배달 (플로이드 와샬)  (0) 2022.03.27
'알고리즘 풀이/프로그래머스' 카테고리의 다른 글
  • [프로그래머스][JAVA] 큰 수 만들기 (스택, 그리디)
  • [프로그래머스][JAVA] 카펫 (완전탐색)
  • [프로그래머스][JAVA] 다리를 지나는 트럭 (큐)
  • [프로그래머스][JAVA] 위장 (해시, 집합)
배게
배게
백엔드배게 님의 블로그입니다.
배게
백엔드
배게
전체
오늘
어제
  • 분류 전체보기 (430)
    • 알고리즘 풀이 (338)
      • 백준 (167)
      • Codility (22)
      • 프로그래머스 (123)
      • LeetCode (2)
      • CodeForces (9)
      • SWEA (15)
    • 백엔드 (11)
    • Coding existing for (3)
    • 무지성 메모 (40)
    • Debug (30)
    • 자바 (8)

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • 카톡 내보내기한 파일 정렬
  • 카카오톡
  • hibernate
  • 카카오톡 txt파일 정렬
  • MYSQL
  • 카톡

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.2.1
배게
[프로그래머스][JAVA] H-Index (정렬, 이진탐색)
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.