zoukankan      html  css  js  c++  java
  • [Swift]LeetCode354. 俄罗斯套娃信封问题 | Russian Doll Envelopes

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
    ➤微信公众号:山青咏芝(shanqingyongzhi)
    ➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
    ➤GitHub地址:https://github.com/strengthen/LeetCode
    ➤原文地址:https://www.cnblogs.com/strengthen/p/10276020.html 
    ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
    ➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★You have a number of envelopes with widths and heights given as a pair of integers (w, h). One envelope can fit into another if and only if both the width and height of one envelope is greater than the width and height of the other envelope.

    What is the maximum number of envelopes can you Russian doll? (put one inside other)

    Note:
    Rotation is not allowed.

    Example:

    Input: [[5,4],[6,4],[6,7],[2,3]]
    Output: 3 
    Explanation: The maximum number of envelopes you can Russian doll is 3 ([2,3] => [5,4] => [6,7]).

    给定一些标记了宽度和高度的信封,宽度和高度以整数对形式 (w, h) 出现。当另一个信封的宽度和高度都比这个信封大的时候,这个信封就可以放进另一个信封里,如同俄罗斯套娃一样。

    请计算最多能有多少个信封能组成一组“俄罗斯套娃”信封(即可以把一个信封放到另一个信封里面)。

    说明:
    不允许旋转信封。

    示例:

    输入: envelopes = [[5,4],[6,4],[6,7],[2,3]]
    输出: 3 
    解释: 最多信封的个数为 3, 组合为: [2,3] => [5,4] => [6,7]。

    356ms
     1 class Solution {
     2     //二分法
     3     func maxEnvelopes(_ envelopes: [[Int]]) -> Int {
     4         var envelopes = envelopes
     5         var dp:[Int] = [Int]()
     6         envelopes.sort(by:sortArray)
     7         for i in 0..<envelopes.count
     8         {
     9             var left:Int = 0
    10             var right:Int = dp.count
    11             var t:Int = envelopes[i][1]
    12             while(left < right)
    13             {
    14                 var mid:Int = left + (right - left) / 2
    15                 if dp[mid] < t
    16                 {
    17                     left = mid + 1
    18                 }
    19                 else
    20                 {
    21                     right = mid
    22                 }
    23             }
    24             if right >= dp.count
    25             {
    26                 dp.append(t)
    27             }
    28             else
    29             {
    30                 dp[right] = t
    31             }            
    32         }
    33         return dp.count
    34     }
    35     
    36     func sortArray(_ a:[Int],_ b:[Int]) -> Bool
    37     {
    38         if a[0] == b[0]
    39         {
    40             return a[1] > b[1]
    41         }
    42         return a[0] < b[0]
    43     }
    44 }

    6948ms

     1 class Solution {
     2     func maxEnvelopes(_ envelopes: [[Int]]) -> Int {
     3         if envelopes.count == 0 { return 0 }
     4         let sorted = envelopes.sorted {
     5             return $0[0] < $1[0]
     6         }
     7         
     8         var dp: [Int] = [Int](repeating: 0, count: envelopes.count)
     9         var res = 0
    10         for i in 0..<sorted.count {
    11             dp[i] = 1
    12             for j in 0..<i {
    13                 // if i is bigger than j
    14                 if sorted[i][0] > sorted[j][0] && sorted[i][1] > sorted[j][1] {
    15                     dp[i] = max(dp[i], dp[j] + 1)
    16                 }
    17             }
    18             res = max(res, dp[i])
    19         }
    20         return res
    21     }
    22 }
  • 相关阅读:
    未能从程序集 C:Program Files (x86)MSBuild14.0inMicrosoft.Data.Entity.Build.Tasks.dll 加载任务“EntityClean”
    asp.net mvc 4 项目升级到 asp.net mvc5
    SQL Server查看所有表大小,所占空间
    0x80072f8a未指定的错误
    vs2012 aps.net4.0/4.5尚未在web服务器上注册
    vsphere 出现“在主机的当前连接状况下不允许执行该操作”
    sql server 发布时提示'dbo.sysmergepublications'无效的解决办法
    sql server更改机器名后更改数据库机器名
    Ajax向后台传入File类型参数
    上传下载Azure Blob里的Excel文件。
  • 原文地址:https://www.cnblogs.com/strengthen/p/10276020.html
Copyright © 2011-2022 走看看