[Swift]LeetCode962. 最大寬度坡 | Maximum Width Ramp

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-snugwins-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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.git

Find the maximum width of a ramp in A.  If one doesn't exist, return 0.github

Example 1:數組

Input: [6,0,8,2,1,5]
Output: 4 Explanation: 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: 7 Explanation: The maximum width ramp is achieved at (i, j) = (2, 9): A[2] = 1 and A[9] = 1.

Note:app

  1. 2 <= A.length <= 50000
  2. 0 <= A[i] <= 50000

給定一個整數數組 A是元組 (i, j),其中  i < j 且 A[i] <= A[j]。這樣的坡的寬度爲 j - ispa

找出 A 中的坡的最大寬度,若是不存在,返回 0 。code

示例 1:htm

輸入:[6,0,8,2,1,5]
輸出:4
解釋:
最大寬度的坡爲 (i, j) = (1, 5): A[1] = 0 且 A[5] = 5.

示例 2:blog

輸入:[9,8,1,0,1,9,4,0,4,1]
輸出:7
解釋:
最大寬度的坡爲 (i, j) = (2, 9): A[2] = 1 且 A[9] = 1.

提示:

  1. 2 <= A.length <= 50000
  2. 0 <= A[i] <= 50000

440ms 
 1 class Solution {
 2     func maxWidthRamp(_ A: [Int]) -> Int {
 3         var val:[Int] = [Int]()
 4         var index:[Int] = [Int]()
 5         var n:Int = A.count
 6         var ans:Int = 0
 7         for i in 0..<n
 8         {
 9             var x:Int = A[i]
10             var left:Int = 0
11             var right:Int = val.count - 1
12             while(left < right)
13             {
14                 var mid:Int = (left + right) / 2
15                 if val[mid] <= x
16                 {
17                     right = mid
18                 }
19                 else
20                 {
21                     left = mid + 1
22                 }
23             }
24             if left <= right && val[left] <= x
25             {
26                 ans = max(ans, i - index[left])
27             }
28             if val.isEmpty || val.last! > x
29             {
30                 val.append(x)
31                 index.append(i)
32             }
33         }
34         return ans
35     }
36 }

528ms

 1 class Solution 
 2 {
 3     
 4     struct IndexedValue
 5     {
 6         let index: Int
 7         let value: Int
 8         
 9         init( _ index: Int, _ value: Int )
10         {
11             self.index = index
12             self.value = value
13         }
14     }
15     
16     func maxWidthRamp( _ values: [ Int ] ) -> Int 
17     {
18         var indexedValues: [ IndexedValue ] = []
19         for index in ( 0 ..< values.count )
20         {
21             
22             indexedValues.append( IndexedValue( index, values[ index ] ) )
23             
24         }
25         indexedValues.sort( 
26             by: 
27             { 
28                 if $0.value == $1.value
29                 {
30                     return $0.index < $1.index
31                 }
32                 else
33                 {
34                     return $0.value <= $1.value
35                 }
36             } 
37         )
38         
39         var minIndex: Int = indexedValues[ 0 ].index
40         var maxRamp: Int = 0
41         
42         for indexedValue in indexedValues
43         {
44             if indexedValue.index < minIndex
45             {
46                 minIndex = indexedValue.index
47             }
48             else
49             {
50                 let ramp: Int = ( indexedValue.index - minIndex )
51                 maxRamp = max( maxRamp, ramp )
52             }
53         }
54         
55         return maxRamp
56     }
57 }

1252ms

 1 class Solution {
 2     func maxWidthRamp(_ A: [Int]) -> Int {
 3         var numbers: [[Int]] = Array(repeating: [], count: 50001)
 4         for (i, a) in A.enumerated() {
 5             numbers[a] = numbers[a] + [i]
 6         }
 7         var res: [Int] = []
 8         for i in (0...50000) {
 9             res += numbers[i]
10         }
11         
12         var dist = 0
13         var minN = Int.max
14         for i in (0..<res.count) {
15             if res[i] < minN {
16                 minN = res[i]
17             } else {
18                 dist = max(dist, res[i] - minN)
19             }
20         }
21         
22         
23         return dist
24     }
25 }
相關文章
相關標籤/搜索