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

相关推荐
扫地的小何尚17 小时前
一小时内使用NVIDIA Nemotron创建你自己的Bash计算机使用智能体
开发语言·人工智能·chrome·bash·gpu·nvidia
MoonBit月兔17 小时前
MoonBit Pearls Vol.12:初探 MoonBit 中的 JavaScript 交互
开发语言·javascript·数据库·交互·moonbit
第七序章17 小时前
【C + +】unordered_set 和 unordered_map 的用法、区别、性能全解析
数据结构·c++·人工智能·算法·哈希算法·1024程序员节
草莓熊Lotso17 小时前
《算法闯关指南:优选算法--二分查找》--23.寻找旋转排序数组中的最小值,24.点名
开发语言·c++·算法·1024程序员节
foundbug99917 小时前
C# 实现 Modbus TCP 通信
开发语言·tcp/ip·c#
文火冰糖的硅基工坊17 小时前
[嵌入式系统-150]:智能机器人(具身智能)内部的嵌入式系统以及各自的功能、硬件架构、操作系统、软件架构
android·linux·算法·ubuntu·机器人·硬件架构
郝学胜-神的一滴17 小时前
主成分分析(PCA)在计算机图形学中的深入解析与应用
开发语言·人工智能·算法·机器学习·1024程序员节
離離原上譜18 小时前
python-docx 安装与快速入门
python·word·python-docx·自动化办公·1024程序员节
JuicyActiveGilbert18 小时前
【Python进阶】第2篇:单元测试
开发语言·windows·python·单元测试
今天没ID18 小时前
Numpy加权平均数
python