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

相关推荐
天桥下的卖艺者4 分钟前
R语言演示对没有吸收状态的马尔科夫链分析
开发语言·r语言
aini_lovee5 分钟前
基于 OpenCV 的模板匹配算法的 C 语言实现
c语言·opencv·算法
core5126 分钟前
EM 算法 (期望最大化):在迷雾中寻找真相
算法·em·期望最大化
CoovallyAIHub7 分钟前
YOLO11-4K:面向4K全景图像的高效实时检测框架,CVIP360数据集开源
深度学习·算法·计算机视觉
山沐与山9 分钟前
【设计模式】Python仓储模式:从入门到实战
python·设计模式
q1508039622513 分钟前
数据整理无忧:深度评测高效文本合并工具的实用功能
开发语言·前端·javascript
小新11013 分钟前
Qt 中安全拼接文件路径
开发语言·qt
安_14 分钟前
java Arrays.sort 用的什么算法
java·算法·排序算法
蓝色汪洋15 分钟前
数字(加强版)
算法
MarkHD15 分钟前
智能体在车联网中的应用:第25天 深度Q网络(DQN)实战:在CartPole环境中用PyTorch从零实现
人工智能·pytorch·python