mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #1275 from xiaofei-2020/dp08
添加(0343.整数拆分.md):增加typescript版本
This commit is contained in:
@ -274,7 +274,33 @@ var integerBreak = function(n) {
|
||||
};
|
||||
```
|
||||
|
||||
C:
|
||||
### TypeScript
|
||||
|
||||
```typescript
|
||||
function integerBreak(n: number): number {
|
||||
/**
|
||||
dp[i]: i对应的最大乘积
|
||||
dp[2]: 1;
|
||||
...
|
||||
dp[i]: max(
|
||||
1 * dp[i - 1], 1 * (i - 1),
|
||||
2 * dp[i - 2], 2 * (i - 2),
|
||||
..., (i - 2) * dp[2], (i - 2) * 2
|
||||
);
|
||||
*/
|
||||
const dp: number[] = new Array(n + 1).fill(0);
|
||||
dp[2] = 1;
|
||||
for (let i = 3; i <= n; i++) {
|
||||
for (let j = 1; j <= i - 2; j++) {
|
||||
dp[i] = Math.max(dp[i], j * dp[i - j], j * (i - j));
|
||||
}
|
||||
}
|
||||
return dp[n];
|
||||
};
|
||||
```
|
||||
|
||||
### C
|
||||
|
||||
```c
|
||||
//初始化DP数组
|
||||
int *initDP(int num) {
|
||||
|
Reference in New Issue
Block a user