洛谷题单3-P1420 最长连号-python-流程图重构

题目描述

输入长度为 n n n 的一个正整数序列,要求输出序列中最长连号的长度。

连号指在序列中,从小到大的连续自然数。

输入格式

第一行,一个整数 n n n。

第二行, n n n 个整数 a i a_i ai,之间用空格隔开。

输出格式

一个数,最长连号的个数。

输入输出样例

输入

复制代码
10
1 5 6 2 3 4 5 6 8 9

输出

复制代码
5

说明/提示

数据规模与约定

对于 100 % 100\% 100% 的数据,保证 1 ≤ n ≤ 1 0 4 1 \leq n \leq 10^4 1≤n≤104, 1 ≤ a i ≤ 1 0 9 1 \leq a_i \leq 10^9 1≤ai≤109。

方式-遍历

代码

python 复制代码
class Solution:
    @staticmethod
    def oi_input():
        """从标准输入读取数据"""
        num, nums = int(input()), list(map(int, input().split()))
        return num, nums

    @staticmethod
    def oi_test():
        """提供测试数据"""
        return 10, [1, 5, 6, 2, 3, 4, 5, 6, 8, 9]

    @staticmethod
    def solution(num, nums):
        '''遍历'''
        max_len, current = 1, 1  # 最大 与 当前

        for i in range(1, num):
            if nums[i] == nums[i - 1] + 1:
                current += 1
                max_len = max(max_len, current)
            else:
                current = 1
        print(max_len)


oi_input = Solution.oi_input
oi_test = Solution.oi_test
solution = Solution.solution

if __name__ == '__main__':
    num, nums = oi_test()
    # num, nums = oi_input()
    solution(num, nums)

流程图

处理连续递增序列 是 否 否 是 nums[i] == nums[i-1]+1? 循环遍历i从1到num-1 current +=1
max_len = max(max_len, current) 进入下一个循环 重置current=1 遍历完成? 开始 主函数调用 读取输入数据
num, nums = oi_input() 初始化max_len=1, current=1 输出最长长度max_len 结束

方式-双指针-滑动窗口

代码

python 复制代码
class Solution:
    @staticmethod
    def oi_input():
        """从标准输入读取数据"""
        num, nums = int(input()), list(map(int, input().split()))
        return num, nums

    @staticmethod
    def oi_test():
        """提供测试数据"""
        return 10, [1, 5, 6, 2, 3, 4, 5, 6, 8, 9]

    @staticmethod
    def solution(num, nums):
        max_len, left = 1, 0

        for right in range(num - 1):  # right 表示当前检查的位置的前一个
            if nums[right + 1] != nums[right] + 1:
                left = right + 1
                # 窗口范围为 [left, right+1]
                # 前面加1 是因为 right 跟 right + 1 比的,括号外面加1 是因为要包含被减去的位置
            current_len = (right + 1 - left) + 1
            max_len = max(max_len, current_len)
        print(max_len)


oi_input = Solution.oi_input
oi_test = Solution.oi_test
solution = Solution.solution

if __name__ == '__main__':
    num, nums = oi_test()
    # num, nums = oi_input()
    solution(num, nums)

流程图

滑动窗口处理 否 是 是 否 nums[right+1] == nums[right]+1? 循环right从0到num-2 移动左边界left=right+1 计算窗口长度 current_len = right+1 - left +1 更新max_len 右移right 开始 调用oi_input() 读取第一行输入→num 读取第二行输入→nums 初始化max_len=1, left=0 right < num-1? 输出max_len 结束

方式-递归-缓存

代码

python 复制代码
class Solution:
    @staticmethod
    def oi_input():
        """从标准输入读取数据"""
        num, nums = int(input()), list(map(int, input().split()))
        return num, nums

    @staticmethod
    def oi_test():
        """提供测试数据"""
        return 10, [1, 5, 6, 2, 3, 4, 5, 6, 8, 9]

    @staticmethod
    def solution(num, nums):
        import sys
        from functools import lru_cache

        sys.setrecursionlimit(1000000)  # 强行增大递归深度

        @lru_cache(maxsize=None)
        def max_consecutive_length_from_index(i):
            if i == 0:
                return 1
            if nums[i] == nums[i - 1] + 1:
                return max_consecutive_length_from_index(i - 1) + 1
            else:
                return 1

        print(max(max_consecutive_length_from_index(i) for i in range(num)))


oi_input = Solution.oi_input
oi_test = Solution.oi_test
solution = Solution.solution

if __name__ == '__main__':
    num, nums = oi_test()
    # num, nums = oi_input()
    solution(num, nums)

流程图

递归函数处理 是 否 是 否 i == 0? 定义带缓存的递归函数max_consecutive_length_from_index 返回1 nums[i] == nums[i-1]+1? 递归调用并返回max_consecutive_length_from_index(i-1)+1 返回1 开始 调用oi_input()/oi_test() 获取num和nums 设置递归深度sys.setrecursionlimit(1000000) 遍历计算所有i的最大长度 输出最大值 结束

相关推荐
dr李四维3 分钟前
解决缓存穿透的布隆过滤器与布谷鸟过滤器:谁更适合你的应用场景?
redis·算法·缓存·哈希算法·缓存穿透·布隆过滤器·布谷鸟过滤器
亓才孓4 分钟前
[leetcode]01背包问题
算法·leetcode·职场和发展
插件开发19 分钟前
JavaScript-异步和同步函数使用场景及区别-正确构建程序的核心要点
开发语言·javascript·ecmascript
qq_3659116039 分钟前
WPF 五子棋项目文档
开发语言
学习编程的gas1 小时前
数据结构——二叉树
数据结构·算法
its_a_win1 小时前
蓝桥杯 2023省B 飞机降落 dfs
c++·算法·蓝桥杯
橙色小博1 小时前
GPT:Transformer的专精产品
人工智能·python·gpt·深度学习·神经网络·transformer
MarvinP2 小时前
python基础:位置互换
开发语言·python·算法
声声codeGrandMaster2 小时前
Django项目入门二
后端·python·django