Sorting

K번째 수

import java.util.*;

class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
        
        for (int i = 0; i < commands.length; i ++) {
            answer[i] = cal(commands[i][0]
                            , commands[i][1], commands[i][2], array);
        }
        
        return answer;
    }
    
    public int cal(int from, int to, int at, int[] array) {

        int len = to - from + 1;
        int[] arr = new int[len];
        
        for (int i = 0; i < len; i++) {
            arr[i] = array[from-1];
            from++;
        }
        
        Arrays.sort(arr);
        System.out.println(Arrays.toString(arr));
        return arr[at -1];
        
    }
}
import java.util.Arrays;
class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];

        for(int i=0; i<commands.length; i++){
            int[] temp = Arrays.copyOfRange(array, commands[i][0]-1, commands[i][1]);
            Arrays.sort(temp);
            answer[i] = temp[commands[i][2]-1];
        }

        return answer;
    }
}

❌ 가장 큰 수

import java.util.Arrays;

public class Solution {
    public String solution(int[] numbers) {
        String[] arr = new String[numbers.length];

        for (int i = 0; i < arr.length; i++) {
            arr[i] = String.valueOf(numbers[i]);
        }

        Arrays.sort(arr, (o1, o2) -> (o2 + o1).compareTo(o1 + o2));
        
        if (arr[0].equals("0")) {
           return "0";
        }

        StringBuilder answer = new StringBuilder();

        for (int i = 0; i < arr.length; i++) {
            answer.append(arr[i]);
        }


        return answer.toString();
    }
}
// 다른 사람 코드 
public String solution(int[] numbers) {
	String[] stringNumbers = new String[numbers.length];
	
	for(int i = 0; i < numbers.length; i++) {
		stringNumbers[i] = Integer.toString(numbers[i]);
	}
	
	Arrays.sort(stringNumbers, new Comparator<String>() {
		@Override
		public int compare(String o1, String o2) {
			return (o2+o1).compareTo(o1+o2);
		}
	});
	
	if(stringNumbers[0].equals("0")) return "0";
	
	return String.join("", stringNumbers);
}

❌ H-index

  • 이건 문제를 잘 이해 못했다.

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;
    }
}
import java.util.*;

class Solution {
    public int solution(int[] citations) {
        int n = citations.length;
        int answer = 0;
        // int hIndex;
        
        // 0 1 3 5 6 
        Arrays.sort(citations);
        
        int h;
        for (int i = 0 ; i < n; i++) {
            // i일 때 가장 큰 h값(논문 편수)
            h = n - i;
            if (citations[i] >= h)  { 
                answer = h;
                break;
            }
        }
        
        return answer;
    }
}

Last updated