Skip to content

Commit 9cffeb3

Browse files
committed
feat: add solutions to lc problem: No.1856. Maximum Subarray Min-Product
1 parent d288d59 commit 9cffeb3

File tree

10 files changed

+324
-6
lines changed

10 files changed

+324
-6
lines changed

solution/1000-1099/1094.Car Pooling/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,30 @@ class Solution {
138138
};
139139
```
140140

141+
### **C++**
142+
143+
```cpp
144+
class Solution {
145+
public:
146+
bool carPooling(vector<vector<int>>& trips, int capacity) {
147+
vector<int> delta(1001);
148+
for (auto &trip : trips) {
149+
int num = trip[0], start = trip[1], end = trip[2];
150+
delta[start] += num;
151+
delta[end] -= num;
152+
}
153+
int cur = 0;
154+
for (auto &num : delta) {
155+
cur += num;
156+
if (cur > capacity) {
157+
return false;
158+
}
159+
}
160+
return true;
161+
}
162+
};
163+
```
164+
141165
### **...**
142166
143167
```

solution/1000-1099/1094.Car Pooling/README_EN.md

+24
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,30 @@ class Solution {
136136
};
137137
```
138138

139+
### **C++**
140+
141+
```cpp
142+
class Solution {
143+
public:
144+
bool carPooling(vector<vector<int>>& trips, int capacity) {
145+
vector<int> delta(1001);
146+
for (auto &trip : trips) {
147+
int num = trip[0], start = trip[1], end = trip[2];
148+
delta[start] += num;
149+
delta[end] -= num;
150+
}
151+
int cur = 0;
152+
for (auto &num : delta) {
153+
cur += num;
154+
if (cur > capacity) {
155+
return false;
156+
}
157+
}
158+
return true;
159+
}
160+
};
161+
```
162+
139163
### **...**
140164
141165
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
bool carPooling(vector<vector<int>>& trips, int capacity) {
4+
vector<int> delta(1001);
5+
for (auto &trip : trips) {
6+
int num = trip[0], start = trip[1], end = trip[2];
7+
delta[start] += num;
8+
delta[end] -= num;
9+
}
10+
int cur = 0;
11+
for (auto &num : delta) {
12+
cur += num;
13+
if (cur > capacity) {
14+
return false;
15+
}
16+
}
17+
return true;
18+
}
19+
};

solution/1100-1199/1109.Corporate Flight Bookings/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,28 @@ var corpFlightBookings = function(bookings, n) {
125125
};
126126
```
127127

128+
### **C++**
129+
130+
```cpp
131+
class Solution {
132+
public:
133+
vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {
134+
vector<int> delta(n);
135+
for (auto &booking : bookings) {
136+
int first = booking[0], last = booking[1], seats = booking[2];
137+
delta[first - 1] += seats;
138+
if (last < n) {
139+
delta[last] -= seats;
140+
}
141+
}
142+
for (int i = 0; i < n - 1; ++i) {
143+
delta[i + 1] += delta[i];
144+
}
145+
return delta;
146+
}
147+
};
148+
```
149+
128150
### **...**
129151
130152
```

solution/1100-1199/1109.Corporate Flight Bookings/README_EN.md

+22
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,28 @@ var corpFlightBookings = function(bookings, n) {
116116
};
117117
```
118118

119+
### **C++**
120+
121+
```cpp
122+
class Solution {
123+
public:
124+
vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {
125+
vector<int> delta(n);
126+
for (auto &booking : bookings) {
127+
int first = booking[0], last = booking[1], seats = booking[2];
128+
delta[first - 1] += seats;
129+
if (last < n) {
130+
delta[last] -= seats;
131+
}
132+
}
133+
for (int i = 0; i < n - 1; ++i) {
134+
delta[i + 1] += delta[i];
135+
}
136+
return delta;
137+
}
138+
};
139+
```
140+
119141
### **...**
120142
121143
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<int> corpFlightBookings(vector<vector<int>>& bookings, int n) {
4+
vector<int> delta(n);
5+
for (auto &booking : bookings) {
6+
int first = booking[0], last = booking[1], seats = booking[2];
7+
delta[first - 1] += seats;
8+
if (last < n) {
9+
delta[last] -= seats;
10+
}
11+
}
12+
for (int i = 0; i < n - 1; ++i) {
13+
delta[i + 1] += delta[i];
14+
}
15+
return delta;
16+
}
17+
};

solution/1800-1899/1856.Maximum Subarray Min-Product/README.md

+72-3
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,96 @@
5656
<li><code>1 <= nums[i] <= 10<sup>7</sup></code></li>
5757
</ul>
5858

59-
6059
## 解法
6160

6261
<!-- 这里可写通用的实现逻辑 -->
6362

63+
“前缀和 + 单调栈”实现。
64+
6465
<!-- tabs:start -->
6566

6667
### **Python3**
6768

6869
<!-- 这里可写当前语言的特殊实现逻辑 -->
6970

7071
```python
71-
72+
class Solution:
73+
def maxSumMinProduct(self, nums: List[int]) -> int:
74+
n = len(nums)
75+
# 前缀和
76+
pre_sum = [0] * (n + 1)
77+
for i in range(1, n + 1):
78+
pre_sum[i] = pre_sum[i - 1] + nums[i - 1]
79+
80+
# 单调栈求下一个较小值
81+
stack = []
82+
next_lesser = [n] * n
83+
for i in range(n):
84+
while stack and nums[stack[-1]] > nums[i]:
85+
next_lesser[stack.pop()] = i
86+
stack.append(i)
87+
88+
# 单调栈求前一个较小值
89+
stack = []
90+
prev_lesser = [-1] * n
91+
for i in range(n - 1, -1, -1):
92+
while stack and nums[stack[-1]] > nums[i]:
93+
prev_lesser[stack.pop()] = i
94+
stack.append(i)
95+
96+
res = 0
97+
for i in range(n):
98+
start, end = prev_lesser[i], next_lesser[i]
99+
t = nums[i] * (pre_sum[end] - pre_sum[start + 1])
100+
res = max(res, t)
101+
return res % (10 ** 9 + 7)
72102
```
73103

74104
### **Java**
75105

76106
<!-- 这里可写当前语言的特殊实现逻辑 -->
77107

78108
```java
79-
109+
class Solution {
110+
public int maxSumMinProduct(int[] nums) {
111+
int n = nums.length;
112+
113+
// 前缀和
114+
long[] preSum = new long[n + 1];
115+
for (int i = 1; i < n + 1; ++i) {
116+
preSum[i] = preSum[i - 1] + nums[i - 1];
117+
}
118+
119+
// 单调栈求下一个较小值
120+
Deque<Integer> stack = new ArrayDeque<>();
121+
int[] nextLesser = new int[n];
122+
Arrays.fill(nextLesser, n);
123+
for (int i = 0; i < n; ++i) {
124+
while (!stack.isEmpty() && nums[stack.peek()] > nums[i]) {
125+
nextLesser[stack.pop()] = i;
126+
}
127+
stack.push(i);
128+
}
129+
130+
// 单调栈求前一个较小值
131+
stack = new ArrayDeque<>();
132+
int[] prevLesser = new int[n];
133+
Arrays.fill(prevLesser, -1);
134+
for (int i = n - 1; i >= 0; --i) {
135+
while (!stack.isEmpty() && nums[stack.peek()] > nums[i]) {
136+
prevLesser[stack.pop()] = i;
137+
}
138+
stack.push(i);
139+
}
140+
long res = 0;
141+
for (int i = 0; i < n; ++i) {
142+
int start = prevLesser[i], end = nextLesser[i];
143+
long t = nums[i] * (preSum[end] - preSum[start + 1]);
144+
res = Math.max(res, t);
145+
}
146+
return (int) (res % 1000000007);
147+
}
148+
}
80149
```
81150

82151
### **...**

solution/1800-1899/1856.Maximum Subarray Min-Product/README_EN.md

+62-3
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,80 @@
5252
<li><code>1 &lt;= nums[i] &lt;= 10<sup>7</sup></code></li>
5353
</ul>
5454

55-
5655
## Solutions
5756

5857
<!-- tabs:start -->
5958

6059
### **Python3**
6160

6261
```python
63-
62+
class Solution:
63+
def maxSumMinProduct(self, nums: List[int]) -> int:
64+
n = len(nums)
65+
pre_sum = [0] * (n + 1)
66+
for i in range(1, n + 1):
67+
pre_sum[i] = pre_sum[i - 1] + nums[i - 1]
68+
69+
stack = []
70+
next_lesser = [n] * n
71+
for i in range(n):
72+
while stack and nums[stack[-1]] > nums[i]:
73+
next_lesser[stack.pop()] = i
74+
stack.append(i)
75+
76+
stack = []
77+
prev_lesser = [-1] * n
78+
for i in range(n - 1, -1, -1):
79+
while stack and nums[stack[-1]] > nums[i]:
80+
prev_lesser[stack.pop()] = i
81+
stack.append(i)
82+
83+
res = 0
84+
for i in range(n):
85+
start, end = prev_lesser[i], next_lesser[i]
86+
t = nums[i] * (pre_sum[end] - pre_sum[start + 1])
87+
res = max(res, t)
88+
return res % (10 ** 9 + 7)
6489
```
6590

6691
### **Java**
6792

6893
```java
69-
94+
class Solution {
95+
public int maxSumMinProduct(int[] nums) {
96+
int n = nums.length;
97+
long[] preSum = new long[n + 1];
98+
for (int i = 1; i < n + 1; ++i) {
99+
preSum[i] = preSum[i - 1] + nums[i - 1];
100+
}
101+
Deque<Integer> stack = new ArrayDeque<>();
102+
int[] nextLesser = new int[n];
103+
Arrays.fill(nextLesser, n);
104+
for (int i = 0; i < n; ++i) {
105+
while (!stack.isEmpty() && nums[stack.peek()] > nums[i]) {
106+
nextLesser[stack.pop()] = i;
107+
}
108+
stack.push(i);
109+
}
110+
111+
stack = new ArrayDeque<>();
112+
int[] prevLesser = new int[n];
113+
Arrays.fill(prevLesser, -1);
114+
for (int i = n - 1; i >= 0; --i) {
115+
while (!stack.isEmpty() && nums[stack.peek()] > nums[i]) {
116+
prevLesser[stack.pop()] = i;
117+
}
118+
stack.push(i);
119+
}
120+
long res = 0;
121+
for (int i = 0; i < n; ++i) {
122+
int start = prevLesser[i], end = nextLesser[i];
123+
long t = nums[i] * (preSum[end] - preSum[start + 1]);
124+
res = Math.max(res, t);
125+
}
126+
return (int) (res % 1000000007);
127+
}
128+
}
70129
```
71130

72131
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public int maxSumMinProduct(int[] nums) {
3+
int n = nums.length;
4+
long[] preSum = new long[n + 1];
5+
for (int i = 1; i < n + 1; ++i) {
6+
preSum[i] = preSum[i - 1] + nums[i - 1];
7+
}
8+
Deque<Integer> stack = new ArrayDeque<>();
9+
int[] nextLesser = new int[n];
10+
Arrays.fill(nextLesser, n);
11+
for (int i = 0; i < n; ++i) {
12+
while (!stack.isEmpty() && nums[stack.peek()] > nums[i]) {
13+
nextLesser[stack.pop()] = i;
14+
}
15+
stack.push(i);
16+
}
17+
18+
stack = new ArrayDeque<>();
19+
int[] prevLesser = new int[n];
20+
Arrays.fill(prevLesser, -1);
21+
for (int i = n - 1; i >= 0; --i) {
22+
while (!stack.isEmpty() && nums[stack.peek()] > nums[i]) {
23+
prevLesser[stack.pop()] = i;
24+
}
25+
stack.push(i);
26+
}
27+
long res = 0;
28+
for (int i = 0; i < n; ++i) {
29+
int start = prevLesser[i], end = nextLesser[i];
30+
long t = nums[i] * (preSum[end] - preSum[start + 1]);
31+
res = Math.max(res, t);
32+
}
33+
return (int) (res % 1000000007);
34+
}
35+
}

0 commit comments

Comments
 (0)