Skip to content

Commit 587cc5c

Browse files
Improvement
Signed-off-by: begeekmyfriend <[email protected]>
1 parent 6de6ce5 commit 587cc5c

File tree

4 files changed

+49
-16
lines changed

4 files changed

+49
-16
lines changed

0072_edit_distance/edit_distance.c

+14-11
Original file line numberDiff line numberDiff line change
@@ -31,31 +31,34 @@ static inline int min(int a, int b)
3131
static int minDistance(char* word1, char* word2)
3232
{
3333
int i, j;
34-
int len1 = strlen(word1);
35-
int len2 = strlen(word2);
36-
int *table = malloc((len1 + 1) * (len2 + 1) * sizeof(int));
37-
int **dp = malloc((len1 + 1) * sizeof(int *));
38-
for (i = 0; i < len1 + 1; i++) {
39-
dp[i] = table + i * (len2 + 1);
34+
int l1 = strlen(word1);
35+
int l2 = strlen(word2);
36+
int *table = malloc((l1 + 1) * (l2 + 1) * sizeof(int));
37+
int **dp = malloc((l1 + 1) * sizeof(int *));
38+
39+
for (i = 0; i < l1 + 1; i++) {
40+
dp[i] = table + i * (l2 + 1);
4041
}
4142

42-
for (i = 0; i < len2 + 1; i++) {
43+
dp[0][0] = 0;
44+
for (i = 1; i <= l2; i++) {
4345
dp[0][i] = i;
4446
}
45-
for (i = 0; i < len1 + 1; i++) {
47+
for (i = 1; i <= l1; i++) {
4648
dp[i][0] = i;
4749
}
4850

49-
for (i = 1; i < len1 + 1; i++) {
50-
for (j = 1; j < len2 + 1; j++) {
51+
for (i = 1; i <= l1; i++) {
52+
for (j = 1; j <= l2; j++) {
5153
if (word1[i - 1] == word2[j - 1]) {
5254
dp[i][j] = dp[i - 1][j - 1];
5355
} else {
5456
dp[i][j] = 1 + min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1]));
5557
}
5658
}
5759
}
58-
return dp[len1][len2];
60+
61+
return dp[l1][l2];
5962
}
6063

6164
int main(int argc, char **argv)

0072_edit_distance/edit_distance.cc

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Solution {
2626
left_up = up;
2727
}
2828
}
29+
2930
return dp[l2];
3031
}
3132
};

0300_longest_increasing_subsequence/lis.c

+29-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
#include <stdlib.h>
33

44

5+
static int max(int a, int b)
6+
{
7+
return a > b ? a : b;
8+
}
9+
510
static int binary_search(int *nums, int lo, int hi, int target)
611
{
712
while (lo + 1 < hi) {
@@ -15,7 +20,9 @@ static int binary_search(int *nums, int lo, int hi, int target)
1520
return hi;
1621
}
1722

18-
int lengthOfLIS(int* nums, int numsSize){
23+
int lengthOfLIS(int* nums, int numsSize)
24+
{
25+
#if 0
1926
int i, piles = 0;
2027
int *tops = malloc(numsSize * sizeof(int));
2128
for (i = 0; i < numsSize; i++) {
@@ -26,6 +33,27 @@ int lengthOfLIS(int* nums, int numsSize){
2633
tops[pos] = nums[i];
2734
}
2835
return piles;
36+
#else
37+
int i, j, res = 0;
38+
int *dp = malloc(numsSize * sizeof(int));
39+
40+
/* dp array records subsequence length of nums[0...i], so we need to
41+
* initialize each dp[i] with one element length in the beginning. */
42+
for (i = 0; i < numsSize; i++) {
43+
dp[i] = 1;
44+
for (j = 0; j < i; j++) {
45+
if (nums[j] > nums[i]) {
46+
dp[i] = max(dp[i], dp[j] + 1);
47+
}
48+
}
49+
}
50+
51+
for (i = 0; i < numsSize; i++) {
52+
res = max(res, dp[i]);
53+
}
54+
55+
return res;
56+
#endif
2957
}
3058

3159
int main(int argc, char **argv)

0322_coin_change/coin_change.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ int coinChange(int* coins, int coinsSize, int amount)
66
{
77
int i, j;
88
int *dp = malloc((amount + 1) * sizeof(int));
9-
for (i = 1; i <= amount; i++) {
10-
/* INT_MAX */
11-
dp[i] = amount + 1;
12-
}
139

10+
/* The dp array records minimum coin number corresponding to the
11+
* amount of coins. So we need to initialize each dp[i] with
12+
* amount + 1 as an invalid value */
1413
dp[0] = 0;
1514
for (i = 1; i <= amount; i++) {
15+
/* initialized with INT_MAX */
16+
dp[i] = amount + 1;
1617
for (j = 0; j < coinsSize; j++) {
1718
if (i - coins[j] >= 0) {
1819
int tmp = 1 + dp[i - coins[j]];

0 commit comments

Comments
 (0)