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

相关推荐
做怪小疯子11 小时前
LeetCode 热题 100——矩阵——旋转图像
算法·leetcode·矩阵
努力学习的小廉11 小时前
我爱学算法之—— BFS之最短路径问题
算法·宽度优先
python零基础入门小白11 小时前
【万字长文】大模型应用开发:意图路由与查询重写设计模式(从入门到精通)
java·开发语言·设计模式·语言模型·架构·大模型应用开发·大模型学习
Eric.Lee202111 小时前
ubuntu 安装 Miniconda
linux·运维·python·ubuntu·miniconda
天若有情67311 小时前
【c++】手撸C++ Promise:从零实现通用异步回调组件,支持链式调用+异常安全
开发语言·前端·javascript·c++·promise
无心水11 小时前
【Python实战进阶】1、Python高手养成指南:四阶段突破法从入门到架构师
开发语言·python·django·matplotlib·gil·python实战进阶·python工程化实战进阶
高山上有一只小老虎11 小时前
构造A+B
java·算法
木头左11 小时前
缺失值插补策略比较线性回归vs.相邻填充在LSTM输入层的性能差异分析
算法·线性回归·lstm
李剑一12 小时前
Python学习笔记1
python
sin_hielo12 小时前
leetcode 2435
数据结构·算法·leetcode