Skip to content

Commit

Permalink
Lecture 122: Number of Dice Rolls with Target Sum
Browse files Browse the repository at this point in the history
  • Loading branch information
kishanrajput23 authored Nov 3, 2024
1 parent 361feaf commit b3f85be
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 0 deletions.
Binary file added Lectures/Lecture_122/Lecture_122_Hands_On.pdf
Binary file not shown.
5 changes: 5 additions & 0 deletions Lectures/Lecture_122/Lecture_Codes/README.md
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 |
38 changes: 38 additions & 0 deletions Lectures/Lecture_122/Lecture_Codes/dice_throw_1.cpp
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);
}
};
43 changes: 43 additions & 0 deletions Lectures/Lecture_122/Lecture_Codes/dice_throw_2.cpp
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);
}
};
33 changes: 33 additions & 0 deletions Lectures/Lecture_122/Lecture_Codes/dice_throw_3.cpp
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);
}
};
35 changes: 35 additions & 0 deletions Lectures/Lecture_122/Lecture_Codes/dice_throw_4.cpp
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);
}
};

0 comments on commit b3f85be

Please sign in to comment.