diff --git a/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/README.md b/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/README.md
new file mode 100644
index 0000000000000..21fee14a7e0a9
--- /dev/null
+++ b/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/README.md
@@ -0,0 +1,183 @@
+# [2892. Minimizing Array After Replacing Pairs With Their Product](https://leetcode.cn/problems/minimizing-array-after-replacing-pairs-with-their-product)
+
+[English Version](/solution/2800-2899/2892.Minimizing%20Array%20After%20Replacing%20Pairs%20With%20Their%20Product/README_EN.md)
+
+## 题目描述
+
+
+
+
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
+
+
+## 解法
+
+
+
+**方法一:贪心**
+
+我们用一个变量 $ans$ 记录当前数组的长度,用一个变量 $y$ 记录当前数组的乘积,初始时 $ans = 1,ドル $y = nums[0]$。
+
+我们从数组的第二个元素开始遍历,设当前元素为 $x$:
+
+- 如果 $x = 0,ドル那么整个数组的乘积为 0ドル \le k,ドル因此答案数组的最小长度为 1ドル,ドル直接返回即可。
+- 如果 $x \times y \le k,ドル那么我们可以将 $x$ 与 $y$ 合并,即 $y = x \times y$。
+- 如果 $x \times y \gt k,ドル那么我们无法将 $x$ 与 $y$ 合并,因此我们需要将 $x$ 单独作为一个元素,即 $ans = ans + 1,ドル并且 $y = x$。
+
+最终答案即为 $ans$。
+
+时间复杂度 $O(n),ドル其中 $n$ 为数组的长度。空间复杂度 $O(1)$。
+
+
+
+### **Python3**
+
+
+
+```python
+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 +``` + +### **Java** + + + +```java +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; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int minArrayLength(vector& 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; + } +}; +``` + +### **Go** + +```go +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 +} +``` + +### **TypeScript** + +```ts +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; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/README_EN.md b/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/README_EN.md new file mode 100644 index 0000000000000..50fb3d0e8517e --- /dev/null +++ b/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/README_EN.md @@ -0,0 +1,175 @@ +# [2892. Minimizing Array After Replacing Pairs With Their Product](https://leetcode.com/problems/minimizing-array-after-replacing-pairs-with-their-product) + +[中文文档](/solution/2800-2899/2892.Minimizing%20Array%20After%20Replacing%20Pairs%20With%20Their%20Product/README.md) + +## 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
+
+**Method 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)$.
+
+
+
+### **Python3**
+
+```python
+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 +``` + +### **Java** + +```java +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; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + int minArrayLength(vector& 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; + } +}; +``` + +### **Go** + +```go +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 +} +``` + +### **TypeScript** + +```ts +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; +} +``` + +### **...** + +``` + +``` + + diff --git a/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/Solution.cpp b/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/Solution.cpp new file mode 100644 index 0000000000000..4cff9aa7dd502 --- /dev/null +++ b/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/Solution.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int minArrayLength(vector& 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; + } +}; \ No newline at end of file diff --git a/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/Solution.go b/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/Solution.go new file mode 100644 index 0000000000000..2dbba87aadaa3 --- /dev/null +++ b/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/Solution.go @@ -0,0 +1,15 @@ +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 +} \ No newline at end of file diff --git a/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/Solution.java b/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/Solution.java new file mode 100644 index 0000000000000..babd338cd650e --- /dev/null +++ b/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/Solution.java @@ -0,0 +1,19 @@ +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; + } +} \ No newline at end of file diff --git a/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/Solution.py b/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/Solution.py new file mode 100644 index 0000000000000..4091c08cfae10 --- /dev/null +++ b/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/Solution.py @@ -0,0 +1,12 @@ +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 diff --git a/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/Solution.ts b/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/Solution.ts new file mode 100644 index 0000000000000..84161c5e30fe3 --- /dev/null +++ b/solution/2800-2899/2892.Minimizing Array After Replacing Pairs With Their Product/Solution.ts @@ -0,0 +1,15 @@ +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; +} diff --git a/solution/README.md b/solution/README.md index 33dd33b338e8c..988c0c1082dd2 100644 --- a/solution/README.md +++ b/solution/README.md @@ -2902,6 +2902,7 @@ | 2889 | [Reshape Data Pivot](/solution/2800-2899/2889.Reshape%20Data%20Pivot/README.md) | | 简单 | | | 2890 | [Reshape Data Melt](/solution/2800-2899/2890.Reshape%20Data%20Melt/README.md) | | 简单 | | | 2891 | [Method Chaining](/solution/2800-2899/2891.Method%20Chaining/README.md) | | 简单 | | +| 2892 | [Minimizing Array After Replacing Pairs With Their Product](/solution/2800-2899/2892.Minimizing%20Array%20After%20Replacing%20Pairs%20With%20Their%20Product/README.md) | | 中等 | 🔒 | | 2893 | [Calculate Orders Within Each Interval](/solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 6d588bc9a4be4..040c0fa3e8ba5 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -2900,6 +2900,7 @@ Press Control + F(or Command + F on
| 2889 | [Reshape Data Pivot](/solution/2800-2899/2889.Reshape%20Data%20Pivot/README_EN.md) | | Easy | |
| 2890 | [Reshape Data Melt](/solution/2800-2899/2890.Reshape%20Data%20Melt/README_EN.md) | | Easy | |
| 2891 | [Method Chaining](/solution/2800-2899/2891.Method%20Chaining/README_EN.md) | | Easy | |
+| 2892 | [Minimizing Array After Replacing Pairs With Their Product](/solution/2800-2899/2892.Minimizing%20Array%20After%20Replacing%20Pairs%20With%20Their%20Product/README_EN.md) | | Medium | 🔒 |
| 2893 | [Calculate Orders Within Each Interval](/solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README_EN.md) | | Medium | 🔒 |
## Copyright
diff --git a/solution/summary.md b/solution/summary.md
index 1372f084c2fd7..664db13ecb20d 100644
--- a/solution/summary.md
+++ b/solution/summary.md
@@ -2947,4 +2947,5 @@
- [2889.Reshape Data Pivot](/solution/2800-2899/2889.Reshape%20Data%20Pivot/README.md)
- [2890.Reshape Data Melt](/solution/2800-2899/2890.Reshape%20Data%20Melt/README.md)
- [2891.Method Chaining](/solution/2800-2899/2891.Method%20Chaining/README.md)
+ - [2892.Minimizing Array After Replacing Pairs With Their Product](/solution/2800-2899/2892.Minimizing%20Array%20After%20Replacing%20Pairs%20With%20Their%20Product/README.md)
- [2893.Calculate Orders Within Each Interval](/solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README.md)
diff --git a/solution/summary_en.md b/solution/summary_en.md
index 825ce2fdfec89..6200030e654ae 100644
--- a/solution/summary_en.md
+++ b/solution/summary_en.md
@@ -2947,4 +2947,5 @@
- [2889.Reshape Data Pivot](/solution/2800-2899/2889.Reshape%20Data%20Pivot/README_EN.md)
- [2890.Reshape Data Melt](/solution/2800-2899/2890.Reshape%20Data%20Melt/README_EN.md)
- [2891.Method Chaining](/solution/2800-2899/2891.Method%20Chaining/README_EN.md)
+ - [2892.Minimizing Array After Replacing Pairs With Their Product](/solution/2800-2899/2892.Minimizing%20Array%20After%20Replacing%20Pairs%20With%20Their%20Product/README_EN.md)
- [2893.Calculate Orders Within Each Interval](/solution/2800-2899/2893.Calculate%20Orders%20Within%20Each%20Interval/README_EN.md)