C++ | Runtime- 0ms sometimes 4ms beats 100% | Size- 7.6 MB beats 100% | DP | Subset sum |
Time Complexity: O(s*n^2), s=sessionTime, n= tasks length\n\nLets take this example tasks=[2,3,4,4,5,6,7,10], sessionTime=12\n\nWe can store number of times an 
**Time Complexity: O(s*n^2)**, s=sessionTime, n= tasks length\n\nLets take this example **tasks=[2,3,4,4,5,6,7,10], sessionTime=12**\n\nWe can store number of times an element is appearing in tasks, to a vector (like a hash map).\nLets create **hashVector=[0,0,1,1,2,1,1,1,0,0,1]** for elements 0 to 10 and initialise our answer as **ans=0**\n\nWe select elements from tasks having maximum sum upto **sessionTime** to create a new session for them. We can use DP to check **maxSum** and elements adding to **maxSum** as used in [Subset sum problem](http://leetcode.com/problems/partition-equal-subset-sum/) in a loop. We will update **hashVector** & loop until no task is remaining.\n\nUpdates after every loop:\n* **Loop 1:** tasks=[2,3,4,4,5,6,7,10], **maxSum**=12, elements taken out to form a new session=(2,10), **ans=1**\n* **Loop 2:** tasks=[3,4,4,5,6,7], **maxSum**=12, elements taken out to form a new session=(5,7), **ans=2**\n* **Loop 3:** tasks=[3,4,4,6], **maxSum**=11, elements taken out to form a new session=(3,4,4), **ans=3**\n* **Loop 4:** tasks=[6], **maxSum**=6, elements taken out to form a new session=(6), **ans=4**.\n* **Loop 5:** tasks=[], loop breaks.\n\nAfter tasks=[], loop will not run.\n\n```\nclass Solution {\npublic:\n    bool tasksRemaining(vector<int>& v, int s){\n\t\t// Vector to store remaining tasks\n        vector<int> vec;\n        for(int i=1;i<v.size();i++){\n            int count=v[i];\n            while(count--) vec.push_back(i);\n        }\n        int n=vec.size();\n\t\t// If no tasks remaining\n        if(n==0) return false;\n\t\t\n\t\t// DP like subset sum problem\n        bool dp[n+1][s+1];\n\t\t// Max sum upto sessionTime from the remaining tasks\n        int maxSum=0;\n        for(int i=0;i<n+1;i++){\n            for(int j=0;j<s+1;j++){\n                if(j==0) dp[i][j]=true;\n                else if(i==0) dp[i][j]=false;\n                else{\n                    if(vec[i-1]>j) dp[i][j]=dp[i-1][j];\n                    else dp[i][j]=(dp[i-1][j]||dp[i-1][j-vec[i-1]]);\n                }\n                if(i==n && dp[i][j]) maxSum=j;\n            }\n        }\n\t\t\n\t\t// Selecting elements adding to "maxSum"\n        int i=n;\n        while(maxSum){\n            while(vec[i-1]>maxSum) i--;\n            if(dp[i-1][maxSum-vec[i-1]]){\n\t\t\t\t// Removing them from hashVector\n                v[vec[i-1]]--;\n                maxSum-=vec[i-1];\n            }\n            i--;\n        }\n        return true;\n    }\n    \n    int minSessions(vector<int>& t, int s) {\n        int n=t.size(),ans=0,m=0;\n        for(int i=0;i<n;i++) \n\t\t\tm=max(m,t[i]);\n\t\t\n\t\t// Create hash vector of size "m+1"(max element of array+1)\n        vector<int> v(m+1,0);\n        for(int i=0;i<n;i++)\n            v[t[i]]++;\n\t\t\t\n\t\t// Loop until all tasks are not done\n        while(tasksRemaining(v,s)) ans++;\n        return ans;\n    }\n};\n```\n\nFeel free to correct me or add any improvements.\nThis is my first post, please upvote if you like the solution
['Dynamic Programming', 'C']