-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.sequence.lcis.js
More file actions
63 lines (54 loc) · 1.25 KB
/
string.sequence.lcis.js
File metadata and controls
63 lines (54 loc) · 1.25 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* @title: Longest CONTINUOUS Increasing SubSequence
* @description: lis in string
* @author: Thorsten Kober
* @email: [email protected]
*/
function lcis1(arr) {
let max = 0;
let count = 0;
let previous = -Infinity;
for (let i = 0; i < arr.length; i++) {
if (arr[i] > previous) {
count++;
} else {
count = 1;
}
previous = arr[i];
max = Math.max(max, count);
}
return max;
}
function lcis2(arr) {
if (arr.length <= 1) return arr.length;
let start = 0;
let max = 1;
for (let i = 1; i <= arr.length; i++) {
if (!arr[i] || arr[i] <= arr[i - 1]) {
max = Math.max(i - start, max);
start = i;
}
}
return max;
}
// npx jest algorithms/string/string.sequence.lcis.js
describe('lcis1', () => {
test('lis([1,3,5,4,7])', () => {
const nums = [1, 3, 5, 4, 7];
expect(lcis1(nums)).toEqual(3);
});
test('lis[50, 3, 10, 7, 40, 80])', () => {
const nums = [2, 2, 2, 2, 2];
expect(lcis1(nums)).toEqual(1);
});
});
describe('lcis2', () => {
test('lis([1,3,5,4,7])', () => {
const nums = [1, 3, 5, 4, 7];
expect(lcis2(nums)).toEqual(3);
});
test('lis[50, 3, 10, 7, 40, 80])', () => {
const nums = [2, 2, 2, 2, 2];
expect(lcis2(nums)).toEqual(1);
});
});