clean dp with memoization
credit to @shourabhpayal\nfor c++ https://leetcode.com/problems/minimum-number-of-work-sessions-to-finish-the-tasks/discuss/1431836/C%2B%2B-Solution-or-Recursio
credit to @shourabhpayal\nfor c++ https://leetcode.com/problems/minimum-number-of-work-sessions-to-finish-the-tasks/discuss/1431836/C%2B%2B-Solution-or-Recursion-%2B-Memoization\n\n\nidea\n1. use bit mask  to represent array  \n   1) for selected element, set ith index to 1\n   2) for unselected element, is ith index is 0\n\t\n\te.g: [1,2,3,4,5,6]   \n\t      if 0 and 4th element selected , 100010\n\n2. use memoization to store interim result \n  e.g:   in #1 above example , \n          10010 binary equals to 18 ,         \n          therefore, minimum value is in between memo[18][0] to memo[18][sessionTime]\n\t\t  \n3. final result is  in between memo[(1<<n)-1][0] and memo[(1<<n)-1][sessionTime]\n4. \n\t\t  \n```\n\n\npublic int minSessions(int[] tasks, int sessionTime) {        \n        int n=tasks.length;\n        Integer[][] memo= new Integer[1<<n][sessionTime+1];        \n        return dp(tasks,memo,0,0,sessionTime,(1<<n)-1);                \n    }\n    \n    private int dp(int[] tasks,Integer[][] memo,int mask,int currTime,int sessionTime,int all){\n        if(currTime>sessionTime)\n            return Integer.MAX_VALUE;\n        \n        if(mask==all)\n            return 1;        \n        \n        if(memo[mask][currTime]!=null)\n            return memo[mask][currTime];\n        \n        int res=Integer.MAX_VALUE;\n        for(int i=0;i<tasks.length;i++){\n            if((mask&(1<<i))==0){\n                int a=dp(tasks,memo,mask|(1<<i),currTime+tasks[i],sessionTime,all);                \n                int b=dp(tasks,memo,mask|(1<<i),tasks[i],sessionTime,all)+1;\n                res=Math.min(res,Math.min(a,b));\n            }                        \n        }\n        \n        return memo[mask][currTime]=res;\n    }\n\n\n\n```\n\n\n\n
[]