Skip to content

Commit 7003bb3

Browse files
committed
+ problem 152
1 parent 534f2ff commit 7003bb3

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# 152. Maximum Product Subarray
2+
Given an integer array `nums`, find a subarray that has the largest product, and return *the product*.
3+
4+
The test cases are generated so that the answer will fit in a **32-bit** integer.
5+
6+
#### Example 1:
7+
<pre>
8+
<strong>Input:</strong> nums = [2,3,-2,4]
9+
<strong>Output:</strong> 6
10+
<strong>Explanation:</strong> [2,3] has the largest product 6.
11+
</pre>
12+
13+
#### Example 2:
14+
<pre>
15+
<strong>Input:</strong> nums = [-2,0,-1]
16+
<strong>Output:</strong> 0
17+
<strong>Explanation:</strong> The result cannot be 2, because [-2,-1] is not a subarray.
18+
</pre>
19+
20+
#### Constraints:
21+
* <code>1 <= nums.length <= 2 * 10<sup>4</sup></code>
22+
* `-10 <= nums[i] <= 10`
23+
* The product of any prefix or suffix of `nums` is **guaranteed** to fit in a **32-bit** integer.
24+
25+
## Solutions (Rust)
26+
27+
### 1. Solution
28+
```Rust
29+
impl Solution {
30+
pub fn max_product(nums: Vec<i32>) -> i32 {
31+
let mut ret = *nums.iter().max().unwrap();
32+
33+
for slice in nums.split(|&num| num == 0) {
34+
if slice.iter().filter(|&&num| num < 0).count() % 2 == 1 {
35+
let i = slice.iter().position(|&num| num < 0).unwrap();
36+
let j = slice.iter().rposition(|&num| num < 0).unwrap();
37+
38+
if i + 1 < slice.len() {
39+
ret = ret.max(slice.iter().skip(i + 1).product());
40+
}
41+
if j > 0 {
42+
ret = ret.max(slice.iter().take(j).product());
43+
}
44+
} else if !slice.is_empty() {
45+
ret = ret.max(slice.iter().product());
46+
}
47+
}
48+
49+
ret
50+
}
51+
}
52+
```
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# 152. 乘积最大子数组
2+
给你一个整数数组 `nums` ,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。
3+
4+
测试用例的答案是一个 **32-位** 整数。
5+
6+
**子数组** 是数组的连续子序列。
7+
8+
#### 示例 1:
9+
<pre>
10+
<strong>输入:</strong> nums = [2,3,-2,4]
11+
<strong>输出:</strong> 6
12+
<strong>解释:</strong> 子数组 [2,3] 有最大乘积 6。
13+
</pre>
14+
15+
#### 示例 2:
16+
<pre>
17+
<strong>输入:</strong> nums = [-2,0,-1]
18+
<strong>输出:</strong> 0
19+
<strong>解释:</strong> 结果不能为 2, 因为 [-2,-1] 不是子数组。
20+
</pre>
21+
22+
#### 提示:
23+
* <code>1 <= nums.length <= 2 * 10<sup>4</sup></code>
24+
* `-10 <= nums[i] <= 10`
25+
* `nums` 的任何前缀或后缀的乘积都 **保证** 是一个 **32-位** 整数
26+
27+
## 题解 (Rust)
28+
29+
### 1. 题解
30+
```Rust
31+
impl Solution {
32+
pub fn max_product(nums: Vec<i32>) -> i32 {
33+
let mut ret = *nums.iter().max().unwrap();
34+
35+
for slice in nums.split(|&num| num == 0) {
36+
if slice.iter().filter(|&&num| num < 0).count() % 2 == 1 {
37+
let i = slice.iter().position(|&num| num < 0).unwrap();
38+
let j = slice.iter().rposition(|&num| num < 0).unwrap();
39+
40+
if i + 1 < slice.len() {
41+
ret = ret.max(slice.iter().skip(i + 1).product());
42+
}
43+
if j > 0 {
44+
ret = ret.max(slice.iter().take(j).product());
45+
}
46+
} else if !slice.is_empty() {
47+
ret = ret.max(slice.iter().product());
48+
}
49+
}
50+
51+
ret
52+
}
53+
}
54+
```
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
impl Solution {
2+
pub fn max_product(nums: Vec<i32>) -> i32 {
3+
let mut ret = *nums.iter().max().unwrap();
4+
5+
for slice in nums.split(|&num| num == 0) {
6+
if slice.iter().filter(|&&num| num < 0).count() % 2 == 1 {
7+
let i = slice.iter().position(|&num| num < 0).unwrap();
8+
let j = slice.iter().rposition(|&num| num < 0).unwrap();
9+
10+
if i + 1 < slice.len() {
11+
ret = ret.max(slice.iter().skip(i + 1).product());
12+
}
13+
if j > 0 {
14+
ret = ret.max(slice.iter().take(j).product());
15+
}
16+
} else if !slice.is_empty() {
17+
ret = ret.max(slice.iter().product());
18+
}
19+
}
20+
21+
ret
22+
}
23+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
[147][147l] |[Insertion Sort List][147] |![py]
114114
[150][150l] |[Evaluate Reverse Polish Notation][150] |![rs]
115115
[151][151l] |[Reverse Words in a String][151] |![rs]
116+
[152][152l] |[Maximum Product Subarray][152] |![rs]
116117
[153][153l] |[Find Minimum in Rotated Sorted Array][153] |![rb]&nbsp;&nbsp;![rs]
117118
[155][155l] |[Min Stack][155] |![rs]
118119
[160][160l] |[Intersection of Two Linked Lists][160] |![py]
@@ -1393,6 +1394,7 @@
13931394
[147]:Problemset/0147-Insertion%20Sort%20List/README.md#147-insertion-sort-list
13941395
[150]:Problemset/0150-Evaluate%20Reverse%20Polish%20Notation/README.md#150-evaluate-reverse-polish-notation
13951396
[151]:Problemset/0151-Reverse%20Words%20in%20a%20String/README.md#151-reverse-words-in-a-string
1397+
[152]:Problemset/0152-Maximum%20Product%20Subarray/README.md#152-maximum-product-subarray
13961398
[153]:Problemset/0153-Find%20Minimum%20in%20Rotated%20Sorted%20Array/README.md#153-find-minimum-in-rotated-sorted-array
13971399
[155]:Problemset/0155-Min%20Stack/README.md#155-min-stack
13981400
[160]:Problemset/0160-Intersection%20of%20Two%20Linked%20Lists/README.md#160-intersection-of-two-linked-lists
@@ -2668,6 +2670,7 @@
26682670
[147l]:https://leetcode.com/problems/insertion-sort-list/
26692671
[150l]:https://leetcode.com/problems/evaluate-reverse-polish-notation/
26702672
[151l]:https://leetcode.com/problems/reverse-words-in-a-string/
2673+
[152l]:https://leetcode.com/problems/maximum-product-subarray/
26712674
[153l]:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
26722675
[155l]:https://leetcode.com/problems/min-stack/
26732676
[160l]:https://leetcode.com/problems/intersection-of-two-linked-lists/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
[147][147l] |[对链表进行插入排序][147] |![py]
114114
[150][150l] |[逆波兰表达式求值][150] |![rs]
115115
[151][151l] |[翻转字符串里的单词][151] |![rs]
116+
[152][152l] |[乘积最大子数组][152] |![rs]
116117
[153][153l] |[寻找旋转排序数组中的最小值][153] |![rb]&nbsp;&nbsp;![rs]
117118
[155][155l] |[最小栈][155] |![rs]
118119
[160][160l] |[相交链表][160] |![py]
@@ -1393,6 +1394,7 @@
13931394
[147]:Problemset/0147-Insertion%20Sort%20List/README_CN.md#147-对链表进行插入排序
13941395
[150]:Problemset/0150-Evaluate%20Reverse%20Polish%20Notation/README_CN.md#150-逆波兰表达式求值
13951396
[151]:Problemset/0151-Reverse%20Words%20in%20a%20String/README_CN.md#151-翻转字符串里的单词
1397+
[152]:Problemset/0152-Maximum%20Product%20Subarray/README_CN.md#152-乘积最大子数组
13961398
[153]:Problemset/0153-Find%20Minimum%20in%20Rotated%20Sorted%20Array/README_CN.md#153-寻找旋转排序数组中的最小值
13971399
[155]:Problemset/0155-Min%20Stack/README_CN.md#155-最小栈
13981400
[160]:Problemset/0160-Intersection%20of%20Two%20Linked%20Lists/README_CN.md#160-相交链表
@@ -2668,6 +2670,7 @@
26682670
[147l]:https://leetcode.cn/problems/insertion-sort-list/
26692671
[150l]:https://leetcode.cn/problems/evaluate-reverse-polish-notation/
26702672
[151l]:https://leetcode.cn/problems/reverse-words-in-a-string/
2673+
[152l]:https://leetcode.cn/problems/maximum-product-subarray/
26712674
[153l]:https://leetcode.cn/problems/find-minimum-in-rotated-sorted-array/
26722675
[155l]:https://leetcode.cn/problems/min-stack/
26732676
[160l]:https://leetcode.cn/problems/intersection-of-two-linked-lists/

0 commit comments

Comments
 (0)