-
Notifications
You must be signed in to change notification settings - Fork 0
/
07_AlmostIncreasngSequence
56 lines (40 loc) · 1.54 KB
/
07_AlmostIncreasngSequence
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
boolean almostIncreasingSequence(int[] sequence) {
int a, b, count;
a=sequence[1];
b=sequence[0];
count=b>=a?1:0;
for(int i=2;i<sequence.length;i++){
if(a<sequence[i]){
b=a;
a=sequence[i];
}else{
if(b<sequence[i]){
a=sequence[i];
//replaced "a", kept B and kept the next int in the array
}
else{
//don't keep the next in the the array and just ignor it
}
count++;
}
}
return count<2?true:false;
}
/**
Given a sequence of integers as an array, determine whether it is possible to obtain a strictly increasing sequence by removing no more than one element from the array.
Example
For sequence = [1, 3, 2, 1], the output should be
almostIncreasingSequence(sequence) = false;
There is no one element in this array that can be removed in order to get a strictly increasing sequence.
For sequence = [1, 3, 2], the output should be
almostIncreasingSequence(sequence) = true.
You can remove 3 from the array to get the strictly increasing sequence [1, 2]. Alternately, you can remove 2 to get the strictly increasing sequence [1, 3].
Input/Output
[execution time limit] 3 seconds (java)
[input] array.integer sequence
Guaranteed constraints:
2 ≤ sequence.length ≤ 105,
-105 ≤ sequence[i] ≤ 105.
[output] boolean
Return true if it is possible to remove one element from the array in order to get a strictly increasing sequence, otherwise return false.
**/