LeetCode每日一题(2528. Maximize the Minimum Powered City)

给定一个整数数组 stations,表示各个城市的发电站数量,以及一个整数 r,表示发电站的覆盖范围。政府计划再建造 k 座相同覆盖范围的发电站。返回在最优情况下,城市中最小电力值的最大可能值。使用二分搜索和滑动窗口策略解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

Constraints:

  • n == stations.length
  • 1 <= n <= 105
  • 0 <= stations[i] <= 105
  • 0 <= r <= n - 1
  • 0 <= k <= 109

通过 binary search 来查找最终结果, 提示里说检查的方法用到了 Line Sweep, 查了一下, 还是没看出来这里怎么用 Line Sweep, 我觉得更像是 Sliding Window。 检查时如果某个城市的电量达不到目标值, 那我们就需要建电站,所建的位置应该是当前位置+辐射半径, 因为前面遍历过的城市的电量一定是可以达到目标值的, 所以给他们增加电量并不会给带来额外的好处。但是后面的城市可能会出现电力不足的情况, 所以我们应该向后建,并保证当前位置是其辐射范围的最左侧即可。



impl Solution {
    fn check(mut stations: Vec<i64>, r: usize, mut k: i64, target: i64) -> bool {
        let length = stations.len();
        let mut sum: i64 = stations[..r].iter().sum();
        for i in 0..stations.len() {
            if i > r {
                sum -= stations[i - r - 1];
            }
            if i + r <= stations.len() - 1 {
                sum += stations[i + r];
            }
            if sum >= target {
                continue;
            }
            let diff = target - sum;
            if k < diff {
                return false;
            }
            stations[(i + r).min(length - 1)] += diff;
            sum += diff;
            k -= diff;
        }
        true
    }
    pub fn max_power(stations: Vec<i32>, r: i32, k: i32) -> i64 {
        let stations: Vec<i64> = stations.into_iter().map(|n| n as i64).collect();
        let mut l = 0;
        let mut h = i64::MAX;
        while l < h {
            let m = (l + h) / 2;
            if Solution::check(stations.clone(), r as usize, k as i64, m) {
                l = m + 1;
                continue;
            }
            h = m;
        }
        if Solution::check(stations.clone(), r as usize, k as i64, l) {
            return l;
        }
        l - 1
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值