|
| 1 | +# [2907. Maximum Profitable Triplets With Increasing Prices I](https://leetcode.cn/problems/maximum-profitable-triplets-with-increasing-prices-i) |
| 2 | + |
| 3 | +[English Version](/solution/2900-2999/2907.Maximum%20Profitable%20Triplets%20With%20Increasing%20Prices%20I/README_EN.md) |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +<!-- 这里写题目描述 --> |
| 8 | + |
| 9 | +<p>Given the <strong>0-indexed</strong> arrays <code>prices</code> and <code>profits</code> of length <code>n</code>. There are <code>n</code> items in an store where the <code>i<sup>th</sup></code> item has a price of <code>prices[i]</code> and a profit of <code>profits[i]</code>.</p> |
| 10 | + |
| 11 | +<p>We have to pick three items with the following condition:</p> |
| 12 | + |
| 13 | +<ul> |
| 14 | + <li><code>prices[i] < prices[j] < prices[k]</code> where <code>i < j < k</code>.</li> |
| 15 | +</ul> |
| 16 | + |
| 17 | +<p>If we pick items with indices <code>i</code>, <code>j</code> and <code>k</code> satisfying the above condition, the profit would be <code>profits[i] + profits[j] + profits[k]</code>.</p> |
| 18 | + |
| 19 | +<p>Return<em> the <strong>maximum profit</strong> we can get, and </em><code>-1</code><em> if it's not possible to pick three items with the given condition.</em></p> |
| 20 | + |
| 21 | +<p> </p> |
| 22 | +<p><strong class="example">Example 1:</strong></p> |
| 23 | + |
| 24 | +<pre> |
| 25 | +<strong>Input:</strong> prices = [10,2,3,4], profits = [100,2,7,10] |
| 26 | +<strong>Output:</strong> 19 |
| 27 | +<strong>Explanation:</strong> We can't pick the item with index i=0 since there are no indices j and k such that the condition holds. |
| 28 | +So the only triplet we can pick, are the items with indices 1, 2 and 3 and it's a valid pick since prices[1] < prices[2] < prices[3]. |
| 29 | +The answer would be sum of their profits which is 2 + 7 + 10 = 19.</pre> |
| 30 | + |
| 31 | +<p><strong class="example">Example 2:</strong></p> |
| 32 | + |
| 33 | +<pre> |
| 34 | +<strong>Input:</strong> prices = [1,2,3,4,5], profits = [1,5,3,4,6] |
| 35 | +<strong>Output:</strong> 15 |
| 36 | +<strong>Explanation:</strong> We can select any triplet of items since for each triplet of indices i, j and k such that i < j < k, the condition holds. |
| 37 | +Therefore the maximum profit we can get would be the 3 most profitable items which are indices 1, 3 and 4. |
| 38 | +The answer would be sum of their profits which is 5 + 4 + 6 = 15.</pre> |
| 39 | + |
| 40 | +<p><strong class="example">Example 3:</strong></p> |
| 41 | + |
| 42 | +<pre> |
| 43 | +<strong>Input:</strong> prices = [4,3,2,1], profits = [33,20,19,87] |
| 44 | +<strong>Output:</strong> -1 |
| 45 | +<strong>Explanation:</strong> We can't select any triplet of indices such that the condition holds, so we return -1. |
| 46 | +</pre> |
| 47 | + |
| 48 | +<p> </p> |
| 49 | +<p><strong>Constraints:</strong></p> |
| 50 | + |
| 51 | +<ul> |
| 52 | + <li><code>3 <= prices.length == profits.length <= 2000</code></li> |
| 53 | + <li><code>1 <= prices[i] <= 10<sup>6</sup></code></li> |
| 54 | + <li><code>1 <= profits[i] <= 10<sup>6</sup></code></li> |
| 55 | +</ul> |
| 56 | + |
| 57 | +## 解法 |
| 58 | + |
| 59 | +<!-- 这里可写通用的实现逻辑 --> |
| 60 | + |
| 61 | +**方法一:枚举中间元素** |
| 62 | + |
| 63 | +我们可以枚举中间元素 $profits[j]$,然后再枚举左边元素 $profits[i]$ 和右边元素 $profits[k]$。对于每个 $profits[j]$,我们需要找到最大的 $profits[i]$ 和最大的 $profits[k]$,使得 $prices[i] < prices[j] < prices[k]$。我们记 $left$ 为 $profits[j]$ 左边的最大值,而 $right$ 为 $profits[j]$ 右边的最大值。如果存在,那么我们更新答案为 $ans = \max(ans, left + profits[j] + right)$。 |
| 64 | + |
| 65 | +时间复杂度 $O(n^2)$,其中 $n$ 为数组长度。空间复杂度 $O(1)$。 |
| 66 | + |
| 67 | +<!-- tabs:start --> |
| 68 | + |
| 69 | +### **Python3** |
| 70 | + |
| 71 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 72 | + |
| 73 | +```python |
| 74 | +class Solution: |
| 75 | + def maxProfit(self, prices: List[int], profits: List[int]) -> int: |
| 76 | + n = len(prices) |
| 77 | + ans = -1 |
| 78 | + for j, x in enumerate(profits): |
| 79 | + left = right = 0 |
| 80 | + for i in range(j): |
| 81 | + if prices[i] < prices[j] and left < profits[i]: |
| 82 | + left = profits[i] |
| 83 | + for k in range(j + 1, n): |
| 84 | + if prices[j] < prices[k] and right < profits[k]: |
| 85 | + right = profits[k] |
| 86 | + if left and right: |
| 87 | + ans = max(ans, left + x + right) |
| 88 | + return ans |
| 89 | +``` |
| 90 | + |
| 91 | +### **Java** |
| 92 | + |
| 93 | +<!-- 这里可写当前语言的特殊实现逻辑 --> |
| 94 | + |
| 95 | +```java |
| 96 | +class Solution { |
| 97 | + public int maxProfit(int[] prices, int[] profits) { |
| 98 | + int n = prices.length; |
| 99 | + int ans = -1; |
| 100 | + for (int j = 0; j < n; ++j) { |
| 101 | + int left = 0, right = 0; |
| 102 | + for (int i = 0; i < j; ++i) { |
| 103 | + if (prices[i] < prices[j]) { |
| 104 | + left = Math.max(left, profits[i]); |
| 105 | + } |
| 106 | + } |
| 107 | + for (int k = j + 1; k < n; ++k) { |
| 108 | + if (prices[j] < prices[k]) { |
| 109 | + right = Math.max(right, profits[k]); |
| 110 | + } |
| 111 | + } |
| 112 | + if (left > 0 && right > 0) { |
| 113 | + ans = Math.max(ans, left + profits[j] + right); |
| 114 | + } |
| 115 | + } |
| 116 | + return ans; |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +### **C++** |
| 122 | + |
| 123 | +```cpp |
| 124 | +class Solution { |
| 125 | +public: |
| 126 | + int maxProfit(vector<int>& prices, vector<int>& profits) { |
| 127 | + int n = prices.size(); |
| 128 | + int ans = -1; |
| 129 | + for (int j = 0; j < n; ++j) { |
| 130 | + int left = 0, right = 0; |
| 131 | + for (int i = 0; i < j; ++i) { |
| 132 | + if (prices[i] < prices[j]) { |
| 133 | + left = max(left, profits[i]); |
| 134 | + } |
| 135 | + } |
| 136 | + for (int k = j + 1; k < n; ++k) { |
| 137 | + if (prices[j] < prices[k]) { |
| 138 | + right = max(right, profits[k]); |
| 139 | + } |
| 140 | + } |
| 141 | + if (left && right) { |
| 142 | + ans = max(ans, left + profits[j] + right); |
| 143 | + } |
| 144 | + } |
| 145 | + return ans; |
| 146 | + } |
| 147 | +}; |
| 148 | +``` |
| 149 | +
|
| 150 | +### **Go** |
| 151 | +
|
| 152 | +```go |
| 153 | +func maxProfit(prices []int, profits []int) int { |
| 154 | + n := len(prices) |
| 155 | + ans := -1 |
| 156 | + for j, x := range profits { |
| 157 | + left, right := 0, 0 |
| 158 | + for i := 0; i < j; i++ { |
| 159 | + if prices[i] < prices[j] { |
| 160 | + left = max(left, profits[i]) |
| 161 | + } |
| 162 | + } |
| 163 | + for k := j + 1; k < n; k++ { |
| 164 | + if prices[j] < prices[k] { |
| 165 | + right = max(right, profits[k]) |
| 166 | + } |
| 167 | + } |
| 168 | + if left > 0 && right > 0 { |
| 169 | + ans = max(ans, left+x+right) |
| 170 | + } |
| 171 | + } |
| 172 | + return ans |
| 173 | +} |
| 174 | +
|
| 175 | +func max(a, b int) int { |
| 176 | + if a > b { |
| 177 | + return a |
| 178 | + } |
| 179 | + return b |
| 180 | +} |
| 181 | +``` |
| 182 | + |
| 183 | +### **TypeScript** |
| 184 | + |
| 185 | +```ts |
| 186 | +function maxProfit(prices: number[], profits: number[]): number { |
| 187 | + const n = prices.length; |
| 188 | + let ans = -1; |
| 189 | + for (let j = 0; j < n; ++j) { |
| 190 | + let [left, right] = [0, 0]; |
| 191 | + for (let i = 0; i < j; ++i) { |
| 192 | + if (prices[i] < prices[j]) { |
| 193 | + left = Math.max(left, profits[i]); |
| 194 | + } |
| 195 | + } |
| 196 | + for (let k = j + 1; k < n; ++k) { |
| 197 | + if (prices[j] < prices[k]) { |
| 198 | + right = Math.max(right, profits[k]); |
| 199 | + } |
| 200 | + } |
| 201 | + if (left > 0 && right > 0) { |
| 202 | + ans = Math.max(ans, left + profits[j] + right); |
| 203 | + } |
| 204 | + } |
| 205 | + return ans; |
| 206 | +} |
| 207 | +``` |
| 208 | + |
| 209 | +### **Rust** |
| 210 | + |
| 211 | +```rust |
| 212 | +impl Solution { |
| 213 | + pub fn max_profit(prices: Vec<i32>, profits: Vec<i32>) -> i32 { |
| 214 | + let n = prices.len(); |
| 215 | + let mut ans = -1; |
| 216 | + |
| 217 | + for j in 0..n { |
| 218 | + let mut left = 0; |
| 219 | + let mut right = 0; |
| 220 | + |
| 221 | + for i in 0..j { |
| 222 | + if prices[i] < prices[j] { |
| 223 | + left = left.max(profits[i]); |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + for k in (j + 1)..n { |
| 228 | + if prices[j] < prices[k] { |
| 229 | + right = right.max(profits[k]); |
| 230 | + } |
| 231 | + } |
| 232 | + |
| 233 | + if left > 0 && right > 0 { |
| 234 | + ans = ans.max(left + profits[j] + right); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + ans |
| 239 | + } |
| 240 | +} |
| 241 | +``` |
| 242 | + |
| 243 | +### **...** |
| 244 | + |
| 245 | +``` |
| 246 | +
|
| 247 | +``` |
| 248 | + |
| 249 | +<!-- tabs:end --> |
0 commit comments