Given an array, determine whether there are three elements A[i],A[j],A[k], such that A[i]<A[j]<A[k] & i<j<k.

Analysis:

It is a special case of the Longest Increasing Sequence problem. We can use the O(nlog(n)) algorithm, since we only need sequence with length three, we can do it in O(n).

Solution:

 public static boolean threeIncSeq(int[] A){
if (A.length<3) return false; int oneLen = 0;
int twoLen = -1;
for (int i=1;i<A.length;i++){
//check whether current element is larger then A[twoLen].
if (twoLen!=-1 && A[i]>A[twoLen]) return true;
if (twoLen!=-1 && A[i]>A[twoLen] && A[i]<A[oneLen]){
twoLen = i;
continue;
}
if (twoLen==-1 && A[i]>A[oneLen]){
twoLen = i;
continue;
}
if (A[i]<A[oneLen]){
oneLen = i;
continue;
}
} return false;
}

Variant:

If we need to output the sequence, we then need to record the sequence of current length 1 seq and length 2 seq.

 public static List<Integer> threeIncSeq(int[] A){
if (A.length<3) return (new ArrayList<Integer>()); int oneLen = 0;
int twoLen = -1;
List<Integer> oneList = new ArrayList<Integer>();
List<Integer> twoList = new ArrayList<Integer>();
oneList.add(A[0]);
for (int i=1;i<A.length;i++){
//check whether current element is larger then A[twoLen].
if (twoLen!=-1 && A[i]>A[twoLen]){
twoList.add(A[i]);
return twoList;
}
if (twoLen!=-1 && A[i]>A[twoLen] && A[i]<A[oneLen]){
twoLen = i;
twoList = new ArrayList<Integer>();
twoList.addAll(oneList);
twoList.add(A[i]);
continue;
}
if (twoLen==-1 && A[i]>A[oneLen]){
twoLen = i;
twoList = new ArrayList<Integer>();
twoList.addAll(oneList);
twoList.add(A[i]);
continue;
}
if (A[i]<A[oneLen]){
oneLen = i;
oneList = new ArrayList<Integer>();
oneList.add(A[i]);
continue;
}
} return (new ArrayList<Integer>()); }

NOTE: This is more compliated then needed, when using List<> in this case, but this idea can be used to print the LIS.

Interview-Increasing Sequence with Length 3.的更多相关文章

  1. Codeforces Round #279 (Div. 2) E. Restoring Increasing Sequence 二分

    E. Restoring Increasing Sequence time limit per test 1 second memory limit per test 256 megabytes in ...

  2. Codeforces Beta Round #11 A. Increasing Sequence 贪心

    A. Increasing Sequence 题目连接: http://www.codeforces.com/contest/11/problem/A Description A sequence a ...

  3. Increasing Sequence CodeForces - 11A

    Increasing Sequence CodeForces - 11A 很简单的贪心.由于不能减少元素,只能增加,过程只能是从左到右一个个看过去,看到一个小于等于左边的数的数就把它加到比左边大,并记 ...

  4. Longest Increasing Sequence

    public class Longest_Increasing_Subsequence { /** * O(N^2) * DP * 思路: * 示例:[1,0,2,4,10,5] * 找出以上数组的L ...

  5. cf 11A Increasing Sequence(水,)

    题意: A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i <  ...

  6. 动态规划 ---- 最长不下降子序列(Longest Increasing Sequence, LIS)

    分析: 完整 代码: // 最长不下降子序列 #include <stdio.h> #include <algorithm> using namespace std; ; in ...

  7. The Longest Increasing Subsequence (LIS)

    传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...

  8. SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治

    Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...

  9. [Leetcode] Binary search, DP--300. Longest Increasing Subsequence

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

随机推荐

  1. asp.net zip 压缩传输

    在实际生产中,比如使用xml json 等传输大量数据的时候,有时候会出现等待时间过长,这里分享一个压缩传输的方法 首先到网上去下载一个 ICSharpCode.SharpZipLib.dll 的dl ...

  2. 深入理解ThreadLocal(转)(2015年06月11日)

    注明:转自:http://my.oschina.net/clopopo/blog/149368 学习一个东西首先要知道为什么要引入它,就是我们能用它来干什么.所以我们先来看看ThreadLocal对我 ...

  3. 关于arraylist.remove的一些小问题。

    public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<Integer> ...

  4. Ubuntu 16.04 - python3 安装mysql驱动

    阿西吧,今天碰到一件特别蛋疼的事,给Ubuntu安装Python的MySQL驱动,驱动显示安装成功了 pip install mysql-connector 但是 在程序中导入,老是报错. Trace ...

  5. 在rdlc 中 显示成 yyyy年MM月dd日

    在rdlc 中  显示成  yyyy年MM月dd日, 采用: =First(Format(Fields!添加时间.Value,"yyyy年MM月dd日")  )

  6. Android之天气APP

    做一个天气信息APP,通过读取公开发布的气象数据,提供实时天气更新,帮助用户时刻了解天气情况.通过APP可以查询到自己所处位置的天气预报,也可以通过定位系统,获取省内任意位置的气象环境信息. API接 ...

  7. 【学习笔记】【C语言】二维数组

    1. 什么是二维数组 一个数组能表示一个班人的年龄,如果想表示很多班呢? 什么是二维数组?int ages[3][10]; 三个班,每个班10个人 相当于3行10列 相当于装着3个一维数组 二维数组是 ...

  8. Extjs Cmd 学习笔记

    1.sencha app build 命令 <!-- <x-compile> -->                  <!-- <x-bootstrap> ...

  9. java中的生产者和消费者的问题

    1----使用Java.util.concurrent.locks包中的lock接口取代synchronized,通过ReentrantLock这个已经实现Lock接口的类, 创建ReentrantL ...

  10. 一款类似塔防类的保卫羊村游戏android源码

    一款类似塔防类的保卫羊村游戏源码,这个游戏很像我们平时玩的塔防游戏的,游戏的源码比较完整的,大家可以修改一下或者添加一些广告就可以上线到应用商店了,而且这个游戏目前已经上线国内的一些应用商店了,360 ...