Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7227f89

Browse files
committed
Merge branch 'master' of https://github.com/lennylxx/leetcode
2 parents e8869f3 + da85c4f commit 7227f89

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

‎README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
2525

2626
| | Problem | Solution |
2727
| --- | ------------------------------------------------------------ | ------------------ |
28+
| 300 | [Longest Increasing Subsequence] | [C](src/300.c) |
2829
| 299 | [Bulls and Cows] | [C](src/299.c) |
2930
| 298 | [Binary Tree Longest Consecutive Sequence]| |
3031
| 297 | [Serialize and Deserialize Binary Tree] | [C++](src/297.cpp) |
@@ -270,7 +271,7 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
270271
| 40 | [Combination Sum II] | [C++](src/40.cpp) |
271272
| 39 | [Combination Sum] | [C++](src/39.cpp) |
272273
| 38 | [Count and Say] | [C](src/38.c) |
273-
| 37 | [Sudoku Solver] | [C](src/37.cpp) |
274+
| 37 | [Sudoku Solver] | [C++](src/37.cpp) |
274275
| 36 | [Valid Sudoku] | [C](src/36.c) |
275276
| 35 | [Search Insert Position] | [C](src/35.c) |
276277
| 34 | [Search for a Range] | [C++](src/34.cpp) |
@@ -312,6 +313,7 @@ The `☢` means that you need to have a LeetCode Premium Subscription.
312313
[LeetCode algorithm problems]: https://leetcode.com/problemset/algorithms/
313314

314315

316+
[Longest Increasing Subsequence]: https://leetcode.com/problems/longest-increasing-subsequence/
315317
[Bulls and Cows]: https://leetcode.com/problems/bulls-and-cows/
316318
[Binary Tree Longest Consecutive Sequence]: https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/
317319
[Serialize and Deserialize Binary Tree]: https://leetcode.com/problems/serialize-and-deserialize-binary-tree/

‎src/300.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <assert.h>
4+
5+
/* Dynamic Programming, O(n*n) */
6+
int lengthOfLIS0(int* nums, int numsSize) {
7+
if (nums == NULL || numsSize == 0) return 0;
8+
int *dp = (int *)malloc(numsSize * sizeof(int));
9+
int i, j;
10+
int max = 1;
11+
for (i = 0; i < numsSize; i++) {
12+
int v = 1; /* default value for dp[i] */
13+
for (j = 0; j < i; j++) {
14+
if (nums[i] > nums[j] && dp[j] + 1 > v)
15+
v = dp[j] + 1;
16+
}
17+
dp[i] = v;
18+
if (v > max)
19+
max = v;
20+
}
21+
free(dp);
22+
return max;
23+
}
24+
25+
/* Trace the LIS using an array and use binary search, O(nlogn) */
26+
int lengthOfLIS(int* nums, int numsSize) {
27+
if (nums == NULL || numsSize == 0) return 0;
28+
int *lis = (int *)malloc(numsSize * sizeof(int));
29+
lis[0] = nums[0];
30+
int len = 1;
31+
int i;
32+
for (i = 1; i < numsSize; i++) {
33+
if (nums[i] > lis[len - 1]) {
34+
lis[len++] = nums[i];
35+
}
36+
else {
37+
int l = 0, r = len - 1;
38+
while (l < r) {
39+
int m = l + (r - l) / 2;
40+
if (lis[m] >= nums[i]) {
41+
r = m;
42+
}
43+
else {
44+
l = m + 1;
45+
}
46+
}
47+
lis[l] = nums[i];
48+
}
49+
}
50+
free(lis);
51+
return len;
52+
}
53+
54+
int main() {
55+
int nums0[] = { 10, 9, 2, 5, 3, 7, 101, 18 };
56+
int nums1[] = { 1, 0, 1, 1 };
57+
assert(lengthOfLIS(nums0, sizeof(nums0) / sizeof(nums0[0])) == 4);
58+
assert(lengthOfLIS(nums1, sizeof(nums1) / sizeof(nums1[0])) == 2);
59+
60+
printf("all tests passed!\n");
61+
62+
return 0;
63+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /