File tree Expand file tree Collapse file tree 2 files changed +53
-2
lines changed Expand file tree Collapse file tree 2 files changed +53
-2
lines changed Original file line number Diff line number Diff line change @@ -389,6 +389,30 @@ function numSquares(n: number): number {
389
389
};
390
390
```
391
391
392
+ ## C
393
+
394
+ ``` c
395
+ #define min (a, b ) ((a) > (b) ? (b) : (a))
396
+
397
+ int numSquares (int n) {
398
+ int* dp = (int* )malloc(sizeof(int) * (n + 1));
399
+ for (int j = 0; j < n + 1; j++) {
400
+ dp[ j] = INT_MAX;
401
+ }
402
+ dp[ 0] = 0;
403
+ // 遍历背包
404
+ for (int i = 0; i <= n; ++i) {
405
+ // 遍历物品
406
+ for (int j = 1; j * j <= i; ++j) {
407
+ dp[ i] = min(dp[ i - j * j] + 1, dp[ i] );
408
+ }
409
+ }
410
+ return dp[ n] ;
411
+ }
412
+ ```
413
+
414
+
415
+
392
416
### Rust:
393
417
394
418
```rust
@@ -439,4 +463,3 @@ impl Solution {
439
463
<a href =" https://programmercarl.com/other/kstar.html " target =" _blank " >
440
464
<img src =" ../pics/网站星球宣传海报.jpg " width =" 1000 " />
441
465
</a >
442
-
Original file line number Diff line number Diff line change @@ -352,6 +352,35 @@ func min(a, b int) int {
352
352
353
353
```
354
354
355
+ ## C
356
+
357
+ ``` c
358
+ #define min (a, b ) ((a) > (b) ? (b) : (a))
359
+
360
+ int coinChange (int* coins, int coinsSize, int amount) {
361
+ int* dp = (int* )malloc(sizeof(int) * (amount + 1));
362
+ for (int j = 0; j < amount + 1; j++) {
363
+ dp[ j] = INT_MAX;
364
+ }
365
+ dp[ 0] = 0;
366
+ // 遍历背包
367
+ for(int i = 0; i <= amount; i++){
368
+ // 遍历物品
369
+ for(int j = 0; j < coinsSize; j++){
370
+ if(i - coins[ j] >= 0 && dp[ i - coins[ j]] != INT_MAX){
371
+ dp[ i] = min(dp[ i] , dp[ i - coins[ j]] + 1);
372
+ }
373
+ }
374
+ }
375
+ if(dp[ amount] == INT_MAX){
376
+ return -1;
377
+ }
378
+ return dp[ amount] ;
379
+ }
380
+ ```
381
+
382
+
383
+
355
384
### Rust:
356
385
357
386
```rust
@@ -474,4 +503,3 @@ function coinChange(coins: number[], amount: number): number {
474
503
<a href =" https://programmercarl.com/other/kstar.html " target =" _blank " >
475
504
<img src =" ../pics/网站星球宣传海报.jpg " width =" 1000 " />
476
505
</a >
477
-
You can’t perform that action at this time.
0 commit comments