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

数组中的最长连续子序列

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

全部评论

相关推荐

机械打工仔:第一位颇有孟德之志
点赞 评论 收藏
分享
自由水:笑死了,敢这么面试不敢让别人说
点赞 评论 收藏
分享
每晚夜里独自颤抖:这个在牛客不是老熟人了吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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