728x90
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
풀이
{{2}, {2, 1}, {2, 1, 3}, {2, 1, 3, 4}}
{{2, 1, 3, 4}, {2}, {2, 1, 3}, {2, 1}}
{{1, 2, 3}, {2, 1}, {1, 2, 4, 3}, {2}}
예제에서 위 처럼 집합이 나올 경우, 튜플은 (2, 1, 3, 4)가 된다.
튜플의 순서는 어떻게 설정해줘야 할까에 대해 의문이 있었으나
가장 많이 나온 빈도수 순으로 정렬해주면 된다.
전체코드
import java.io.*;
import java.util.*;
class Solution {
public Integer[] solution(String s) {
HashMap<Integer, Integer> map = new HashMap<>();
String tmp = "";
for(int i=0; i<s.length(); i++){
if(s.charAt(i) >=48 && s.charAt(i) <= 57){
tmp += s.charAt(i);
}else{
if(tmp.length()!=0){
if(map.containsKey(Integer.valueOf(tmp))){
map.put(Integer.valueOf(tmp) ,map.get(Integer.valueOf(tmp))+1);
}else{
map.put(Integer.valueOf(tmp), 1);
}
tmp="";
}else{
continue;
}
}
}
Integer arr[] = new Integer[map.size()];
int cnt = 0;
for(int mapKey : map.keySet()){
arr[cnt++] = mapKey;
}
Arrays.sort(arr, new Comparator<Integer>(){
public int compare(Integer o1, Integer o2){
if(map.get(o1) > map.get(o2)){
return -1;
}else if(map.get(o1) < map.get(o2)){
return 1;
}
return 0;
}
});
return arr;
}
}
728x90
'코딩테스트 > Algorithm' 카테고리의 다른 글
[Programmers] 귤 고르기 (JAVA) (0) | 2024.07.25 |
---|---|
[Programmers] 연속된 부분 수열의 합 (JAVA) (2) | 2024.07.23 |
[BOJ] 2870: 수학숙제 (JAVA) (0) | 2024.07.20 |
[Programmers] 2 x n 타일링 (JAVA) (0) | 2024.07.18 |
[Programmers] 땅따먹기 (JAVA) (0) | 2024.07.17 |