Skip to content

Commit ad4671d

Browse files
committed
Add solution and test-cases for problem 2528
1 parent f03226b commit ad4671d

File tree

3 files changed

+116
-13
lines changed

3 files changed

+116
-13
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# [2528.Maximize the Minimum Powered City][title]
2+
3+
## Description
4+
You are given a **0-indexed** integer array `stations` of length `n`, where `stations[i]` represents the number of power stations in the `ith` city.
5+
6+
Each power station can provide power to every city in a fixed **range**. In other words, if the range is denoted by r, then a power station at city `i` can provide power to all cities `j` such that `|i - j| <= r` and `0 <= i, j <= n - 1`.
7+
8+
- Note that `|x|` denotes absolute value. For example, `|7 - 5| = 2` and `|3 - 10| = 7`.
9+
10+
The **power** of a city is the total number of power stations it is being provided power from.
11+
12+
The government has sanctioned building `k` more power stations, each of which can be built in any city, and have the same range as the pre-existing ones.
13+
14+
Given the two integers `r` and `k`, return the **maximum possible minimum power** of a city, if the additional power stations are built optimally.
15+
16+
**Note** that you can build the k power stations in multiple cities.
17+
18+
**Example 1:**
19+
20+
```
21+
Input: stations = [1,2,4,5,0], r = 1, k = 2
22+
Output: 5
23+
Explanation:
24+
One of the optimal ways is to install both the power stations at city 1.
25+
So stations will become [1,4,4,5,0].
26+
- City 0 is provided by 1 + 4 = 5 power stations.
27+
- City 1 is provided by 1 + 4 + 4 = 9 power stations.
28+
- City 2 is provided by 4 + 4 + 5 = 13 power stations.
29+
- City 3 is provided by 5 + 4 = 9 power stations.
30+
- City 4 is provided by 5 + 0 = 5 power stations.
31+
So the minimum power of a city is 5.
32+
Since it is not possible to obtain a larger power, we return 5.
33+
```
34+
35+
**Example 2:**
36+
37+
```
38+
Input: stations = [4,4,4,4], r = 0, k = 3
39+
Output: 4
40+
Explanation:
41+
It can be proved that we cannot make the minimum power of a city greater than 4.
42+
```
43+
44+
## 结语
45+
46+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
47+
48+
[title]: https://leetcode.com/problems/maximize-the-minimum-powered-city
49+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,59 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(stations []int, r int, k int) int64 {
4+
n := len(stations)
5+
cnt := make([]int64, n+1)
6+
for i := 0; i < n; i++ {
7+
left := max(0, i-r)
8+
right := min(n, i+r+1)
9+
cnt[left] += int64(stations[i])
10+
cnt[right] -= int64(stations[i])
11+
}
12+
13+
minVal := int64(stations[0])
14+
sumTotal := int64(0)
15+
for _, s := range stations {
16+
if int64(s) < minVal {
17+
minVal = int64(s)
18+
}
19+
sumTotal += int64(s)
20+
}
21+
22+
lo, hi := minVal, sumTotal+int64(k)
23+
var res int64 = 0
24+
25+
for lo <= hi {
26+
mid := lo + (hi-lo)/2
27+
if check(cnt, mid, r, k) {
28+
res = mid
29+
lo = mid + 1
30+
} else {
31+
hi = mid - 1
32+
}
33+
}
34+
return res
35+
}
36+
37+
func check(cnt []int64, val int64, r int, k int) bool {
38+
n := len(cnt) - 1
39+
diff := make([]int64, len(cnt))
40+
copy(diff, cnt)
41+
var sum int64 = 0
42+
remaining := int64(k)
43+
44+
for i := 0; i < n; i++ {
45+
sum += diff[i]
46+
if sum < val {
47+
add := val - sum
48+
if remaining < add {
49+
return false
50+
}
51+
remaining -= add
52+
end := min(n, i+2*r+1)
53+
diff[end] -= add
54+
sum += add
55+
}
56+
}
57+
58+
return true
559
}

leetcode/2501-2600/2528.Maximize-the-Minimum-Powered-City/Solution_test.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,31 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
stations []int
14+
r, k int
15+
expect int64
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", []int{1, 2, 4, 5, 0}, 1, 2, 5},
18+
{"TestCase2", []int{4, 4, 4, 4}, 0, 3, 4},
1919
}
2020

2121
// 开始测试
2222
for i, c := range cases {
2323
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
24+
got := Solution(c.stations, c.r, c.k)
2525
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
26+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
27+
c.expect, got, c.stations, c.r, c.k)
2828
}
2929
})
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)