쥬로그

Programmers 코딩테스트 고득점 Kit - 정렬 본문

Algorithm/Programmers

Programmers 코딩테스트 고득점 Kit - 정렬

쥬쥬씨 2022. 6. 1. 23:41
반응형

 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

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++){
            int[] command = commands[i];
            int startNum = command[0];
            int endNum = command[1];
            int idx = command[2];
            ArrayList<Integer> temp = new ArrayList<>();
            for(int j = startNum - 1; j < endNum; j++){
                temp.add(array[j]);
            }
            Collections.sort(temp);
            answer[i] = temp.get(idx-1);
        }
        return answer;
    }
}
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.*;
class Solution {
    public String solution(int[] numbers) {
        String[] stringNum = new String[numbers.length];

        for(int i = 0; i < numbers.length; i++){
            stringNum[i] = Integer.toString(numbers[i]);
        }
        Arrays.sort(stringNum, new Comparator<String>(){
            @Override
            public int compare(String s1, String s2){
                return(s2 + s1).compareTo(s1 + s2);
            }
        });
        if(stringNum[0].equals("0")){
            return "0";
        }
        return String.join("", stringNum);
    }
}
import java.util.*;
class Solution {
    public String solution(int[] numbers) {
        String[] stringNum = new String[numbers.length];

        for(int i = 0; i < numbers.length; i++){
            stringNum[i] = numbers[i]+"";
        }
        Arrays.sort(stringNum, new Comparator<String>(){
        	public int compare(String o1, String o2){
            	return (o2 + o1).compareTo(o1 + o2);
            }
        });
        
        String answer = "";
        for(int i = 0; i < numbers.length; i++){
        	answer += stringNum[i];
        }
        return answer.charAt(0) == '0' ? "0" : answer;
    }
}
import java.util.*;
class Solution {
	List<Integer> list = new ArrayList<>();
		for(int i = 0; i < numbers.length; i++) {
			list.add(numbers[i]);
		}
		Collections.sort(list, (a, b) -> {
			String as = String.valueOf(a), bs = String.valueOf(b);
			System.out.println("as: " + as + " bs: " + bs);
			System.out.println(list.toString()+"\n");
			return -Integer.compare(Integer.parseInt(as+bs), Integer.parseInt(bs+as));
		});
		StringBuilder sb = new StringBuilder();
		for(Integer i : list) {
			sb.append(i);
		}
		String answer = "";
		answer = sb.toString();
		if(answer.charAt(0) == '0') {
			return "0";
		} else {
			return answer;
		}
}

 

 

H-Index
import java.util.*;
class Solution {
    public int solution(int[] citations) {
        int answer = 0;
        
        Arrays.sort(citations);
        int h_index = 0;
        for(int i = citations.length - 1; i >= 0; i--){
            if(h_index < citations[i]){
                h_index++;
            }
        }
        answer = h_index;
        return answer;
    }
}
import java.util.*;

class Solution{
	public int solution(int[] citations){
    	Arrays.sort(citations);
        
        int max = 0;
        for(int i = citations.length-1; i >= 0; i--){
        	int min = (int)Math.min(citations[i], citations.length-i);
            if(max < min){
            	max = min;
            }
        }
        return max;
    }
}
반응형