674. Longest Continuous Increasing Subsequence

2023-12-21 16:38

本文主要是介绍674. Longest Continuous Increasing Subsequence,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

674. 最长连续递增序列

给定一个未经排序的整数数组,找到最长且连续的的递增序列。

示例 1:

输入: [1,3,5,4,7]
输出: 3
解释: 最长连续递增序列是 [1,3,5], 长度为3。
尽管 [1,3,5,7] 也是升序的子序列, 但它不是连续的,因为5和7在原数组里被4隔开。 

示例 2:

输入: [2,2,2,2,2]
输出: 1
解释: 最长连续递增序列是 [2], 长度为1。

注意:数组长度不会超过10000。

解法一

//时间复杂度O(n), 空间复杂度O(1)
class Solution {
public:int findLengthOfLCIS(vector<int>& nums) {int n = nums.size();if(n == 0) return 0;int res = 0, start = 0, end;for(end = 1; end < n; end++) {if(nums[end] > nums[end - 1]) continue;res = max(end - start, res);start = end;}return max(end - start, res);}
};

遍历一次,使用start和end记录增序序列的起始位置,用res记录end - start的最大值,遍历完成后返回res即可。注意处理输入元素数为0和1这两种特殊情况。

2019/06/18 22:34

这篇关于674. Longest Continuous Increasing Subsequence的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/520731

相关文章

674 - Coin Change

一道很水的DP,但我做的时候也很费劲,由于存在面值为1的时候,所以所有面额都至少有1种方法。 推导出状态方程就是 dp(V,step) += dp(V - i * coin[step],step - 1); 其中V为当前的面值,coin[step]为硬币的面额,递归边界,如果step == 0(也就是递归到硬币面额为1的时候,返回1); #include<cstdio>#include<

leetcode#32. Longest Valid Parentheses

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", wh

java常用算法之最长回文子串(Longest Palindromic Substring)

方法一:时间复杂度为O(n^3) public static String longestPalindrome1(String s) {int maxPalinLength = 0;String longestPalindrome = null;int length = s.length();// check all possible sub stringsfor (int i = 0; i

[LeetCode] 300. Longest Increasing Subsequence

题:https://leetcode.com/problems/longest-increasing-subsequence/description/ 题目 Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7

[LeetCode] 409. Longest Palindrome

题:https://leetcode.com/problems/longest-palindrome/description/ 题目 Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with

[LeetCode] 128. Longest Consecutive Sequence

题:https://leetcode.com/problems/longest-consecutive-sequence/description/ 题目 Given an unsorted array of integers, find the length of the longest consecutive elements sequence. Your algorithm should

[LeetCode] 594. Longest Harmonious Subsequence

题:https://leetcode.com/problems/longest-harmonious-subsequence/description/ 题目 We define a harmonious array is an array where the difference between its maximum value and its minimum value is exactl

[LeetCode] 687. Longest Univalue Path

题:https://leetcode.com/problems/longest-univalue-path/description/ 题目 Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may no

[LeetCode] 524. Longest Word in Dictionary through Deleting

题:https://leetcode.com/problems/longest-word-in-dictionary-through-deleting/ 题目大意 对s删除某些元素,使得删除后的s 为 d中的某个元素。 思路 trie 首先将 建立tire,然后是用dfs搜索,遍历s。 boolean dfs(TrieNode node,String s,StringBuilder cu

LeetCode - 32. Longest Valid Parentheses

32. Longest Valid Parentheses  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个由'('和')'组成的字符串,求最长连续匹配子串长度. analyse: 定义一个stack<pair