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 8fa7d09 commit f9ded2dCopy full SHA for f9ded2d
c/300-Longest-Increasing-Subsequence.c
@@ -0,0 +1,26 @@
1
+/*
2
+Given an integer array nums, return the length of the longest strictly increasing subsequence.
3
+Time: O(nlog(n))
4
+Space: O(n)
5
+*/
6
+
7
+int lengthOfLIS(int* nums, int numsSize){
8
+ int max=0, mid, i, j;
9
+ int* dp = calloc(numsSize, sizeof(int)); // dp[k] is the minimal last value of a subsequence of size k+1;
10
+ dp[0] = nums[0];
11
+ for (int k=0; k<numsSize; k++) {
12
+ i=0, j=max;
13
+ while (i!=j) {
14
+ mid = (i+j)/2;
15
+ if (dp[mid]<nums[k])
16
+ i=mid+1;
17
+ else
18
+ j=mid;
19
+ }
20
+ dp[i] = nums[k];
21
+ if (i==max)
22
+ max++;
23
24
+ free(dp);
25
+ return max;
26
+}
0 commit comments