Skip to content

Commit 43a2a84

Browse files
authored
feat: add solutions to lc problem: No.3584 (#4500)
No.3584.Maximum Product of First and Last Elements of a Subsequence
1 parent 53d1406 commit 43a2a84

File tree

7 files changed

+234
-8
lines changed

7 files changed

+234
-8
lines changed

solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/README.md

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,32 +75,109 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3584.Ma
7575

7676
<!-- solution:start -->
7777

78-
### 方法一
78+
### 方法一:枚举 + 维护前缀最值
79+
80+
我们可以枚举子序列的最后一个元素,假设它是 $\textit{nums}[i]$,那么子序列的第一个元素可以是 $\textit{nums}[j]$,其中 $j \leq i - m + 1$。因此,我们用两个变量 $\textit{mi}$ 和 $\textit{mx}$ 分别维护前缀最小值和最大值,遍历到 $\textit{nums}[i]$ 时,更新 $\textit{mi}$ 和 $\textit{mx}$,然后计算 $\textit{nums}[i]$ 和 $\textit{mi}$ 以及 $\textit{mx}$ 的乘积,取最大值即可。
81+
82+
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
7983

8084
<!-- tabs:start -->
8185

8286
#### Python3
8387

8488
```python
85-
89+
class Solution:
90+
def maximumProduct(self, nums: List[int], m: int) -> int:
91+
ans = mx = -inf
92+
mi = inf
93+
for i in range(m - 1, len(nums)):
94+
x = nums[i]
95+
y = nums[i - m + 1]
96+
mi = min(mi, y)
97+
mx = max(mx, y)
98+
ans = max(ans, x * mi, x * mx)
99+
return ans
86100
```
87101

88102
#### Java
89103

90104
```java
91-
105+
class Solution {
106+
public long maximumProduct(int[] nums, int m) {
107+
long ans = Long.MIN_VALUE;
108+
int mx = Integer.MIN_VALUE;
109+
int mi = Integer.MAX_VALUE;
110+
for (int i = m - 1; i < nums.length; ++i) {
111+
int x = nums[i];
112+
int y = nums[i - m + 1];
113+
mi = Math.min(mi, y);
114+
mx = Math.max(mx, y);
115+
ans = Math.max(ans, Math.max(1L * x * mi, 1L * x * mx));
116+
}
117+
return ans;
118+
}
119+
}
92120
```
93121

94122
#### C++
95123

96124
```cpp
97-
125+
class Solution {
126+
public:
127+
long long maximumProduct(vector<int>& nums, int m) {
128+
long long ans = LLONG_MIN;
129+
int mx = INT_MIN;
130+
int mi = INT_MAX;
131+
for (int i = m - 1; i < nums.size(); ++i) {
132+
int x = nums[i];
133+
int y = nums[i - m + 1];
134+
mi = min(mi, y);
135+
mx = max(mx, y);
136+
ans = max(ans, max(1LL * x * mi, 1LL * x * mx));
137+
}
138+
return ans;
139+
}
140+
};
98141
```
99142
100143
#### Go
101144
102145
```go
146+
func maximumProduct(nums []int, m int) int64 {
147+
ans := int64(math.MinInt64)
148+
mx := math.MinInt32
149+
mi := math.MaxInt32
150+
151+
for i := m - 1; i < len(nums); i++ {
152+
x := nums[i]
153+
y := nums[i-m+1]
154+
mi = min(mi, y)
155+
mx = max(mx, y)
156+
ans = max(ans, max(int64(x)*int64(mi), int64(x)*int64(mx)))
157+
}
158+
159+
return ans
160+
}
161+
```
162+
163+
#### TypeScript
164+
165+
```ts
166+
function maximumProduct(nums: number[], m: number): number {
167+
let ans = Number.MIN_SAFE_INTEGER;
168+
let mx = Number.MIN_SAFE_INTEGER;
169+
let mi = Number.MAX_SAFE_INTEGER;
170+
171+
for (let i = m - 1; i < nums.length; i++) {
172+
const x = nums[i];
173+
const y = nums[i - m + 1];
174+
mi = Math.min(mi, y);
175+
mx = Math.max(mx, y);
176+
ans = Math.max(ans, x * mi, x * mx);
177+
}
103178

179+
return ans;
180+
}
104181
```
105182

106183
<!-- tabs:end -->

solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/README_EN.md

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,109 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3584.Ma
7070

7171
<!-- solution:start -->
7272

73-
### Solution 1
73+
### Solution 1: Enumeration + Maintaining Prefix Extremes
74+
75+
We can enumerate the last element of the subsequence, assuming it is $\textit{nums}[i]$. Then the first element of the subsequence can be $\textit{nums}[j]$, where $j \leq i - m + 1$. Therefore, we use two variables $\textit{mi}$ and $\textit{mx}$ to maintain the prefix minimum and maximum values respectively. When traversing to $\textit{nums}[i]$, we update $\textit{mi}$ and $\textit{mx}$, then calculate the products of $\textit{nums}[i]$ with $\textit{mi}$ and $\textit{mx}$, taking the maximum value.
76+
77+
The time complexity is $O(n)$, where $n$ is the length of array $\textit{nums}$. And the space complexity is $O(1)$.
7478

7579
<!-- tabs:start -->
7680

7781
#### Python3
7882

7983
```python
80-
84+
class Solution:
85+
def maximumProduct(self, nums: List[int], m: int) -> int:
86+
ans = mx = -inf
87+
mi = inf
88+
for i in range(m - 1, len(nums)):
89+
x = nums[i]
90+
y = nums[i - m + 1]
91+
mi = min(mi, y)
92+
mx = max(mx, y)
93+
ans = max(ans, x * mi, x * mx)
94+
return ans
8195
```
8296

8397
#### Java
8498

8599
```java
86-
100+
class Solution {
101+
public long maximumProduct(int[] nums, int m) {
102+
long ans = Long.MIN_VALUE;
103+
int mx = Integer.MIN_VALUE;
104+
int mi = Integer.MAX_VALUE;
105+
for (int i = m - 1; i < nums.length; ++i) {
106+
int x = nums[i];
107+
int y = nums[i - m + 1];
108+
mi = Math.min(mi, y);
109+
mx = Math.max(mx, y);
110+
ans = Math.max(ans, Math.max(1L * x * mi, 1L * x * mx));
111+
}
112+
return ans;
113+
}
114+
}
87115
```
88116

89117
#### C++
90118

91119
```cpp
92-
120+
class Solution {
121+
public:
122+
long long maximumProduct(vector<int>& nums, int m) {
123+
long long ans = LLONG_MIN;
124+
int mx = INT_MIN;
125+
int mi = INT_MAX;
126+
for (int i = m - 1; i < nums.size(); ++i) {
127+
int x = nums[i];
128+
int y = nums[i - m + 1];
129+
mi = min(mi, y);
130+
mx = max(mx, y);
131+
ans = max(ans, max(1LL * x * mi, 1LL * x * mx));
132+
}
133+
return ans;
134+
}
135+
};
93136
```
94137
95138
#### Go
96139
97140
```go
141+
func maximumProduct(nums []int, m int) int64 {
142+
ans := int64(math.MinInt64)
143+
mx := math.MinInt32
144+
mi := math.MaxInt32
145+
146+
for i := m - 1; i < len(nums); i++ {
147+
x := nums[i]
148+
y := nums[i-m+1]
149+
mi = min(mi, y)
150+
mx = max(mx, y)
151+
ans = max(ans, max(int64(x)*int64(mi), int64(x)*int64(mx)))
152+
}
153+
154+
return ans
155+
}
156+
```
157+
158+
#### TypeScript
159+
160+
```ts
161+
function maximumProduct(nums: number[], m: number): number {
162+
let ans = Number.MIN_SAFE_INTEGER;
163+
let mx = Number.MIN_SAFE_INTEGER;
164+
let mi = Number.MAX_SAFE_INTEGER;
165+
166+
for (let i = m - 1; i < nums.length; i++) {
167+
const x = nums[i];
168+
const y = nums[i - m + 1];
169+
mi = Math.min(mi, y);
170+
mx = Math.max(mx, y);
171+
ans = Math.max(ans, x * mi, x * mx);
172+
}
98173

174+
return ans;
175+
}
99176
```
100177

101178
<!-- tabs:end -->
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
long long maximumProduct(vector<int>& nums, int m) {
4+
long long ans = LLONG_MIN;
5+
int mx = INT_MIN;
6+
int mi = INT_MAX;
7+
for (int i = m - 1; i < nums.size(); ++i) {
8+
int x = nums[i];
9+
int y = nums[i - m + 1];
10+
mi = min(mi, y);
11+
mx = max(mx, y);
12+
ans = max(ans, max(1LL * x * mi, 1LL * x * mx));
13+
}
14+
return ans;
15+
}
16+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func maximumProduct(nums []int, m int) int64 {
2+
ans := int64(math.MinInt64)
3+
mx := math.MinInt32
4+
mi := math.MaxInt32
5+
6+
for i := m - 1; i < len(nums); i++ {
7+
x := nums[i]
8+
y := nums[i-m+1]
9+
mi = min(mi, y)
10+
mx = max(mx, y)
11+
ans = max(ans, max(int64(x)*int64(mi), int64(x)*int64(mx)))
12+
}
13+
14+
return ans
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public long maximumProduct(int[] nums, int m) {
3+
long ans = Long.MIN_VALUE;
4+
int mx = Integer.MIN_VALUE;
5+
int mi = Integer.MAX_VALUE;
6+
for (int i = m - 1; i < nums.length; ++i) {
7+
int x = nums[i];
8+
int y = nums[i - m + 1];
9+
mi = Math.min(mi, y);
10+
mx = Math.max(mx, y);
11+
ans = Math.max(ans, Math.max(1L * x * mi, 1L * x * mx));
12+
}
13+
return ans;
14+
}
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def maximumProduct(self, nums: List[int], m: int) -> int:
3+
ans = mx = -inf
4+
mi = inf
5+
for i in range(m - 1, len(nums)):
6+
x = nums[i]
7+
y = nums[i - m + 1]
8+
mi = min(mi, y)
9+
mx = max(mx, y)
10+
ans = max(ans, x * mi, x * mx)
11+
return ans
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function maximumProduct(nums: number[], m: number): number {
2+
let ans = Number.MIN_SAFE_INTEGER;
3+
let mx = Number.MIN_SAFE_INTEGER;
4+
let mi = Number.MAX_SAFE_INTEGER;
5+
6+
for (let i = m - 1; i < nums.length; i++) {
7+
const x = nums[i];
8+
const y = nums[i - m + 1];
9+
mi = Math.min(mi, y);
10+
mx = Math.max(mx, y);
11+
ans = Math.max(ans, x * mi, x * mx);
12+
}
13+
14+
return ans;
15+
}

0 commit comments

Comments
 (0)