Skip to content

[pull] master from begeekmyfriend:master #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions 0072_edit_distance/edit_distance.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,34 @@ static inline int min(int a, int b)
static int minDistance(char* word1, char* word2)
{
int i, j;
int len1 = strlen(word1);
int len2 = strlen(word2);
int *table = malloc((len1 + 1) * (len2 + 1) * sizeof(int));
int **dp = malloc((len1 + 1) * sizeof(int *));
for (i = 0; i < len1 + 1; i++) {
dp[i] = table + i * (len2 + 1);
int l1 = strlen(word1);
int l2 = strlen(word2);
int *table = malloc((l1 + 1) * (l2 + 1) * sizeof(int));
int **dp = malloc((l1 + 1) * sizeof(int *));

for (i = 0; i < l1 + 1; i++) {
dp[i] = table + i * (l2 + 1);
}

for (i = 0; i < len2 + 1; i++) {
dp[0][0] = 0;
for (i = 1; i <= l2; i++) {
dp[0][i] = i;
}
for (i = 0; i < len1 + 1; i++) {
for (i = 1; i <= l1; i++) {
dp[i][0] = i;
}

for (i = 1; i < len1 + 1; i++) {
for (j = 1; j < len2 + 1; j++) {
for (i = 1; i <= l1; i++) {
for (j = 1; j <= l2; j++) {
if (word1[i - 1] == word2[j - 1]) {
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = 1 + min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1]));
}
}
}
return dp[len1][len2];

return dp[l1][l2];
}

int main(int argc, char **argv)
Expand Down
1 change: 1 addition & 0 deletions 0072_edit_distance/edit_distance.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Solution {
left_up = up;
}
}

return dp[l2];
}
};
30 changes: 29 additions & 1 deletion 0300_longest_increasing_subsequence/lis.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#include <stdlib.h>


static int max(int a, int b)
{
return a > b ? a : b;
}

static int binary_search(int *nums, int lo, int hi, int target)
{
while (lo + 1 < hi) {
Expand All @@ -15,7 +20,9 @@ static int binary_search(int *nums, int lo, int hi, int target)
return hi;
}

int lengthOfLIS(int* nums, int numsSize){
int lengthOfLIS(int* nums, int numsSize)
{
#if 0
int i, piles = 0;
int *tops = malloc(numsSize * sizeof(int));
for (i = 0; i < numsSize; i++) {
Expand All @@ -26,6 +33,27 @@ int lengthOfLIS(int* nums, int numsSize){
tops[pos] = nums[i];
}
return piles;
#else
int i, j, res = 0;
int *dp = malloc(numsSize * sizeof(int));

/* dp array records subsequence length of nums[0...i], so we need to
* initialize each dp[i] with one element length in the beginning. */
for (i = 0; i < numsSize; i++) {
dp[i] = 1;
for (j = 0; j < i; j++) {
if (nums[j] > nums[i]) {
dp[i] = max(dp[i], dp[j] + 1);
}
}
}

for (i = 0; i < numsSize; i++) {
res = max(res, dp[i]);
}

return res;
#endif
}

int main(int argc, char **argv)
Expand Down
9 changes: 5 additions & 4 deletions 0322_coin_change/coin_change.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ int coinChange(int* coins, int coinsSize, int amount)
{
int i, j;
int *dp = malloc((amount + 1) * sizeof(int));
for (i = 1; i <= amount; i++) {
/* INT_MAX */
dp[i] = amount + 1;
}

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