feat: add solutions to lc problem: No.3584 (#4500)

No.3584.Maximum Product of First and Last Elements of a Subsequence
This commit is contained in:
Libin YANG 2025-06-16 12:47:46 +08:00 committed by GitHub
parent 53d14062fd
commit 43a2a84ae2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 234 additions and 8 deletions

View File

@ -75,32 +75,109 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3584.Ma
<!-- solution:start -->
### 方法一
### 方法一:枚举 + 维护前缀最值
我们可以枚举子序列的最后一个元素,假设它是 $\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}$ 的乘积,取最大值即可。
时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。
<!-- tabs:start -->
#### Python3
```python
class Solution:
def maximumProduct(self, nums: List[int], m: int) -> int:
ans = mx = -inf
mi = inf
for i in range(m - 1, len(nums)):
x = nums[i]
y = nums[i - m + 1]
mi = min(mi, y)
mx = max(mx, y)
ans = max(ans, x * mi, x * mx)
return ans
```
#### Java
```java
class Solution {
public long maximumProduct(int[] nums, int m) {
long ans = Long.MIN_VALUE;
int mx = Integer.MIN_VALUE;
int mi = Integer.MAX_VALUE;
for (int i = m - 1; i < nums.length; ++i) {
int x = nums[i];
int y = nums[i - m + 1];
mi = Math.min(mi, y);
mx = Math.max(mx, y);
ans = Math.max(ans, Math.max(1L * x * mi, 1L * x * mx));
}
return ans;
}
}
```
#### C++
```cpp
class Solution {
public:
long long maximumProduct(vector<int>& nums, int m) {
long long ans = LLONG_MIN;
int mx = INT_MIN;
int mi = INT_MAX;
for (int i = m - 1; i < nums.size(); ++i) {
int x = nums[i];
int y = nums[i - m + 1];
mi = min(mi, y);
mx = max(mx, y);
ans = max(ans, max(1LL * x * mi, 1LL * x * mx));
}
return ans;
}
};
```
#### Go
```go
func maximumProduct(nums []int, m int) int64 {
ans := int64(math.MinInt64)
mx := math.MinInt32
mi := math.MaxInt32
for i := m - 1; i < len(nums); i++ {
x := nums[i]
y := nums[i-m+1]
mi = min(mi, y)
mx = max(mx, y)
ans = max(ans, max(int64(x)*int64(mi), int64(x)*int64(mx)))
}
return ans
}
```
#### TypeScript
```ts
function maximumProduct(nums: number[], m: number): number {
let ans = Number.MIN_SAFE_INTEGER;
let mx = Number.MIN_SAFE_INTEGER;
let mi = Number.MAX_SAFE_INTEGER;
for (let i = m - 1; i < nums.length; i++) {
const x = nums[i];
const y = nums[i - m + 1];
mi = Math.min(mi, y);
mx = Math.max(mx, y);
ans = Math.max(ans, x * mi, x * mx);
}
return ans;
}
```
<!-- tabs:end -->

View File

@ -70,32 +70,109 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3584.Ma
<!-- solution:start -->
### Solution 1
### Solution 1: Enumeration + Maintaining Prefix Extremes
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.
The time complexity is $O(n)$, where $n$ is the length of array $\textit{nums}$. And the space complexity is $O(1)$.
<!-- tabs:start -->
#### Python3
```python
class Solution:
def maximumProduct(self, nums: List[int], m: int) -> int:
ans = mx = -inf
mi = inf
for i in range(m - 1, len(nums)):
x = nums[i]
y = nums[i - m + 1]
mi = min(mi, y)
mx = max(mx, y)
ans = max(ans, x * mi, x * mx)
return ans
```
#### Java
```java
class Solution {
public long maximumProduct(int[] nums, int m) {
long ans = Long.MIN_VALUE;
int mx = Integer.MIN_VALUE;
int mi = Integer.MAX_VALUE;
for (int i = m - 1; i < nums.length; ++i) {
int x = nums[i];
int y = nums[i - m + 1];
mi = Math.min(mi, y);
mx = Math.max(mx, y);
ans = Math.max(ans, Math.max(1L * x * mi, 1L * x * mx));
}
return ans;
}
}
```
#### C++
```cpp
class Solution {
public:
long long maximumProduct(vector<int>& nums, int m) {
long long ans = LLONG_MIN;
int mx = INT_MIN;
int mi = INT_MAX;
for (int i = m - 1; i < nums.size(); ++i) {
int x = nums[i];
int y = nums[i - m + 1];
mi = min(mi, y);
mx = max(mx, y);
ans = max(ans, max(1LL * x * mi, 1LL * x * mx));
}
return ans;
}
};
```
#### Go
```go
func maximumProduct(nums []int, m int) int64 {
ans := int64(math.MinInt64)
mx := math.MinInt32
mi := math.MaxInt32
for i := m - 1; i < len(nums); i++ {
x := nums[i]
y := nums[i-m+1]
mi = min(mi, y)
mx = max(mx, y)
ans = max(ans, max(int64(x)*int64(mi), int64(x)*int64(mx)))
}
return ans
}
```
#### TypeScript
```ts
function maximumProduct(nums: number[], m: number): number {
let ans = Number.MIN_SAFE_INTEGER;
let mx = Number.MIN_SAFE_INTEGER;
let mi = Number.MAX_SAFE_INTEGER;
for (let i = m - 1; i < nums.length; i++) {
const x = nums[i];
const y = nums[i - m + 1];
mi = Math.min(mi, y);
mx = Math.max(mx, y);
ans = Math.max(ans, x * mi, x * mx);
}
return ans;
}
```
<!-- tabs:end -->

View File

@ -0,0 +1,16 @@
class Solution {
public:
long long maximumProduct(vector<int>& nums, int m) {
long long ans = LLONG_MIN;
int mx = INT_MIN;
int mi = INT_MAX;
for (int i = m - 1; i < nums.size(); ++i) {
int x = nums[i];
int y = nums[i - m + 1];
mi = min(mi, y);
mx = max(mx, y);
ans = max(ans, max(1LL * x * mi, 1LL * x * mx));
}
return ans;
}
};

View File

@ -0,0 +1,15 @@
func maximumProduct(nums []int, m int) int64 {
ans := int64(math.MinInt64)
mx := math.MinInt32
mi := math.MaxInt32
for i := m - 1; i < len(nums); i++ {
x := nums[i]
y := nums[i-m+1]
mi = min(mi, y)
mx = max(mx, y)
ans = max(ans, max(int64(x)*int64(mi), int64(x)*int64(mx)))
}
return ans
}

View File

@ -0,0 +1,15 @@
class Solution {
public long maximumProduct(int[] nums, int m) {
long ans = Long.MIN_VALUE;
int mx = Integer.MIN_VALUE;
int mi = Integer.MAX_VALUE;
for (int i = m - 1; i < nums.length; ++i) {
int x = nums[i];
int y = nums[i - m + 1];
mi = Math.min(mi, y);
mx = Math.max(mx, y);
ans = Math.max(ans, Math.max(1L * x * mi, 1L * x * mx));
}
return ans;
}
}

View File

@ -0,0 +1,11 @@
class Solution:
def maximumProduct(self, nums: List[int], m: int) -> int:
ans = mx = -inf
mi = inf
for i in range(m - 1, len(nums)):
x = nums[i]
y = nums[i - m + 1]
mi = min(mi, y)
mx = max(mx, y)
ans = max(ans, x * mi, x * mx)
return ans

View File

@ -0,0 +1,15 @@
function maximumProduct(nums: number[], m: number): number {
let ans = Number.MIN_SAFE_INTEGER;
let mx = Number.MIN_SAFE_INTEGER;
let mi = Number.MAX_SAFE_INTEGER;
for (let i = m - 1; i < nums.length; i++) {
const x = nums[i];
const y = nums[i - m + 1];
mi = Math.min(mi, y);
mx = Math.max(mx, y);
ans = Math.max(ans, x * mi, x * mx);
}
return ans;
}