洛谷题单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的最大长度 输出最大值 结束

相关推荐
Felven1 小时前
E. Scuza
数据结构·c++·算法
PingdiGuo_guo1 小时前
C++指针(三)
开发语言·c++
PixelMind2 小时前
【LUT技术专题】极小尺寸LUT算法:TinyLUT
人工智能·深度学习·算法·lut·图像超分辨率
asom223 小时前
LeetCode Hot100(字串)
算法·leetcode
蟹至之4 小时前
【Java】异常的初步认识
java·开发语言·类和对象·异常
lkx097884 小时前
第九天的尝试
python
佩奇的技术笔记4 小时前
Python入门手册:Python基础语法
开发语言·python
学习使我变快乐4 小时前
C++:无序容器
数据结构·c++·算法
朱剑君5 小时前
贪心算法——分数背包问题
算法·贪心算法
小O的算法实验室5 小时前
2008年EJOR SCI2区,连续蚁群优化算法ACOR,深度解析+性能实测
算法·智能算法