当前位置:网站首页>Force buckle - 354 Russian Doll envelope problem
Force buckle - 354 Russian Doll envelope problem
2022-04-22 05:37:00 【Node_ Su】

First , The width will change Ascending , If the width is the same, the height Descending ( Because you have to find the largest letter package in the same width first )
then , Then the sorted height , Find the longest increasing subsequence Power button -300. The longest increasing subsequence _Node_Su The blog of -CSDN Blog
For example, original data : After the above steps :
[5,4] [2,3]
[6,4] [5,4]
[6,7] [6,7]
[2,3] [6,4]
Finally, the longest increasing subsequence of height :[2,3]=>[5,4]=>[6,7]
There is a convenient way to implement “ First ” Steps for
envelopes.sort(key=lambda x: (x[0], -x[1]))
key=lambda Is constant ,x[0] Express key Sort ,x[1] Express Key value Sort , The default is ascending
class Solution(object):
def maxEnvelopes(self, envelopes):
"""
:type envelopes: List[List[int]]
:rtype: int
"""
envelopes.sort(key=lambda x: (x[0], -x[1]))
height = []
for i in range(0, len(envelopes)):
height.append(envelopes[i][1])
res = self.lengthOfLIS(height)
return res
def lengthOfLIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
dp = [1] * (len(nums))
for i in range(0, len(nums)):
for j in range(0, i): # You don't need to find anything better than the current nums[i] Small nums[j], Just look in nums[i] The previous small nums[j] that will do
if nums[j] < nums[i]:
dp[i] = max(dp[i], dp[j] + 1) # With the current longer dp[j]+1
res = 0
for i in range(0, len(dp)):
res = max(res, dp[i])
return res
if __name__ == '__main__':
envelopes = [[5, 4], [6, 4], [6, 7], [2, 3]]
Sol = Solution()
res = Solution.maxEnvelopes(Sol, envelopes)
print(res)
版权声明
本文为[Node_ Su]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210617443744.html
边栏推荐
- 我常用的开发软件
- Fundamentals of graphics - Mobile GPU architecture
- Realization of mathematical function curve editor with minscript script language
- Fundamentals of graphics - Ambient Occlusion
- Integer源码
- Fundamentals of graphics - screen spatial reflection (SSR)
- GBase 8s V8.8 SQL 指南:教程-6.1.1(2)
- [untitled] gbase 8s V8 8 SQL Guide: tutorial-6
- C WinForm about incomplete display of listview
- MySQL 第6章 Navicat 的安装与使用
猜你喜欢

Recommended system notes (Miscellaneous)

MySQL 第6章 Navicat 的安装与使用
![[fedmd, a heterogeneous FL training method using model distillation] fedmd: heterogeneous federated learning via model distillation](/img/25/210dd6b74fe555a819097b97721983.png)
[fedmd, a heterogeneous FL training method using model distillation] fedmd: heterogeneous federated learning via model distillation

Simulate the infectious disease model with MATLAB (only do matlab simulation learning and practice, not actual situation and application)

Integer source code

再见2020,2021我来了

力扣876. 链表的中间结点

Network attack and Defense Security Learning Platform - upload key 3

Cookie injection

MySQL表的增删改查
随机推荐
Fundamentals of graphics - flood
Auto.js 画布设置防锯齿paint.setAntiAlias(true);
Arrays常用方法(超详解)
Method for coexistence of Keil-C51 and keil arm
Auto. JS canvas setting anti aliasing paint setAntiAlias(true);
Unity about the real machine failure of ispointerovergameobject interface
环形链表2
redis设置与获取过期时间一网打尽
Unity list uses find or findall to find specific elements and the number of specific elements
GBase 8s V8.8 SQL 指南:教程-6.1.1(2)
The unreal engine uses loadclass to load the blueprint class
Unreal engine sequence effect and audio binding trigger
Recommended system notes (Miscellaneous)
GBase 8s V8. 8 SQL Guide: tutorial-5.3
程序的编译(预处理操作)+ 链接
Implementation of unity simple event system
【FedMD,一种利用模型蒸馏的异构FL训练方法】FedMD: Heterogenous Federated Learning via Model Distillation
GBase 8s V8.8 SQL 指南:教程-6.1
Domain based approach - score prediction
Hloj 1936 covered with squares