Skip to content

Commit b89a82f

Browse files
committed
Longest increasing sequence.
1 parent 8a1fe52 commit b89a82f

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

fib/test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ describe('Fibonacci Tests', () => {
88
test('Fib function is defined', () => {
99
expect(typeof fib).toEqual('function');
1010
});
11-
it('Calculate correct fib value for 1 === 1', async() => {
11+
it('Calculate correct fib value for 1 === 1', () => {
1212
expect(fib(1)).toEqual(1);
1313
});
14-
it('Calculate correct fib value for 2 === 1', async() => {
14+
it('Calculate correct fib value for 2 === 1', () => {
1515
expect(fib(2)).toEqual(1);
1616
});
17-
it('Calculate correct fib value for 5 === 5', async() => {
17+
it('Calculate correct fib value for 5 === 5', () => {
1818
expect(fib(5)).toEqual(5);
1919
});
20-
test('Calculate correct fib value for 6 === 8', async() => {
20+
test('Calculate correct fib value for 6 === 8', () => {
2121
expect(fib(6)).toEqual(8);
2222
});
23-
test('Calculate correct fib value for 50 === 12586269025', async() => {
23+
test('Calculate correct fib value for 50 === 12586269025', () => {
2424
expect(fib(50)).toEqual(12586269025);
2525
});
2626
});

lis/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @author: Chisimdi Damian Ezeanieto
3+
* @date: 23/06/2020
4+
*/
5+
// Find the longest increasing subsequence in a given array.
6+
// Longest Increasing Subsequence
7+
const lis = (arr) => {
8+
if (arr.length < 1) return undefined;
9+
// Fill an array of length arr.length with 1;
10+
const lis = Array(arr.length).fill(1);
11+
12+
let n = arr.length;
13+
for(let i = 1; i <= n; i++) {
14+
for(let j = 0; j < i; j++) {
15+
if (arr[j] < arr[i]) {
16+
lis[i] = Math.max(lis[j] + 1, lis[i]);
17+
}
18+
}
19+
}
20+
21+
return Math.max(...lis);
22+
};
23+
24+
module.exports = lis;

lis/test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* @author: Chisimdi Damian Ezeanieto
3+
* @date: 23/06/2020
4+
*/
5+
const lis = require('./index');
6+
7+
describe('Longest Increasing Subsequence Tests', () => {
8+
test('lis function is defined', () => {
9+
expect(typeof lis).toEqual('function');
10+
});
11+
it('Calculate correct lis value for [5,7,4,-3,9,1,10,4,5,8,9,3] === 6', () => {
12+
expect(lis([5,7,4,-3,9,1,10,4,5,8,9,3])).toEqual(6);
13+
});
14+
});

0 commit comments

Comments
 (0)