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.
Example
Example 1:
Input: [5, 4, 2, 1, 3]
Output: 4
Explanation:
For [5, 4, 2, 1, 3], the LICS is [5, 4, 2, 1], return 4.
Example 2:
Input: [5, 1, 2, 3, 4]
Output: 4
Explanation:
For [5, 1, 2, 3, 4], the LICS is [1, 2, 3, 4], return 4.
思路:本题为直接去找一个数组的最长连续上升/下降子序列,直接从左往右枚举即可,每次到单调性变化的时候更新答案。
public class Solution {
public int longestIncreasingContinuousSubsequence(int[] A) {
if (A == null || A.length == 0) {
return 0;
}
int n = A.length;
int answer = 1;
// from left to right
int length = 1; // just A[0] itself
for (int i = 1; i < n; i++) {
if (A[i] > A[i - 1]) {
length++;
} else {
length = 1;
}
answer = Math.max(answer, length);
}
// from right to left
length = 1;
for (int i = n - 2; i >= 0; i--) {
if (A[i] > A[i + 1]) {
length++;
} else {
length = 1;
}
answer = Math.max(answer, length);
}
return answer;
}
}
Challenge
O(n) time and O(1) extra space.
Longest Continuous Increasing Subsequence的更多相关文章
- LeetCode 674. Longest Continuous Increasing Subsequence (最长连续递增序列)
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [LeetCode] Longest Continuous Increasing Subsequence 最长连续递增序列
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [Swift]LeetCode674. 最长连续递增序列 | Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
- [Leetcode]674. Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuous increasing subsequence. E ...
- [LeetCode&Python] Problem 674. Longest Continuous Increasing Subsequence
Given an unsorted array of integers, find the length of longest continuousincreasing subsequence (su ...
- leetcode300. Longest Increasing Subsequence 最长递增子序列 、674. Longest Continuous Increasing Subsequence
Longest Increasing Subsequence 最长递增子序列 子序列不是数组中连续的数. dp表达的意思是以i结尾的最长子序列,而不是前i个数字的最长子序列. 初始化是dp所有的都为1 ...
- 674. Longest Continuous Increasing Subsequence最长连续递增子数组
[抄题]: Given an unsorted array of integers, find the length of longest continuous increasing subseque ...
- LeetCode Longest Continuous Increasing Subsequence
原题链接在这里:https://leetcode.com/problems/longest-continuous-increasing-subsequence/description/ 题目: Giv ...
- 674. Longest Continuous Increasing Subsequence@python
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
- LeetCode 674. Longest Continuous Increasing Subsequence最长连续递增序列 (C++/Java)
题目: Given an unsorted array of integers, find the length of longest continuous increasing subsequenc ...
随机推荐
- C++ 重载运算符返回值 和 返回引用的原因
原因是: +,-,*等返回不了引用,比如+运算符,可以如下重载(为了简单,假设A 只有int x:int y) A operator+(A a,A b) {A sum; sum.x=a.x+b.x ...
- 初始NLTK
NLTK官网:链接 Natural Language Toolkit NLTK corpora and lexical resources such as WordNet, along with a ...
- go 错误
错误 Go 程序使用 error 值来表示错误状态. 与 fmt.Stringer 类似,`error` 类型是一个内建接口: type error interface { Error() strin ...
- 【flume】5.采集日志进入hbase
设置我们的flume配置信息 # Licensed to the Apache Software Foundation (ASF) under one # or more contributor li ...
- Itemchanged事件
Itemchanged事件:当数据窗口控件中某个域被修改并且该域失去输入焦点该事件返回的意义为: 0--(缺省返回值),接收新修改的值: 1--不接收新修改的值且不允许改变输入焦点: 2--不接收新修 ...
- JAVA基础_修饰符
引言:Java的修饰符根据修饰的对象不同,分为类修饰符.方法修饰符.变量修饰符,其中每种修饰符又分为访问控制修饰符和非访问控制修饰符.访问控制存在的原因:a.让客户端程序员无法触及他们不应该触及的部分 ...
- 使用postman mock server
需要写一个小的Java程序,用来调用云平台的接口 由于云平台的接口程序还没有写好,只能用模拟的方式先行开发代码, 用了post来模拟接口程序. 需要模拟的接口如下: ■请求地址 /openapi/ip ...
- hdu 1075 map的使用 字符串截取的常用手段 以及string getline 使用起来的注意事项
首先说字符串的截取套路吧 用坐标一个一个的输入 用遍历的方式逐个去检查字符串中的字符是否为符合的情况 如果是的话 把该字符放入截取string 中 让后坐标前移 如果不是的话 截取结束 坐标初始化 然 ...
- java 线程并发(生产者、消费者模式)
线程并发协作(生产者/消费者模式) 多线程环境下,我们经常需要多个线程的并发和协作.这个时候,就需要了解一个重要的多线程并发协作模型“生产者/消费者模式”. Ø 什么是生产者? 生产者指的是负责生产数 ...
- windows下搭建nginx负载均衡
学习笔记,第一次记录避免忘记 首先介绍一下本地环境是windows2008 R2-64位. 1. 到nginx官网上下载最新稳定版的安装包,http://nginx.org/en/download. ...