From 4c03ad7a78fcb324da45e97a2cfca618af1086c8 Mon Sep 17 00:00:00 2001 From: Steve2020 <841532108@qq.com> Date: Sun, 1 May 2022 22:32:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=880343.=E6=95=B4?= =?UTF-8?q?=E6=95=B0=E6=8B=86=E5=88=86.md=EF=BC=89=EF=BC=9A=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0typescript=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0343.整数拆分.md | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/problems/0343.整数拆分.md b/problems/0343.整数拆分.md index 4a7ba6ab..279f1d71 100644 --- a/problems/0343.整数拆分.md +++ b/problems/0343.整数拆分.md @@ -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) {