From ad271c07cdb52bb9195de1f1a798ab78eae17d86 Mon Sep 17 00:00:00 2001 From: Vishwajeet Shivaji Hogale Date: Mon, 3 Oct 2022 11:34:04 +0530 Subject: [PATCH 1/5] Oct 2nd : Number of dice rolls with target sum --- .../number_of_dice_rolls_with_target_sum.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Dynamic Programming/number_of_dice_rolls_with_target_sum.cpp diff --git a/Dynamic Programming/number_of_dice_rolls_with_target_sum.cpp b/Dynamic Programming/number_of_dice_rolls_with_target_sum.cpp new file mode 100644 index 00000000..14ee230f --- /dev/null +++ b/Dynamic Programming/number_of_dice_rolls_with_target_sum.cpp @@ -0,0 +1,39 @@ +#include +#include +using namespace std; +int dp[31][1001] = {0}; // Table of n * target sum ; This is used to save the results in bottom up approach +int numRollsToTarget(int n, int k, int target) { + dp[0][0] = 1; // When you have 0 dices and target sum = 0; the number of ways to get the answer is 1 + + for(int i = 1;i<=target;i++){ // when you have 0 dices; [1,targetsum] numbers there are zero ways to get the answer + dp[0][i] = 0; + } + for(int i=1;i<=n;i++){ // when you have 0 target; n =[1,n] dices there are zero ways to get the answer + dp[i][0] = 0; + } + + /* + The below loop has Time complexity = O(n*targetSum*k) + It finds the answer for smaller target sums and build up to find the solution of the targetSum + */ + for(int i=1;i<=n;i++){ + for(int j=1;j<=target;j++){ + long long res = 0; + for(int x=1;x<=k;x++){ + if(j>=x){ + res = (res % 1000000007) + (dp[i-1][j-x] % 1000000007); + // res = res % 1000000007; + } + } + dp[i][j] = res ; + } + } + return dp[n][target] % 1000000007; +} + +int main(){ + + int n = 7,k=6,target = 30; + cout< Date: Mon, 3 Oct 2022 11:42:53 +0530 Subject: [PATCH 2/5] Oct 2nd : Decode ways --- Python/decodeWays.py | 46 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Python/decodeWays.py diff --git a/Python/decodeWays.py b/Python/decodeWays.py new file mode 100644 index 00000000..b8f56184 --- /dev/null +++ b/Python/decodeWays.py @@ -0,0 +1,46 @@ +""" + Leetcode 91. Decode ways + Method used : Memoization +""" + + +def check(s,mp): # Checks if the string is a valid number in the map of numbers + return int(s in mp.keys()) +g_d = dict() +class Solution(): + def helper(self,s,mp): + if(s in g_d): + return g_d[s] + if(s[0] == '0'): # if the string starts with 0, you cannot decode + return 0 + if(len(s) == 2): # len(s) == 2; two cases : checking two characters(s[0] && s[1]) seperately and the s[0:2] + return check(s[0:2],mp) + self.helper(s[1:],mp) + if(len(s) == 1 ): + return int(s in mp.keys()) + """ + There are two cases + * Pick one character from the front + * Check if the charcater chosen is valid + * Pick two character from the front + * Check if the charcater chosen is valid + the conditions below help to validate the conditions mentioned here. + """ + if not check(s[0:2],mp) and check(s[0:1],mp): + g_d[s] = self.helper(s[1:],mp) + return g_d[s] + if not check(s[0:1],mp) and check(s[0:2],mp): + g_d[s] = self.helper(s[2:],mp) + return g_d[s] + g_d[s] = self.helper(s[1:],mp) + self.helper(s[2:],mp) + + return g_d[s] + def numDecodings(self, s): + """ + :type s: str + :rtype: int + """ + mp = dict() + for i in range(1,27): + mp[str(i)] = chr(64+i) + # print(mp) + return self.helper(s,mp) \ No newline at end of file From 6af86ee1752dd953bd943fd1a753418969100bc8 Mon Sep 17 00:00:00 2001 From: Vishwajeet Shivaji Hogale Date: Tue, 4 Oct 2022 23:50:05 +0530 Subject: [PATCH 3/5] Oct 4th : Path sum --- Trees/pathSum.cpp | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Trees/pathSum.cpp diff --git a/Trees/pathSum.cpp b/Trees/pathSum.cpp new file mode 100644 index 00000000..a514eee1 --- /dev/null +++ b/Trees/pathSum.cpp @@ -0,0 +1,15 @@ +#include +#include +using namespace std; +struct TreeNode{ + int val; + TreeNode *left,*right; +}; +bool hasPathSum(TreeNode* root, int sum) { + if(!root) + return 0; + + if(!root->left && !root->right) + return sum-root->val == 0; + return hasPathSum(root->left,sum-root->val) || hasPathSum(root->right,sum-root->val); +} From 3fdd75821d7cb7d96b4d281ddd6d8c6b8d639b7d Mon Sep 17 00:00:00 2001 From: Vishwajeet Shivaji Hogale Date: Tue, 4 Oct 2022 23:50:26 +0530 Subject: [PATCH 4/5] Oct 4th : Path sum --- Trees/pathSum.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Trees/pathSum.cpp b/Trees/pathSum.cpp index a514eee1..44150f07 100644 --- a/Trees/pathSum.cpp +++ b/Trees/pathSum.cpp @@ -9,7 +9,7 @@ bool hasPathSum(TreeNode* root, int sum) { if(!root) return 0; - if(!root->left && !root->right) + if(!root->left && !root->right) // checks if the sum at the root is equal to target sum return sum-root->val == 0; - return hasPathSum(root->left,sum-root->val) || hasPathSum(root->right,sum-root->val); + return hasPathSum(root->left,sum-root->val) || hasPathSum(root->right,sum-root->val); // Traverses the tree to find the solution at one of the leaf nodes } From 5cb03adb0f44040a5f021ce2a02e1fb8e1ab1c0e Mon Sep 17 00:00:00 2001 From: Vishwajeet Shivaji Hogale Date: Wed, 5 Oct 2022 00:25:18 +0530 Subject: [PATCH 5/5] Two sum 2 --- CPP/arrays/Two sum 2/twoSum.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 CPP/arrays/Two sum 2/twoSum.cpp diff --git a/CPP/arrays/Two sum 2/twoSum.cpp b/CPP/arrays/Two sum 2/twoSum.cpp new file mode 100644 index 00000000..c226101d --- /dev/null +++ b/CPP/arrays/Two sum 2/twoSum.cpp @@ -0,0 +1,30 @@ +#include +#include +using namespace std; +vector twoSum(vector& numbers, int target) { + vector res; + int n = numbers.size(); + int low = 0,high = n - 1; + while(lowtarget) + high -= 1; + else if(sum numbers({2,3,4}); + int target = 6; + vector res = twoSum(numbers,target); + for(auto x:res) + cout<