|
| 1 | +# 2874. Maximum Value of an Ordered Triplet II |
| 2 | + |
| 3 | +## 📌 Problem Statement |
| 4 | +You are given a `0-indexed` integer array `nums`. |
| 5 | +Return the **maximum value** over all ordered triplets `(i, j, k)` such that `i < j < k`. If all such triplets have a negative value, return `0`. |
| 6 | + |
| 7 | +The value of the triplet `(i, j, k)` is calculated as: |
| 8 | +\[ |
| 9 | +(nums[i] - nums[j]) \times nums[k] |
| 10 | +\] |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +## ✅ Approach & Solution |
| 15 | +Since the constraints are **large (`n ≤ 10^5`)**, a brute-force `O(n³)` solution is infeasible. Instead, we can solve this in **O(n) time** using a greedy approach. |
| 16 | + |
| 17 | +### **Algorithm** |
| 18 | +1. **Iterate through `nums` while tracking:** |
| 19 | + - `mx` → Maximum value encountered so far. |
| 20 | + - `mx_diff` → Maximum difference `(nums[i] - nums[j])` encountered. |
| 21 | + - `ans` → Maximum valid triplet value. |
| 22 | +2. **Update `ans` at each step** using `mx_diff * nums[k]`. |
| 23 | +3. **Update `mx_diff` and `mx`** accordingly. |
| 24 | +4. **Return `ans`**, ensuring we return `0` if no positive value is found. |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +## 📌 Code Implementation |
| 29 | +```python |
| 30 | +from typing import List |
| 31 | + |
| 32 | +class Solution: |
| 33 | + def maximumTripletValue(self, nums: List[int]) -> int: |
| 34 | + ans = mx = mx_diff = 0 |
| 35 | + for x in nums: |
| 36 | + ans = max(ans, mx_diff * x) |
| 37 | + mx_diff = max(mx_diff, mx - x) |
| 38 | + mx = max(mx, x) |
| 39 | + return ans |
| 40 | +``` |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## 🔥 Complexity Analysis |
| 45 | +- **Time Complexity**: **O(n)** → We traverse `nums` once. |
| 46 | +- **Space Complexity**: **O(1)** → We use only a few integer variables. |
| 47 | + |
| 48 | +--- |
| 49 | + |
| 50 | +## 🛠 Example Walkthrough |
| 51 | + |
| 52 | +### **Example 1** |
| 53 | +🔹 `nums = [12,6,1,2,7]` |
| 54 | +✔️ Maximum triplet: `(0, 2, 4)` |
| 55 | +✔️ Computation: `(12 - 1) * 7 = 77` |
| 56 | +✅ Output: `77` |
| 57 | + |
| 58 | +### **Example 2** |
| 59 | +🔹 `nums = [1,10,3,4,19]` |
| 60 | +✔️ Maximum triplet: `(1, 2, 4)` |
| 61 | +✔️ Computation: `(10 - 3) * 19 = 133` |
| 62 | +✅ Output: `133` |
| 63 | + |
| 64 | +### **Example 3** |
| 65 | +🔹 `nums = [1,2,3]` |
| 66 | +✔️ Only possible triplet: `(0, 1, 2)` |
| 67 | +✔️ Computation: `(1 - 2) * 3 = -3` |
| 68 | +✔️ Since it's negative, return `0`. |
| 69 | +✅ Output: `0` |
| 70 | + |
| 71 | +--- |
| 72 | + |
| 73 | +## 🚀 Alternative Approaches |
| 74 | +1️⃣ **Brute Force (O(n³))** |
| 75 | + - Check all `(i, j, k)` triplets and compute values. |
| 76 | + - **Inefficient for `n = 10^5`**. |
| 77 | + |
| 78 | +2️⃣ **Using Prefix Arrays (O(n²))** |
| 79 | + - Precompute max values up to `j` and use them for calculations. |
| 80 | + |
| 81 | +📌 **Why is our approach optimal?** |
| 82 | +🔹 It **avoids unnecessary iterations** and **only keeps track of necessary values**, leading to an efficient **O(n) solution**. |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +## ❓ Discussion |
| 87 | +💬 **Q: Can this handle large inputs efficiently?** |
| 88 | +✔️ Yes! Our **O(n) solution** works in milliseconds for `n ≤ 10^5`. |
| 89 | + |
| 90 | +💬 **Q: What if all values in `nums` are the same?** |
| 91 | +✔️ `mx_diff = 0`, so the result will be `0`. |
| 92 | + |
| 93 | +💬 **Q: Can we solve this using DP?** |
| 94 | +✔️ Yes, but unnecessary since **greedy works optimally in O(n)**. |
| 95 | + |
| 96 | +💬 **Q: What are some edge cases we should test?** |
| 97 | +✔️ Are there any input cases that might break our approach? |
| 98 | + |
| 99 | +💬 **Q: Could this be optimized further?** |
| 100 | +✔️ The current solution is already `O(n)`, but is there a way to reduce the number of operations or improve readability? |
| 101 | + |
| 102 | +--- |
| 103 | + |
| 104 | +## 🔥 PRs & Issues Welcome! |
| 105 | +👥 Feel free to suggest optimizations or discuss edge cases! 🚀 |
| 106 | + |
0 commit comments