首页 > 其他分享 >leetcode 674. Longest Continuous Increasing Subsequence

leetcode 674. Longest Continuous Increasing Subsequence

时间:2023-05-30 17:36:44浏览次数:42  
标签:cnt nums 674 max Continuous int Subsequence ans dp

Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray).

Example 1:

Input: [1,3,5,4,7]
Output: 3
Explanation: The longest continuous increasing subsequence is [1,3,5], its length is 3. 
Even though [1,3,5,7] is also an increasing subsequence, it's not a continuous one where 5 and 7 are separated by 4.

Example 2:

Input: [2,2,2,2,2]
Output: 1
Explanation: The longest continuous increasing subsequence is [2], its length is 1.

Note: Length of the array will not exceed 10,000.

class Solution(object):
    def findLengthOfLCIS(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        if not nums: return 0
        ans = 1
        cnt = 1
        for i in xrange(1, len(nums)):
            if nums[i] > nums[i-1]:
                cnt += 1
                ans = max(ans, cnt)
            else:
                cnt = 1
        return ans

本质上还是计数器!发现没有递增就reset计数器。

 

或者是用贪心也可以:

class Solution(object):
    def findLengthOfLCIS(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """        
        ans = 0
        i = 0
        while i < len(nums):
            cnt = 1        
            while i+1<len(nums) and nums[i+1]>nums[i]:
                i += 1
                cnt += 1
            ans = max(ans, cnt)
            i += 1
        return ans

还有dp解法,虽然看起来不是那么舒服:

class Solution {
    public int findLengthOfLCIS(int[] nums) {
        if (nums == null || nums.length == 0) return 0;
        int n = nums.length;
        int[] dp = new int[n];
        
        int max = 1;
        dp[0] = 1;
        for (int i = 1; i < n; i++) {
            if (nums[i] > nums[i - 1]) {
                dp[i] = dp[i - 1] + 1;
            }
            else {
                dp[i] = 1;
            }
            max = Math.max(max, dp[i]);
        }
        
        return max;
    }
}

标签:cnt,nums,674,max,Continuous,int,Subsequence,ans,dp
From: https://blog.51cto.com/u_11908275/6380983

相关文章

  • leetcode 594. Longest Harmonious Subsequence
    Wedefineaharmoniousarrayisanarraywherethedifferencebetweenitsmaximumvalueanditsminimumvalueisexactly1.Now,givenanintegerarray,youneedtofindthelengthofitslongestharmonioussubsequenceamongallitspossiblesubsequences.E......
  • 创龙教仪TL6748-PlusTEB教学实验箱实验操作教程:2-2 LED灯控制实验
    2-2LED灯控制实验(点击查看完整视频)1、实验目的本次视频教程是基于创龙教仪TL6748-PlusTEB教学实验箱完成的。本节视频的目的是学习基于StarterWare开发环境配置GPIO管脚的方法和原理,并实现StarterWare开发环境下的LED灯控制。2、实验原理StarterWareStarterWare是一个免费的软件开......
  • upc 6597: Don't Be a Subsequence (字符串的最短不匹配子序列 dp)
    6597:Don'tBeaSubsequence时间限制:1Sec  内存限制:128MB提交:237  解决:45[提交][状态][讨论版][命题人:admin] 题目描述AsubsequenceofastringSisastringthatcanbeobtainedbydeletingzeroormorecharactersfromSwithoutchangingtheor......
  • 1332. Remove Palindromic Subsequences刷题笔记
    容易陷入思维盲区,只有a和b的字符串,只会有2个或1个回文classSolution:defremovePalindromeSub(self,s:str)->int:return2-(s==s[::-1])......
  • 创龙教仪TL6748-PlusTEB教学实验箱
    目 录1.实验箱简介 2.软硬件参数 3.可选摄像头模块 4.开发资料 5.电气特性 6.实验箱机械尺寸 7.产品认证 8.实验箱套件清单 9.技术支持 10.增值服务 更多帮助 附录A教学实验 1. 实验箱简介Ø 基于TITMS320C6748定点/浮点DSPC674x处理器,主频456MHz,高达3648MIPS和274......
  • abc271_e Subsequence Path 题解
    SubsequencePath题意有\(n\)个城市和\(m\)条有向道路,编号从\(1\)开始,第\(i\)条道路从\(a_i\)到\(b_i\),长度为\(c_i\)。给定一个长度为\(k\)的序列\(e\),我们定义从\(1\)到\(n\)的一条路径是优秀的当且仅当:经过的边的编号按顺序构成\(e\)的一个子序列。......
  • June 2021-Continuous Transition: Improving Sample Efficiency for Continuous Cont
    摘要:尽管深度强化学习(RL)已成功应用于各种机器人控制任务,但由于样本效率较差,将其应用于现实世界任务仍然具有挑战性。为了克服这一缺点,一些工作侧重于在训练过程中重用收集的轨迹数据,将其分解为一组策略无关的离散变迁。然而,它们的改进有些边际,因为i)转换的数量通常很小,ii)值分......
  • subsequence1 (牛客多校) (2个串比大小, DP, 组合数)
    题面大意:给定2个字符串,问有多少个子字符串S,是大于t的 思路数据范围很小,因此考虑n^2做法分2步,位数s>位数t的时候然后位数相等的时候利用DP,处理,分别就是枚举前k个数和s相同,然后k+1个数比t大就可以. 具体思路自己想想,和那个比较像   cons......
  • CF1794C Scoring Subsequences题解
    文中\(a\)为题目中给的\(a\)。如果我们要求\(a_1,a_2,a_3,\dots,a_m\)的结果,那么我们可以把\(a\)数组从后往前依次除以\(i\),\(i\)从\(1\)到\(n\),即为\(\frac{a_1}{m},\frac{a_2}{m-1},\frac{a_3}{m-2},\dots,\frac{a_{m-1}}{2},\frac{a_m}{1}\),并将其保......
  • 「USACO2016JAN」Subsequences Summing to Sevens
    [USACO16JAN]SubsequencesSummingtoSevensS题目描述FarmerJohn's\(N\)cowsarestandinginarow,astheyhaveatendencytodofromtimetotime.EachcowislabeledwithadistinctintegerIDnumbersoFJcantellthemapart.FJwouldliketota......