본문 바로가기

정렬3

[프로그래머스] (정렬) H-Index 논문배열을 역정렬하거나, Arrays.sort를 해서 뒤부터 찾도록 하자. 원소의 인용횟수(인자) vs 정렬한 배열에서 원소보다 크거나 같은 갯수(루프 반복횟수) 를 세어서 역전이 일어나기 바로 직전이 H-Index. import java.util.*; class Solution { public int solution(int[] citations) { int answer = 0; Arrays.sort(citations); int len = citations.length; for (int i = len - 1; i >= 0; i--) { int citation = citations[i]; int iy = len - i; if(iy 2021. 3. 25.
[프로그래머스] (정렬) 가장 큰 수 굉장히 쉽다고 생각했지만 생각보다 굉장히 오래걸린 풀이... numbers sort return [2, 23, 231] 23 231 2 232312 [3, 321, 32] 3 32 321 332321 [3, 312, 31] 3 31 312 331312 위 처럼 다양한 경우에 따라 정렬이 달라져야 하는데, 처음에생각한 방법은 number의 원소는 최대 1000 이므로 첫번째자리의 숫자로 오른쪽을 다 채운다음 단순정렬. numbers rpad sort return [2, 23, 231] [2(22), 23(2), 231] [23(2), 231, 2(22)] 232312 [3, 321, 32] [3(33), 321, 32(3)] [3(33), 32(3), 321] 332321 [3, 312, 31] [3(33.. 2021. 3. 25.
[프로그래머스] (정렬) K번째 수 핵심은 Arrays.copyOfRange 를 사용하는 것. 제출후에 다시보니 쓸데 없이 List를 사용한 것 같음. 괜히 List를 Array로 변환하는 작업만 추가됨. import java.util.*; class Solution { public int[] solution(int[] array, int[][] commands) { int[] answer = new int[commands.length]; List ans = new ArrayList(); for (int[] command : commands) { int[] tmp = Arrays.copyOfRange(array, command[0]-1, command[1]); Arrays.sort(tmp); int t = tmp[command[2]-1];.. 2021. 3. 22.
반응형