Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

458.Poor Pigs added #466

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions C++/Poor-Pigs.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//Problem 458.POOR PIGS
/*
Constraints:
1 <= buckets <= 1000
1 <= minutesToDie <= minutesToTest <= 100
*/
class Solution {
public:
int poorPigs(int N, int I, int T) {
vector<long long> f(11, 1);
for(int i = 1; i <= 10; i++) f[i] = f[i - 1] * i;
T = T / I;
if(N == 1) return 0LL;
vector<vector<int>> dp(11, vector<int> (T + 1));
for(int i = 0; i <= 10; i++) dp[i][0] = 1;
for(int i = 0; i <= T; i++) dp[0][i] = 1;
for(int i = 1; i <= 10; i++){
for(int j = 1; j <= T; j++){
for(int k = 0; k <= i; k++) dp[i][j] += f[i] / (f[i - k] * f[k]) * dp[i - k][j - 1];
}
if(dp[i][T] >= N) return i;
}
return 11LL;
}
};
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
| 1221 | [Split a String in Balanced Strings](https://leetcode.com/problems/split-a-string-in-balanced-strings/) | [Python](./Python/split-a-string-in-balanced-strings.py) | _O(n)_ | _O(1)_ | Easy | | |
| 1374 | [Generate a String With Characters That Have Odd Counts](https://leetcode.com/problems/generate-a-string-with-characters-that-have-odd-counts/) | [Java](./Java/generate-a-string-with-characters-that-have-odd-counts.java) | _O(n)_ | _O(1)_ | Easy | | |
| 1614 | [Maximum Nesting Depth of the Parentheses](https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/) | [Java](./Java/max-nesting-depth-parentheses.java) | _O(n)_ | _O(1)_ | Easy | | |
| 1614 | [Poor Pigs](https://leetcode.com/problems/poor-pigs/) | _O(nlogn)_ | _O(n2)_ | Hard | | |
<br/>
<div align="right">
<b><a href="#algorithms">⬆️ Back to Top</a></b>
Expand Down