[ํ๋ก๊ทธ๋๋จธ์ค] 2022 KAKAO BLIND RECRUITMENT ์ ๊ณ ๊ฒฐ๊ณผ ๋ฐ๊ธฐ
https://programmers.co.kr/learn/courses/30/lessons/92334
๋ฌธ์ ์ ๊ทผ
์ผ๋จ ๋ฌธ์ ๊ฐ ๊ณ ๋ คํด์ผ ํ ๊ฒ๋ค์ด ๋ง์์ ์ฒ์์๋ ์ ๊ณ ๋นํ ํ์, ์ ๊ณ ํ ์ฌ๋, ์๋ฆผ ๋ฉ์ผ์ ๋ฐ์ ํ์๋ฅผ ๋ชจ๋ ๋ฐ๋ก map์ ์ด์ฉํด์ 3๊ฐ์ map์ ๋ง๋ค์ด์ ์ ๊ทผํ๋๋ฐ ๋๋ฌด ๋ณต์กํด์ ๊ทธ๋ฅ 3๊ฐ์ ์ ๋ณด๋ฅผ ๊ฐ์ง User ํด๋์ค๋ฅผ ๋ง๋ค๊ฒ ๋์๋ค. ์ด๋ ๊ฒ ํ๋๊น ๋ ์๊ฐํ๊ธฐ ์ฌ์ ๊ณ ๋ด๊ฐ ๋๋ผ๊ธฐ์๋ ๋ณต์กํจ์ด ๋ง์ด ์ค์ด๋ค์๋ค.
Code
import java.util.HashMap;
import java.util.HashSet;
class Solution {
class User {
HashSet<String> report; // ์ ๊ณ ํ ์ฌ๋๋ค ๋ชจ์
int count; // ์ ๊ณ ๋ฐ์ ํ์
int alarm; // ๋ฉ์ผ ํ์
User() {
count = 0;
alarm = 0;
report = new HashSet<>();
}
public void beReported(){
count += 1;
}
public void addAlarm(){
alarm += 1;
}
}
public int[] solution(String[] id_list, String[] report, int k) {
int[] answer = new int[id_list.length];
HashMap<String, User> users = new HashMap<>();
for(int i = 0; i<id_list.length; i++){
// users ์ด๊ธฐํ
users.put(id_list[i],new User());
}
for (int i = 0; i < report.length; i++) {
// ์ ๊ณ ์ฒ๋ฆฌ
String[] temp = report[i].split(" ");
if(users.get(temp[0]).report.contains(temp[1])) continue;
users.get(temp[0]).report.add(temp[1]);
users.get(temp[1]).beReported();
}
for(int i = 0; i< id_list.length; i++){
// ์ ๊ณ ๊ฒฐ๊ณผ ๋ฉ์ผ ์ฒ๋ฆฌ
if(users.get(id_list[i]).report.isEmpty()){
answer[i] = 0;
continue;
}
for(String reported: users.get(id_list[i]).report){
if(users.get(reported).count >= k){
answer[i] += 1;
}
}
}
return answer;
}
}
์ด๋ ค์ ๋ ์ / ๋ฐฐ์ด ์ / ๋๋ ์
class๋ฅผ ์ด์ฉํ๋๊น ๋ ๊ฐ๋จํ๊ฒ? ์๊ฐํ๊ฒ ๋ ๊ฒ ๊ฐ๋ค. ์ฒ์์ ๋ฌธ์ ๋ฅผ ๋ดค์ ๋๋ ๋ญ์ง ์ด๋ ค์ ๋ณด์ด๋๋ฐ ๋ผ๊ณ ์๊ฐ์ ํ๋๋ฐ ๋ง์ ํ๊ณ ๋ณด๋๊น ์ฌ์ด ๋ฌธ์ ๊ฐ๋ค๊ณ ๋๊ปด์ก๋ค.
๊ทธ๋ฆฌ๊ณ ์ฝํ
์์๋ private์ผ๋ก ์ ์ธํ์ฌ getter/setter๋ฅผ ๋ง๋ค์ด ์ค์ผ ํ ๊น? ๋ผ๋ ์๋ฌธ์ ๊ฐ์ง๊ฒ ๋์๋๋ฐ... ๋๋ฌด ๊ธธ์ด์ ธ์ ๊ตณ์ด ํ ํ์ ์์๊ฒ ๊ฐ๋ค๋ ๋๋์ ๋ฐ์์ ๊ทธ๋ ๊ฒ ํ์ง๋ ์์๋ค.
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Algorithm/Java][ํ๋ก๊ทธ๋๋จธ์ค] ์นด์นด์คํ๋ ์ฆ ์ปฌ๋ฌ๋ง๋ถ (0) | 2022.02.17 |
---|---|
[Algorithm/Java][๋ฐฑ์ค] 1676๋ฒ ํฉํ ๋ฆฌ์ผ 0์ ๊ฐ์ (0) | 2022.02.14 |
[Algorithm/Java][LeetCode] 121. Best Time to Buy and Sell Stock (0) | 2022.02.05 |
[Algorithm/Java][LeetCode] 101. Symmetric Tree (0) | 2022.02.05 |
[Algorithm/Java][๋ฐฑ์ค] 2805๋ฒ ๋๋ฌด ์๋ฅด๊ธฐ (0) | 2022.01.28 |