issues
search
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
π λκΈ ν νλ¦Ώ
Language : Java
μ±λ₯
μ½λ νμ΄
```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
Language : C++
μ±λ₯
μ½λ νμ΄
```cpp #include
#include
#include
#include
using namespace std; //08:52 //0μ μ, 1μ λλ // visitμ²λ¦¬ λμ dfs //μ μͺ½μ μ΅λ μμ κ°μ Έμ¬ μ μλ κ°μ vector
link[17]; bool visit[17]; int maxS; vector
info; queue
sq; void dfs(int node,int s, int w, queue
q){ 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
μ½λ©νΈ
- μ λ§.. μ΄λ° μκ°μ μ΄λ»κ² νμ£ μ΄λ €μ μ΅λλ€.
π μκ³Ό λλ