题解 | #数组中的最长连续子序列#

数组中的最长连续子序列

https://www.nowcoder.com/practice/eac1c953170243338f941959146ac4bf

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# max increasing subsequence
# @param arr int整型一维数组 the array
# @return int整型
#
class Solution:
    def MLS(self , arr: List[int]) -> int:
        # write code here
        root={}
        rs = 1
        for i in arr:
            if i not in root:
                root[i] = [i]
                if i-1 in root and i+1 in root:
                    root[i-1] = root[i-1] + root[i] + root[i+1]
                    root[i+1] = root[i-1]
                    root[i] = root[i-1]
                elif i-1 in root:
                    root[i-1] = root[i-1] + root[i]
                    root[i] = root[i-1]
                elif i+1 in root:
                    root[i+1] = root[i] + root[i+1]
                    root[i] = root[i+1]
                root[root[i][0]] = root[i]
                root[root[i][-1]] = root[i]
                rs = max(rs, len(set(root[i])))
        return rs

全部评论

相关推荐

这不纯纯作弊了吗😢😢😢
编程界菜鸡:信这个的这辈子有了,这智商你靠啥都没用
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-04 15:36
点赞 评论 收藏
分享
程序员饺子:正常 我沟通了200多个 15个要简历 面试2个 全投的成都的小厂。很多看我是27直接不会了😅
点赞 评论 收藏
分享
每晚夜里独自颤抖:你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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