We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 6feab8f commit 025408dCopy full SHA for 025408d
dynamic-programming/longest-increasing-subsequence.js
@@ -0,0 +1,35 @@
1
+/**
2
+ * LeetCode | #300 | Medium
3
+ *
4
+ * Given an integer array nums, return the length of the longest strictly increasing
5
+ * subsequence.
6
7
+ * Constraints:
8
+ * 1. 1 <= nums.length <= 2500
9
+ * 2. -104 <= nums[i] <= 104
10
11
+ */
12
+
13
14
+ * Time complexity: O(n^2) - Space complexity: O(n)
15
16
+ * @param {number[]} nums
17
+ * @return {number}
18
19
+function lengthOfLIS(nums) {
20
+ const memo = [];
21
22
+ for (let i = 0; i < nums.length; i++) {
23
+ let longestSubSeq = 1;
24
25
+ for (let j = 0; j < i; j++) {
26
+ if (nums[i] > nums[j] && memo[j] + 1 > longestSubSeq) {
27
+ longestSubSeq = memo[j] + 1;
28
+ }
29
30
31
+ memo[i] = longestSubSeq;
32
33
34
+ return Math.max(...memo);
35
+}
0 commit comments