https://leetcode.com/problems/minimum-score-of-a-path-between-two-cities/
두 도시 간 경로의 최소 점수 – LeetCode
이 실제 인터뷰 질문을 해결할 수 있습니까? 두 도시 간 경로의 최소 점수 – 1에서 n까지 번호가 매겨진 n개의 도시를 나타내는 양의 정수 n을 가져옵니다. 또한 도로(i) = (ai, bi, distancei)가 다음을 나타내는 도로의 2D 배열을 얻습니다.
leetcode.com
일반적인 BFS 문제
한 가지 주의할 점 1로 시작해서 n으로 끝나기만 하면 됩니다.가리키다!
중간에 1을 방문하든 n을 방문하든 상관없습니다.
연결된 노드와 가중치는 HashMap을 사용하여 저장했습니다. 짧은 코딩 없음
(암호)
class Solution {
public int minScore(int n, int()() roads) {
int start = 1, end = n;
HashMap<Integer, ArrayList<int()>> hm = new HashMap<>();
for(int i=0; i<roads.length; i++){
int a = roads(i)(0), b = roads(i)(1), w = roads(i)(2);
ArrayList<int()> arr = hm.getOrDefault(a, new ArrayList<>());
arr.add(new int() {b,w});
hm.put(a, arr);
arr = hm.getOrDefault(b, new ArrayList<>());
arr.add(new int() {a,w});
hm.put(b, arr);
}
boolean() visited = new boolean(n+1);
visited(start)=true;
Queue<Integer> q = new LinkedList<>();
q.add(start);
int len = Integer.MAX_VALUE;
while(!q.isEmpty()){
int node = q.poll();
if(node == 1)
System.out.println(hm.get(node).size());
for(int() p : hm.getOrDefault(node, new ArrayList<>())){
len = Math.min(p(1), len);
if(visited(p(0)))
continue;
visited(p(0))=true;
q.add(p(0));
}
}
return len;
}
}