返回介绍

solution / 2400-2499 / 2439.Minimize Maximum of Array / README_EN

发布于 2024-06-17 01:03:06 字数 4534 浏览 0 评论 0 收藏 0

2439. Minimize Maximum of Array

中文文档

Description

You are given a 0-indexed array nums comprising of n non-negative integers.

In one operation, you must:

  • Choose an integer i such that 1 <= i < n and nums[i] > 0.
  • Decrease nums[i] by 1.
  • Increase nums[i - 1] by 1.

Return_ the minimum possible value of the maximum integer of _nums_ after performing any number of operations_.

 

Example 1:

Input: nums = [3,7,1,6]
Output: 5
Explanation:
One set of optimal operations is as follows:
1. Choose i = 1, and nums becomes [4,6,1,6].
2. Choose i = 3, and nums becomes [4,6,2,5].
3. Choose i = 1, and nums becomes [5,5,2,5].
The maximum integer of nums is 5. It can be shown that the maximum number cannot be less than 5.
Therefore, we return 5.

Example 2:

Input: nums = [10,1]
Output: 10
Explanation:
It is optimal to leave nums as is, and since 10 is the maximum value, we return 10.

 

Constraints:

  • n == nums.length
  • 2 <= n <= 105
  • 0 <= nums[i] <= 109

Solutions

Solution 1: Binary Search

To minimize the maximum value of the array, it is intuitive to use binary search. We binary search for the maximum value $mx$ of the array, and find the smallest $mx$ that satisfies the problem requirements.

The time complexity is $O(n \times \log M)$, where $n$ is the length of the array, and $M$ is the maximum value in the array.

class Solution:
  def minimizeArrayValue(self, nums: List[int]) -> int:
    def check(mx):
      d = 0
      for x in nums[:0:-1]:
        d = max(0, d + x - mx)
      return nums[0] + d <= mx

    left, right = 0, max(nums)
    while left < right:
      mid = (left + right) >> 1
      if check(mid):
        right = mid
      else:
        left = mid + 1
    return left
class Solution {
  private int[] nums;

  public int minimizeArrayValue(int[] nums) {
    this.nums = nums;
    int left = 0, right = max(nums);
    while (left < right) {
      int mid = (left + right) >> 1;
      if (check(mid)) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
    return left;
  }

  private boolean check(int mx) {
    long d = 0;
    for (int i = nums.length - 1; i > 0; --i) {
      d = Math.max(0, d + nums[i] - mx);
    }
    return nums[0] + d <= mx;
  }

  private int max(int[] nums) {
    int v = nums[0];
    for (int x : nums) {
      v = Math.max(v, x);
    }
    return v;
  }
}
class Solution {
public:
  int minimizeArrayValue(vector<int>& nums) {
    int left = 0, right = *max_element(nums.begin(), nums.end());
    auto check = [&](int mx) {
      long d = 0;
      for (int i = nums.size() - 1; i; --i) {
        d = max(0l, d + nums[i] - mx);
      }
      return nums[0] + d <= mx;
    };
    while (left < right) {
      int mid = (left + right) >> 1;
      if (check(mid))
        right = mid;
      else
        left = mid + 1;
    }
    return left;
  }
};
func minimizeArrayValue(nums []int) int {
  check := func(mx int) bool {
    d := 0
    for i := len(nums) - 1; i > 0; i-- {
      d = max(0, nums[i]+d-mx)
    }
    return nums[0]+d <= mx
  }

  left, right := 0, slices.Max(nums)
  for left < right {
    mid := (left + right) >> 1
    if check(mid) {
      right = mid
    } else {
      left = mid + 1
    }
  }
  return left
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文