博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python O(nlogn) 排序+dp 解决俄罗斯套信封问题
阅读量:3943 次
发布时间:2019-05-23

本文共 1157 字,大约阅读时间需要 3 分钟。

leecode 354. Russian Doll Envelopes

解题思路

‘’’

先排序,然后考虑一个最长上升子序列问题:

‘’’

image.png

代码

class Solution:    def maxEnvelopes(self, envelopes: List[List[int]]) -> int:        '''        先排序,然后考虑一个最长上升子序列问题:        链接:https://leetcode-cn.com/problems/longest-increasing-subsequence/solution/pythonguan-fang-ti-jie-si-lu-liang-chong-zp4g/        '''        def Env_cmp(x,y):            if x[0] < y[0]:                return -1            elif x[0] == y[0]:                if x[1] >= y[1]:                    return -1                else: return 1            else: return 1        import functools        envelopes.sort(key=functools.cmp_to_key(Env_cmp))        print(envelopes)        ### 排序完之后, 考虑一个最长上升子序列问题:        dp = []        for pair in envelopes:            h = pair[1]            if not dp or h > dp[-1]:                dp.append(h)            else:                left, right = 0, len(dp)                while left < right:                    mid = (left + right) // 2                    if h > dp[mid]:                        left = mid + 1                    elif h <= dp[mid]:                        right = mid                dp[left] = h        return len(dp)

转载地址:http://gnywi.baihongyu.com/

你可能感兴趣的文章
3.9.1 - Lists in Python
查看>>
3.9.2 - Lists - Adding and Removing Objects
查看>>
3.9.3 - Sorting Lists
查看>>
3.10 - Maya Commands: ls
查看>>
3.11 - Dictionaries in Python
查看>>
3.12 - Tuples in Python
查看>>
4.4 - For Loops
查看>>
4.2.2 - Logical and/or Operators
查看>>
Lesson 4 Part 2 Softmax Regression
查看>>
文章中运用到的数学公式
查看>>
Projective Dynamics: Fusing Constraint Projections for Fast Simulation
查看>>
从2D恢复出3D的数据
查看>>
glm 中 数据类型 与 原始数据(c++ 数组)之间的转换
查看>>
Derivatives of scalars, vector functions and matrices
查看>>
the jacobian matrix and the gradient matrix
查看>>
VS2010 将背景设为保护色
查看>>
ubutun里面用命令行安装软件
查看>>
ubuntu 常用命令
查看>>
SQLite Tutorial 4 : How to export SQLite file into CSV or Excel file
查看>>
how to move pivot to origin
查看>>