Skip to content

Commit 84e46d6

Browse files
add 1334
1 parent 26c71e1 commit 84e46d6

File tree

3 files changed

+114
-0
lines changed
  • paginated_contents/algorithms/2nd_thousand
  • src

3 files changed

+114
-0
lines changed

Diff for: paginated_contents/algorithms/2nd_thousand/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@
321321
| 1339 | [Maximum Product of Splitted Binary Tree](https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1339.java) | | Medium | DFS, Tree |
322322
| 1338 | [Reduce Array Size to The Half](https://leetcode.com/problems/reduce-array-size-to-the-half/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1338.java) | | Medium ||
323323
| 1337 | [Remove Palindromic Subsequences](https://leetcode.com/problems/remove-palindromic-subsequences/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1337.java) | | Easy | String |
324+
| 1334 | [Find the City With the Smallest Number of Neighbors at a Threshold Distance](https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1334.java) | | Medium | Dijkstra's algorithm, Graph
324325
| 1333 | [Filter Restaurants by Vegan-Friendly, Price and Distance](https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1333.java) | | Medium ||
325326
| 1332 | [Remove Palindromic Subsequences](https://leetcode.com/problems/remove-palindromic-subsequences/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1332.java) | | Easy | String |
326327
| 1331 | [Rank Transform of an Array](https://leetcode.com/problems/rank-transform-of-an-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1331.java) | | Easy ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package com.fishercoder.solutions.secondthousand;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.PriorityQueue;
7+
8+
public class _1334 {
9+
public static class Solution1 {
10+
/**
11+
* Dijakstra's algorithm to find the shortest path from each node to all possibly reachable nodes within limit.
12+
* Keys to implement Dijkstra's algorithm:
13+
* 1. use an array to hold the shortest distance to each node for each node;
14+
* 2. initially, only the starting node distance is zero, all other nodes' distances to be infinity;
15+
* 3. use a PriorityQueue to poll out the next node that has the shortest distance and scan through all its neighbors,
16+
* if the cost can be updated, then put it into the priority queue
17+
*/
18+
public int findTheCity(int n, int[][] edges, int distanceThreshold) {
19+
List<List<int[]>> graph = new ArrayList();
20+
int[][] shortestPaths = new int[n][n];
21+
for (int i = 0; i < n; i++) {
22+
graph.add(new ArrayList<>());
23+
}
24+
for (int[] edge : edges) {
25+
int source = edge[0];
26+
int dest = edge[1];
27+
int weight = edge[2];
28+
graph.get(source).add(new int[]{dest, weight});
29+
graph.get(dest).add(new int[]{source, weight});
30+
}
31+
for (int i = 0; i < n; i++) {
32+
dijkstraAlgo(graph, i, shortestPaths[i]);
33+
}
34+
return findCity(shortestPaths, distanceThreshold);
35+
}
36+
37+
private int findCity(int[][] shortestPaths, int distanceThreshold) {
38+
int ans = 0;
39+
int fewestConnected = shortestPaths.length;
40+
for (int i = 0; i < shortestPaths.length; i++) {
41+
int reachable = 0;
42+
for (int j = 0; j < shortestPaths[0].length; j++) {
43+
if (i != j && shortestPaths[i][j] <= distanceThreshold) {
44+
reachable++;
45+
}
46+
}
47+
if (reachable <= fewestConnected) {
48+
fewestConnected = reachable;
49+
ans = i;
50+
}
51+
}
52+
return ans;
53+
}
54+
55+
private void dijkstraAlgo(List<List<int[]>> graph, int startCity, int[] shortestPath) {
56+
Arrays.fill(shortestPath, Integer.MAX_VALUE);
57+
shortestPath[startCity] = 0;
58+
PriorityQueue<int[]> minHeap = new PriorityQueue<>((a, b) -> a[1] - b[1]);
59+
minHeap.offer(new int[]{startCity, 0});
60+
while (!minHeap.isEmpty()) {
61+
int[] curr = minHeap.poll();
62+
int currCity = curr[0];
63+
int currCost = curr[1];
64+
if (currCost > shortestPath[currCity]) {
65+
continue;
66+
}
67+
for (int[] neighbor : graph.get(currCity)) {
68+
int neighborCity = neighbor[0];
69+
int neighborCost = neighbor[1];
70+
if (currCost + neighborCost < shortestPath[neighborCity]) {
71+
shortestPath[neighborCity] = currCost + neighborCost;
72+
minHeap.offer(new int[]{neighborCity, shortestPath[neighborCity]});
73+
}
74+
}
75+
}
76+
}
77+
78+
}
79+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder.secondthousand;
2+
3+
import com.fishercoder.common.utils.CommonUtils;
4+
import com.fishercoder.solutions.secondthousand._1334;
5+
import org.junit.jupiter.api.BeforeEach;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
11+
public class _1334Test {
12+
private static _1334.Solution1 solution1;
13+
14+
@BeforeEach
15+
public void setup() {
16+
solution1 = new _1334.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(3, solution1.findTheCity(4,
22+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,1,3],[1,2,1],[1,3,4],[2,3,1]"),
23+
4));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
assertEquals(5, solution1.findTheCity(6,
29+
CommonUtils.convertLeetCodeIrregularLengths2DArrayInputIntoJavaArray("[0,1,10],[0,2,1],[2,3,1],[1,3,1],[1,4,1],[4,5,10]"),
30+
20));
31+
}
32+
33+
34+
}

0 commit comments

Comments
 (0)