Skip to content

Commit 403859f

Browse files
committed
solutions: 2873 - Maximum Value of an Ordered Triplet I (Easy)
1 parent 3ec2974 commit 403859f

File tree

1 file changed

+123
-0
lines changed

1 file changed

+123
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
---
2+
description: 'Author: @wkw | https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-i'
3+
tags: [Array]
4+
---
5+
6+
# 2873 - Maximum Value of an Ordered Triplet I (Easy)
7+
8+
## Problem Link
9+
10+
https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-i
11+
12+
# Problem Statement
13+
14+
You are given a 0-indexed integer array nums.
15+
16+
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$.
17+
18+
The value of a triplet of indices $(i, j, k)$ is equal to $(nums[i] - nums[j]) * nums[k]$.
19+
20+
## Examples
21+
22+
### Example 1
23+
24+
```
25+
Input: nums = [12,6,1,2,7]
26+
Output: 77
27+
```
28+
29+
**Explanation:**
30+
31+
The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) \* nums[4] = 77. It can be shown that there are no ordered triplets of indices with a value greater than 77.
32+
33+
### Example 2
34+
35+
```
36+
Input: nums = [1,10,3,4,19]
37+
Output: 133
38+
```
39+
40+
**Explanation:**
41+
42+
The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) \* nums[4] = 133. It can be shown that there are no ordered triplets of indices with a value greater than 133.
43+
44+
### Example 3
45+
46+
```
47+
Input: nums = [1,2,3]
48+
Output: 0
49+
```
50+
51+
**Explanation:**
52+
53+
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.
54+
55+
## Constraints
56+
57+
- $3 <= nums.length <= 100$
58+
- $0 <= nums[i] <= 10^6$
59+
60+
## Approach 1: Brute Force
61+
62+
Since $n$ is small, the simplest way is to brute force the solution by enumerating all possible triplets using three nested loops.
63+
64+
Time complexity is $O(n ^ 3)$ as we need three nested loops.
65+
66+
Space comlexity is $O(1)$.
67+
68+
<Tabs>
69+
<TabItem value="cpp" label="Python">
70+
<SolutionAuthor name="@wkw"/>
71+
72+
```py
73+
# O(n ^ 3)
74+
class Solution:
75+
def maximumTripletValue(self, nums: List[int]) -> int:
76+
n = len(nums)
77+
res = float('-inf')
78+
# guarantees that indices are i < j < k
79+
for i in range(n):
80+
for j in range(i + 1, n):
81+
for k in range(j + 1, n):
82+
# get the max value of (nums[i] - nums[j]) * nums[k]
83+
res = max(res, (nums[i] - nums[j]) * nums[k])
84+
# if all such triplets have a negative value, return 0.
85+
return res if res >= 0 else 0
86+
```
87+
88+
</TabItem>
89+
</Tabs>
90+
91+
## Approach 2: Greedy
92+
93+
This problem can be also solved in $O(n)$. When we are at an element $x$, we consider the candidate for different position. Let $i_{mx}$ be the maximum of $nums[i]$ and $d_{mx}$ be the maximum of the value of the diff $nums[i] - nums[j]$. At a point, we consider
94+
95+
- $x$ as $nums[k]$, we can maximize the answer $res$ with $d_{mx} * x$.
96+
- $x$ as $nums[j]$, we can maximize the difference $d_{mx}$ with $i_{mx} - x$.
97+
- $x$ as $nums[i]$, we can maximize $i_{mx}$ with $i$.
98+
99+
Time complexity is $O(n)$
100+
101+
Space comlexity is $O(1)$.
102+
103+
<Tabs>
104+
<TabItem value="cpp" label="Python">
105+
<SolutionAuthor name="@wkw"/>
106+
107+
```py
108+
# O(n)
109+
class Solution:
110+
def maximumTripletValue(self, nums: List[int]) -> int:
111+
res, i_mx, d_mx = 0, 0, 0
112+
for x in nums:
113+
res = max(res, d_mx * x) # x as nums[k]
114+
# maximize (nums[i] - nums[j])
115+
d_mx = max(d_mx, i_mx - x) # x as nums[j]
116+
# maximize nums[i]
117+
i_mx = max(i_mx, x) # x as nums[i]
118+
return res
119+
120+
```
121+
122+
</TabItem>
123+
</Tabs>

0 commit comments

Comments
 (0)