返回介绍

solution / 2800-2899 / 2874.Maximum Value of an Ordered Triplet II / README_EN

发布于 2024-06-17 01:02:59 字数 4123 浏览 0 评论 0 收藏 0

2874. Maximum Value of an Ordered Triplet II

中文文档

Description

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_. _If all such triplets have a negative value, return 0.

The value of a triplet of indices (i, j, k) is equal to (nums[i] - nums[j]) * nums[k].

 

Example 1:

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.
It can be shown that there are no ordered triplets of indices with a value greater than 77. 

Example 2:

Input: nums = [1,10,3,4,19]
Output: 133
Explanation: 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.

Example 3:

Input: nums = [1,2,3]
Output: 0
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.

 

Constraints:

  • 3 <= nums.length <= 105
  • 1 <= nums[i] <= 106

Solutions

Solution 1: Maintain Maximum Prefix Value and Maximum Difference

We can use two variables $mx$ and $mx_diff$ to maintain the maximum prefix value and maximum difference, respectively. When traversing the array, we update these two variables, and the answer is the maximum value of all $mx_diff \times nums[i]$.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

class Solution:
  def maximumTripletValue(self, nums: List[int]) -> int:
    ans = mx = mx_diff = 0
    for num in nums:
      ans = max(ans, mx_diff * num)
      mx = max(mx, num)
      mx_diff = max(mx_diff, mx - num)
    return ans
class Solution {
  public long maximumTripletValue(int[] nums) {
    long max, maxDiff, ans;
    max = 0;
    maxDiff = 0;
    ans = 0;
    for (int num : nums) {
      ans = Math.max(ans, num * maxDiff);
      max = Math.max(max, num);
      maxDiff = Math.max(maxDiff, max - num);
    }
    return ans;
  }
}
class Solution {
public:
  long long maximumTripletValue(vector<int>& nums) {
    long long ans = 0;
    int mx = 0, mx_diff = 0;
    for (int num : nums) {
      ans = max(ans, 1LL * mx_diff * num);
      mx = max(mx, num);
      mx_diff = max(mx_diff, mx - num);
    }
    return ans;
  }
};
func maximumTripletValue(nums []int) int64 {
  ans, mx, mx_diff := 0, 0, 0
  for _, num := range nums {
    ans = max(ans, mx_diff*num)
    mx = max(mx, num)
    mx_diff = max(mx_diff, mx-num)
  }
  return int64(ans)
}
function maximumTripletValue(nums: number[]): number {
  let [ans, mx, mx_diff] = [0, 0, 0];
  for (const num of nums) {
    ans = Math.max(ans, mx_diff * num);
    mx = Math.max(mx, num);
    mx_diff = Math.max(mx_diff, mx - num);
  }
  return ans;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文