Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c45c772

Browse files
committed
Merge pull request #401 from 0xff-dev/1856
Add solution and test-cases for problem 1856
2 parents 75ce266 + dcd6d86 commit c45c772

File tree

3 files changed

+90
-22
lines changed

3 files changed

+90
-22
lines changed

‎leetcode/1801-1900/1856.Maximum-Subarray-Min-Product/README.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,42 @@
11
# [1856.Maximum Subarray Min-Product][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
The **min-product** of an array is equal to the **minimum value** in the array **multiplied by** the array's **sum**.
5+
6+
- For example, the array `[3,2,5]` (minimum value is `2`) has a min-product of `2 * (3+2+5) = 2 * 10 = 20`.
7+
8+
Given an array of integers `nums`, return the **maximum min-product** of any **non-empty subarray** of `nums`. Since the answer may be large, return it **modulo** 10<sup>9</sup> + 7.
9+
10+
Note that the min-product should be maximized **before** performing the modulo operation. Testcases are generated such that the maximum min-product **without** modulo will fit in a **64-bit signed integer**.
11+
12+
A **subarray** is a **contiguous** part of an array.
713

814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: nums = [1,2,3,2]
18+
Output: 14
19+
Explanation: The maximum min-product is achieved with the subarray [2,3,2] (minimum value is 2).
20+
2 * (2+3+2) = 2 * 7 = 14.
1321
```
1422

15-
## 题意
16-
> ...
23+
**Example 2:**
1724

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Maximum Subarray Min-Product
23-
```go
2425
```
26+
Input: nums = [2,3,3,1,2]
27+
Output: 18
28+
Explanation: The maximum min-product is achieved with the subarray [3,3] (minimum value is 3).
29+
3 * (3+3) = 3 * 6 = 18.
30+
```
31+
32+
**Example 3:**
2533

34+
```
35+
Input: nums = [3,1,5,6,4,2]
36+
Output: 60
37+
Explanation: The maximum min-product is achieved with the subarray [5,6,4] (minimum value is 4).
38+
4 * (5+6+4) = 4 * 15 = 60.
39+
```
2640

2741
## 结语
2842

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,59 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
const mod1856 = 1000000007
4+
5+
func Solution(nums []int) int {
6+
l := len(nums)
7+
sum := make([]int64, l)
8+
sum[0] = int64(nums[0])
9+
for i := 1; i < l; i++ {
10+
sum[i] = int64(nums[i]) + sum[i-1]
11+
}
12+
13+
nextSmaller := make([]int, l)
14+
prevSmaleer := make([]int, l)
15+
stack := make([]int, l)
16+
17+
for i := 0; i < l; i++ {
18+
nextSmaller[i] = l
19+
prevSmaleer[i] = -1
20+
}
21+
22+
stackIdx := -1
23+
for i := 0; i < l; i++ {
24+
for stackIdx >= 0 && nums[stack[stackIdx]] > nums[i] {
25+
nextSmaller[stack[stackIdx]] = i
26+
stackIdx--
27+
}
28+
stackIdx++
29+
stack[stackIdx] = i
30+
}
31+
32+
stackIdx = -1
33+
for i := l - 1; i >= 0; i-- {
34+
for stackIdx >= 0 && nums[stack[stackIdx]] >= nums[i] {
35+
prevSmaleer[stack[stackIdx]] = i
36+
stackIdx--
37+
}
38+
stackIdx++
39+
stack[stackIdx] = i
40+
}
41+
42+
n := int64(0)
43+
for i := 0; i < l; i++ {
44+
right := nextSmaller[i] - 1 // lastidx
45+
left := prevSmaleer[i] //
46+
s := sum[right]
47+
if left != -1 {
48+
s -= sum[left]
49+
}
50+
51+
tmp := int64(nums[i]) * s
52+
53+
if n == -1 || n < tmp {
54+
n = tmp
55+
}
56+
}
57+
58+
return int(n % mod1856)
559
}

‎leetcode/1801-1900/1856.Maximum-Subarray-Min-Product/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{2, 3, 3, 1, 2}, 18},
17+
{"TestCase2", []int{3, 1, 5, 6, 4, 2}, 60},
18+
{"TestCase3", []int{2, 5, 4, 2, 4, 5, 3, 1, 2}, 50},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
//压力测试
33+
//压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
//使用案列
37+
//使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /