Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d0205e0

Browse files
authoredAug 10, 2023
Create 2787-ways-to-express-an-integer-as-sum-of-powers.js
1 parent 98ca324 commit d0205e0

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
 
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number} n
3+
* @param {number} x
4+
* @return {number}
5+
*/
6+
const numberOfWays = function(n, x) {
7+
const dp = Array.from({ length: n + 1 }, () => Array(n + 1).fill(0))
8+
dp[0][0] = 1
9+
const mod = 1e9 + 7
10+
for(let i = 0; i <= n; i++) {
11+
for(let j = 1; j <= n; j++) {
12+
dp[i][j] = (dp[i][j] + dp[i][j - 1]) % mod
13+
const tmp = Math.pow(j, x)
14+
if(i >= tmp) {
15+
dp[i][j] = (dp[i][j] + dp[i - tmp][j - 1]) % mod
16+
}
17+
}
18+
}
19+
20+
return dp[n][n]
21+
};

0 commit comments

Comments
 (0)
Please sign in to comment.