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. 如何为不定高度(height:auto)的元素添加CSS3 transition-property:height 动画

    但一个元素不设置height时,它的默认值是 auto,浏览器会计算出实际的高度. 但如果想给一个 height:auto 的块级元素的高度添加 CSS3 动画时,该怎么办呢? 从 MDN 的可以查到 ...

  2. 浅谈 css3 box盒子模型以及box-flex的使用

    display:box;box-flex是css3新添加的盒子模型属性,它的出现可以解决我们通过N多结构.css实现的布局方式.经典的一个布局应用就是布局的垂直等高.水平均分.按比例划分.   一.使 ...

  3. CSS3写常用的形状

    正方形: 1 .square{ width: 100px;height: 100px; background: #E57779;}   长方形: .rectangle{ width: 200px;he ...

  4. Socket 两平台互相 通信 .NET

    两个平台互相通信,对方发送数据过来,我方接收数据,对数据进行处理后发送结果给对方,对方进行相应的操作. 首页,我方开启服务监听: Socket socket = new Socket(AddressF ...

  5. .NET程序编译和运行

    一次面试的时候遇到的一道题目,简要说明.NET的编译过程,在网上看了很多资料,简单总结如下: 1.一般的编译过程 通常高级语言的程序编译过程是:首先写好的程序是源代码,然后编译器编译为本地机器语言,最 ...

  6. C#垃圾回收机制

    C#属于托管的面相对象的语言,内存回收机制就是一个代表, C#有一套类似"全自动"的垃圾回收机制,也就是虚拟机会自动来判断执行内存的回收, 我们一般常用的Dispose(),Usi ...

  7. C#调用SSIS包及读取DataReader目标

    C#调用SSIS包需要引用两个DLL .(具体位置在C盘搜索,MSDN和百度提供的路径都不太正确) Microsoft.SQLServer.ManagedDTS.dll Microsoft.SqlSe ...

  8. jQuery Ajax应用

    jQuery Ajax应用 本章主要了解jQuery的Ajax与传统的Ajax的区别,掌握JQuery的Ajax常用的方法与Ajax相关的函数. 详细内容,请点击jQuery Ajax应用查看:

  9. 标识域 Identify Field

    在对象中保存DB的ID字段,以维持内存对象和DB数据Row之间的identify. 关系DB使用key来区分数据行. 而内存对象不需要这样的键.因为对象系统能够保证身份确认. 读取时没有问题,但是为了 ...

  10. javascript之Array基础篇

    整理了 Array 中很基础的要掌握的知识点,希望可以帮助初学者,也希望自己以后多用多融会贯通. 创建数组 使用Array构造函数: var a=new Array();//创建一个空数组 var a ...