leetcode674-Longest Continuous Increasing Subsequence

2024-04-12 02:20

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

题目

给定一个未经排序的整数数组,找到最长且 连续递增的子序列,并返回该序列的长度。
连续递增的子序列 可以由两个下标 l 和 r(l < r)确定,如果对于每个 l <= i < r,都有 nums[i] < nums[i + 1] ,那么子序列 [nums[l], nums[l + 1], …, nums[r - 1], nums[r]] 就是连续递增子序列。
示例 1:
输入:nums = [1,3,5,4,7]
输出:3
解释:最长连续递增序列是 [1,3,5], 长度为3。
尽管 [1,3,5,7] 也是升序的子序列, 但它不是连续的,因为 5 和 7 在原数组里被 4 隔开。

分析

这道题目直接遍历就可以解,一个哨兵记录当前连续递增子数组的长,如果下个元素比当前元素大那么哨兵加1,如果下个元素比当前元素小,那么更新哨兵元素,并且取哨兵元素和最终递增子数组的最大值

public class longestContinuousIncreasingSubsequence {public static void main(String[] args) {int[] arr = {1,3,5,4,7};System.out.println(getSubLen(arr));}public static int getSubLen(int[] arr) {int len = arr.length;int ret = 1;int subLen = 1;for(int i = 0;i<len-1;i++) {if(arr[i] < arr[i+1]) {subLen++;} else {ret = Math.max(subLen,ret);subLen = 1;}}ret = Math.max(subLen,ret);return ret;}
}

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



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

相关文章

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

【HDU】1595 find the longest of the shortest 枚举+最短路

传送门:【HDU】1595 find the longest of the shortest 题目分析:首先求出一条最短路,记录下最短路上用到的边,枚举删除每一条边,求一次最短路,求完后恢复删除的边。重复这一过程直到枚举完所有的边为止。所有删除边后求得的最短路里最长的那条就是答案。 代码如下: #include <cstdio>#include <cstring>