diff --git a/leetcode/2501-2600/2528.Maximize-the-Minimum-Powered-City/README.md b/leetcode/2501-2600/2528.Maximize-the-Minimum-Powered-City/README.md new file mode 100644 index 000000000..963c58fb1 --- /dev/null +++ b/leetcode/2501-2600/2528.Maximize-the-Minimum-Powered-City/README.md @@ -0,0 +1,49 @@ +# [2528.Maximize the Minimum Powered City][title] + +## Description +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. + +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`. + +- Note that `|x|` denotes absolute value. For example, `|7 - 5| = 2` and `|3 - 10| = 7`. + +The **power** of a city is the total number of power stations it is being provided power from. + +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. + +Given the two integers `r` and `k`, return the **maximum possible minimum power** of a city, if the additional power stations are built optimally. + +**Note** that you can build the k power stations in multiple cities. + +**Example 1:** + +``` +Input: stations = [1,2,4,5,0], r = 1, k = 2 +Output: 5 +Explanation: +One of the optimal ways is to install both the power stations at city 1. +So stations will become [1,4,4,5,0]. +- City 0 is provided by 1 + 4 = 5 power stations. +- City 1 is provided by 1 + 4 + 4 = 9 power stations. +- City 2 is provided by 4 + 4 + 5 = 13 power stations. +- City 3 is provided by 5 + 4 = 9 power stations. +- City 4 is provided by 5 + 0 = 5 power stations. +So the minimum power of a city is 5. +Since it is not possible to obtain a larger power, we return 5. +``` + +**Example 2:** + +``` +Input: stations = [4,4,4,4], r = 0, k = 3 +Output: 4 +Explanation: +It can be proved that we cannot make the minimum power of a city greater than 4. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/maximize-the-minimum-powered-city +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2501-2600/2528.Maximize-the-Minimum-Powered-City/Solution.go b/leetcode/2501-2600/2528.Maximize-the-Minimum-Powered-City/Solution.go index d115ccf5e..39c4a248f 100755 --- a/leetcode/2501-2600/2528.Maximize-the-Minimum-Powered-City/Solution.go +++ b/leetcode/2501-2600/2528.Maximize-the-Minimum-Powered-City/Solution.go @@ -1,5 +1,59 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(stations []int, r int, k int) int64 { + n := len(stations) + cnt := make([]int64, n+1) + for i := 0; i < n; i++ { + left := max(0, i-r) + right := min(n, i+r+1) + cnt[left] += int64(stations[i]) + cnt[right] -= int64(stations[i]) + } + + minVal := int64(stations[0]) + sumTotal := int64(0) + for _, s := range stations { + if int64(s) < minVal { + minVal = int64(s) + } + sumTotal += int64(s) + } + + lo, hi := minVal, sumTotal+int64(k) + var res int64 = 0 + + for lo <= hi { + mid := lo + (hi-lo)/2 + if check(cnt, mid, r, k) { + res = mid + lo = mid + 1 + } else { + hi = mid - 1 + } + } + return res +} + +func check(cnt []int64, val int64, r int, k int) bool { + n := len(cnt) - 1 + diff := make([]int64, len(cnt)) + copy(diff, cnt) + var sum int64 = 0 + remaining := int64(k) + + for i := 0; i < n; i++ { + sum += diff[i] + if sum < val { + add := val - sum + if remaining < add { + return false + } + remaining -= add + end := min(n, i+2*r+1) + diff[end] -= add + sum += add + } + } + + return true } diff --git a/leetcode/2501-2600/2528.Maximize-the-Minimum-Powered-City/Solution_test.go b/leetcode/2501-2600/2528.Maximize-the-Minimum-Powered-City/Solution_test.go index 14ff50eb4..aa1cffbf6 100755 --- a/leetcode/2501-2600/2528.Maximize-the-Minimum-Powered-City/Solution_test.go +++ b/leetcode/2501-2600/2528.Maximize-the-Minimum-Powered-City/Solution_test.go @@ -9,31 +9,31 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + stations []int + r, k int + expect int64 }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 2, 4, 5, 0}, 1, 2, 5}, + {"TestCase2", []int{4, 4, 4, 4}, 0, 3, 4}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.stations, c.r, c.k) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v", + c.expect, got, c.stations, c.r, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }