2874. Maximum Value of an Ordered Triplet II Medium

@problem@discussion
#Array



1/**
2 * [2874] Maximum Value of an Ordered Triplet II
3 *
4 * You are given a 0-indexed integer array nums.
5 * 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.
6 * The value of a triplet of indices (i, j, k) is equal to (nums[i] - nums[j]) * nums[k].
7 *  
8 * <strong class="example">Example 1:
9 * 
10 * Input: nums = [12,6,1,2,7]
11 * Output: 77
12 * Explanation: The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
13 * It can be shown that there are no ordered triplets of indices with a value greater than 77. 
14 * 
15 * <strong class="example">Example 2:
16 * 
17 * Input: nums = [1,10,3,4,19]
18 * Output: 133
19 * Explanation: The value of the triplet (1, 2, 4) is (nums[1] - nums[2]) * nums[4] = 133.
20 * It can be shown that there are no ordered triplets of indices with a value greater than 133.
21 * 
22 * <strong class="example">Example 3:
23 * 
24 * Input: nums = [1,2,3]
25 * Output: 0
26 * 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.
27 * 
28 *  
29 * Constraints:
30 * 
31 * 	3 <= nums.length <= 10^5
32 * 	1 <= nums[i] <= 10^6
33 * 
34 */
35pub struct Solution {}
36
37// problem: https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-ii/
38// discuss: https://leetcode.com/problems/maximum-value-of-an-ordered-triplet-ii/discuss/?currentPage=1&orderBy=most_votes&query=
39
40// submission codes start here
41
42impl Solution {
43    pub fn maximum_triplet_value(nums: Vec<i32>) -> i64 {
44        
45    }
46}
47
48// submission codes end
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn test_2874() {
56    }
57}
58


Back
© 2025 bowen.ge All Rights Reserved.