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 fdff12a

Browse files
committed
"Longest Increasing Subsequence"
1 parent fbb8e06 commit fdff12a

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <assert.h>
4+
5+
int lengthOfLIS(int* nums, int numsSize) {
6+
if (nums == NULL || numsSize == 0) return 0;
7+
int *dp = (int *)malloc(numsSize * sizeof(int));
8+
int i, j;
9+
int max = 1;
10+
for (i = 0; i < numsSize; i++) {
11+
int v = 1; /* default value for dp[i] */
12+
for (j = 0; j < i; j++) {
13+
if (nums[i] > nums[j] && dp[j] + 1 > v)
14+
v = dp[j] + 1;
15+
}
16+
dp[i] = v;
17+
if (v > max)
18+
max = v;
19+
}
20+
return max;
21+
}
22+
23+
int main() {
24+
int nums0[] = { 10, 9, 2, 5, 3, 7, 101, 18 };
25+
int nums1[] = { 1, 0, 1, 1 };
26+
assert(lengthOfLIS(nums0, sizeof(nums0) / sizeof(nums0[0])) == 4);
27+
assert(lengthOfLIS(nums1, sizeof(nums1) / sizeof(nums1[0])) == 2);
28+
29+
printf("all tests passed!\n");
30+
31+
return 0;
32+
}

0 commit comments

Comments
(0)

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