返回介绍

solution / 1600-1699 / 1674.Minimum Moves to Make Array Complementary / README_EN

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

1674. Minimum Moves to Make Array Complementary

中文文档

Description

You are given an integer array nums of even length n and an integer limit. In one move, you can replace any integer from nums with another integer between 1 and limit, inclusive.

The array nums is complementary if for all indices i (0-indexed), nums[i] + nums[n - 1 - i] equals the same number. For example, the array [1,2,3,4] is complementary because for all indices i, nums[i] + nums[n - 1 - i] = 5.

Return the _minimum number of moves required to make _nums_ complementary_.

 

Example 1:

Input: nums = [1,2,4,3], limit = 4
Output: 1
Explanation: In 1 move, you can change nums to [1,2,2,3] (underlined elements are changed).
nums[0] + nums[3] = 1 + 3 = 4.
nums[1] + nums[2] = 2 + 2 = 4.
nums[2] + nums[1] = 2 + 2 = 4.
nums[3] + nums[0] = 3 + 1 = 4.
Therefore, nums[i] + nums[n-1-i] = 4 for every i, so nums is complementary.

Example 2:

Input: nums = [1,2,2,1], limit = 2
Output: 2
Explanation: In 2 moves, you can change nums to [2,2,2,2]. You cannot change any number to 3 since 3 > limit.

Example 3:

Input: nums = [1,2,1,2], limit = 2
Output: 0
Explanation: nums is already complementary.

 

Constraints:

  • n == nums.length
  • 2 <= n <= 105
  • 1 <= nums[i] <= limit <= 105
  • n is even.

Solutions

Solution 1: Difference Array

Let's denote $a$ as the smaller value between $nums[i]$ and $nums[n-i-1]$, and $b$ as the larger value between $nums[i]$ and $nums[n-i-1]$.

Suppose that after replacement, the sum of the two numbers is $x$. From the problem, we know that the minimum value of $x$ is $2$, which means both numbers are replaced by $1$; the maximum value is $2 \times limit$, which means both numbers are replaced by $limit$. Therefore, the range of $x$ is $[2,… 2 \times limit]$.

How to find the minimum number of replacements for different $x$?

We analyze and find:

  • If $x = a + b$, then the number of replacements we need is $0$, which means the current pair of numbers already meets the complement requirement;
  • Otherwise, if $1 + a \le x \le limit + b $, then the number of replacements we need is $1$, which means we can replace one of the numbers;
  • Otherwise, if $2 \le x \le 2 \times limit$, then the number of replacements we need is $2$, which means we need to replace both numbers.

Therefore, we can iterate over each pair of numbers and perform the following operations:

  1. First, add $2$ to the number of operations required in the range $[2,… 2 \times limit]$.
  2. Then, subtract $1$ from the number of operations required in the range $[1 + a,… limit + b]$.
  3. Finally, subtract $1$ from the number of operations required in the range $[a + b,… a + b]$.

We can see that this is actually adding and subtracting elements in a continuous interval, so we can use a difference array to implement it.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.

class Solution:
  def minMoves(self, nums: List[int], limit: int) -> int:
    d = [0] * (limit * 2 + 2)
    n = len(nums)

    for i in range(n >> 1):
      a, b = min(nums[i], nums[n - i - 1]), max(nums[i], nums[n - i - 1])

      d[2] += 2
      d[limit * 2 + 1] -= 2

      d[a + 1] -= 1
      d[b + limit + 1] += 1

      d[a + b] -= 1
      d[a + b + 1] += 1

    ans, s = n, 0
    for v in d[2 : limit * 2 + 1]:
      s += v
      if ans > s:
        ans = s
    return ans
class Solution {
  public int minMoves(int[] nums, int limit) {
    int n = nums.length;
    int[] d = new int[limit * 2 + 2];
    for (int i = 0; i < n >> 1; ++i) {
      int a = Math.min(nums[i], nums[n - i - 1]);
      int b = Math.max(nums[i], nums[n - i - 1]);

      d[2] += 2;
      d[limit * 2 + 1] -= 2;

      d[a + 1] -= 1;
      d[b + limit + 1] += 1;

      d[a + b] -= 1;
      d[a + b + 1] += 1;
    }
    int ans = n, s = 0;
    for (int i = 2; i <= limit * 2; ++i) {
      s += d[i];
      if (ans > s) {
        ans = s;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int minMoves(vector<int>& nums, int limit) {
    int n = nums.size();
    vector<int> d(limit * 2 + 2);
    for (int i = 0; i < n >> 1; ++i) {
      int a = min(nums[i], nums[n - i - 1]);
      int b = max(nums[i], nums[n - i - 1]);

      d[2] += 2;
      d[limit * 2 + 1] -= 2;

      d[a + 1] -= 1;
      d[b + limit + 1] += 1;

      d[a + b] -= 1;
      d[a + b + 1] += 1;
    }
    int ans = n, s = 0;
    for (int i = 2; i <= limit * 2; ++i) {
      s += d[i];
      if (ans > s) {
        ans = s;
      }
    }
    return ans;
  }
};
func minMoves(nums []int, limit int) int {
  d := make([]int, limit*2+2)
  n := len(nums)
  for i := 0; i < n>>1; i++ {
    a, b := min(nums[i], nums[n-i-1]), max(nums[i], nums[n-i-1])
    d[2] += 2
    d[limit*2+1] -= 2

    d[a+1] -= 1
    d[b+limit+1] += 1

    d[a+b] -= 1
    d[a+b+1] += 1
  }
  ans, s := n, 0
  for _, v := range d[2 : limit*2+1] {
    s += v
    if ans > s {
      ans = s
    }
  }
  return ans
}
function minMoves(nums: number[], limit: number): number {
  const n = nums.length;
  const d: number[] = Array(limit * 2 + 2).fill(0);
  for (let i = 0; i < n >> 1; ++i) {
    const a = Math.min(nums[i], nums[n - i - 1]);
    const b = Math.max(nums[i], nums[n - i - 1]);

    d[2] += 2;
    d[limit * 2 + 1] -= 2;

    d[a + 1] -= 1;
    d[b + limit + 1] += 1;

    d[a + b] -= 1;
    d[a + b + 1] += 1;
  }
  let ans = n;
  let s = 0;
  for (let i = 2; i <= limit * 2; ++i) {
    s += d[i];
    if (ans > s) {
      ans = s;
    }
  }
  return ans;
}

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

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

发布评论

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