Interview-Increasing Sequence with Length 3.
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.的更多相关文章
- 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 ...
- Codeforces Beta Round #11 A. Increasing Sequence 贪心
A. Increasing Sequence 题目连接: http://www.codeforces.com/contest/11/problem/A Description A sequence a ...
- Increasing Sequence CodeForces - 11A
Increasing Sequence CodeForces - 11A 很简单的贪心.由于不能减少元素,只能增加,过程只能是从左到右一个个看过去,看到一个小于等于左边的数的数就把它加到比左边大,并记 ...
- Longest Increasing Sequence
public class Longest_Increasing_Subsequence { /** * O(N^2) * DP * 思路: * 示例:[1,0,2,4,10,5] * 找出以上数组的L ...
- cf 11A Increasing Sequence(水,)
题意: A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < ...
- 动态规划 ---- 最长不下降子序列(Longest Increasing Sequence, LIS)
分析: 完整 代码: // 最长不下降子序列 #include <stdio.h> #include <algorithm> using namespace std; ; in ...
- The Longest Increasing Subsequence (LIS)
传送门 The task is to find the length of the longest subsequence in a given array of integers such that ...
- SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治
Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...
- [Leetcode] Binary search, DP--300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
随机推荐
- 【转载】php程序员:从1.5K到18K 一个程序员的5年成长之路
昨天收到了心仪企业的口头offer, 回首当初什么都不会开始学编程, 到现在恰好五年. 整天在社区晃悠, 看了不少的总结, 在这个时间点, 我也写一份自己的总结吧. 我一直在社区分享, 所以, 这篇总 ...
- GridView自带分页 1总页数 首页 下一页 上一页 尾页 X 页 go 实现方法 .
在前台GRIDVIEW中添加如下代码 <PagerTemplate> <table> <tr> <td style="text-align: rig ...
- Xcode中常用的快捷键
各种新建 shift + comand + n 新建xcode项目 option + command + n 新建分组 command + n 新建文件 搜索 shift + command + ...
- 理解C#系列 / 核心C# / 判断&循环&跳转
判断&循环&跳转 说明 本节写的是C#语言的控制程序流的语句,“控制程序流”就是控制程序运行流程的意思. 判断 很容易理解:如果……就…… if语句:测试特定条件是否满足,如果满足就执 ...
- 理解C#系列 / C#语言的特性
C#语言的特性 大多数语句都已(;)结尾 用({})定义语句块 单行注释(//),多行注释(/*......*/)智能注释(///) 区分大小写 用namespace名称空间对类进行分类 在C#中的所 ...
- WCF之安全
传输安全. 点对点,对整个消息进行加密,性能损失,当中间者不安全时,消息也就不安全了. WCF中支持传输安全和消息安全模式. 通过配置和绑定来设置.<Security Mode =Transct ...
- hibernate 一对多映射关系
1. 单项多对一映射 custom(顾客)与order(订单) :一个顾客可以有多个订单,一个订单只能有一个顾客 配置方法:在多的一端配置<many -to one& ...
- JVM基础知识总结
因为没深入搞底层研究,所以也就没做很细致的笔记.相关笔记内容是直接从度娘那儿来的,重新删减.整理和加了点自己的东西. 1.JVM(Java Virtual Machine)是什么:JVM是一种用于计算 ...
- const关键字在C和C++区别
1)C++默认为内部链接:C默认为外部链接2)在C++中,一般一个const不会创建内存空间,而是将其保存在符号表(待看).比如: ; char buf[bufsize]; 这里无需为const创建内 ...
- Android开发代码规范
目录 1.命名基本原则 2.命名基本规范 2.1编程基本命名规范 2.2分类命名规范 3.分类命名规范 3.1基本数据类型命名规范 3.2控件命名规范 3.3变量命名规范 3.4整个项目的目录规范化 ...