2024-TEAM-05 / algorithm-for-kakao

카카였 기좜 문제 κ°€μ¦ˆμ•„πŸ£
0 stars 0 forks source link

[2022 KAKAO BLIND RECRUITMENT] μ–‘κ³Ό λŠ‘λŒ€ #20

Open uijin-j opened 1 month ago

uijin-j commented 1 month ago

πŸ”— μ–‘κ³Ό λŠ‘λŒ€

uijin-j commented 1 month ago

πŸ“‘ λŒ“κΈ€ ν…œν”Œλ¦Ώ

μ½”λ“œ 풀이

```java import java.util.*; // 18:55 START! 20:41 END! (μ•½ 2μ‹œκ°„..) // PQλ₯Ό 2개 μ“°λŠ” 문제! class Solution { /** * 이뢄탐색? X * 그리디? 항상 κ°€μž₯ κ°€κΉŒμš΄ μ–‘(λ§Œμ•½ 양이 μ—†μœΌλ©΄ κ°€μž₯ μ–‘κ³Ό κ°€κΉŒμš΄ λŠ‘λŒ€)을 데렀가닀가, 갈 수 μ—†μœΌλ©΄ κ·ΈλŒ€λ‘œ 끝! * 1. λͺ¨λ“  λŠ‘λŒ€ λ…Έλ“œμ—μ„œ κ°€μž₯ κ°€κΉŒμš΄ μ–‘κΉŒμ§€ 거리 κ΅¬ν•˜κΈ° (μ•„λž˜ λ°©ν–₯으둜만) * 2. 루트 λ…Έλ“œλΆ€ν„° bfs with μš°μ„ μˆœμœ„ 큐 * * ❗️ 그런데, λ‹¨μˆœνžˆ κ°€μž₯ κ°€κΉŒμš΄ μ–‘μœΌλ‘œ ν•˜λ©΄ μ•ˆλ¨! λ‹€μŒ 양이 ν˜„μž¬ 갈 수 μžˆλŠ” 거리 내에 있으면 ν•˜μœ„μ— 양이 더 λ§Žμ€ μͺ½μœΌλ‘œ κ°€λŠ” 것이 더 이득! */ public class Node { int num; int type; // 양은 0, λŠ‘λŒ€λŠ” 1 int distToSheep; // κ°€μž₯ κ°€κΉŒμš΄ μ–‘κΉŒμ§€μ˜ 거리 int numOfSheep; // ν•˜μœ„μ— μžˆλŠ” μ–‘μ˜ 수 public Node(int num, int type, int dist, int numOfSheep) { this.num = num; this.type = type; this.distToSheep = dist; this.numOfSheep = numOfSheep; } } public int solution(int[] info, int[][] edges) { int n = info.length; List> tree = new ArrayList<>(); for(int i = 0; i < n; ++i) tree.add(new ArrayList<>()); for(int[] edge : edges) tree.get(edge[0]).add(edge[1]); Map map = new HashMap<>(); for(int i = 0; i < n; ++i) { int type = info[i]; int[] distAndCount = calculateDistAndCount(i, tree, info); map.put(i, new Node(i, type, distAndCount[0], distAndCount[1])); } PriorityQueue pq = new PriorityQueue<>((a, b) -> a.distToSheep - b.distToSheep); pq.offer(map.get(0)); int sheep = 0; int wolf = 0; while(!pq.isEmpty()) { int possible = Math.max(0, sheep - 1 - wolf); PriorityQueue pq2 = new PriorityQueue<>((a, b) -> b.numOfSheep - a.numOfSheep); while(!pq.isEmpty() && pq.peek().distToSheep <= possible) { pq2.offer(pq.poll()); } if(pq2.isEmpty()) break; Node node = pq2.poll(); while(!pq2.isEmpty()) { pq.offer(pq2.poll()); } if(node.type == 0) sheep += 1; else wolf += 1; for(int next : tree.get(node.num)) { pq.offer(map.get(next)); } } return sheep; } // bfs! public int[] calculateDistAndCount(int start, List> tree, int[] info) { Queue q = new LinkedList<>(); q.offer(start); int min = Integer.MAX_VALUE; int count = 0; if(info[start] == 0) { min = 0; count = 1; } int dist = 1; while(!q.isEmpty()) { int size = q.size(); for(int i = 0; i < size; ++i) { int node = q.poll(); for(int next : tree.get(node)) { if(info[next] == 0) { min = Math.min(min, dist); count++; }; q.offer(next); } } dist++; } return new int[]{min, count}; } } ```

μ½”λ©˜νŠΈ

- μ—­μ‹œλ‚˜ 쉽지 μ•Šμ€ λ¬Έμ œμ˜€μŠ΅λ‹ˆλ‹€.. 힌트보고 ν’€μ—ˆλ„€μš”..
hye-on commented 1 month ago

μ½”λ“œ 풀이

```cpp #include #include #include #include using namespace std; //08:52 //0은 μ–‘, 1은 λŠ‘λŒ€ // visit처리 λ•œμ— dfs //μ–‘ μͺ½μ˜ μ΅œλŒ€ 양을 κ°€μ Έμ˜¬ 수 μžˆλŠ” 개수 vectorlink[17]; bool visit[17]; int maxS; vector info; queuesq; void dfs(int node,int s, int w, queueq){ if(s<=w) return; if(s>maxS){ maxS = s; } for(int i=0;i _info, vector> edges) { int answer = 0; info = _info; for(int i=0;i

μ½”λ©˜νŠΈ

- 정말.. 이런 생각을 μ–΄λ–»κ²Œ ν•˜μ£  μ–΄λ €μ› μŠ΅λ‹ˆλ‹€.