λ¬Έμ μμ½
[쑰건]μ λ§μ‘±νλ μ¬λ μ€ // μ½λ© ν μ€νΈ μ μλ₯Ό Xμ μ΄μ λ°μ μ¬λμ λͺ¨λ λͺ λͺ μΈκ°?
[쑰건]
- μ½λ© ν μ€νΈ μ°Έμ¬ κ°λ° μΈμ΄ νλͺ© λ· μ€ νλλ₯Ό μ ννλ€. (cpp, java, python, -)
- μ§μ μ§κ΅° νλͺ© μ μ€ νλλ₯Ό μ ννλ€. (backend, frontend, -)
- μ§μ κ²½λ ₯ κ΅¬λΆ νλͺ© μ μ€ νλλ₯Ό μ ννλ€. (junior, senior, -)
- μ νΈνλ μμΈ νΈλ μ μ€ νλλ₯Ό μ ννλ€. (chicken, pizza, -)
- 쑰건 λͺ¨λλ₯Ό μ νν μλ μκ³ μ‘°κ±΄ μ€ μΌλΆλ§ μ νν μ μλ€.
- '-' λ μ무 κ²λ μ ννμ§ μμλ€λ κ²μ μλ―Ένλ€.
μ μ½ μ‘°κ±΄
- info: μ§μμμ μ λ ₯ν 4κ°μ§ μ 보μ νλν μ½λ© ν μ€νΈ μ μλ₯Ό νλμ λ¬Έμμ΄λ‘ ꡬμ±ν λ°°μ΄ (1 μ΄μ 50,000 μ΄ν)
- query: κ°λ°νμ΄ κΆκΈν΄νλ λ¬Έμ 쑰건 λ°°μ΄ (νν: "[쑰건] X" - Xλ μ μ μλ―Έ, 1μ΄μ 100,000 μ΄ν)
info | query | result |
["java backend junior pizza 150","python frontend senior chicken 210","python frontend senior chicken 150","cpp backend senior pizza 260","java backend junior chicken 80","python backend senior chicken 50"] | ["java and backend and junior and pizza 100","python and frontend and senior and chicken 200","cpp and - and senior and pizza 250","- and backend and senior and - 150","- and - and - and chicken 100","- and - and - and - 150"] | [1,1,1,1,2,4] |
λ¬Έμ μ κ·Ό
1. μ νμ±λ§ λ§μΆ μ (μ λ ¬ / Binary Search μ΄μ©)
- ν¨μ¨μ± ν
μ€νΈλ₯Ό ν΅κ³Όνμ§ λͺ»ν μ΄μ
- μ΅μ μ κ²½μ° query κ°μ (100,000)
- μ΅μ μ κ²½μ° μ‘°κ±΄μ λ§μ‘±νλ μ§μμ μ (50,000)
- μ§μμμ λͺ¨λ 쑰건 νμ (4)
- O(100,000 * 50,000 * 4) = O(2e10)
- 1 μ΅ λ² κ³μ°μ 1 μ΄λΌ κ°μ νλ€λ©΄, 1μ΄ λ³΄λ€ ν¨μ¬ μ€λ μκ°μ΄ 걸리λ―λ‘ ν¨μ¨μ± ν μ€νΈ ν΅κ³Ό X
// 22.05.15. - 2021 Kakao Blind - Lv2. μμ κ²μ
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
public class Kakao_2021B_lv2 {
public static void main(String[] args) {
String[] info = new String[]{"java backend junior pizza 150",
"python frontend senior chicken 210", "python frontend senior chicken 150",
"cpp backend senior pizza 260", "java backend junior chicken 80",
"python backend senior chicken 50"};
String[] query = new String[]{"java and backend and junior and pizza 100",
"python and frontend and senior and chicken 200",
"cpp and - and senior and pizza 250",
"- and backend and senior and - 150",
"- and - and - and chicken 100",
"- and - and - and - 150"};
int[] answer = solution(info, query);
System.out.println(Arrays.toString(answer));
}
public static int[] solution(String[] info, String[] query) {
int[] answer = new int[query.length];
// input λΆλ₯
ArrayList<String[]> infos = new ArrayList<>();
for (int i = 0, size = info.length; i < size; i++) {
String[] input = info[i].split(" ");
infos.add(input);
}
// μ μμ λ°λΌ μ€λ¦μ°¨μ μ λ ¬
Collections.sort(infos, new Comparator<String[]>() {
@Override
public int compare(String[] o1, String[] o2) {
int item1 = Integer.parseInt(o1[4]);
int item2 = Integer.parseInt(o2[4]);
return item1 - item2;
}
});
// μ μ λ°°μ΄ λ§λ€κΈ°
int[] scores = new int[info.length];
for (int i = 0, size = info.length; i < size; i++) {
scores[i] = Integer.parseInt(infos.get(i)[4]);
}
ArrayList<Integer> result = new ArrayList<Integer>();
for (int q = 0, size = query.length; q < size; q++) {
String[] order = query[q].replace(" and ", " ").split(" ");
int score = Integer.parseInt(order[order.length - 1]);
int cnt = 0;
// μ μλ₯Ό κΈ°μ€μΌλ‘ μμμ μ°ΎκΈ°
int start_point = search(scores, score);
// queryλ₯Ό λΆμνμ¬ μ‘°κ±΄μ λ§μ‘±νλ μ§μμκ° λͺ λͺ
μλμ§ μ°ΎκΈ°
for (int i = start_point, size_infos = infos.size(); i < size_infos; i++) {
String[] each_info = infos.get(i);
boolean flag = true;
for (int j = 0; j < 4; j++) {
if (order[j].equals("-") || order[j].trim().equals(each_info[j]))
continue;
else {
flag = false;
break;
}
}
if (flag)
cnt++;
}
answer[q] = cnt;
}
return answer;
}
// binary search λ₯Ό μ΄μ©ν μμμ μ°ΎκΈ°
public static int search(int[] scores, int criteria) {
int left = 0, right = scores.length;
while (left < right) {
int mid = (left + right) / 2;
if (criteria > scores[mid]) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
}
2. μ νμ± & ν¨μ¨μ± ν΅κ³Ό (HashMap/ DFS/ Binary Search μ΄μ©)
- μ΄λ»κ²νλ©΄ κ²μ μλ₯Ό μ€μΌ μ μμκΉ? HashMap μ΄μ©
- key: 쑰건μ μ‘°ν© (μ μ μ μΈ) = 4 * 3 * 3 * 3 = 108
- value: μ μ
- queryμ μκ° μ‘°ν©μ λ§λλ κ²½μ°μ μλ³΄λ€ ν¨μ¬ ν¬κΈ° λλ¬Έμ μ΅μ μ κ²½μ° μκ° λ³΅μ‘λ: O(100,000)
// 22.05.15. - 2021 Kakao Blind - Lv2. μμ κ²μ
import java.util.*;
public class Kakao_2021B_lv2 {
public static void main(String[] args) {
String[] info = new String[]{"java backend junior pizza 150",
"python frontend senior chicken 210", "python frontend senior chicken 150",
"cpp backend senior pizza 260", "java backend junior chicken 80",
"python backend senior chicken 50"};
String[] query = new String[]{"java and backend and junior and pizza 100",
"python and frontend and senior and chicken 200",
"cpp and - and senior and pizza 250",
"- and backend and senior and - 150",
"- and - and - and chicken 100",
"- and - and - and - 150"};
int[] answer = solution(info, query);
System.out.println(Arrays.toString(answer));
}
private static int[] solution(String[] info, String[] query) {
int[] answer = new int[query.length];
// info κΈ°λ° ν€ μμ±
HashMap<String, ArrayList<Integer>> hm = new HashMap<String, ArrayList<Integer>>();
for (int i = 0, size = info.length; i < size; i++) {
dfs(0, "", info[i].split(" "), hm);
}
// κ° keyλ³ value μ€λ¦μ°¨μ μ λ ¬
for (String key : hm.keySet()) {
Collections.sort(hm.get(key));
}
// 쑰건μ λ§μ‘±νλ μ§μμκ° λͺ λͺ
μΈμ§ μ°ΎκΈ°
for (int i = 0, size = query.length; i < size; i++) {
String[] each = query[i].replace(" and ", " ").split(" ");
int criteria = Integer.parseInt(each[4]);
StringBuilder key = new StringBuilder();
for (int j = 0; j < 4; j++) {
key.append(each[j].trim());
}
// λ°νμ μλ¬κ° λ°μνλ μ΄μ :
// info κΈ°λ°μΌλ‘ hashmapμ keyκ° λ§λ€μ΄μ§κΈ° λλ¬Έμ,
// queryμμ μ°Ύκ³ μ νλ μ‘°κ±΄μ΄ hashmapμ keyλ‘ μ‘΄μ¬νμ§ μμ μ μμ΅λλ€.
if(hm.containsKey(key)){
ArrayList<Integer> scores = hm.get(key.toString());
int search_point = search(scores, criteria);
answer[i] = hm.get(key.toString()).size() - search_point;
}
}
return answer;
}
// μ§μμ μ 보 κΈ°λ° κ°λ₯ν λͺ¨λ ν€ μμ±
private static void dfs(int d, String str, String[] info, HashMap<String, ArrayList<Integer>> hm) {
if (d == 4) {
if (!hm.containsKey(str)) {
ArrayList<Integer> list = new ArrayList<>();
list.add(Integer.parseInt(info[4]));
hm.put(str, list);
} else {
hm.get(str).add(Integer.parseInt(info[4]));
}
return;
}
dfs(d + 1, str + info[d], info, hm);
dfs(d + 1, str + "-", info, hm);
}
// binary search λ₯Ό μ΄μ©ν μμμ μ°ΎκΈ°
private static int search(ArrayList<Integer> scores, int criteria) {
int left = 0, right = scores.size()-1;
while (left < right) {
int mid = (left + right) / 2;
if (criteria > scores.get(mid)) {
left = mid + 1;
} else {
right = mid;
}
}
return left;
}
}
λ¬Έμ λ§ν¬: https://programmers.co.kr/learn/courses/30/lessons/72412
μ½λ©ν μ€νΈ μ°μ΅ - μμ κ²μ
["java backend junior pizza 150","python frontend senior chicken 210","python frontend senior chicken 150","cpp backend senior pizza 260","java backend junior chicken 80","python backend senior chicken 50"] ["java and backend and junior and pizza 100","pyt
programmers.co.kr
곡μ ν΄μ€: https://tech.kakao.com/2021/01/25/2021-kakao-recruitment-round-1/
2021 μΉ΄μΉ΄μ€ μ μ κ³΅μ± 1μ°¨ μ¨λΌμΈ μ½λ© ν μ€νΈ for Tech developers λ¬Έμ ν΄μ€
μ§λ 2020λ 9μ 12μΌ ν μμΌ μ€ν 2μλΆν° 7μκΉμ§ 5μκ° λμ 2021 μΉ΄μΉ΄μ€ μ μ κ°λ°μ κ³΅μ± 1μ°¨ μ½λ© ν μ€νΈκ° μ§νλμμ΅λλ€. ν μ€νΈμλ μ΄ 7κ°μ λ¬Έμ κ° μΆμ λμμΌλ©°, κ°λ° μΈμ΄λ C++, Java, Jav
tech.kakao.com
'Algorithm > Coding Test' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
Kakao 2022 Blind Test - Lv2. μκΆλν (Java) (0) | 2022.06.11 |
---|---|
Kakao 2022 Blind Test - Lv2. kμ§μμμ μμ κ°μ ꡬνκΈ° (Java) (0) | 2022.06.04 |
Kakao 2021 Blind Test - Lv3. κ΄κ³ μ½μ (Java) (0) | 2022.06.02 |
Kakao 2021 Blind Test - Lv3. ν©μΉ νμ μκΈ (Java) (0) | 2022.06.02 |
Kakao 2022 Blind Test - Lv1. μ κ³ κ²°κ³Ό λ°κΈ° (Java) (0) | 2022.05.17 |
λκΈ