Leetcode 2874. Maximum Value of an Ordered Triplet II

  • [Leetcode 2874. Maximum Value of an Ordered Triplet II](#Leetcode 2874. Maximum Value of an Ordered Triplet II)
    • [1. 解题思路](#1. 解题思路)
    • [2. 代码实现](#2. 代码实现)

1. 解题思路

这一题我的思路的核心点在于将中间点j作为关键点,此时,要令triplet最大,事实上就是分别在其左右找到最大元素即可,而这个就是一个类似累计数组的算法,倒是无需赘述了......

2. 代码实现

给出python代码实现如下:

python 复制代码
class Solution:
    def maximumTripletValue(self, nums: List[int]) -> int:
        n = len(nums)
        left, right = deepcopy(nums), deepcopy(nums)
        
        for i in range(n-1):
            left[i+1] = max(left[i], left[i+1])
            
        for i in range(n-2, -1, -1):
            right[i] = max(right[i+1], right[i])
            
        res = max((left[i-1] - nums[i]) * right[i+1] for i in range(1, n-1))
        return max(res, 0)

提交代码评测得到:耗时1185ms,占用内存29MB。

相关推荐
wenyq73 小时前
LeetCode 2904. Shortest and Lexicographically Smallest Beautiful String
算法·leetcode
_Narcissus_5 小时前
分治&递归
数据结构·c++·笔记·算法·leetcode·递归·分治
青 春 记 忆8 小时前
LeetCode 283. 移动零|Python 解法详解
python·算法·leetcode
evans在进步12 小时前
LeetCode 189 轮转数组:三次反转为什么能实现右旋?
数据结构·算法·leetcode
圣保罗的大教堂13 小时前
leetcode 3734. 大于目标字符串的最小字典序回文排列 困难
leetcode
土司大王1 天前
LeetCode hot100——对称二叉树
算法·leetcode·职场和发展
Navigator_Z1 天前
LeetCode //C - 1208. Get Equal Substrings Within Budget
c语言·算法·leetcode