diff --git a/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/README.md b/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/README.md index 15eae422999ba..28c3cc46f8904 100644 --- a/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/README.md +++ b/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/README.md @@ -75,32 +75,109 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3584.Ma -### 方法一 +### 方法一:枚举 + 维护前缀最值 + +我们可以枚举子序列的最后一个元素,假设它是 $\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)$。 #### 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& 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; +} ``` diff --git a/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/README_EN.md b/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/README_EN.md index ef99654783cad..982e3fec018b7 100644 --- a/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/README_EN.md +++ b/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/README_EN.md @@ -70,32 +70,109 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3584.Ma -### 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)$. #### 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& 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; +} ``` diff --git a/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/Solution.cpp b/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/Solution.cpp new file mode 100644 index 0000000000000..73c9227439932 --- /dev/null +++ b/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/Solution.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + long long maximumProduct(vector& 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; + } +}; diff --git a/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/Solution.go b/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/Solution.go new file mode 100644 index 0000000000000..bac2efd33dfbf --- /dev/null +++ b/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/Solution.go @@ -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 +} diff --git a/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/Solution.java b/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/Solution.java new file mode 100644 index 0000000000000..07d31a544194c --- /dev/null +++ b/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/Solution.java @@ -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; + } +} \ No newline at end of file diff --git a/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/Solution.py b/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/Solution.py new file mode 100644 index 0000000000000..3d61b52d60c23 --- /dev/null +++ b/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/Solution.py @@ -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 diff --git a/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/Solution.ts b/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/Solution.ts new file mode 100644 index 0000000000000..037f639edd74c --- /dev/null +++ b/solution/3500-3599/3584.Maximum Product of First and Last Elements of a Subsequence/Solution.ts @@ -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; +}