Skip to content

Commit 0fc61aa

Browse files
committed
Apr-2
1 parent cf0d76e commit 0fc61aa

12 files changed

+713
-9
lines changed

README.md

+13-6
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
5555
| [] | [leetcode-top-100-liked.md](./list/leetcode-top-100-liked.md) | 100/100 | - |
5656
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
5757
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 14/128 | 3 vips |
58-
| [🔲] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 70/2209 | 3 vips |
58+
| [🔲] | [灵茶山艾府.md](./list/灵茶山艾府.md) | 73/2210 | 3 vips |
5959

60-
**Solved**: 635 problems
60+
**Solved**: 640 problems
6161

6262
## 类型/Category
6363

@@ -312,7 +312,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
312312

313313
## Dynamic Programming
314314

315-
| Link | Problem(84) | Solution | Tag | Time | Space | Ref |
315+
| Link | Problem(86) | Solution | Tag | Time | Space | Ref |
316316
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
317317
| [Leetcode-542](https://leetcode.com/problems/01-matrix/) | 01 Matrix | [c++](./leetcode/542.01-matrix.cpp), [python3](./leetcode/542.01-matrix.py) | Dynamic Programming | O\(MN\) | O\(MN\) | - |
318318
| [Leetcode-650](https://leetcode.com/problems/2-keys-keyboard/) | 2 Keys Keyboard | [c++](./leetcode/650.2-keys-keyboard.cpp), [python3](./leetcode/650.2-keys-keyboard.py) | Dynamic Programming | O\(NlogN\) | O\(N\) | - |
@@ -361,6 +361,8 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
361361
| [Leetcode-152](https://leetcode.com/problems/maximum-product-subarray/) | Maximum Product Subarray | [c++](./leetcode/152.maximum-product-subarray.cpp), [python3](./leetcode/152.maximum-product-subarray.py) | Dynamic Programming | O\(N\) | O\(1\) | - |
362362
| [Leetcode-1235](https://leetcode.com/problems/maximum-profit-in-job-scheduling/) | Maximum Profit In Job Scheduling | [c++](./leetcode/1235.maximum-profit-in-job-scheduling.cpp), [python3](./leetcode/1235.maximum-profit-in-job-scheduling.py) | Dynamic Programming | O\(NlogN\) | O\(N\) | - |
363363
| [Leetcode-53](https://leetcode.com/problems/maximum-subarray/) | Maximum Subarray | [c++](./leetcode/53.maximum-subarray.cpp), [python3](./leetcode/53.maximum-subarray.py) | Dynamic Programming | O\(N\) | O\(N\) | - |
364+
| [Leetcode-2873](https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-i/) | Maximum Value Of An Ordered Triplet I | [c++](./leetcode/2873.maximum-value-of-an-ordered-triplet-i.cpp), [python3](./leetcode/2873.maximum-value-of-an-ordered-triplet-i.py) | Dynamic Programming | O\(N\) | O\(1\) | - |
365+
| [Leetcode-2874](https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-ii/) | Maximum Value Of An Ordered Triplet II | [c++](./leetcode/2874.maximum-value-of-an-ordered-triplet-ii.cpp), [python3](./leetcode/2874.maximum-value-of-an-ordered-triplet-ii.py) | Dynamic Programming | O\(N\) | O\(1\) | - |
364366
| [Leetcode-3277](https://leetcode.com/problems/maximum-xor-score-subarray-queries/) | Maximum Xor Score Subarray Queries | [c++](./leetcode/3277.maximum-xor-score-subarray-queries.cpp), [python3](./leetcode/3277.maximum-xor-score-subarray-queries.py) | Dynamic Programming | O\(N^2 \+ M\) | O\(N^2\) | - |
365367
| [Leetcode-1888](https://leetcode.com/problems/minimum-number-of-flips-to-make-the-binary-string-alternating/) | Minimum Number Of Flips To Make The Binary String Alternating | [c++](./leetcode/1888.minimum-number-of-flips-to-make-the-binary-string-alternating.cpp), [python3](./leetcode/1888.minimum-number-of-flips-to-make-the-binary-string-alternating.py) | Dynamic Programming | O\(N\) | O\(1\) | - |
366368
| [Leetcode-64](https://leetcode.com/problems/minimum-path-sum/) | Minimum Path Sum | [c++](./leetcode/64.minimum-path-sum.cpp), [python3](./leetcode/64.minimum-path-sum.py) | Dynamic Programming | O\(MN\) | O\(MN\) | - |
@@ -507,7 +509,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
507509

508510
## Hash Table
509511

510-
| Link | Problem(105) | Solution | Tag | Time | Space | Ref |
512+
| Link | Problem(107) | Solution | Tag | Time | Space | Ref |
511513
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
512514
| [Leetcode-721](https://leetcode.com/problems/accounts-merge/) | Accounts Merge | [c++](./leetcode/721.accounts-merge.cpp), [python3](./leetcode/721.accounts-merge.py) | Hash Table | O\(MlogM\) | O\(N\) | - |
513515
| [Leetcode-863](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/) | All Nodes Distance K In Binary Tree | [c++](./leetcode/863.all-nodes-distance-k-in-binary-tree.cpp), [python3](./leetcode/863.all-nodes-distance-k-in-binary-tree.py) | Hash Table | O\(N\) | O\(N\) | - |
@@ -522,7 +524,9 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
522524
| [Leetcode-2953](https://leetcode.com/problems/count-complete-substrings/) | Count Complete Substrings | [c++](./leetcode/2953.count-complete-substrings.cpp), [python3](./leetcode/2953.count-complete-substrings.py) | Hash Table | O\(N\) | O\(N\) | - |
523525
| [Leetcode-2364](https://leetcode.com/problems/count-number-of-bad-pairs/) | Count Number Of Bad Pairs | [c++](./leetcode/2364.count-number-of-bad-pairs.cpp), [python3](./leetcode/2364.count-number-of-bad-pairs.py) | Hash Table | O\(N\) | O\(N\) | - |
524526
| [Leetcode-3306](https://leetcode.com/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/) | Count Of Substrings Containing Every Vowel And K Consonants II | [c++](./leetcode/3306.count-of-substrings-containing-every-vowel-and-k-consonants-ii.cpp), [python3](./leetcode/3306.count-of-substrings-containing-every-vowel-and-k-consonants-ii.py) | Hash Table | O\(N\) | O\(N\) | - |
527+
| [Leetcode-3298](https://leetcode.com/problems/count-substrings-that-can-be-rearranged-to-contain-a-string-ii/) | Count Substrings That Can Be Rearranged To Contain A String II | [c++](./leetcode/3298.count-substrings-that-can-be-rearranged-to-contain-a-string-ii.cpp), [python3](./leetcode/3298.count-substrings-that-can-be-rearranged-to-contain-a-string-ii.py) | Hash Table | O\(N\) | O\(M\) | - |
525528
| [Leetcode-3325](https://leetcode.com/problems/count-substrings-with-k-frequency-characters-i/) | Count Substrings With K Frequency Characters I | [c++](./leetcode/3325.count-substrings-with-k-frequency-characters-i.cpp), [python3](./leetcode/3325.count-substrings-with-k-frequency-characters-i.py) | Hash Table | O\(N\) | O\(N\) | - |
529+
| [Leetcode-2537](https://leetcode.com/problems/count-the-number-of-good-subarrays/) | Count The Number Of Good Subarrays | [c++](./leetcode/2537.count-the-number-of-good-subarrays.cpp), [python3](./leetcode/2537.count-the-number-of-good-subarrays.py) | Hash Table | O\(N\) | O\(N\) | - |
526530
| [Leetcode-697](https://leetcode.com/problems/degree-of-an-array/) | Degree Of An Array | [c++](./leetcode/697.degree-of-an-array.cpp), [python3](./leetcode/697.degree-of-an-array.py) | Hash Table | O\(N\) | O\(N\) | - |
527531
| [Leetcode-1110](https://leetcode.com/problems/delete-nodes-and-return-forest/) | Delete Nodes And Return Forest | [c++](./leetcode/1110.delete-nodes-and-return-forest.cpp), [python3](./leetcode/1110.delete-nodes-and-return-forest.py) | Hash Table | O\(N\) | O\(N\) | - |
528532
| [Leetcode-1943](https://leetcode.com/problems/describe-the-painting/) | Describe The Painting | [c++](./leetcode/1943.describe-the-painting.cpp), [python3](./leetcode/1943.describe-the-painting.py) | Hash Table | O\(NlogN\) | O\(N\) | - |
@@ -812,7 +816,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
812816

813817
## Sliding Window
814818

815-
| Link | Problem(75) | Solution | Tag | Time | Space | Ref |
819+
| Link | Problem(77) | Solution | Tag | Time | Space | Ref |
816820
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
817821
| [Leetcode-3206](https://leetcode.com/problems/alternating-groups-i/) | Alternating Groups I | [c++](./leetcode/3206.alternating-groups-i.cpp), [python3](./leetcode/3206.alternating-groups-i.py) | Sliding Window | O\(N\) | O\(1\) | - |
818822
| [Leetcode-3208](https://leetcode.com/problems/alternating-groups-ii/) | Alternating Groups II | [c++](./leetcode/3208.alternating-groups-ii.cpp), [python3](./leetcode/3208.alternating-groups-ii.py) | Sliding Window | O\(N\) | O\(1\) | - |
@@ -822,7 +826,9 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
822826
| [Leetcode-2953](https://leetcode.com/problems/count-complete-substrings/) | Count Complete Substrings | [c++](./leetcode/2953.count-complete-substrings.cpp), [python3](./leetcode/2953.count-complete-substrings.py) | Sliding Window | O\(N\) | O\(N\) | - |
823827
| [Leetcode-3306](https://leetcode.com/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/) | Count Of Substrings Containing Every Vowel And K Consonants II | [c++](./leetcode/3306.count-of-substrings-containing-every-vowel-and-k-consonants-ii.cpp), [python3](./leetcode/3306.count-of-substrings-containing-every-vowel-and-k-consonants-ii.py) | Sliding Window | O\(N\) | O\(N\) | - |
824828
| [Leetcode-2962](https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times/) | Count Subarrays Where Max Element Appears At Least K Times | [c++](./leetcode/2962.count-subarrays-where-max-element-appears-at-least-k-times.cpp), [python3](./leetcode/2962.count-subarrays-where-max-element-appears-at-least-k-times.py) | Sliding Window | O\(N\) | O\(1\) | - |
829+
| [Leetcode-3298](https://leetcode.com/problems/count-substrings-that-can-be-rearranged-to-contain-a-string-ii/) | Count Substrings That Can Be Rearranged To Contain A String II | [c++](./leetcode/3298.count-substrings-that-can-be-rearranged-to-contain-a-string-ii.cpp), [python3](./leetcode/3298.count-substrings-that-can-be-rearranged-to-contain-a-string-ii.py) | Sliding Window | O\(N\) | O\(M\) | - |
825830
| [Leetcode-3325](https://leetcode.com/problems/count-substrings-with-k-frequency-characters-i/) | Count Substrings With K Frequency Characters I | [c++](./leetcode/3325.count-substrings-with-k-frequency-characters-i.cpp), [python3](./leetcode/3325.count-substrings-with-k-frequency-characters-i.py) | Sliding Window | O\(N\) | O\(N\) | - |
831+
| [Leetcode-2537](https://leetcode.com/problems/count-the-number-of-good-subarrays/) | Count The Number Of Good Subarrays | [c++](./leetcode/2537.count-the-number-of-good-subarrays.cpp), [python3](./leetcode/2537.count-the-number-of-good-subarrays.py) | Sliding Window | O\(N\) | O\(N\) | - |
826832
| [Leetcode-1652](https://leetcode.com/problems/defuse-the-bomb/) | Defuse The Bomb | [c++](./leetcode/1652.defuse-the-bomb.cpp), [python3](./leetcode/1652.defuse-the-bomb.py) | Sliding Window | O\(N\) | O\(1\) | - |
827833
| [Leetcode-438](https://leetcode.com/problems/find-all-anagrams-in-a-string/) | Find All Anagrams In A String | [c++](./leetcode/438.find-all-anagrams-in-a-string.cpp), [python3](./leetcode/438.find-all-anagrams-in-a-string.py) | Sliding Window | O\(N\) | O\(K\) | - |
828834
| [Leetcode-658](https://leetcode.com/problems/find-k-closest-elements/) | Find K Closest Elements | [c++](./leetcode/658.find-k-closest-elements.cpp), [python3](./leetcode/658.find-k-closest-elements.py) | Sliding Window | O\(Log\(N \- K\)\) | O\(1\) | - |
@@ -1313,13 +1319,14 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
13131319

13141320
## Array
13151321

1316-
| Link | Problem(5) | Solution | Tag | Time | Space | Ref |
1322+
| Link | Problem(6) | Solution | Tag | Time | Space | Ref |
13171323
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
13181324
| [Leetcode-3386](https://leetcode.com/problems/button-with-longest-push-time/) | Button With Longest Push Time | [c++](./leetcode/3386.button-with-longest-push-time.cpp), [python3](./leetcode/3386.button-with-longest-push-time.py) | Array | O\(N\) | O\(1\) | - |
13191325
| [Leetcode-2210](https://leetcode.com/problems/count-hills-and-valleys-in-an-array/) | Count Hills And Valleys In An Array | [c++](./leetcode/2210.count-hills-and-valleys-in-an-array.cpp), [python3](./leetcode/2210.count-hills-and-valleys-in-an-array.py) | Array | O\(N\) | O\(1\) | - |
13201326
| [Leetcode-3285](https://leetcode.com/problems/find-indices-of-stable-mountains/) | Find Indices Of Stable Mountains | [c++](./leetcode/3285.find-indices-of-stable-mountains.cpp), [python3](./leetcode/3285.find-indices-of-stable-mountains.py) | Array | O\(N\) | O\(1\) | - |
13211327
| [Leetcode-674](https://leetcode.com/problems/longest-continuous-increasing-subsequence/) | Longest Continuous Increasing Subsequence | [c++](./leetcode/674.longest-continuous-increasing-subsequence.cpp), [python3](./leetcode/674.longest-continuous-increasing-subsequence.py) | Array | O\(N\) | O\(1\) | - |
13221328
| [Leetcode-665](https://leetcode.com/problems/non-decreasing-array/) | Non Decreasing Array | [c++](./leetcode/665.non-decreasing-array.cpp), [python3](./leetcode/665.non-decreasing-array.py) | Array | O\(N\) | O\(1\) | - |
1329+
| [Lintcode-3847](https://www.lintcode.com/problem/number-of-subarrays-having-even-product/) | Number Of Subarrays Having Even Product | [c++](./lintcode/3847.number-of-subarrays-having-even-product.cpp), [python3](./lintcode/3847.number-of-subarrays-having-even-product.py) | Array | O\(N\) | O\(1\) | Leetcode-2495 |
13231330

13241331
## String
13251332

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Tag: Array, Hash Table, Sliding Window
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given an integer array nums and an integer k, return the number of good subarrays of nums.
8+
// A subarray arr is good if there are at least k pairs of indices (i, j) such that i < j and arr[i] == arr[j].
9+
// A subarray is a contiguous non-empty sequence of elements within an array.
10+
//  
11+
// Example 1:
12+
//
13+
// Input: nums = [1,1,1,1,1], k = 10
14+
// Output: 1
15+
// Explanation: The only good subarray is the array nums itself.
16+
//
17+
// Example 2:
18+
//
19+
// Input: nums = [3,1,4,3,2,2,4], k = 2
20+
// Output: 4
21+
// Explanation: There are 4 different good subarrays:
22+
// - [3,1,4,3,2,2] that has 2 pairs.
23+
// - [3,1,4,3,2,2,4] that has 3 pairs.
24+
// - [1,4,3,2,2,4] that has 2 pairs.
25+
// - [4,3,2,2,4] that has 2 pairs.
26+
//
27+
//  
28+
// Constraints:
29+
//
30+
// 1 <= nums.length <= 105
31+
// 1 <= nums[i], k <= 109
32+
//
33+
//
34+
35+
class Solution {
36+
public:
37+
long long countGood(vector<int>& nums, int k) {
38+
int n = nums.size();
39+
unordered_map<int, int> counter;
40+
int count = 0;
41+
long long res = 0;
42+
int i = 0;
43+
for (int j = 0; j < n; j++) {
44+
count += counter[nums[j]];
45+
counter[nums[j]] += 1;
46+
while (count >= k) {
47+
res += n - j;
48+
counter[nums[i]] -= 1;
49+
count -= counter[nums[i]];
50+
i += 1;
51+
}
52+
}
53+
return res;
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Tag: Array, Hash Table, Sliding Window
2+
# Time: O(N)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
7+
# Given an integer array nums and an integer k, return the number of good subarrays of nums.
8+
# A subarray arr is good if there are at least k pairs of indices (i, j) such that i < j and arr[i] == arr[j].
9+
# A subarray is a contiguous non-empty sequence of elements within an array.
10+
#  
11+
# Example 1:
12+
#
13+
# Input: nums = [1,1,1,1,1], k = 10
14+
# Output: 1
15+
# Explanation: The only good subarray is the array nums itself.
16+
#
17+
# Example 2:
18+
#
19+
# Input: nums = [3,1,4,3,2,2,4], k = 2
20+
# Output: 4
21+
# Explanation: There are 4 different good subarrays:
22+
# - [3,1,4,3,2,2] that has 2 pairs.
23+
# - [3,1,4,3,2,2,4] that has 3 pairs.
24+
# - [1,4,3,2,2,4] that has 2 pairs.
25+
# - [4,3,2,2,4] that has 2 pairs.
26+
#
27+
#  
28+
# Constraints:
29+
#
30+
# 1 <= nums.length <= 105
31+
# 1 <= nums[i], k <= 109
32+
#
33+
#
34+
35+
from collections import defaultdict
36+
class Solution:
37+
def countGood(self, nums: List[int], k: int) -> int:
38+
n = len(nums)
39+
counter = defaultdict(int)
40+
count = 0
41+
res = 0
42+
i = 0
43+
for j in range(n):
44+
count += counter[nums[j]]
45+
counter[nums[j]] += 1
46+
while count >= k:
47+
res += n - j
48+
counter[nums[i]] -= 1
49+
count -= counter[nums[i]]
50+
i += 1
51+
52+
return res
53+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Tag: Array, Dynamic Programming
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/3am7EjpmG7Y
7+
8+
// You are given a 0-indexed integer array nums.
9+
// Return the maximum value over all triplets of indices (i, j, k) such that i < j < k. If all such triplets have a negative value, return 0.
10+
// The value of a triplet of indices (i, j, k) is equal to (nums[i] - nums[j]) * nums[k].
11+
//  
12+
// Example 1:
13+
//
14+
// Input: nums = [12,6,1,2,7]
15+
// Output: 77
16+
// Explanation: The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
17+
// It can be shown that there are no ordered triplets of indices with a value greater than 77.
18+
//
19+
// Example 2:
20+
//
21+
// Input: nums = [1,10,3,4,19]
22+
// Output: 133
23+
// Explanation: The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
24+
// It can be shown that there are no ordered triplets of indices with a value greater than 133.
25+
//
26+
// Example 3:
27+
//
28+
// Input: nums = [1,2,3]
29+
// Output: 0
30+
// Explanation: The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.
31+
//
32+
//  
33+
// Constraints:
34+
//
35+
// 3 <= nums.length <= 100
36+
// 1 <= nums[i] <= 106
37+
//
38+
//
39+
40+
class Solution {
41+
public:
42+
long long maximumTripletValue(vector<int>& nums) {
43+
int n = nums.size();
44+
long long res = 0;
45+
int a = 0;
46+
int b = 0;
47+
for (int i = 0; i < n; i++) {
48+
res = max(res, 1LL * b * nums[i]);
49+
a = max(a, nums[i]);
50+
b = max(b, a - nums[i]);
51+
}
52+
53+
return res;
54+
}
55+
};
56+
57+
class Solution {
58+
public:
59+
long long maximumTripletValue(vector<int>& nums) {
60+
int n = nums.size();
61+
vector<int> prefix(n + 1, 0);
62+
vector<int> suffix(n + 1, 0);
63+
64+
for (int i = 1; i <= n; ++i) {
65+
prefix[i] = max(nums[i - 1], prefix[i - 1]);
66+
}
67+
68+
for (int i = n - 1; i >= 0; --i) {
69+
suffix[i] = max(nums[i], suffix[i + 1]);
70+
}
71+
72+
long long res = 0;
73+
for (int i = 0; i < n; ++i) {
74+
res = max(res, 1LL * (prefix[i] - nums[i]) * suffix[i + 1]);
75+
}
76+
77+
return res;
78+
}
79+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Tag: Array, Dynamic Programming
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/3am7EjpmG7Y
7+
8+
# You are given a 0-indexed integer array nums.
9+
# Return the maximum value over all triplets of indices (i, j, k) such that i < j < k. If all such triplets have a negative value, return 0.
10+
# The value of a triplet of indices (i, j, k) is equal to (nums[i] - nums[j]) * nums[k].
11+
#  
12+
# Example 1:
13+
#
14+
# Input: nums = [12,6,1,2,7]
15+
# Output: 77
16+
# Explanation: The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
17+
# It can be shown that there are no ordered triplets of indices with a value greater than 77.
18+
#
19+
# Example 2:
20+
#
21+
# Input: nums = [1,10,3,4,19]
22+
# Output: 133
23+
# Explanation: The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
24+
# It can be shown that there are no ordered triplets of indices with a value greater than 133.
25+
#
26+
# Example 3:
27+
#
28+
# Input: nums = [1,2,3]
29+
# Output: 0
30+
# Explanation: The only ordered triplet of indices (0, 1, 2) has a negative value of (nums[0] - nums[1]) * nums[2] = -3. Hence, the answer would be 0.
31+
#
32+
#  
33+
# Constraints:
34+
#
35+
# 3 <= nums.length <= 100
36+
# 1 <= nums[i] <= 106
37+
#
38+
#
39+
40+
class Solution:
41+
def maximumTripletValue(self, nums: List[int]) -> int:
42+
n = len(nums)
43+
res = 0
44+
a = 0
45+
b = 0
46+
47+
for i in range(n):
48+
res = max(res, b * nums[i])
49+
a = max(a, nums[i])
50+
b = max(b, a - nums[i])
51+
52+
return res
53+
54+
class Solution:
55+
def maximumTripletValue(self, nums: List[int]) -> int:
56+
n = len(nums)
57+
58+
prefix = [0 for i in range(n + 1)]
59+
suffix = [0 for i in range(n + 1)]
60+
61+
for i in range(1, n + 1):
62+
prefix[i] = max(nums[i - 1], prefix[i - 1])
63+
64+
for i in range(n - 1, -1, -1):
65+
suffix[i] = max(nums[i], suffix[i + 1])
66+
67+
res = 0
68+
for i in range(n):
69+
res = max(res, (prefix[i] - nums[i]) * suffix[i + 1])
70+
71+
return res

0 commit comments

Comments
 (0)