-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalmostIncreasingSequence.ts
More file actions
37 lines (28 loc) · 1.14 KB
/
almostIncreasingSequence.ts
File metadata and controls
37 lines (28 loc) · 1.14 KB
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
function almostIncreasingSequence(sequence: number[]): boolean {
let count = 0;
for (let i = 0; i < sequence.length; i += 1) {
if (sequence[i] <= sequence[i - 1]) {
count += 1;
if (count > 1) break;
if (
sequence[i] <= sequence[i - 2] &&
sequence[i + 1] <= sequence[i - 1]
) {
return false;
}
}
}
return count <= 1;
}
console.log(almostIncreasingSequence([2, 3, 1, 2]));
console.log(almostIncreasingSequence([1, 2, 5, 2, 99, 100, 99, 7676, 87686]));
/*
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].
*/