397. Longest Continuous Increasing Subsequence

Description

Give an integer array,find the longest increasing continuous subsequence in this array.

An increasing continuous subsequence:

  • Can be from right to left or from left to right.
  • Indices of the integers in the subsequence should be continuous.

O(n) time and O(1) extra space.

Example

For [5, 4, 2, 1, 3], the LICS is [5, 4, 2, 1], return 4.

For [5, 1, 2, 3, 4], the LICS is [1, 2, 3, 4], return 4.

解题:记录连续增大或者连续减少的个数,返回最大值。代码如下:

public class Solution {
    /**
     * @param A: An array of Integer
     * @return: an integer
     */
    public int longestIncreasingContinuousSubsequence(int[] A) {
        // write your code here
        int i_count = 1;//上升的时候的个数
        int d_count = 1;//下降时候的个数
        int temp = 1;
        //注意,当下标有减号时,要注意返回,下标不为负
        if(A.length == 0){
            return 0;
        }
        for(int i = 1; i < A.length; i++){
            if(A[i] > A[i-1]){
                //说明增大
                temp++;
            }else{
                //否则
                if(temp > i_count){
                    i_count = temp;//更新
                }
                temp = 1;
            }
        }
        if(temp > i_count){
            //如果一直到最后,可能缺少一次跟新
            i_count = temp;
        }
        temp = 1;
        for(int i = 1; i < A.length; i++){
            if(A[i] < A[i-1]){
                //说明减小
                temp++;
            }else{
                //否则
                if(temp > d_count){
                    d_count = temp;//更新
                }
                temp = 1;
            }
        }
        if(temp > d_count){
            //如果一直到最后,可能缺少一次跟新
            d_count = temp;
        }
        if(i_count >= d_count)
            return i_count;
        else return d_count;
    }
}

原文地址:https://www.cnblogs.com/phdeblog/p/9313335.html

时间: 2024-11-09 02:58:55

397. Longest Continuous Increasing Subsequence的相关文章

674. Longest Continuous Increasing Subsequence

Given an unsorted array of integers, find the length of longest continuous increasing subsequence. 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] i

674. Longest Continuous Increasing Subsequence 最长连续增长的子序列

Given an unsorted array of integers, find the length of longest continuous increasing subsequence. 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] i

[LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列

Given an unsorted array of integers, find the length of longest continuous increasing subsequence. 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] i

674. Longest Continuous Increasing Subsequence最长连续递增子数组

[抄题]: 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 t

Longest Continuous Increasing Subsequence LT674

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

LeetCode 674. 最长连续递增序列(Longest Continuous Increasing Subsequence) 18

674. 最长连续递增序列 674. Longest Continuous Increasing Subsequence 题目描述 给定一个未经排序的整型数组,找到最长且连续的递增序列. Given an unsorted array of integers, find the length of longest continuous increasing subsequence (subarray). 每日一算法2019/5/21Day 18LeetCode674. Longest Conti

LeetCode 674. Longest Continuous Increasing Subsequence最长连续递增序列 (C++/Java)

题目: 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 tho

[LC] 674. Longest Continuous Increasing Subsequence

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

Longest Continuous Increasing Subsequence II

Description Given an integer matrix. Find the longest increasing continuous subsequence in this matrix and return the length of it. The longest increasing continuous subsequence here can start at any position and go up/down/left/right. Example Exampl