Skip to content

Commit 4bb142b

Browse files
authored
feat: add solutions to lc problem: No.2892 (#1753)
No.2892.Minimizing Array After Replacing Pairs With Their Product
1 parent 9c95bdb commit 4bb142b

File tree

11 files changed

+443
-0
lines changed

11 files changed

+443
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
# [2892. Minimizing Array After Replacing Pairs With Their Product](https://leetcode.cn/problems/minimizing-array-after-replacing-pairs-with-their-product)
2+
3+
[English Version](/solution/2800-2899/2892.Minimizing%20Array%20After%20Replacing%20Pairs%20With%20Their%20Product/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, you can perform the following operation on the array any number of times:</p>
10+
11+
<ul>
12+
<li>Select two <strong>adjacent</strong> elements of the array like <code>x</code> and <code>y</code>, such that <code>x * y &lt;= k</code>, and replace both of them with a <strong>single element</strong> with value <code>x * y</code> (e.g. in one operation the array <code>[1, 2, 2, 3]</code> with <code>k = 5</code> can become <code>[1, 4, 3]</code> or <code>[2, 2, 3]</code>, but can&#39;t become <code>[1, 2, 6]</code>).</li>
13+
</ul>
14+
15+
<p>Return <em>the <strong>minimum</strong> possible length of </em><code>nums</code><em> after any number of operations</em>.</p>
16+
17+
<p>&nbsp;</p>
18+
<p><strong class="example">Example 1:</strong></p>
19+
20+
<pre>
21+
<strong>Input:</strong> nums = [2,3,3,7,3,5], k = 20
22+
<strong>Output:</strong> 3
23+
<strong>Explanation:</strong> We perform these operations:
24+
1. [<u>2,3</u>,3,7,3,5] -&gt; [<u>6</u>,3,7,3,5]
25+
2. [<u>6,3</u>,7,3,5] -&gt; [<u>18</u>,7,3,5]
26+
3. [18,7,<u>3,5</u>] -&gt; [18,7,<u>15</u>]
27+
It can be shown that 3 is the minimum length possible to achieve with the given operation.
28+
</pre>
29+
30+
<p><strong class="example">Example 2:</strong></p>
31+
32+
<pre>
33+
<strong>Input:</strong> nums = [3,3,3,3], k = 6
34+
<strong>Output:</strong> 4
35+
<strong>Explanation:</strong> We can&#39;t perform any operations since the product of every two adjacent elements is greater than 6.
36+
Hence, the answer is 4.</pre>
37+
38+
<p>&nbsp;</p>
39+
<p><strong>Constraints:</strong></p>
40+
41+
<ul>
42+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
43+
<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
44+
<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
45+
</ul>
46+
47+
## 解法
48+
49+
<!-- 这里可写通用的实现逻辑 -->
50+
51+
**方法一:贪心**
52+
53+
我们用一个变量 $ans$ 记录当前数组的长度,用一个变量 $y$ 记录当前数组的乘积,初始时 $ans = 1$, $y = nums[0]$。
54+
55+
我们从数组的第二个元素开始遍历,设当前元素为 $x$:
56+
57+
- 如果 $x = 0$,那么整个数组的乘积为 $0 \le k$,因此答案数组的最小长度为 $1$,直接返回即可。
58+
- 如果 $x \times y \le k$,那么我们可以将 $x$ 与 $y$ 合并,即 $y = x \times y$。
59+
- 如果 $x \times y \gt k$,那么我们无法将 $x$ 与 $y$ 合并,因此我们需要将 $x$ 单独作为一个元素,即 $ans = ans + 1$,并且 $y = x$。
60+
61+
最终答案即为 $ans$。
62+
63+
时间复杂度 $O(n)$,其中 $n$ 为数组的长度。空间复杂度 $O(1)$。
64+
65+
<!-- tabs:start -->
66+
67+
### **Python3**
68+
69+
<!-- 这里可写当前语言的特殊实现逻辑 -->
70+
71+
```python
72+
class Solution:
73+
def minArrayLength(self, nums: List[int], k: int) -> int:
74+
ans, y = 1, nums[0]
75+
for x in nums[1:]:
76+
if x == 0:
77+
return 1
78+
if x * y <= k:
79+
y *= x
80+
else:
81+
y = x
82+
ans += 1
83+
return ans
84+
```
85+
86+
### **Java**
87+
88+
<!-- 这里可写当前语言的特殊实现逻辑 -->
89+
90+
```java
91+
class Solution {
92+
public int minArrayLength(int[] nums, int k) {
93+
int ans = 1;
94+
long y = nums[0];
95+
for (int i = 1; i < nums.length; ++i) {
96+
int x = nums[i];
97+
if (x == 0) {
98+
return 1;
99+
}
100+
if (x * y <= k) {
101+
y *= x;
102+
} else {
103+
y = x;
104+
++ans;
105+
}
106+
}
107+
return ans;
108+
}
109+
}
110+
```
111+
112+
### **C++**
113+
114+
```cpp
115+
class Solution {
116+
public:
117+
int minArrayLength(vector<int>& nums, int k) {
118+
int ans = 1;
119+
long long y = nums[0];
120+
for (int i = 1; i < nums.size(); ++i) {
121+
int x = nums[i];
122+
if (x == 0) {
123+
return 1;
124+
}
125+
if (x * y <= k) {
126+
y *= x;
127+
} else {
128+
y = x;
129+
++ans;
130+
}
131+
}
132+
return ans;
133+
}
134+
};
135+
```
136+
137+
### **Go**
138+
139+
```go
140+
func minArrayLength(nums []int, k int) int {
141+
ans, y := 1, nums[0]
142+
for _, x := range nums[1:] {
143+
if x == 0 {
144+
return 1
145+
}
146+
if x*y <= k {
147+
y *= x
148+
} else {
149+
y = x
150+
ans++
151+
}
152+
}
153+
return ans
154+
}
155+
```
156+
157+
### **TypeScript**
158+
159+
```ts
160+
function minArrayLength(nums: number[], k: number): number {
161+
let [ans, y] = [1, nums[0]];
162+
for (const x of nums.slice(1)) {
163+
if (x === 0) {
164+
return 1;
165+
}
166+
if (x * y <= k) {
167+
y *= x;
168+
} else {
169+
y = x;
170+
++ans;
171+
}
172+
}
173+
return ans;
174+
}
175+
```
176+
177+
### **...**
178+
179+
```
180+
181+
```
182+
183+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# [2892. Minimizing Array After Replacing Pairs With Their Product](https://leetcode.com/problems/minimizing-array-after-replacing-pairs-with-their-product)
2+
3+
[中文文档](/solution/2800-2899/2892.Minimizing%20Array%20After%20Replacing%20Pairs%20With%20Their%20Product/README.md)
4+
5+
## Description
6+
7+
<p>Given an integer array <code>nums</code> and an integer <code>k</code>, you can perform the following operation on the array any number of times:</p>
8+
9+
<ul>
10+
<li>Select two <strong>adjacent</strong> elements of the array like <code>x</code> and <code>y</code>, such that <code>x * y &lt;= k</code>, and replace both of them with a <strong>single element</strong> with value <code>x * y</code> (e.g. in one operation the array <code>[1, 2, 2, 3]</code> with <code>k = 5</code> can become <code>[1, 4, 3]</code> or <code>[2, 2, 3]</code>, but can&#39;t become <code>[1, 2, 6]</code>).</li>
11+
</ul>
12+
13+
<p>Return <em>the <strong>minimum</strong> possible length of </em><code>nums</code><em> after any number of operations</em>.</p>
14+
15+
<p>&nbsp;</p>
16+
<p><strong class="example">Example 1:</strong></p>
17+
18+
<pre>
19+
<strong>Input:</strong> nums = [2,3,3,7,3,5], k = 20
20+
<strong>Output:</strong> 3
21+
<strong>Explanation:</strong> We perform these operations:
22+
1. [<u>2,3</u>,3,7,3,5] -&gt; [<u>6</u>,3,7,3,5]
23+
2. [<u>6,3</u>,7,3,5] -&gt; [<u>18</u>,7,3,5]
24+
3. [18,7,<u>3,5</u>] -&gt; [18,7,<u>15</u>]
25+
It can be shown that 3 is the minimum length possible to achieve with the given operation.
26+
</pre>
27+
28+
<p><strong class="example">Example 2:</strong></p>
29+
30+
<pre>
31+
<strong>Input:</strong> nums = [3,3,3,3], k = 6
32+
<strong>Output:</strong> 4
33+
<strong>Explanation:</strong> We can&#39;t perform any operations since the product of every two adjacent elements is greater than 6.
34+
Hence, the answer is 4.</pre>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
41+
<li><code>0 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
42+
<li><code>1 &lt;= k &lt;= 10<sup>9</sup></code></li>
43+
</ul>
44+
45+
## Solutions
46+
47+
**Method 1: Greedy**
48+
49+
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]$.
50+
51+
We start traversing from the second element of the array. Let the current element be $x$:
52+
53+
- 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.
54+
- If $x \times y \le k$, then we can merge $x$ and $y$, that is, $y = x \times y$.
55+
- 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$.
56+
57+
The final answer is $ans$.
58+
59+
The time complexity is $O(n)$, where n is the length of the array. The space complexity is $O(1)$.
60+
61+
<!-- tabs:start -->
62+
63+
### **Python3**
64+
65+
```python
66+
class Solution:
67+
def minArrayLength(self, nums: List[int], k: int) -> int:
68+
ans, y = 1, nums[0]
69+
for x in nums[1:]:
70+
if x == 0:
71+
return 1
72+
if x * y <= k:
73+
y *= x
74+
else:
75+
y = x
76+
ans += 1
77+
return ans
78+
```
79+
80+
### **Java**
81+
82+
```java
83+
class Solution {
84+
public int minArrayLength(int[] nums, int k) {
85+
int ans = 1;
86+
long y = nums[0];
87+
for (int i = 1; i < nums.length; ++i) {
88+
int x = nums[i];
89+
if (x == 0) {
90+
return 1;
91+
}
92+
if (x * y <= k) {
93+
y *= x;
94+
} else {
95+
y = x;
96+
++ans;
97+
}
98+
}
99+
return ans;
100+
}
101+
}
102+
```
103+
104+
### **C++**
105+
106+
```cpp
107+
class Solution {
108+
public:
109+
int minArrayLength(vector<int>& nums, int k) {
110+
int ans = 1;
111+
long long y = nums[0];
112+
for (int i = 1; i < nums.size(); ++i) {
113+
int x = nums[i];
114+
if (x == 0) {
115+
return 1;
116+
}
117+
if (x * y <= k) {
118+
y *= x;
119+
} else {
120+
y = x;
121+
++ans;
122+
}
123+
}
124+
return ans;
125+
}
126+
};
127+
```
128+
129+
### **Go**
130+
131+
```go
132+
func minArrayLength(nums []int, k int) int {
133+
ans, y := 1, nums[0]
134+
for _, x := range nums[1:] {
135+
if x == 0 {
136+
return 1
137+
}
138+
if x*y <= k {
139+
y *= x
140+
} else {
141+
y = x
142+
ans++
143+
}
144+
}
145+
return ans
146+
}
147+
```
148+
149+
### **TypeScript**
150+
151+
```ts
152+
function minArrayLength(nums: number[], k: number): number {
153+
let [ans, y] = [1, nums[0]];
154+
for (const x of nums.slice(1)) {
155+
if (x === 0) {
156+
return 1;
157+
}
158+
if (x * y <= k) {
159+
y *= x;
160+
} else {
161+
y = x;
162+
++ans;
163+
}
164+
}
165+
return ans;
166+
}
167+
```
168+
169+
### **...**
170+
171+
```
172+
173+
```
174+
175+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int minArrayLength(vector<int>& nums, int k) {
4+
int ans = 1;
5+
long long y = nums[0];
6+
for (int i = 1; i < nums.size(); ++i) {
7+
int x = nums[i];
8+
if (x == 0) {
9+
return 1;
10+
}
11+
if (x * y <= k) {
12+
y *= x;
13+
} else {
14+
y = x;
15+
++ans;
16+
}
17+
}
18+
return ans;
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func minArrayLength(nums []int, k int) int {
2+
ans, y := 1, nums[0]
3+
for _, x := range nums[1:] {
4+
if x == 0 {
5+
return 1
6+
}
7+
if x*y <= k {
8+
y *= x
9+
} else {
10+
y = x
11+
ans++
12+
}
13+
}
14+
return ans
15+
}

0 commit comments

Comments
 (0)