Skip to content

Commit 1f50517

Browse files
committed
Add solution #1334
1 parent d4376b6 commit 1f50517

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,235 LeetCode solutions in JavaScript
1+
# 1,236 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1012,6 +1012,7 @@
10121012
1331|[Rank Transform of an Array](./solutions/1331-rank-transform-of-an-array.js)|Easy|
10131013
1332|[Remove Palindromic Subsequences](./solutions/1332-remove-palindromic-subsequences.js)|Easy|
10141014
1333|[Filter Restaurants by Vegan-Friendly, Price and Distance](./solutions/1333-filter-restaurants-by-vegan-friendly-price-and-distance.js)|Medium|
1015+
1334|[Find the City With the Smallest Number of Neighbors at a Threshold Distance](./solutions/1334-find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance.js)|Medium|
10151016
1342|[Number of Steps to Reduce a Number to Zero](./solutions/1342-number-of-steps-to-reduce-a-number-to-zero.js)|Easy|
10161017
1351|[Count Negative Numbers in a Sorted Matrix](./solutions/1351-count-negative-numbers-in-a-sorted-matrix.js)|Easy|
10171018
1352|[Product of the Last K Numbers](./solutions/1352-product-of-the-last-k-numbers.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* 1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance
3+
* https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/
4+
* Difficulty: Medium
5+
*
6+
* There are n cities numbered from 0 to n-1. Given the array edges where
7+
* edges[i] = [fromi, toi, weighti] represents a bidirectional and weighted edge between cities
8+
* fromi and toi, and given the integer distanceThreshold.
9+
*
10+
* Return the city with the smallest number of cities that are reachable through some path and whose
11+
* distance is at most distanceThreshold, If there are multiple such cities, return the city with
12+
* the greatest number.
13+
*
14+
* Notice that the distance of a path connecting cities i and j is equal to the sum of the edges'
15+
* weights along that path.
16+
*/
17+
18+
/**
19+
* @param {number} n
20+
* @param {number[][]} edges
21+
* @param {number} distanceThreshold
22+
* @return {number}
23+
*/
24+
var findTheCity = function(n, edges, distanceThreshold) {
25+
const distances = Array.from({ length: n }, () => Array(n).fill(Infinity));
26+
27+
for (let i = 0; i < n; i++) {
28+
distances[i][i] = 0;
29+
}
30+
31+
for (const [from, to, weight] of edges) {
32+
distances[from][to] = weight;
33+
distances[to][from] = weight;
34+
}
35+
36+
for (let k = 0; k < n; k++) {
37+
for (let i = 0; i < n; i++) {
38+
for (let j = 0; j < n; j++) {
39+
distances[i][j] = Math.min(distances[i][j], distances[i][k] + distances[k][j]);
40+
}
41+
}
42+
}
43+
44+
let minNeighbors = n;
45+
let result = 0;
46+
47+
for (let i = 0; i < n; i++) {
48+
const neighbors = distances[i].filter(dist => dist <= distanceThreshold).length - 1;
49+
if (neighbors <= minNeighbors) {
50+
minNeighbors = neighbors;
51+
result = i;
52+
}
53+
}
54+
55+
return result;
56+
};

0 commit comments

Comments
 (0)