-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy path剪绳子2.js
More file actions
28 lines (25 loc) · 704 Bytes
/
剪绳子2.js
File metadata and controls
28 lines (25 loc) · 704 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* @param {number} n
* @return {number}
*/
var cuttingRope = function(n) {
const dp = new Array(n + 1).fill(0 n); // 0n
const mod = BigInt(1e9 + 7);
dp[1] = 1 n; // 1n
for (let i = 2; i <= n; i++) {
for (let j = 1; j < i; j++) {
let curr = dp[i];
if (BigInt(j * (i - j)) > curr) {
curr = BigInt(j * (i - j));
}
if (BigInt(dp[j] * BigInt(i - j)) > curr) {
curr = BigInt(dp[j] * BigInt(i - j));
}
if (BigInt(dp[j] * dp[i - j]) > curr) {
curr = BigInt(dp[j] * dp[i - j]);
}
dp[i] = curr;
}
}
return dp[n] % mod;
};