diff --git a/problems/0045.跳跃游戏II.md b/problems/0045.跳跃游戏II.md index d290f55e..c6433ea8 100644 --- a/problems/0045.跳跃游戏II.md +++ b/problems/0045.跳跃游戏II.md @@ -492,7 +492,34 @@ impl Solution { } } ``` +### C + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int jump(int* nums, int numsSize) { + if(numsSize == 1){ + return 0; + } + int count = 0; + // 记录当前能走的最远距离 + int curDistance = 0; + // 记录下一步能走的最远距离 + int nextDistance = 0; + for(int i = 0; i < numsSize; i++){ + nextDistance = max(i + nums[i], nextDistance); + // 下标到了当前的最大距离 + if(i == nextDistance){ + count++; + curDistance = nextDistance; + } + } + return count; +} +``` + ### C# + ```csharp // 版本二 public class Solution @@ -518,3 +545,4 @@ public class Solution + diff --git a/problems/0056.合并区间.md b/problems/0056.合并区间.md index 122e783a..f9d6f654 100644 --- a/problems/0056.合并区间.md +++ b/problems/0056.合并区间.md @@ -336,7 +336,49 @@ impl Solution { } } ``` +### C + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +// 根据左边界进行排序 +int cmp(const void * var1, const void * var2){ + int *v1 = *(int **) var1; + int *v2 = *(int **) var2; + return v1[0] - v2[0]; +} + +int** merge(int** intervals, int intervalsSize, int* intervalsColSize, int* returnSize, int** returnColumnSizes) { + int ** result = malloc(sizeof (int *) * intervalsSize); + * returnColumnSizes = malloc(sizeof (int ) * intervalsSize); + for(int i = 0; i < intervalsSize; i++){ + result[i] = malloc(sizeof (int ) * 2); + } + qsort(intervals, intervalsSize, sizeof (int *), cmp); + int count = 0; + for(int i = 0; i < intervalsSize; i++){ + // 记录区间的左右边界 + int L = intervals[i][0], R = intervals[i][1]; + // 如果count为0或者前一区间的右区间小于此时的左边,加入结果中 + if (count == 0 || result[count - 1][1] < L) { + returnColumnSizes[0][count] = 2; + result[count][0] = L; + result[count][1] = R; + count++; + } + else{ // 更新右边界的值 + result[count - 1][1] = max(R, result[count - 1][1]); + } + } + *returnSize = count; + return result; +} +``` + + + ### C# + ```csharp public class Solution { @@ -367,4 +409,3 @@ public class Solution - diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md index cbdf40e8..fb548cbc 100644 --- a/problems/0121.买卖股票的最佳时机.md +++ b/problems/0121.买卖股票的最佳时机.md @@ -531,6 +531,52 @@ public class Solution } ``` +### C: + +> 贪心 + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define min(a, b) ((a) > (b) ? (b) : (a)) + +int maxProfit(int* prices, int pricesSize) { + int low = INT_MIN; + int result = 0; + for(int i = 0; i < pricesSize; i++){ + low = min(low, prices[i]); + result = max(result, prices[i] - low); + } + return result; +} +``` + +> 动态规划 + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int maxProfit(int* prices, int pricesSize){ + if(pricesSize == 0){ + return 0; + } + // dp初始化 + int ** dp = malloc(sizeof (int *) * pricesSize); + for(int i = 0; i < pricesSize; i++){ + dp[i] = malloc(sizeof (int ) * 2); + } + // 下标0表示持有股票的情况下的最大现金,下标1表示不持有股票的情况下获得的最大现金 + dp[0][0] = -prices[0]; + dp[0][1] = 0; + for(int i = 1; i < pricesSize; i++){ + dp[i][0] = max(dp[i - 1][0], - prices[i]); + dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]); + } + return dp[pricesSize - 1][1]; +} +``` + + + ### Rust: > 贪心 @@ -568,4 +614,3 @@ impl Solution { - diff --git a/problems/0122.买卖股票的最佳时机II(动态规划).md b/problems/0122.买卖股票的最佳时机II(动态规划).md index 6e08b57c..24c7f168 100644 --- a/problems/0122.买卖股票的最佳时机II(动态规划).md +++ b/problems/0122.买卖股票的最佳时机II(动态规划).md @@ -365,6 +365,49 @@ public class Solution } ``` +### C: + +> 动态规划 + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int maxProfit(int* prices, int pricesSize){ + int **dp = malloc(sizeof (int *) * pricesSize); + for (int i = 0; i < pricesSize; ++i) { + dp[i] = malloc(sizeof (int ) * 2); + } + // 0表示持有该股票所得最大,1表示不持有所得最大 + dp[0][0] = -prices[0]; + dp[0][1] = 0; + for (int i = 1; i < pricesSize; ++i) { + dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]); + dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]); + } + return dp[pricesSize - 1][1]; +} +``` + +> 贪心 + +```c +int maxProfit(int* prices, int pricesSize) { + if(pricesSize == 0){ + return 0; + } + int result = 0; + for(int i = 1; i < pricesSize; i++){ + // 如果今天股票价格大于昨天,代表有利润 + if(prices[i] > prices[i - 1]){ + result += prices[i] - prices[i - 1]; + } + } + return result; +} +``` + + + ### Rust: > 贪心 @@ -416,3 +459,4 @@ impl Solution { + diff --git a/problems/0123.买卖股票的最佳时机III.md b/problems/0123.买卖股票的最佳时机III.md index 72dd9042..18f19c51 100644 --- a/problems/0123.买卖股票的最佳时机III.md +++ b/problems/0123.买卖股票的最佳时机III.md @@ -413,6 +413,34 @@ function maxProfit(prices: number[]): number { }; ``` +### C: + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) +#define min(a, b) ((a) > (b) ? (b) : (a)) + +int maxProfit(int* prices, int pricesSize) { + int buy1 = prices[0], buy2 = prices[0]; + int profit1 = 0, profit2 = 0; + for (int i = 0; i < pricesSize; ++i) { + // 寻找最低点买入 + buy1 = min(buy1, prices[i]); + // 找到第一次交易的最大盈利,并不断维护这一最大值 + profit1 = max(profit1, prices[i] - buy1); + + // 寻找第二次交易的最低投资点,并且考虑前一次交易的成本 + // 当前价格 - 第一次操作的盈利=新的投入成本( + // 为了让盈利最大,要寻找最小的成本) + buy2 = min(buy2, prices[i] - profit1); + // 第二次卖出后的盈利:当前价格减去成本,不断维护这一最大的总利润 + profit2 = max(profit2, prices[i] - buy2); + } + return profit2; +} +``` + + + ### Rust: > 版本一 @@ -465,4 +493,3 @@ impl Solution { - diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md index d93288ae..a3d59ec7 100644 --- a/problems/0139.单词拆分.md +++ b/problems/0139.单词拆分.md @@ -498,6 +498,33 @@ function wordBreak(s: string, wordDict: string[]): boolean { }; ``` +### C + +```c +bool wordBreak(char* s, char** wordDict, int wordDictSize) { + int len = strlen(s); + // 初始化 + bool dp[len + 1]; + memset(dp, false, sizeof (dp)); + dp[0] = true; + for (int i = 1; i < len + 1; ++i) { + for(int j = 0; j < wordDictSize; j++){ + int wordLen = strlen(wordDict[j]); + // 分割点是由i和字典单词长度决定 + int k = i - wordLen; + if(k < 0){ + continue; + } + // 这里注意要限制长度,故用strncmp + dp[i] = (dp[k] && !strncmp(s + k, wordDict[j], wordLen)) || dp[i]; + } + } + return dp[len]; +} +``` + + + ### Rust: ```rust @@ -521,4 +548,3 @@ impl Solution { - diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md index e4c5c484..2521749f 100644 --- a/problems/0188.买卖股票的最佳时机IV.md +++ b/problems/0188.买卖股票的最佳时机IV.md @@ -474,6 +474,34 @@ function maxProfit(k: number, prices: number[]): number { }; ``` +### C: + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int maxProfit(int k, int* prices, int pricesSize) { + if(pricesSize == 0){ + return 0; + } + + int dp[pricesSize][2 * k + 1]; + memset(dp, 0, sizeof(int) * pricesSize * (2 * k + 1)); + for (int j = 1; j < 2 * k; j += 2) { + dp[0][j] = -prices[0]; + } + + for (int i = 1;i < pricesSize; i++) {//枚举股票 + for (int j = 0; j < 2 * k - 1; j += 2) { //更新每一次买入卖出 + dp[i][j + 1] = max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i]); + dp[i][j + 2] = max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i]); + } + } + return dp[pricesSize - 1][2 * k]; +} +``` + + + ### Rust: ```rust @@ -529,3 +557,4 @@ impl Solution { + diff --git a/problems/0198.打家劫舍.md b/problems/0198.打家劫舍.md index a7bc4c99..480222ef 100644 --- a/problems/0198.打家劫舍.md +++ b/problems/0198.打家劫舍.md @@ -315,6 +315,31 @@ function rob(nums: number[]): number { }; ``` +### C + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int rob(int* nums, int numsSize) { + if(numsSize == 0){ + return 0; + } + if(numsSize == 1){ + return nums[0]; + } + // dp初始化 + int dp[numsSize]; + dp[0] = nums[0]; + dp[1] = max(nums[0], nums[1]); + for(int i = 2; i < numsSize; i++){ + dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]); + } + return dp[numsSize - 1]; +} +``` + + + ### Rust: ```rust @@ -339,3 +364,4 @@ impl Solution { + diff --git a/problems/0213.打家劫舍II.md b/problems/0213.打家劫舍II.md index 385c5867..ba996f2b 100644 --- a/problems/0213.打家劫舍II.md +++ b/problems/0213.打家劫舍II.md @@ -308,6 +308,34 @@ function robRange(nums: number[], start: number, end: number): number { } ``` +### C + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +// 198.打家劫舍的逻辑 +int robRange(int* nums, int start, int end, int numsSize) { + if (end == start) return nums[start]; + int dp[numsSize]; + dp[start] = nums[start]; + dp[start + 1] = max(nums[start], nums[start + 1]); + for (int i = start + 2; i <= end; i++) { + dp[i] = max(dp[i - 2] + nums[i], dp[i - 1]); + } + return dp[end]; +} + +int rob(int* nums, int numsSize) { + if (numsSize == 0) return 0; + if (numsSize == 1) return nums[0]; + int result1 = robRange(nums, 0, numsSize - 2, numsSize); // 情况二 + int result2 = robRange(nums, 1, numsSize - 1, numsSize); // 情况三 + return max(result1, result2); +} +``` + + + ### Rust: ```rust @@ -343,4 +371,3 @@ impl Solution { - diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index a0e62d48..f7c06dbd 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -389,6 +389,30 @@ function numSquares(n: number): number { }; ``` +## C + +```c +#define min(a, b) ((a) > (b) ? (b) : (a)) + +int numSquares(int n) { + int* dp = (int*)malloc(sizeof(int) * (n + 1)); + for (int j = 0; j < n + 1; j++) { + dp[j] = INT_MAX; + } + dp[0] = 0; + // 遍历背包 + for (int i = 0; i <= n; ++i) { + // 遍历物品 + for (int j = 1; j * j <= i; ++j) { + dp[i] = min(dp[i - j * j] + 1, dp[i]); + } + } + return dp[n]; +} +``` + + + ### Rust: ```rust @@ -439,4 +463,3 @@ impl Solution { - diff --git a/problems/0300.最长上升子序列.md b/problems/0300.最长上升子序列.md index 64f75291..6d82eae1 100644 --- a/problems/0300.最长上升子序列.md +++ b/problems/0300.最长上升子序列.md @@ -288,6 +288,36 @@ function lengthOfLIS(nums: number[]): number { }; ``` +### C: + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int lengthOfLIS(int* nums, int numsSize) { + if(numsSize <= 1){ + return numsSize; + } + int dp[numsSize]; + for(int i = 0; i < numsSize; i++){ + dp[i]=1; + } + int result = 1; + for (int i = 1; i < numsSize; ++i) { + for (int j = 0; j < i; ++j) { + if(nums[i] > nums[j]){ + dp[i] = max(dp[i], dp[j] + 1); + } + if(dp[i] > result){ + result = dp[i]; + } + } + } + return result; +} +``` + + + ### Rust: ```rust @@ -311,4 +341,3 @@ pub fn length_of_lis(nums: Vec) -> i32 { - diff --git a/problems/0309.最佳买卖股票时机含冷冻期.md b/problems/0309.最佳买卖股票时机含冷冻期.md index 0eb66fb5..9dc35bdf 100644 --- a/problems/0309.最佳买卖股票时机含冷冻期.md +++ b/problems/0309.最佳买卖股票时机含冷冻期.md @@ -457,6 +457,40 @@ function maxProfit(prices: number[]): number { }; ``` +### C: + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +/** + * 状态一:持有股票状态(今天买入股票, + * 或者是之前就买入了股票然后没有操作,一直持有) + * 不持有股票状态,这里就有两种卖出股票状态 + * 状态二:保持卖出股票的状态(两天前就卖出了股票,度过一天冷冻期。 + * 或者是前一天就是卖出股票状态,一直没操作) + * 状态三:今天卖出股票 + * 状态四:今天为冷冻期状态,但冷冻期状态不可持续,只有一天! + + */ +int maxProfit(int* prices, int pricesSize) { + if(pricesSize == 0){ + return 0; + } + int dp[pricesSize][4]; + memset(dp, 0, sizeof (int ) * pricesSize * 4); + dp[0][0] = -prices[0]; + for (int i = 1; i < pricesSize; ++i) { + dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][1] - prices[i], dp[i - 1][3] - prices[i])); + dp[i][1] = max(dp[i - 1][1], dp[i - 1][3]); + dp[i][2] = dp[i - 1][0] + prices[i]; + dp[i][3] = dp[i - 1][2]; + } + return max(dp[pricesSize - 1][1], max(dp[pricesSize - 1][2], dp[pricesSize - 1][3])); +} +``` + + + ### Rust: ```rust @@ -486,4 +520,3 @@ impl Solution { - diff --git a/problems/0322.零钱兑换.md b/problems/0322.零钱兑换.md index eae4ab3a..156b5ff3 100644 --- a/problems/0322.零钱兑换.md +++ b/problems/0322.零钱兑换.md @@ -352,6 +352,35 @@ func min(a, b int) int { ``` +## C + +```c +#define min(a, b) ((a) > (b) ? (b) : (a)) + +int coinChange(int* coins, int coinsSize, int amount) { + int* dp = (int*)malloc(sizeof(int) * (amount + 1)); + for (int j = 0; j < amount + 1; j++) { + dp[j] = INT_MAX; + } + dp[0] = 0; + // 遍历背包 + for(int i = 0; i <= amount; i++){ + // 遍历物品 + for(int j = 0; j < coinsSize; j++){ + if(i - coins[j] >= 0 && dp[i - coins[j]] != INT_MAX){ + dp[i] = min(dp[i], dp[i - coins[j]] + 1); + } + } + } + if(dp[amount] == INT_MAX){ + return -1; + } + return dp[amount]; +} +``` + + + ### Rust: ```rust @@ -474,4 +503,3 @@ function coinChange(coins: number[], amount: number): number { - diff --git a/problems/0337.打家劫舍III.md b/problems/0337.打家劫舍III.md index f616ec74..61b9f99c 100644 --- a/problems/0337.打家劫舍III.md +++ b/problems/0337.打家劫舍III.md @@ -490,6 +490,33 @@ function robNode(node: TreeNode | null): MaxValueArr { } ``` +### C + +```c +int *robTree(struct TreeNode *node) { + int* amounts = (int*) malloc(sizeof(int) * 2); + memset(amounts, 0, sizeof(int) * 2); + if(node == NULL){ + return amounts; + } + int * left = robTree(node->left); + int * right = robTree(node->right); + // 偷当前节点 + amounts[1] = node->val + left[0] + right[0]; + // 不偷当前节点 + amounts[0] = max(left[0], left[1]) + max(right[0], right[1]); + return amounts; +} + +int rob(struct TreeNode* root) { + int * dp = robTree(root); + // 0代表不偷当前节点可以获得的最大值,1表示偷当前节点可以获取的最大值 + return max(dp[0], dp[1]); +} +``` + + + ### Rust 动态规划: @@ -523,4 +550,3 @@ impl Solution { - diff --git a/problems/0377.组合总和Ⅳ.md b/problems/0377.组合总和Ⅳ.md index a840ec9b..6f81bffe 100644 --- a/problems/0377.组合总和Ⅳ.md +++ b/problems/0377.组合总和Ⅳ.md @@ -312,7 +312,28 @@ impl Solution { } } ``` +### C + +```c +int combinationSum4(int* nums, int numsSize, int target) { + int dp[target + 1]; + memset(dp, 0, sizeof (dp )); + dp[0] = 1; + for(int i = 0; i <= target; i++){ + for(int j = 0; j < numsSize; j++){ + if(i - nums[j] >= 0 && dp[i] < INT_MAX - dp[i - nums[j]]){ + dp[i] += dp[i - nums[j]]; + } + } + } + return dp[target]; +} +``` + + + ### C# + ```csharp public class Solution { @@ -340,4 +361,3 @@ public class Solution - diff --git a/problems/0435.无重叠区间.md b/problems/0435.无重叠区间.md index cf9996dd..b668e860 100644 --- a/problems/0435.无重叠区间.md +++ b/problems/0435.无重叠区间.md @@ -441,7 +441,37 @@ impl Solution { } } ``` +### C + +```c +// 按照区间右边界排序 +int cmp(const void * var1, const void * var2){ + return (*(int **) var1)[1] - (*(int **) var2)[1]; +} + +int eraseOverlapIntervals(int** intervals, int intervalsSize, int* intervalsColSize) { + if(intervalsSize == 0){ + return 0; + } + qsort(intervals, intervalsSize, sizeof (int *), cmp); + // 记录非重叠的区间数量 + int count = 1; + // 记录区间分割点 + int end = intervals[0][1]; + for(int i = 1; i < intervalsSize; i++){ + if(end <= intervals[i][0]){ + end = intervals[i][1]; + count++; + } + } + return intervalsSize - count; +} +``` + + + ### C# + ```csharp public class Solution { @@ -468,3 +498,4 @@ public class Solution + diff --git a/problems/0474.一和零.md b/problems/0474.一和零.md index 904d941e..af50fa5c 100644 --- a/problems/0474.一和零.md +++ b/problems/0474.一和零.md @@ -533,7 +533,41 @@ impl Solution { } } ``` +## C + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int findMaxForm(char** strs, int strsSize, int m, int n) { + int dp[m + 1][n + 1]; + memset(dp, 0, sizeof (int ) * (m + 1) * (n + 1)); + for(int i = 0; i < strsSize; i++){ + // 统计0和1的数量 + int count0 = 0; + int count1 = 0; + char *str = strs[i]; + while (*str != '\0'){ + if(*str == '0'){ + count0++; + } else{ + count1++; + } + str++; + } + for(int j = m; j >= count0; j--){ + for(int k = n; k >= count1; k--){ + dp[j][k] = max(dp[j][k], dp[j - count0][k - count1] + 1); + } + } + } + return dp[m][n]; +} +``` + + + ### C# + ```csharp public class Solution { diff --git a/problems/0494.目标和.md b/problems/0494.目标和.md index e7a05d45..02edad4d 100644 --- a/problems/0494.目标和.md +++ b/problems/0494.目标和.md @@ -585,7 +585,44 @@ impl Solution { } } ``` +## C + +```c +int getSum(int * nums, int numsSize){ + int sum = 0; + for(int i = 0; i < numsSize; i++){ + sum += nums[i]; + } + return sum; +} + +int findTargetSumWays(int* nums, int numsSize, int target) { + int sum = getSum(nums, numsSize); + int diff = sum - target; + // 两种情况不满足 + if(diff < 0 || diff % 2 != 0){ + return 0; + } + int bagSize = diff / 2; + int dp[numsSize + 1][bagSize + 1]; + dp[0][0] = 1; + for(int i = 1; i <= numsSize; i++){ + int num = nums[i - 1]; + for(int j = 0; j <= bagSize; j++){ + dp[i][j] = dp[i - 1][j]; + if(j >= num){ + dp[i][j] += dp[i - 1][j - num]; + } + } + } + return dp[numsSize][bagSize]; +} +``` + + + ### C# + ```csharp public class Solution { diff --git a/problems/0518.零钱兑换II.md b/problems/0518.零钱兑换II.md index da1c4755..59fdf6cd 100644 --- a/problems/0518.零钱兑换II.md +++ b/problems/0518.零钱兑换II.md @@ -353,7 +353,28 @@ object Solution { } } ``` +## C + +```c +int change(int amount, int* coins, int coinsSize) { + int dp[amount + 1]; + memset(dp, 0, sizeof (dp)); + dp[0] = 1; + // 遍历物品 + for(int i = 0; i < coinsSize; i++){ + // 遍历背包 + for(int j = coins[i]; j <= amount; j++){ + dp[j] += dp[j - coins[i]]; + } + } + return dp[amount]; +} +``` + + + ### C# + ```csharp public class Solution { @@ -378,3 +399,4 @@ public class Solution + diff --git a/problems/0674.最长连续递增序列.md b/problems/0674.最长连续递增序列.md index 485e321c..ece62944 100644 --- a/problems/0674.最长连续递增序列.md +++ b/problems/0674.最长连续递增序列.md @@ -425,6 +425,57 @@ function findLengthOfLCIS(nums: number[]): number { }; ``` +### C: + +> 动态规划: + +```c +int findLengthOfLCIS(int* nums, int numsSize) { + if(numsSize == 0){ + return 0; + } + int dp[numsSize]; + for(int i = 0; i < numsSize; i++){ + dp[i] = 1; + } + int result = 1; + for (int i = 1; i < numsSize; ++i) { + if(nums[i] > nums[i - 1]){ + dp[i] = dp[i - 1] + 1; + } + if(dp[i] > result){ + result = dp[i]; + } + } + return result; +} +``` + + + +> 贪心: + +```c +int findLengthOfLCIS(int* nums, int numsSize) { + int result = 1; + int count = 1; + if(numsSize == 0){ + return result; + } + for (int i = 1; i < numsSize; ++i) { + if(nums[i] > nums[i - 1]){ + count++; + } else{ + count = 1; + } + if(count > result){ + result = count; + } + } + return result; +} +``` + @@ -432,4 +483,3 @@ function findLengthOfLCIS(nums: number[]): number { - diff --git a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md index 7e8e3d7c..88ba9271 100644 --- a/problems/0714.买卖股票的最佳时机含手续费(动态规划).md +++ b/problems/0714.买卖股票的最佳时机含手续费(动态规划).md @@ -247,7 +247,29 @@ function maxProfit(prices: number[], fee: number): number { }; ``` +### C: + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +// dp[i][0] 表示第i天持有股票所省最多现金。 +// dp[i][1] 表示第i天不持有股票所得最多现金 +int maxProfit(int* prices, int pricesSize, int fee) { + int dp[pricesSize][2]; + dp[0][0] = -prices[0]; + dp[0][1] = 0; + for (int i = 1; i < pricesSize; ++i) { + dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]); + dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee); + } + return dp[pricesSize - 1][1]; +} +``` + + + ### Rust: + **贪心** ```Rust @@ -304,3 +326,4 @@ impl Solution { + diff --git a/problems/0718.最长重复子数组.md b/problems/0718.最长重复子数组.md index 272cf2b2..e00b3ded 100644 --- a/problems/0718.最长重复子数组.md +++ b/problems/0718.最长重复子数组.md @@ -560,10 +560,30 @@ impl Solution { } ``` +### C: + +```c +int findLength(int* nums1, int nums1Size, int* nums2, int nums2Size) { + int dp[nums1Size + 1][nums2Size + 1]; + memset(dp, 0, sizeof(dp)); + int result = 0; + for (int i = 1; i <= nums1Size; ++i) { + for (int j = 1; j <= nums2Size; ++j) { + if(nums1[i - 1] == nums2[j - 1]){ + dp[i][j] = dp[i - 1][j - 1] + 1; + } + if(dp[i][j] > result){ + result = dp[i][j]; + } + } + } + return result; +} +``` +

- diff --git a/problems/0738.单调递增的数字.md b/problems/0738.单调递增的数字.md index 400dc90d..4a8a6e08 100644 --- a/problems/0738.单调递增的数字.md +++ b/problems/0738.单调递增的数字.md @@ -392,7 +392,33 @@ impl Solution { } } ``` +### C + +```c +int monotoneIncreasingDigits(int n) { + char str[11]; + // 将数字转换为字符串 + sprintf(str, "%d", n); + int len = strlen(str); + int flag = strlen(str); + for(int i = len - 1; i > 0; i--){ + if(str[i] < str[i - 1]){ + str[i - 1]--; + flag = i; + } + } + for(int i = flag; i < len; i++){ + str[i] = '9'; + } + // 字符串转数字 + return atoi(str); +} +``` + + + ### C# + ```csharp public class Solution { @@ -421,4 +447,3 @@ public class Solution - diff --git a/problems/0763.划分字母区间.md b/problems/0763.划分字母区间.md index 4e9ec578..8b0ca7b8 100644 --- a/problems/0763.划分字母区间.md +++ b/problems/0763.划分字母区间.md @@ -404,7 +404,38 @@ impl Solution { } } ``` +### C + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int* partitionLabels(char* s, int* returnSize) { + // 记录每个字符最远出现的位置 + int last[26] = {0}; + int len = strlen(s); + for (int i = 0; i < len; ++i) { + last[s[i] - 'a'] = i; + } + int left = 0, right = 0; + int * partition = malloc(sizeof (int ) * len); + // 初始化值 + *returnSize = 0; + for(int i = 0; i < len; i++){ + right = max(right, last[s[i] - 'a']); + // 到达最远位置,加入答案,并且更新左边下标 + if(i == right){ + partition[(*returnSize)++] = right - left + 1; + left = i + 1; + } + } + return partition; +} +``` + + + ### C# + ```csharp public class Solution { @@ -435,4 +466,3 @@ public class Solution - diff --git a/problems/1143.最长公共子序列.md b/problems/1143.最长公共子序列.md index f33391c3..12bd90f8 100644 --- a/problems/1143.最长公共子序列.md +++ b/problems/1143.最长公共子序列.md @@ -376,10 +376,32 @@ impl Solution { } ``` +### C: + +```c +#define max(a, b) ((a) > (b) ? (a) : (b)) + +int longestCommonSubsequence(char* text1, char* text2) { + int text1Len = strlen(text1); + int text2Len = strlen(text2); + int dp[text1Len + 1][text2Len + 1]; + memset(dp, 0, sizeof (dp)); + for (int i = 1; i <= text1Len; ++i) { + for (int j = 1; j <= text2Len; ++j) { + if(text1[i - 1] == text2[j - 1]){ + dp[i][j] = dp[i - 1][j - 1] + 1; + } else{ + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); + } + } + } + return dp[text1Len][text2Len]; +} +``` +

-