-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path여행경로.java
More file actions
26 lines (20 loc) · 789 Bytes
/
여행경로.java
File metadata and controls
26 lines (20 loc) · 789 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.util.*;
class Solution {
public String[] solution(String[][] tickets) {
Map<String, PriorityQueue<String>> map = new HashMap<>();
List<String> res = new ArrayList<>();
for (String[] ticket : tickets) {
map.putIfAbsent(ticket[0], new PriorityQueue<>());
map.get(ticket[0]).add(ticket[1]);
}
Deque<String> stack = new ArrayDeque<>();
stack.push("ICN");
while (!stack.isEmpty()) {
while (map.containsKey(stack.getFirst()) && !map.get(stack.getFirst()).isEmpty()) {
stack.push(map.get(stack.getFirst()).poll());
}
res.add(0, stack.pop());
}
return res.toArray(new String[0]);
}
}