728x90
๐ํ์ด
1. ๊ฐ ์ฃผ๋ฌธ์ ์ํ๋ฒณ ์์๋ก ์ ๋ ฌํ ๋ค, ์กฐํฉ์ ์์ฑํ๋ค.
2. ์์ฑ๋ ์กฐํฉ์ด ๋ํ๋๋ ํ์๋ฅผ ๊ธฐ๋กํ ๋ค, ๊ฐ ๊ธธ์ด์ ๋ํด ๊ฐ์ฅ ๋ง์ด ์ฃผ๋ฌธ๋ ์กฐํฉ์ ์ฐพ๋๋ค.
3. ์ต์ข ์ ์ผ๋ก ๋ชจ๋ ๊ฒฐ๊ณผ๋ฅผ ์ฌ์ ์์ผ๋ก ์ ๋ ฌํ์ฌ ๋ฐํํ๋ค.
์ต์ ์ ๊ฒฝ์ฐ 10*20*2^10
๐์ฝ๋
import java.util.*;
class Solution {
public static void getCombination(Map<String, Integer> ret, String str, int N, int depth, int start, char[] cur){
if(depth>=N){
if(!ret.containsKey(new String(cur))) ret.put(new String(cur), 0);
ret.put(new String(cur), ret.get(new String(cur))+1);
return;
}
for(int i=start, LEN=str.length(); i<LEN; i++){
cur[depth]=str.charAt(i);
getCombination(ret, str, N, depth+1, i+1, cur);
}
}
public String[] solution(String[] orders, int[] course) {
List<String> answer = new ArrayList<>();
for(int c : course) {
Map<String, Integer> map = new HashMap<>();
for(String order : orders){
char[] tmp = order.toCharArray();
Arrays.sort(tmp);
String sortedStr = new String(tmp);
getCombination(map, sortedStr, c, 0, 0, new char[c]);
}
int maxVal = 2;
for(int val : map.values()){
if(val>maxVal) maxVal = val;
}
for(String key : map.keySet()){
if(map.get(key)==maxVal) answer.add(key);
}
}
Collections.sort(answer);
return answer.toArray(new String[0]);
}
}
728x90
'์๊ณ ๋ฆฌ์ฆ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๊ณ ๋ฆฌ์ฆ]์ต์์ ์ฅํธ๋ฆฌ: kruskal,prim์๊ณ ๋ฆฌ์ฆ - Java๋ก ๊ตฌํํ๊ธฐ (1) | 2024.02.24 |
---|---|
[์๊ณ ๋ฆฌ์ฆ]์์ด, ์กฐํฉ, ๋ถ๋ถ์งํฉ - Java๋ก ๊ตฌํํ๊ธฐ (1) | 2024.02.24 |
[์๊ณ ๋ฆฌ์ฆ]DynamicProgramming (0) | 2022.04.23 |
[์๊ณ ๋ฆฌ์ฆ]Sorting Algorithm_RadixSort (0) | 2022.04.23 |
[์๊ณ ๋ฆฌ์ฆ]Sorting Algorithm_HeapSort (0) | 2022.04.23 |