✅✅Easy To Understand || C++ Code
Using Recursion && Memoization\n\n\nclass Solution {\npublic:\n    \n    // declare a dp\n    \n    unordered_map<string, int> dp;\n    \n    // sessions will s
* ***Using Recursion && Memoization***\n\n```\nclass Solution {\npublic:\n    \n    // declare a dp\n    \n    unordered_map<string, int> dp;\n    \n    // sessions will store the no. of active sessions\n    \n    vector<int> sessions;\n    \n    // function for creating key\n    \n    // we are sorting the arr to avoid duplicate states, [10, 20, 10, 30] and [10, 10, 20, 30] will form the same key\n    \n    string create_key(int i)\n    {\n        vector<int> arr = sessions;\n        \n        sort(arr.begin(), arr.end());\n        \n        string res = "";\n        \n        for(int i = 0; i < arr.size(); i++)\n        {\n            res += to_string(arr[i]);\n            \n            res += \'#\';\n        }\n        \n        res += to_string(i);\n        \n        return res;\n    }\n    \n    // dfs function\n    \n    int dfs(vector<int>& nums, int i, int n, int time_limit)\n    {\n        if(i == n)\n        {\n            return 0;\n        }\n        \n        // if already calculated\n        \n        string key = create_key(i);\n        \n        if(dp.count(key))\n            return dp[key];\n        \n        int ans = INT_MAX;\n        \n        // create a new session\n        \n        sessions.push_back(nums[i]);\n        \n        ans = min(ans, 1 + dfs(nums, i + 1, n, time_limit));\n        \n        // backtrack part\n        \n        sessions.pop_back();\n        \n        // include the curr task in the previously active sessions\n        \n        for(int k = 0; k < sessions.size(); k++)\n        {\n            if(sessions[k] + nums[i] <= time_limit)\n            {\n                sessions[k] += nums[i];\n                \n                ans = min(ans, dfs(nums, i + 1, n, time_limit));\n                \n                // backtrack part\n                \n                sessions[k] -= nums[i];\n            }\n        }\n        \n        // store the res in dp and return it\n        \n        return dp[key] = ans;\n    }\n    \n    int minSessions(vector<int>& nums, int sessionTime) {\n        \n        int n = nums.size();\n        \n        return dfs(nums, 0, n, sessionTime);\n    }\n};\n```
['Dynamic Programming', 'Recursion', 'Memoization', 'C', 'C++']