返回介绍

solution / 2800-2899 / 2892.Minimizing Array After Replacing Pairs With Their Product / README_EN

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

2892. Minimizing Array After Replacing Pairs With Their Product

中文文档

Description

Given an integer array nums and an integer k, you can perform the following operation on the array any number of times:

  • Select two adjacent elements of the array like x and y, such that x * y <= k, and replace both of them with a single element with value x * y (e.g. in one operation the array [1, 2, 2, 3] with k = 5 can become [1, 4, 3] or [2, 2, 3], but can't become [1, 2, 6]).

Return _the minimum possible length of _nums_ after any number of operations_.

 

Example 1:

Input: nums = [2,3,3,7,3,5], k = 20
Output: 3
Explanation: We perform these operations:
1. [2,3,3,7,3,5] -> [6,3,7,3,5]
2. [6,3,7,3,5] -> [18,7,3,5]
3. [18,7,3,5] -> [18,7,15]
It can be shown that 3 is the minimum length possible to achieve with the given operation.

Example 2:

Input: nums = [3,3,3,3], k = 6
Output: 4
Explanation: We can't perform any operations since the product of every two adjacent elements is greater than 6.
Hence, the answer is 4.

 

Constraints:

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

Solutions

Solution 1: Greedy

We use a variable $ans$ to record the current length of the array, and a variable $y$ to record the current product of the array. Initially, $ans = 1$ and $y = nums[0]$.

We start traversing from the second element of the array. Let the current element be $x$:

  • If $x = 0$, then the product of the entire array is $0 \le k$, so the minimum length of the answer array is $1$, and we can return directly.
  • If $x \times y \le k$, then we can merge $x$ and $y$, that is, $y = x \times y$.
  • If $x \times y \gt k$, then we cannot merge $x$ and $y$, so we need to treat $x$ as a separate element, that is, $ans = ans + 1$, and $y = x$.

The final answer is $ans$.

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

class Solution:
  def minArrayLength(self, nums: List[int], k: int) -> int:
    ans, y = 1, nums[0]
    for x in nums[1:]:
      if x == 0:
        return 1
      if x * y <= k:
        y *= x
      else:
        y = x
        ans += 1
    return ans
class Solution {
  public int minArrayLength(int[] nums, int k) {
    int ans = 1;
    long y = nums[0];
    for (int i = 1; i < nums.length; ++i) {
      int x = nums[i];
      if (x == 0) {
        return 1;
      }
      if (x * y <= k) {
        y *= x;
      } else {
        y = x;
        ++ans;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int minArrayLength(vector<int>& nums, int k) {
    int ans = 1;
    long long y = nums[0];
    for (int i = 1; i < nums.size(); ++i) {
      int x = nums[i];
      if (x == 0) {
        return 1;
      }
      if (x * y <= k) {
        y *= x;
      } else {
        y = x;
        ++ans;
      }
    }
    return ans;
  }
};
func minArrayLength(nums []int, k int) int {
  ans, y := 1, nums[0]
  for _, x := range nums[1:] {
    if x == 0 {
      return 1
    }
    if x*y <= k {
      y *= x
    } else {
      y = x
      ans++
    }
  }
  return ans
}
function minArrayLength(nums: number[], k: number): number {
  let [ans, y] = [1, nums[0]];
  for (const x of nums.slice(1)) {
    if (x === 0) {
      return 1;
    }
    if (x * y <= k) {
      y *= x;
    } else {
      y = x;
      ++ans;
    }
  }
  return ans;
}

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

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

发布评论

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