Skip to content

Commit da0d3c6

Browse files
committed
feat: add solutions to lc problem: No.1620
No.1620.Coordinate With Maximum Network Quality
1 parent d14210a commit da0d3c6

File tree

8 files changed

+273
-3
lines changed

8 files changed

+273
-3
lines changed

solution/0600-0699/0670.Maximum Swap/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
**方法一:贪心**
3838

39-
先将数字转为字符串 `s`,然后从右往左遍历字符串 `s`用数组 `d` 记录每个字符右侧的最大位置(可以是字符本身的位置)。
39+
先将数字转为字符串 `s`,然后从右往左遍历字符串 `s`用数组或哈希表 `d` 记录每个数字右侧的最大数字的位置(可以是字符本身的位置)。
4040

4141
接着从左到右遍历 `d`,如果 `s[i] < s[d[i]]`,则进行交换,并退出遍历的过程。
4242

solution/1600-1699/1619.Mean of Array After Removing Some Elements/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@
6262

6363
<!-- 这里可写通用的实现逻辑 -->
6464

65+
**方法一:模拟**
66+
67+
直接模拟。
68+
69+
先对数组 `arr` 排序,然后截取中间的 90% 个元素,求平均值。
70+
71+
时间复杂度 $O(n\log n)$。其中 $n$ 为数组 `arr` 的长度。
72+
6573
<!-- tabs:start -->
6674

6775
### **Python3**

solution/1600-1699/1620.Coordinate With Maximum Network Quality/README.md

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,116 @@
7373

7474
<!-- 这里可写通用的实现逻辑 -->
7575

76+
**方法一:暴力枚举**
77+
78+
由于坐标点的范围是 `[0, 50]`,因此我们可以直接暴力枚举所有的坐标点 $(i, j)$,计算每个坐标点的信号强度,然后找出信号强度最大的坐标点。
79+
80+
时间复杂度 $O(n \times C^2)$,其中 $n$ 是信号塔的数量,而 $C$ 是坐标点的范围大小。
81+
7682
<!-- tabs:start -->
7783

7884
### **Python3**
7985

8086
<!-- 这里可写当前语言的特殊实现逻辑 -->
8187

8288
```python
83-
89+
class Solution:
90+
def bestCoordinate(self, towers: List[List[int]], radius: int) -> List[int]:
91+
mx = 0
92+
ans = [0, 0]
93+
for i in range(51):
94+
for j in range(51):
95+
t = 0
96+
for x, y, q in towers:
97+
d = ((x - i) ** 2 + (y - j) ** 2) ** 0.5
98+
if d <= radius:
99+
t += floor(q / (1 + d))
100+
if t > mx:
101+
mx = t
102+
ans = [i, j]
103+
return ans
84104
```
85105

86106
### **Java**
87107

88108
<!-- 这里可写当前语言的特殊实现逻辑 -->
89109

90110
```java
111+
class Solution {
112+
public int[] bestCoordinate(int[][] towers, int radius) {
113+
int mx = 0;
114+
int[] ans = new int[] {0, 0};
115+
for (int i = 0; i < 51; ++i) {
116+
for (int j = 0; j < 51; ++j) {
117+
int t = 0;
118+
for (var e : towers) {
119+
double d = Math.sqrt((i - e[0]) * (i - e[0]) + (j - e[1]) * (j - e[1]));
120+
if (d <= radius) {
121+
t += Math.floor(e[2] / (1 + d));
122+
}
123+
}
124+
if (mx < t) {
125+
mx = t;
126+
ans = new int[] {i, j};
127+
}
128+
}
129+
}
130+
return ans;
131+
}
132+
}
133+
```
134+
135+
### **C++**
136+
137+
```cpp
138+
class Solution {
139+
public:
140+
vector<int> bestCoordinate(vector<vector<int>>& towers, int radius) {
141+
int mx = 0;
142+
vector<int> ans = {0, 0};
143+
for (int i = 0; i < 51; ++i) {
144+
for (int j = 0; j < 51; ++j) {
145+
int t = 0;
146+
for (auto& e : towers) {
147+
double d = sqrt((i - e[0]) * (i - e[0]) + (j - e[1]) * (j - e[1]));
148+
if (d <= radius) {
149+
t += floor(e[2] / (1 + d));
150+
}
151+
}
152+
if (mx < t) {
153+
mx = t;
154+
ans = {i, j};
155+
}
156+
}
157+
}
158+
return ans;
159+
}
160+
};
161+
```
91162
163+
### **Go**
164+
165+
```go
166+
func bestCoordinate(towers [][]int, radius int) []int {
167+
ans := []int{0, 0}
168+
mx := 0
169+
for i := 0; i < 51; i++ {
170+
for j := 0; j < 51; j++ {
171+
t := 0
172+
for _, e := range towers {
173+
d := math.Sqrt(float64((i-e[0])*(i-e[0]) + (j-e[1])*(j-e[1])))
174+
if d <= float64(radius) {
175+
t += int(float64(e[2]) / (1 + d))
176+
}
177+
}
178+
if mx < t {
179+
mx = t
180+
ans = []int{i, j}
181+
}
182+
}
183+
}
184+
return ans
185+
}
92186
```
93187

94188
### **...**

solution/1600-1699/1620.Coordinate With Maximum Network Quality/README_EN.md

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,101 @@ No other coordinate has a higher network quality.</pre>
7171
### **Python3**
7272

7373
```python
74-
74+
class Solution:
75+
def bestCoordinate(self, towers: List[List[int]], radius: int) -> List[int]:
76+
mx = 0
77+
ans = [0, 0]
78+
for i in range(51):
79+
for j in range(51):
80+
t = 0
81+
for x, y, q in towers:
82+
d = ((x - i) ** 2 + (y - j) ** 2) ** 0.5
83+
if d <= radius:
84+
t += floor(q / (1 + d))
85+
if t > mx:
86+
mx = t
87+
ans = [i, j]
88+
return ans
7589
```
7690

7791
### **Java**
7892

7993
```java
94+
class Solution {
95+
public int[] bestCoordinate(int[][] towers, int radius) {
96+
int mx = 0;
97+
int[] ans = new int[] {0, 0};
98+
for (int i = 0; i < 51; ++i) {
99+
for (int j = 0; j < 51; ++j) {
100+
int t = 0;
101+
for (var e : towers) {
102+
double d = Math.sqrt((i - e[0]) * (i - e[0]) + (j - e[1]) * (j - e[1]));
103+
if (d <= radius) {
104+
t += Math.floor(e[2] / (1 + d));
105+
}
106+
}
107+
if (mx < t) {
108+
mx = t;
109+
ans = new int[] {i, j};
110+
}
111+
}
112+
}
113+
return ans;
114+
}
115+
}
116+
```
117+
118+
### **C++**
119+
120+
```cpp
121+
class Solution {
122+
public:
123+
vector<int> bestCoordinate(vector<vector<int>>& towers, int radius) {
124+
int mx = 0;
125+
vector<int> ans = {0, 0};
126+
for (int i = 0; i < 51; ++i) {
127+
for (int j = 0; j < 51; ++j) {
128+
int t = 0;
129+
for (auto& e : towers) {
130+
double d = sqrt((i - e[0]) * (i - e[0]) + (j - e[1]) * (j - e[1]));
131+
if (d <= radius) {
132+
t += floor(e[2] / (1 + d));
133+
}
134+
}
135+
if (mx < t) {
136+
mx = t;
137+
ans = {i, j};
138+
}
139+
}
140+
}
141+
return ans;
142+
}
143+
};
144+
```
80145
146+
### **Go**
147+
148+
```go
149+
func bestCoordinate(towers [][]int, radius int) []int {
150+
ans := []int{0, 0}
151+
mx := 0
152+
for i := 0; i < 51; i++ {
153+
for j := 0; j < 51; j++ {
154+
t := 0
155+
for _, e := range towers {
156+
d := math.Sqrt(float64((i-e[0])*(i-e[0]) + (j-e[1])*(j-e[1])))
157+
if d <= float64(radius) {
158+
t += int(float64(e[2]) / (1 + d))
159+
}
160+
}
161+
if mx < t {
162+
mx = t
163+
ans = []int{i, j}
164+
}
165+
}
166+
}
167+
return ans
168+
}
81169
```
82170

83171
### **...**
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
vector<int> bestCoordinate(vector<vector<int>>& towers, int radius) {
4+
int mx = 0;
5+
vector<int> ans = {0, 0};
6+
for (int i = 0; i < 51; ++i) {
7+
for (int j = 0; j < 51; ++j) {
8+
int t = 0;
9+
for (auto& e : towers) {
10+
double d = sqrt((i - e[0]) * (i - e[0]) + (j - e[1]) * (j - e[1]));
11+
if (d <= radius) {
12+
t += floor(e[2] / (1 + d));
13+
}
14+
}
15+
if (mx < t) {
16+
mx = t;
17+
ans = {i, j};
18+
}
19+
}
20+
}
21+
return ans;
22+
}
23+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func bestCoordinate(towers [][]int, radius int) []int {
2+
ans := []int{0, 0}
3+
mx := 0
4+
for i := 0; i < 51; i++ {
5+
for j := 0; j < 51; j++ {
6+
t := 0
7+
for _, e := range towers {
8+
d := math.Sqrt(float64((i-e[0])*(i-e[0]) + (j-e[1])*(j-e[1])))
9+
if d <= float64(radius) {
10+
t += int(float64(e[2]) / (1 + d))
11+
}
12+
}
13+
if mx < t {
14+
mx = t
15+
ans = []int{i, j}
16+
}
17+
}
18+
}
19+
return ans
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int[] bestCoordinate(int[][] towers, int radius) {
3+
int mx = 0;
4+
int[] ans = new int[] {0, 0};
5+
for (int i = 0; i < 51; ++i) {
6+
for (int j = 0; j < 51; ++j) {
7+
int t = 0;
8+
for (var e : towers) {
9+
double d = Math.sqrt((i - e[0]) * (i - e[0]) + (j - e[1]) * (j - e[1]));
10+
if (d <= radius) {
11+
t += Math.floor(e[2] / (1 + d));
12+
}
13+
}
14+
if (mx < t) {
15+
mx = t;
16+
ans = new int[] {i, j};
17+
}
18+
}
19+
}
20+
return ans;
21+
}
22+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
def bestCoordinate(self, towers: List[List[int]], radius: int) -> List[int]:
3+
mx = 0
4+
ans = [0, 0]
5+
for i in range(51):
6+
for j in range(51):
7+
t = 0
8+
for x, y, q in towers:
9+
d = ((x - i) ** 2 + (y - j) ** 2) ** 0.5
10+
if d <= radius:
11+
t += floor(q / (1 + d))
12+
if t > mx:
13+
mx = t
14+
ans = [i, j]
15+
return ans

0 commit comments

Comments
 (0)