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, ...
随机推荐
- 地图 ajax-数据库
通过数据库和ajax方法写出地图 客户端部分:html.js.css代码部分: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transit ...
- C# 输出24小时格式时间
比如 MessageBox.Show(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")); 会输出 2014-4-3 5:08:4[1 ...
- DataGridView 操作
//dataGridView 删除选中行 int num = dataGridView2.SelectedRows.Count; ) { DataGridViewRow r = dataGridVie ...
- JS闭包理解_摘
原文地址1:http://www.cnblogs.com/mzwr1982/archive/2012/05/20/2509295.html 闭包是一个比较抽象的概念,尤其是对js新手来说.书上的解释实 ...
- IOS 后台执行
在IOS后台执行是本文要介绍的内容,大多数应用程序进入后台状态不久后转入暂停状态.在这种状态下,应用程序不执行任何代码,并有可能在任意时候从内存中删除.应用程序提供特定的服务,用户可以请求后台执行时间 ...
- eclipse安装tomcate插件步骤
今天在网上看了很多关于eclipse需要安装tomcat的文章,小编也没有指责的意思,只是想说为什么有那么多雷同且不正确的搭建过程,真是ctrl+c, ctrl+v玩多了吧,不知道流程有的是错误的,不 ...
- Linux下动态共享库加载及使用详解【转】
原文地址:http://blog.chinaunix.net/uid-29025972-id-3855500.html 对动态库的实际应用还不太熟悉的读者可能曾经遇到过类似“error while l ...
- ASP.NET MVC强制返回XML
GlobalConfiguration.Configuration.Formatters.Remove(config.Formatters.JsonFormatter);
- 【NPOI】.NET EXCEL导入导出开发包
1.导出 //工作簿HSSFWorkbook HSSFWorkbook hssfworkbook = new HSSFWorkbook(); //ISheet页 ISheet sheet1 = hss ...
- <postfix邮件服务下mysql的升级>
本片服务的环境的红帽的企业版6.5 的,6.3的测试可能会略有不一样,不过方法大致是一样的. 当前系统的postfix的版本为 postfix-2.6.6-2.2.el6_1.x86_64 我们要向使 ...