STL中的Pair方法详解
1126
2022-08-22
[leetcode] 962. Maximum Width ramp
Description
Given an array A of integers, a ramp is a tuple (i, j) for which i < j and A[i] <= A[j]. The width of such a ramp is j - i.
Find the maximum width of a ramp in A. If one doesn’t exist, return 0.
Example 1:
Input: [6,0,8,2,1,5]Output: 4Explanation: The maximum width ramp is achieved at (i, j) = (1, 5): A[1] = 0 and A[5] = 5.
Example 2:
Input: [9,8,1,0,1,9,4,0,4,1]Output: 7Explanation: The maximum width ramp is achieved at (i, j) = (2, 9): A[2] = 1 and A[9] = 1.
Note:
2 <= A.length <= 500000 <= A[i] <= 50000
分析
题目的意思是:给定一个数组,找出最长的ramp的宽度,ramp的宽度定义为i >>> A=[6,0,8,2,1,5]>>> a=sorted(range(len(A)),key=A.__getitem__)>>> a[1, 4, 3, 5, 0, 2] 代码 class Solution: def maxWidthRamp(self, A: List[int]) -> int: n=len(A) m=float('inf') res=0 for i in sorted(range(n),key=A.__getitem__): res=max(res,i-m) m=min(m,i) return res 参考文献 [LeetCode] Approach 1: Sort
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~