forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b836fc1
commit dc5b00a
Showing
7 changed files
with
99 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution { | ||
public: | ||
int numOfArrays(int n, int m, int k) { | ||
int mod = 1000000007; | ||
long long dp[n + 1][k + 1][m + 1]; | ||
memset(dp, 0, sizeof dp); | ||
|
||
for (int c = 0; c <= m; c++) dp[1][1][c] = c; | ||
for (int a = 2; a <= n; a++) { | ||
for (int b = 1; b <= k; b++) { | ||
for (int c = b; c <= m; c++) { | ||
dp[a][b][c] = (dp[a][b][c - 1] + dp[a - 1][b - 1][c - 1] + | ||
(dp[a - 1][b][c] - dp[a - 1][b][c - 1] + mod) * c) % mod; | ||
} | ||
} | ||
} | ||
return dp[n][k][m]; | ||
} | ||
}; |
23 changes: 23 additions & 0 deletions
23
src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
func numOfArrays(n int, m int, k int) int { | ||
mod := 1000000007 | ||
dp := make([][][]int, n + 1) | ||
for i := 0; i <= n; i++ { | ||
dp[i] = make([][]int, k + 1) | ||
for j := 0; j <= k; j++ { | ||
dp[i][j] = make([]int, m + 1) | ||
} | ||
} | ||
|
||
for c := 0; c <= m; c++ { | ||
dp[1][1][c] = c | ||
} | ||
for a := 2; a <= n; a++ { | ||
for b := 1; b <= k; b++ { | ||
for c := b; c <= m; c++ { | ||
dp[a][b][c] = (dp[a][b][c - 1] + dp[a - 1][b - 1][c - 1] + | ||
(dp[a - 1][b][c] - dp[a - 1][b][c - 1] + mod) * c) % mod | ||
} | ||
} | ||
} | ||
return dp[n][k][m] | ||
} |
17 changes: 17 additions & 0 deletions
17
src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution { | ||
public int numOfArrays(int n, int m, int k) { | ||
long mod = 1000000007; | ||
long[][][] dp = new long[n + 1][k + 1][m + 1]; | ||
|
||
for (int c = 0; c <= m; c++) dp[1][1][c] = c; | ||
for (int a = 2; a <= n; a++) { | ||
for (int b = 1; b <= k; b++) { | ||
for (int c = b; c <= m; c++) { | ||
dp[a][b][c] = (dp[a][b][c - 1] + dp[a - 1][b - 1][c - 1] + | ||
(dp[a - 1][b][c] - dp[a - 1][b][c - 1] + mod) * c) % mod; | ||
} | ||
} | ||
} | ||
return (int)(dp[n][k][m]); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
var numOfArrays = function(n, m, k) { | ||
let mod = 1000000007; | ||
let dp = Array(n + 1); | ||
for (let i = 0; i <= n; i++) { | ||
dp[i] = Array(k + 1); | ||
for (let j = 0; j <= k; j++) { | ||
dp[i][j] = Array(m + 1).fill(0); | ||
} | ||
} | ||
|
||
for (let c = 0; c <= m; c++) dp[1][1][c] = c; | ||
for (let a = 2; a <= n; a++) { | ||
for (let b = 1; b <= k; b++) { | ||
for (let c = b; c <= m; c++) { | ||
dp[a][b][c] = (dp[a][b][c - 1] + dp[a - 1][b - 1][c - 1] + | ||
(dp[a - 1][b][c] - dp[a - 1][b][c - 1] + mod) * c) % mod; | ||
} | ||
} | ||
} | ||
return dp[n][k][m]; | ||
}; |
13 changes: 13 additions & 0 deletions
13
src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Solution: | ||
def numOfArrays(self, n: int, m: int, k: int) -> int: | ||
mod = 10**9 + 7 | ||
|
||
dp = [[[0] * (m + 1) for _ in range(k + 1)] for _ in range(n + 1)] | ||
for c in range(m + 1): | ||
dp[1][1][c] = c | ||
for a in range(2, n + 1): | ||
for b in range(1, k + 1): | ||
for c in range(b, m + 1): | ||
dp[a][b][c] = (dp[a][b][c - 1] + dp[a - 1][b - 1][c - 1] + \ | ||
(dp[a - 1][b][c] - dp[a - 1][b][c - 1] + mod) * c) % mod | ||
return dp[-1][-1][-1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters