Skip to content

Commit b2fd42c

Browse files
committedApr 23, 2025
Add solution #1620
1 parent c975daa commit b2fd42c

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,425 LeetCode solutions in JavaScript
1+
# 1,426 LeetCode solutions in JavaScript
22

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

@@ -1249,6 +1249,7 @@
12491249
1616|[Split Two Strings to Make Palindrome](./solutions/1616-split-two-strings-to-make-palindrome.js)|Medium|
12501250
1617|[Count Subtrees With Max Distance Between Cities](./solutions/1617-count-subtrees-with-max-distance-between-cities.js)|Hard|
12511251
1619|[Mean of Array After Removing Some Elements](./solutions/1619-mean-of-array-after-removing-some-elements.js)|Easy|
1252+
1620|[Coordinate With Maximum Network Quality](./solutions/1620-coordinate-with-maximum-network-quality.js)|Medium|
12521253
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12531254
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12541255
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* 1620. Coordinate With Maximum Network Quality
3+
* https://leetcode.com/problems/coordinate-with-maximum-network-quality/
4+
* Difficulty: Medium
5+
*
6+
* You are given an array of network towers towers, where towers[i] = [xi, yi, qi] denotes the
7+
* ith network tower with location (xi, yi) and quality factor qi. All the coordinates are
8+
* integral coordinates on the X-Y plane, and the distance between the two coordinates is the
9+
* Euclidean distance.
10+
*
11+
* You are also given an integer radius where a tower is reachable if the distance is less than
12+
* or equal to radius. Outside that distance, the signal becomes garbled, and the tower is not
13+
* reachable.
14+
*
15+
* The signal quality of the ith tower at a coordinate (x, y) is calculated with the formula
16+
* ⌊qi / (1 + d)⌋, where d is the distance between the tower and the coordinate. The network
17+
* quality at a coordinate is the sum of the signal qualities from all the reachable towers.
18+
*
19+
* Return the array [cx, cy] representing the integral coordinate (cx, cy) where the network
20+
* quality is maximum. If there are multiple coordinates with the same network quality, return
21+
* the lexicographically minimum non-negative coordinate.
22+
*
23+
* Note:
24+
* - A coordinate (x1, y1) is lexicographically smaller than (x2, y2) if either:
25+
* - x1 < x2, or
26+
* - x1 == x2 and y1 < y2.
27+
* - ⌊val⌋ is the greatest integer less than or equal to val (the floor function).
28+
*/
29+
30+
/**
31+
* @param {number[][]} towers
32+
* @param {number} radius
33+
* @return {number[]}
34+
*/
35+
var bestCoordinate = function(towers, radius) {
36+
let maxQuality = 0;
37+
let optimalCoord = [0, 0];
38+
39+
for (let x = 0; x <= 50; x++) {
40+
for (let y = 0; y <= 50; y++) {
41+
let currentQuality = 0;
42+
43+
for (const [towerX, towerY, quality] of towers) {
44+
const distance = calculateDistance(x, y, towerX, towerY);
45+
if (distance <= radius) {
46+
currentQuality += Math.floor(quality / (1 + distance));
47+
}
48+
}
49+
50+
if (currentQuality > maxQuality) {
51+
maxQuality = currentQuality;
52+
optimalCoord = [x, y];
53+
} else if (currentQuality === maxQuality && currentQuality > 0) {
54+
if (x < optimalCoord[0] || (x === optimalCoord[0] && y < optimalCoord[1])) {
55+
optimalCoord = [x, y];
56+
}
57+
}
58+
}
59+
}
60+
61+
return optimalCoord;
62+
63+
function calculateDistance(x1, y1, x2, y2) {
64+
return Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2);
65+
}
66+
};

0 commit comments

Comments
 (0)
Please sign in to comment.