Skip to content

Commit f9ded2d

Browse files
Add 300 in c language
1 parent 8fa7d09 commit f9ded2d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)