Skip to content

Commit c342d7e

Browse files
Merge pull request youngyangyang04#2452 from LYT0905/master
Update 0045.跳跃游戏II.md
2 parents 313a7dd + fac6899 commit c342d7e

25 files changed

+762
-16
lines changed

problems/0045.跳跃游戏II.md

+28
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,34 @@ impl Solution {
492492
}
493493
}
494494
```
495+
### C
496+
497+
```c
498+
#define max(a, b) ((a) > (b) ? (a) : (b))
499+
500+
int jump(int* nums, int numsSize) {
501+
if(numsSize == 1){
502+
return 0;
503+
}
504+
int count = 0;
505+
// 记录当前能走的最远距离
506+
int curDistance = 0;
507+
// 记录下一步能走的最远距离
508+
int nextDistance = 0;
509+
for(int i = 0; i < numsSize; i++){
510+
nextDistance = max(i + nums[i], nextDistance);
511+
// 下标到了当前的最大距离
512+
if(i == nextDistance){
513+
count++;
514+
curDistance = nextDistance;
515+
}
516+
}
517+
return count;
518+
}
519+
```
520+
495521
### C#
522+
496523
```csharp
497524
// 版本二
498525
public class Solution
@@ -518,3 +545,4 @@ public class Solution
518545
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
519546
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
520547
</a>
548+

problems/0056.合并区间.md

+42-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,49 @@ impl Solution {
336336
}
337337
}
338338
```
339+
### C
340+
341+
```c
342+
#define max(a, b) ((a) > (b) ? (a) : (b))
343+
344+
// 根据左边界进行排序
345+
int cmp(const void * var1, const void * var2){
346+
int *v1 = *(int **) var1;
347+
int *v2 = *(int **) var2;
348+
return v1[0] - v2[0];
349+
}
350+
351+
int** merge(int** intervals, int intervalsSize, int* intervalsColSize, int* returnSize, int** returnColumnSizes) {
352+
int ** result = malloc(sizeof (int *) * intervalsSize);
353+
* returnColumnSizes = malloc(sizeof (int ) * intervalsSize);
354+
for(int i = 0; i < intervalsSize; i++){
355+
result[i] = malloc(sizeof (int ) * 2);
356+
}
357+
qsort(intervals, intervalsSize, sizeof (int *), cmp);
358+
int count = 0;
359+
for(int i = 0; i < intervalsSize; i++){
360+
// 记录区间的左右边界
361+
int L = intervals[i][0], R = intervals[i][1];
362+
// 如果count为0或者前一区间的右区间小于此时的左边,加入结果中
363+
if (count == 0 || result[count - 1][1] < L) {
364+
returnColumnSizes[0][count] = 2;
365+
result[count][0] = L;
366+
result[count][1] = R;
367+
count++;
368+
}
369+
else{ // 更新右边界的值
370+
result[count - 1][1] = max(R, result[count - 1][1]);
371+
}
372+
}
373+
*returnSize = count;
374+
return result;
375+
}
376+
```
377+
378+
379+
339380
### C#
381+
340382
```csharp
341383
public class Solution
342384
{
@@ -367,4 +409,3 @@ public class Solution
367409
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
368410
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
369411
</a>
370-

problems/0121.买卖股票的最佳时机.md

+46-1
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,52 @@ public class Solution
531531
}
532532
```
533533

534+
### C:
535+
536+
> 贪心
537+
538+
```c
539+
#define max(a, b) ((a) > (b) ? (a) : (b))
540+
#define min(a, b) ((a) > (b) ? (b) : (a))
541+
542+
int maxProfit(int* prices, int pricesSize) {
543+
int low = INT_MIN;
544+
int result = 0;
545+
for(int i = 0; i < pricesSize; i++){
546+
low = min(low, prices[i]);
547+
result = max(result, prices[i] - low);
548+
}
549+
return result;
550+
}
551+
```
552+
553+
> 动态规划
554+
555+
```c
556+
#define max(a, b) ((a) > (b) ? (a) : (b))
557+
558+
int maxProfit(int* prices, int pricesSize){
559+
if(pricesSize == 0){
560+
return 0;
561+
}
562+
// dp初始化
563+
int ** dp = malloc(sizeof (int *) * pricesSize);
564+
for(int i = 0; i < pricesSize; i++){
565+
dp[i] = malloc(sizeof (int ) * 2);
566+
}
567+
// 下标0表示持有股票的情况下的最大现金,下标1表示不持有股票的情况下获得的最大现金
568+
dp[0][0] = -prices[0];
569+
dp[0][1] = 0;
570+
for(int i = 1; i < pricesSize; i++){
571+
dp[i][0] = max(dp[i - 1][0], - prices[i]);
572+
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
573+
}
574+
return dp[pricesSize - 1][1];
575+
}
576+
```
577+
578+
579+
534580
### Rust:
535581

536582
> 贪心
@@ -568,4 +614,3 @@ impl Solution {
568614
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
569615
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
570616
</a>
571-

problems/0122.买卖股票的最佳时机II(动态规划).md

+44
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,49 @@ public class Solution
365365
}
366366
```
367367

368+
### C:
369+
370+
> 动态规划
371+
372+
```c
373+
#define max(a, b) ((a) > (b) ? (a) : (b))
374+
375+
int maxProfit(int* prices, int pricesSize){
376+
int **dp = malloc(sizeof (int *) * pricesSize);
377+
for (int i = 0; i < pricesSize; ++i) {
378+
dp[i] = malloc(sizeof (int ) * 2);
379+
}
380+
// 0表示持有该股票所得最大,1表示不持有所得最大
381+
dp[0][0] = -prices[0];
382+
dp[0][1] = 0;
383+
for (int i = 1; i < pricesSize; ++i) {
384+
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
385+
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
386+
}
387+
return dp[pricesSize - 1][1];
388+
}
389+
```
390+
391+
> 贪心
392+
393+
```c
394+
int maxProfit(int* prices, int pricesSize) {
395+
if(pricesSize == 0){
396+
return 0;
397+
}
398+
int result = 0;
399+
for(int i = 1; i < pricesSize; i++){
400+
// 如果今天股票价格大于昨天,代表有利润
401+
if(prices[i] > prices[i - 1]){
402+
result += prices[i] - prices[i - 1];
403+
}
404+
}
405+
return result;
406+
}
407+
```
408+
409+
410+
368411
### Rust:
369412

370413
> 贪心
@@ -416,3 +459,4 @@ impl Solution {
416459
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
417460
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
418461
</a>
462+

problems/0123.买卖股票的最佳时机III.md

+28-1
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,34 @@ function maxProfit(prices: number[]): number {
413413
};
414414
```
415415

416+
### C:
417+
418+
```c
419+
#define max(a, b) ((a) > (b) ? (a) : (b))
420+
#define min(a, b) ((a) > (b) ? (b) : (a))
421+
422+
int maxProfit(int* prices, int pricesSize) {
423+
int buy1 = prices[0], buy2 = prices[0];
424+
int profit1 = 0, profit2 = 0;
425+
for (int i = 0; i < pricesSize; ++i) {
426+
// 寻找最低点买入
427+
buy1 = min(buy1, prices[i]);
428+
// 找到第一次交易的最大盈利,并不断维护这一最大值
429+
profit1 = max(profit1, prices[i] - buy1);
430+
431+
// 寻找第二次交易的最低投资点,并且考虑前一次交易的成本
432+
// 当前价格 - 第一次操作的盈利=新的投入成本(
433+
// 为了让盈利最大,要寻找最小的成本)
434+
buy2 = min(buy2, prices[i] - profit1);
435+
// 第二次卖出后的盈利:当前价格减去成本,不断维护这一最大的总利润
436+
profit2 = max(profit2, prices[i] - buy2);
437+
}
438+
return profit2;
439+
}
440+
```
441+
442+
443+
416444
### Rust:
417445
418446
> 版本一
@@ -465,4 +493,3 @@ impl Solution {
465493
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
466494
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
467495
</a>
468-

problems/0139.单词拆分.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,33 @@ function wordBreak(s: string, wordDict: string[]): boolean {
498498
};
499499
```
500500

501+
### C
502+
503+
```c
504+
bool wordBreak(char* s, char** wordDict, int wordDictSize) {
505+
int len = strlen(s);
506+
// 初始化
507+
bool dp[len + 1];
508+
memset(dp, false, sizeof (dp));
509+
dp[0] = true;
510+
for (int i = 1; i < len + 1; ++i) {
511+
for(int j = 0; j < wordDictSize; j++){
512+
int wordLen = strlen(wordDict[j]);
513+
// 分割点是由i和字典单词长度决定
514+
int k = i - wordLen;
515+
if(k < 0){
516+
continue;
517+
}
518+
// 这里注意要限制长度,故用strncmp
519+
dp[i] = (dp[k] && !strncmp(s + k, wordDict[j], wordLen)) || dp[i];
520+
}
521+
}
522+
return dp[len];
523+
}
524+
```
525+
526+
527+
501528
### Rust:
502529
503530
```rust
@@ -521,4 +548,3 @@ impl Solution {
521548
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
522549
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
523550
</a>
524-

problems/0188.买卖股票的最佳时机IV.md

+29
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,34 @@ function maxProfit(k: number, prices: number[]): number {
474474
};
475475
```
476476

477+
### C:
478+
479+
```c
480+
#define max(a, b) ((a) > (b) ? (a) : (b))
481+
482+
int maxProfit(int k, int* prices, int pricesSize) {
483+
if(pricesSize == 0){
484+
return 0;
485+
}
486+
487+
int dp[pricesSize][2 * k + 1];
488+
memset(dp, 0, sizeof(int) * pricesSize * (2 * k + 1));
489+
for (int j = 1; j < 2 * k; j += 2) {
490+
dp[0][j] = -prices[0];
491+
}
492+
493+
for (int i = 1;i < pricesSize; i++) {//枚举股票
494+
for (int j = 0; j < 2 * k - 1; j += 2) { //更新每一次买入卖出
495+
dp[i][j + 1] = max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i]);
496+
dp[i][j + 2] = max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i]);
497+
}
498+
}
499+
return dp[pricesSize - 1][2 * k];
500+
}
501+
```
502+
503+
504+
477505
### Rust:
478506
479507
```rust
@@ -529,3 +557,4 @@ impl Solution {
529557
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
530558
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
531559
</a>
560+

problems/0198.打家劫舍.md

+26
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,31 @@ function rob(nums: number[]): number {
315315
};
316316
```
317317

318+
### C
319+
320+
```c
321+
#define max(a, b) ((a) > (b) ? (a) : (b))
322+
323+
int rob(int* nums, int numsSize) {
324+
if(numsSize == 0){
325+
return 0;
326+
}
327+
if(numsSize == 1){
328+
return nums[0];
329+
}
330+
// dp初始化
331+
int dp[numsSize];
332+
dp[0] = nums[0];
333+
dp[1] = max(nums[0], nums[1]);
334+
for(int i = 2; i < numsSize; i++){
335+
dp[i] = max(dp[i - 1], dp[i - 2] + nums[i]);
336+
}
337+
return dp[numsSize - 1];
338+
}
339+
```
340+
341+
342+
318343
### Rust:
319344
320345
```rust
@@ -339,3 +364,4 @@ impl Solution {
339364
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
340365
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
341366
</a>
367+

0 commit comments

Comments
 (0)