-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathProblem75.js
54 lines (51 loc) · 1.65 KB
/
Problem75.js
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
// Problem 75
//
// This problem was asked by Microsoft.
//
// Given an array of numbers, find the length of the longest increasing subsequence in the array.
// The subsequence does not necessarily have to be contiguous.
//
// For example, given the array [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15],
// the longest increasing subsequence has length 6: it is 0, 2, 6, 9, 11, 15.
//
// https://leetcode.com/problems/longest-increasing-subsequence/
// https://dailycodingproblem.com/blog/longest-increasing-subsequence/
//
// Best solution is N Log N using a variation of binary search
//
// O(N^2) Time complexity
// O(N) Space complexity
// N is the length of the array
/**
* Returns the length of the longest increasing subsequence in the array
* @param {number[]} nums
* @return {number}
*/
function longestIncreasingSubsequence(nums) {
if (nums.length === 0) return 0;
const dp = [];
let longestSubsequenceLength = -1;
for (let i = 0; i < nums.length; i++) {
const num = nums[i];
let longestSubsequenceSoFar = 1;
for (let j = 0; j < i; j++) {
// we need to compare index of j to our current number
// this comparison means there is an increasing subsequence
if (nums[j] < num) {
const currLongestSubsequence = dp[j];
longestSubsequenceSoFar = Math.max(
longestSubsequenceSoFar,
currLongestSubsequence + 1
);
}
}
// we want to update dp[i]
dp[i] = longestSubsequenceSoFar;
longestSubsequenceLength = Math.max(
longestSubsequenceLength,
longestSubsequenceSoFar
);
}
return longestSubsequenceLength;
}
export default longestIncreasingSubsequence;