-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lecture 122: Number of Dice Rolls with Target Sum
- Loading branch information
1 parent
361feaf
commit b3f85be
Showing
6 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
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,5 @@ | ||
# Lecture Codes | ||
|
||
| **Problem** | **Solution** | **Level** | | ||
|:--------------|:--------------:|:-----------:| | ||
| [Dice throw](https://www.geeksforgeeks.org/problems/dice-throw5349/1) | [Solution1](), [Solution2](), [Solution3](), [Solution4]() | Medium | |
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,38 @@ | ||
// Recursion [TLE] | ||
|
||
class Solution { | ||
public: | ||
long long solve(int dice, int faces, int target) { | ||
// base cases | ||
if (target < 0) { | ||
return 0; | ||
} | ||
|
||
if (dice == 0 && target != 0) { | ||
return 0; | ||
} | ||
|
||
if (target == 0 && dice != 0) { | ||
return 0; | ||
} | ||
|
||
if (dice == 0 && target == 0) { | ||
return 1; | ||
} | ||
|
||
// calculating all possible ways for find target sum | ||
int ans = 0; | ||
|
||
for (int i=1; i<=faces; i++) { | ||
ans += solve(dice-1, faces, target-i); | ||
} | ||
|
||
return ans; | ||
|
||
} | ||
|
||
long long noOfWays(int m, int n, int x) { | ||
// code here | ||
return solve(n, m, x); | ||
} | ||
}; |
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,43 @@ | ||
// Recursion + Memoization | ||
|
||
class Solution { | ||
public: | ||
long long solve(int dice, int faces, int target, vector<vector<long long>> &dp) { | ||
// base cases | ||
if (target < 0) { | ||
return 0; | ||
} | ||
|
||
if (dice == 0 && target != 0) { | ||
return 0; | ||
} | ||
|
||
if (target == 0 && dice != 0) { | ||
return 0; | ||
} | ||
|
||
if (dice == 0 && target == 0) { | ||
return 1; | ||
} | ||
|
||
if (dp[dice][target] != -1) { | ||
return dp[dice][target]; | ||
} | ||
|
||
// calculating all possible ways for find target sum | ||
long long ans = 0; | ||
|
||
for (int i=1; i<=faces; i++) { | ||
ans += solve(dice-1, faces, target-i, dp); | ||
} | ||
|
||
return dp[dice][target] = ans; | ||
|
||
} | ||
|
||
long long noOfWays(int m, int n, int x) { | ||
// code here | ||
vector<vector<long long>> dp(n+1, vector<long long>(x+1, -1)); | ||
return solve(n, m, x, dp); | ||
} | ||
}; |
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,33 @@ | ||
// Tabulation Method | ||
|
||
class Solution { | ||
public: | ||
long long solve(int d, int f, int t) { | ||
vector<vector<long long>> dp(d+1, vector<long long>(t+1, 0)); | ||
|
||
dp[0][0] = 1; | ||
|
||
for (int dice=1; dice<=d; dice++) { | ||
for (int target=1; target<=t; target++) { | ||
// calculating all possible ways for find target sum | ||
long long ans = 0; | ||
|
||
for (int i=1; i<=f; i++) { | ||
if (target-i >= 0) { | ||
ans += dp[dice-1][target-i]; | ||
} | ||
} | ||
|
||
dp[dice][target] = ans; | ||
} | ||
} | ||
|
||
return dp[d][t]; | ||
|
||
} | ||
|
||
long long noOfWays(int m, int n, int x) { | ||
// code here | ||
return solve(n, m, x); | ||
} | ||
}; |
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,35 @@ | ||
// Space Optimization | ||
|
||
class Solution { | ||
public: | ||
long long solve(int d, int f, int t) { | ||
vector<long long> prev(t+1, 0); | ||
vector<long long> curr(t+1, 0); | ||
|
||
prev[0] = 1; | ||
|
||
for (int dice=1; dice<=d; dice++) { | ||
for (int target=1; target<=t; target++) { | ||
// calculating all possible ways for find target sum | ||
long long ans = 0; | ||
|
||
for (int i=1; i<=f; i++) { | ||
if (target-i >= 0) { | ||
ans += prev[target-i]; | ||
} | ||
} | ||
|
||
curr[target] = ans; | ||
} | ||
prev = curr; | ||
} | ||
|
||
return prev[t]; | ||
|
||
} | ||
|
||
long long noOfWays(int m, int n, int x) { | ||
// code here | ||
return solve(n, m, x); | ||
} | ||
}; |