题解 | 信封嵌套

信封嵌套

https://www.nowcoder.com/practice/25fe1fc89c4c4e82bbc63df04bc6ca30

import sys

 # 22:51
 # 首先简化问题,将信封按照长或者宽从小到大排序
 # 那么考虑多少层嵌套,只需要考虑当前信封前,有多少信封的宽(或者长)小于当前信封,
 # 相当于找最长的递增子序列,假设dp[i]为以信封i为结尾的嵌套层数,
 # dp[i] = max(dp[0~i-1]中满足width 小于当前信封的dp[j] +1)

from collections import defaultdict

n = int(input())

letters = []

for _ in range(n):
    letter = list(map(int,input().split()))
    letters.append(letter)

letters.sort(key=lambda x: x[0])


dp = [0] * (n+1) # dp[i]为以i为最外层的信封嵌套数

letters = [[0,0]] + letters

for i in range(1,n+1):
    height,width = letters[i]
    for j in range(i):
        cur_height, cur_width = letters[j]
        # remove the situation where height the same but width less
        if cur_height<height and cur_width < width:
            dp[i] = max(dp[i],dp[j])
    dp[i] += 1

print(max(dp))


全部评论

相关推荐

白火同学:能。我当初应届沟通了1200,收简历50,面试10左右吧,加油投吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务