返回介绍

solution / 3000-3099 / 3012.Minimize Length of Array Using Operations / README_EN

发布于 2024-06-17 01:02:57 字数 6530 浏览 0 评论 0 收藏 0

3012. Minimize Length of Array Using Operations

中文文档

Description

You are given a 0-indexed integer array nums containing positive integers.

Your task is to minimize the length of nums by performing the following operations any number of times (including zero):

  • Select two distinct indices i and j from nums, such that nums[i] > 0 and nums[j] > 0.
  • Insert the result of nums[i] % nums[j] at the end of nums.
  • Delete the elements at indices i and j from nums.

Return _an integer denoting the minimum length of _nums_ after performing the operation any number of times._

 

Example 1:

Input: nums = [1,4,3,1]
Output: 1
Explanation: One way to minimize the length of the array is as follows:
Operation 1: Select indices 2 and 1, insert nums[2] % nums[1] at the end and it becomes [1,4,3,1,3], then delete elements at indices 2 and 1.
nums becomes [1,1,3].
Operation 2: Select indices 1 and 2, insert nums[1] % nums[2] at the end and it becomes [1,1,3,1], then delete elements at indices 1 and 2.
nums becomes [1,1].
Operation 3: Select indices 1 and 0, insert nums[1] % nums[0] at the end and it becomes [1,1,0], then delete elements at indices 1 and 0.
nums becomes [0].
The length of nums cannot be reduced further. Hence, the answer is 1.
It can be shown that 1 is the minimum achievable length. 

Example 2:

Input: nums = [5,5,5,10,5]
Output: 2
Explanation: One way to minimize the length of the array is as follows:
Operation 1: Select indices 0 and 3, insert nums[0] % nums[3] at the end and it becomes [5,5,5,10,5,5], then delete elements at indices 0 and 3.
nums becomes [5,5,5,5]. 
Operation 2: Select indices 2 and 3, insert nums[2] % nums[3] at the end and it becomes [5,5,5,5,0], then delete elements at indices 2 and 3. 
nums becomes [5,5,0]. 
Operation 3: Select indices 0 and 1, insert nums[0] % nums[1] at the end and it becomes [5,5,0,0], then delete elements at indices 0 and 1.
nums becomes [0,0].
The length of nums cannot be reduced further. Hence, the answer is 2.
It can be shown that 2 is the minimum achievable length. 

Example 3:

Input: nums = [2,3,4]
Output: 1
Explanation: One way to minimize the length of the array is as follows: 
Operation 1: Select indices 1 and 2, insert nums[1] % nums[2] at the end and it becomes [2,3,4,3], then delete elements at indices 1 and 2.
nums becomes [2,3].
Operation 2: Select indices 1 and 0, insert nums[1] % nums[0] at the end and it becomes [2,3,1], then delete elements at indices 1 and 0.
nums becomes [1].
The length of nums cannot be reduced further. Hence, the answer is 1.
It can be shown that 1 is the minimum achievable length.

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109

Solutions

Solution 1: Case Discussion

Let's denote the smallest element in the array $nums$ as $mi$.

If $mi$ appears only once, we can perform operations with $mi$ and the other elements in the array $nums$ to eliminate all other elements, leaving only $mi$. The answer is $1$.

If $mi$ appears multiple times, we need to check whether all elements in the array $nums$ are multiples of $mi$. If not, there exists at least one element $x$ such that $0 < x \bmod mi < mi$. This means we can construct an element smaller than $mi$ through operations. This smaller element can eliminate all other elements through operations, leaving only this smaller element. The answer is $1$. If all elements are multiples of $mi$, we can first use $mi$ to eliminate all elements larger than $mi$. The remaining elements are all $mi$, with a count of $cnt$. Pair them up, and perform an operation for each pair. Finally, there will be $\lceil cnt / 2 \rceil$ elements left, so the answer is $\lceil cnt / 2 \rceil$.

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

class Solution:
  def minimumArrayLength(self, nums: List[int]) -> int:
    mi = min(nums)
    if any(x % mi for x in nums):
      return 1
    return (nums.count(mi) + 1) // 2
class Solution {
  public int minimumArrayLength(int[] nums) {
    int mi = Arrays.stream(nums).min().getAsInt();
    int cnt = 0;
    for (int x : nums) {
      if (x % mi != 0) {
        return 1;
      }
      if (x == mi) {
        ++cnt;
      }
    }
    return (cnt + 1) / 2;
  }
}
class Solution {
public:
  int minimumArrayLength(vector<int>& nums) {
    int mi = *min_element(nums.begin(), nums.end());
    int cnt = 0;
    for (int x : nums) {
      if (x % mi) {
        return 1;
      }
      cnt += x == mi;
    }
    return (cnt + 1) / 2;
  }
};
func minimumArrayLength(nums []int) int {
  mi := slices.Min(nums)
  cnt := 0
  for _, x := range nums {
    if x%mi != 0 {
      return 1
    }
    if x == mi {
      cnt++
    }
  }
  return (cnt + 1) / 2
}
function minimumArrayLength(nums: number[]): number {
  const mi = Math.min(...nums);
  let cnt = 0;
  for (const x of nums) {
    if (x % mi) {
      return 1;
    }
    if (x === mi) {
      ++cnt;
    }
  }
  return (cnt + 1) >> 1;
}

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

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

发布评论

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