Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.75 KB | None | 0 0
  1. public class Solution {
  2.     int counter=0;
  3.     public int NumRollsToTarget(int d, int f, int target) {
  4.        
  5.        
  6.         return helper(d,f,target,new Dictionary<string,int>());
  7.     }
  8.  
  9.     int helper(int d, int f, int target,Dictionary<string,int> map)
  10.     {
  11.         if (d==1 && f>=target && target>=1)
  12.             return 1;
  13.        
  14.         if (d==0 || target==0)
  15.             return 0;
  16.        
  17.         string key=d+"_"+target;
  18.         if (map.ContainsKey(key))
  19.             return map[key];
  20.        
  21.        
  22.         int val=0;
  23.         for(int i=1;i<=f;i++)
  24.         {
  25.             val+=helper(d-1,f,target-i,map);
  26.             val=val%(1000000000+7);
  27.            
  28.         }
  29.         map.Add(key,val);
  30.         return map[key];
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement