From dc5b00a71450844ea44224451ebbd9a605168f10 Mon Sep 17 00:00:00 2001 From: luliyucoordinate Date: Thu, 23 Apr 2020 21:49:36 +0800 Subject: [PATCH] add 1420 --- README.md | 3 ++- .../1420.cpp | 19 +++++++++++++++ .../1420.go | 23 +++++++++++++++++++ .../1420.java | 17 ++++++++++++++ .../1420.js | 21 +++++++++++++++++ .../1420.py | 13 +++++++++++ src/addProb.py | 8 +++---- 7 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.cpp create mode 100644 src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.go create mode 100644 src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.java create mode 100644 src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.js create mode 100644 src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.py diff --git a/README.md b/README.md index 6e8efe7b..a944448e 100644 --- a/README.md +++ b/README.md @@ -714,4 +714,5 @@ LeetCode |1416|[Restore The Array](https://leetcode.com/problems/restore-the-array/)|c|[c++](./src/1416-Restore-The-Array/1416.cpp)|[python](./src/1416-Restore-The-Array/1416.py)|[go](./src/1416-Restore-The-Array/1416.go)|[js](./src/1416-Restore-The-Array/1416.js)|[java](./src/1416-Restore-The-Array/1416.java)|Hard| |1417|[Reformat The String](https://leetcode.com/problems/reformat-the-string/)|c|[c++](./src/1417-Reformat-The-String/1417.cpp)|[python](./src/1417-Reformat-The-String/1417.py)|[go](./src/1417-Reformat-The-String/1417.go)|[js](./src/1417-Reformat-The-String/1417.js)|[java](./src/1417-Reformat-The-String/1417.java)|Easy| |1418|[Display Table of Food Orders in a Restaurant](https://leetcode.com/problems/display-table-of-food-orders-in-a-restaurant/)|c|[c++](./src/1418-Display-Table-of-Food-Orders-in-a-Restaurant/1418.cpp)|[python](./src/1418-Display-Table-of-Food-Orders-in-a-Restaurant/1418.py)|[go](./src/1418-Display-Table-of-Food-Orders-in-a-Restaurant/1418.go)|[js](./src/1418-Display-Table-of-Food-Orders-in-a-Restaurant/1418.js)|[java](./src/1418-Display-Table-of-Food-Orders-in-a-Restaurant/1418.java)|Medium| -|1419|[Minimum Number of Frogs Croaking](https://leetcode.com/problems/minimum-number-of-frogs-croaking/)|c|[c++](./src/1419-Minimum-Number-of-Frogs-Croaking/1419.cpp)|[python](./src/1419-Minimum-Number-of-Frogs-Croaking/1419.py)|[go](./src/1419-Minimum-Number-of-Frogs-Croaking/1419.go)|[js](./src/1419-Minimum-Number-of-Frogs-Croaking/1419.js)|[java](./src/1419-Minimum-Number-of-Frogs-Croaking/1419.java)|Medium| \ No newline at end of file +|1419|[Minimum Number of Frogs Croaking](https://leetcode.com/problems/minimum-number-of-frogs-croaking/)|c|[c++](./src/1419-Minimum-Number-of-Frogs-Croaking/1419.cpp)|[python](./src/1419-Minimum-Number-of-Frogs-Croaking/1419.py)|[go](./src/1419-Minimum-Number-of-Frogs-Croaking/1419.go)|[js](./src/1419-Minimum-Number-of-Frogs-Croaking/1419.js)|[java](./src/1419-Minimum-Number-of-Frogs-Croaking/1419.java)|Medium| +|1420|[Build Array Where You Can Find The Maximum Exactly K Comparisons](https://leetcode.com/problems/build-array-where-you-can-find-the-maximum-exactly-k-comparisons/)|c|[c++](./src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.cpp)|[python](./src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.py)|[go](./src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.go)|[js](./src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.js)|[java](./src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.java)|Hard| \ No newline at end of file diff --git a/src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.cpp b/src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.cpp new file mode 100644 index 00000000..a3bfa4fd --- /dev/null +++ b/src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.cpp @@ -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]; + } +}; \ No newline at end of file diff --git a/src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.go b/src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.go new file mode 100644 index 00000000..78c72850 --- /dev/null +++ b/src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.go @@ -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] +} \ No newline at end of file diff --git a/src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.java b/src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.java new file mode 100644 index 00000000..8cfa4d6c --- /dev/null +++ b/src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.java @@ -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]); + } +} \ No newline at end of file diff --git a/src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.js b/src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.js new file mode 100644 index 00000000..5f168ff0 --- /dev/null +++ b/src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.js @@ -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]; +}; \ No newline at end of file diff --git a/src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.py b/src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.py new file mode 100644 index 00000000..ff9495cc --- /dev/null +++ b/src/1420-Build-Array-Where-You-Can-Find-The-Maximum-Exactly-K-Comparisons/1420.py @@ -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] \ No newline at end of file diff --git a/src/addProb.py b/src/addProb.py index ce8f03d6..40bd4ba7 100644 --- a/src/addProb.py +++ b/src/addProb.py @@ -2,10 +2,10 @@ import os, bisect # 题目名称 -name = "Minimum Number of Frogs Croaking" -ID = 1419 -url = "https://leetcode.com/problems/minimum-number-of-frogs-croaking/" -difficult = "Medium" +name = "Build Array Where You Can Find The Maximum Exactly K Comparisons" +ID = 1420 +url = "https://leetcode.com/problems/build-array-where-you-can-find-the-maximum-exactly-k-comparisons/" +difficult = "Hard" prog = ['c', 'cpp', 'py', 'go', 'js', 'java']