Maximum Value of an Ordered Triplet I — LeetCode 2873 Python Solution
EasyArray
- Problem
- #2873
- Reading time
- 2 min
- Source
- leetcode.com
The problem
You are given a 0-indexed integer array nums. Return the maximum value over all triplets of indices (i, j, k) such that i < j < k.
Example
- Input
- nums = [12,6,1,2,7]
- Output
- 77
- Explanation
- The value of the triplet (0, 2, 4) is (nums[0] - nums[2]) * nums[4] = 77.
Python solution
Python
class Solution:
def maximumTripletValue(self, nums: List[int]) -> int:
ans = mx = mx_diff = 0
for x in nums:
ans = max(ans, mx_diff * x)
mx_diff = max(mx_diff, mx - x)
mx = max(mx, x)
return ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(n), where n is the length of the array |
| Space | O(1) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 2873. Maximum Value of an Ordered Triplet I?
- LeetCode 2873. Maximum Value of an Ordered Triplet I is rated Easy on LeetCode.
- What is the time complexity of LeetCode 2873. Maximum Value of an Ordered Triplet I?
- The Python solution on this page runs in O(n), where n is the length of the array.
- What is the space complexity of LeetCode 2873. Maximum Value of an Ordered Triplet I?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 2873. Maximum Value of an Ordered Triplet I cover?
- LeetCode 2873. Maximum Value of an Ordered Triplet I is tagged Array on LeetCode.