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:

 1 public static boolean threeIncSeq(int[] A){
 2         if (A.length<3) return false;
 3
 4         int oneLen = 0;
 5         int twoLen = -1;
 6         for (int i=1;i<A.length;i++){
 7             //check whether current element is larger then A[twoLen].
 8             if (twoLen!=-1 && A[i]>A[twoLen]) return true;
 9             if (twoLen!=-1 && A[i]>A[twoLen] && A[i]<A[oneLen]){
10                 twoLen = i;
11                 continue;
12             }
13             if (twoLen==-1 && A[i]>A[oneLen]){
14                 twoLen = i;
15                 continue;
16             }
17             if (A[i]<A[oneLen]){
18                 oneLen = i;
19                 continue;
20             }
21         }
22
23         return false;
24     }

Variant:

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

 1 public static List<Integer> threeIncSeq(int[] A){
 2         if (A.length<3) return (new ArrayList<Integer>());
 3
 4         int oneLen = 0;
 5         int twoLen = -1;
 6         List<Integer> oneList = new ArrayList<Integer>();
 7         List<Integer> twoList = new ArrayList<Integer>();
 8         oneList.add(A[0]);
 9         for (int i=1;i<A.length;i++){
10             //check whether current element is larger then A[twoLen].
11             if (twoLen!=-1 && A[i]>A[twoLen]){
12                 twoList.add(A[i]);
13                 return twoList;
14             }
15             if (twoLen!=-1 && A[i]>A[twoLen] && A[i]<A[oneLen]){
16                 twoLen = i;
17                 twoList = new ArrayList<Integer>();
18                 twoList.addAll(oneList);
19                 twoList.add(A[i]);
20                 continue;
21             }
22             if (twoLen==-1 && A[i]>A[oneLen]){
23                 twoLen = i;
24                 twoList = new ArrayList<Integer>();
25                 twoList.addAll(oneList);
26                 twoList.add(A[i]);
27                 continue;
28             }
29             if (A[i]<A[oneLen]){
30                 oneLen = i;
31                 oneList = new ArrayList<Integer>();
32                 oneList.add(A[i]);
33                 continue;
34             }
35         }
36
37         return (new ArrayList<Integer>());
38
39     }

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

时间: 2024-10-05 23:08:51

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

Codeforces 490E. Restoring Increasing Sequence 二分

统计有几个'?',然后二分检测取满足条件的最小的数就可以了. 注意没有问号的时候也要检测一下.... E. Restoring Increasing Sequence time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Peter wrote on the board a strictly increasing sequence o

Increasing Sequence CodeForces - 11A

Increasing Sequence CodeForces - 11A 很简单的贪心.由于不能减少元素,只能增加,过程只能是从左到右一个个看过去,看到一个小于等于左边的数的数就把它加到比左边大,并记录加的次数. 错误记录: 但是很容易错...以前错了4次..过几个月来再做还是不能1A... 比如下面这个有很明显错误的程序 1 #include<cstdio> 2 int n,d,last,now,ans; 3 int main() 4 { 5 int i; 6 scanf("%d%

Longest Increasing Sequence

1 public class Longest_Increasing_Subsequence { 2 /** 3 * O(N^2) 4 * DP 5 * 思路: 6 * 示例:[1,0,2,4,10,5] 7 * 找出以上数组的LIS的长度 8 * 分析: 9 * 只要求长度,并不要求找出具体的序列 10 * 问题可以拆分为 11 * 1. 对于[1],找出LIS 12 * 2. 对于[1,0],找出LIS 13 * 3. 对于[1,0,2],找出LIS 14 * 4. 对于[1,0,2,4],找

cf 11A Increasing Sequence(水,)

题意: A sequence a0, a1, ..., at - 1 is called increasing if ai - 1 < ai for each i: 0 < i < t. You are given a sequence b0, b1, ..., bn - 1 and a positive integer d. In each move you may choose one element of the given sequence and add d to it. Wh

CodeForces 490E Restoring Increasing Sequence(贪心)

CodeForces 490E 题目大意:给N个正整数,然而这些正整数中间有些数字是被'?'遮挡住了,每个'?'可以还原回一个数字,希望给定的这N个整数形成一个递增的序列.可以的话,给出这N个整数的序列,不行返回N0. 解题思路:每个整数都在满足条件的情况下尽量的小,写了一个非递归版本的,发现要考虑的因素太多了,wa了太多遍.后面改成了递归版本的,但是这个比较耗时. 代码: #include <cstdio> #include <cstring> const int MAXL =

CodeForces 490E Restoring Increasing Sequence

题意: 一个严格递增序列  某些数字的某些位被盖住了  求  恢复后的序列 思路: 贪心  让每个数在大于前一个的基础上尽量的小 先讨论数字长度 len[i]<len[i-1] 一定是NO len[i]>len[i-1] 除了第一位如果是?就填1以外  其他?全填0 len[i]==len[i-1] dfs搜索num[i]格式下大于num[i-1]的最小的数 代码: #include<cstdio> #include<iostream> #include<cstr

中南OJ1551: Longest Increasing Subsequence Again(分块+离散化线段树)

1551: Longest Increasing Subsequence Again Time Limit: 2 Sec  Memory Limit: 256 MB Submit: 29  Solved: 15 [Submit][Status][Web Board] Description Give you a numeric sequence. If you can demolish arbitrary amount of numbers, what is the length of the

[LeetCode] Increasing Subsequences 递增子序列

Given an integer array, your task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2 . Example: Input: [4, 6, 7, 7] Output: [[4, 6], [4, 7], [4, 6, 7], [4

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

Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19929 Description Given a sequence of N pairs of integers, find the length of the longest incre