쥬로그
Programmers 코딩테스트 고득점 Kit - 정렬 본문
반응형
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;
}
}
더보기
Reference )
가장 큰 수
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;
}
}
반응형
'Algorithm > Programmers' 카테고리의 다른 글
Programmers 코딩테스트 고득점 Kit - 힙 (0) | 2022.06.02 |
---|---|
Programmers 코딩테스트 고득점 Kit - 스택/큐 (0) | 2022.06.02 |
Programmers 코딩테스트 고득점 Kit - 해시 (0) | 2022.06.01 |
Programmers SQL 고득점 Kit - String, Date (MySQL) (0) | 2022.05.31 |
Programmers SQL 고득점 Kit - JOIN (MySQL) (0) | 2022.05.31 |