Simple backtracking avoiding repeated recursive calls using hash set - C++
Just note that only backtracking will lead to TLE as there will be many repeated calls - you just need to skip those.\nHope this helps!\n\n    void fun(vectorta
Just note that only backtracking will lead to TLE as there will be many repeated calls - you just need to skip those.\nHope this helps!\n\n    void fun(vector<int>tasks,int s,int k,int session,set<pair<vector<int>,int>>&st,vector<int>&v){\n        if(tasks.empty()){\n                v.push_back(k+1);\n            return;\n        }\n        \n        if(st.find(make_pair(tasks,s))!=st.end())\n            return;\n        \n        st.insert(make_pair(tasks,s));\n        \n        if(!tasks.empty()){\n            int flag=0;\n            for(int i=0;i<tasks.size();i++){\n                if(s+tasks[i]<=session){\n                    flag=1;\n                    int p=tasks[i];\n                    tasks.erase(tasks.begin()+i);\n                    if(s+p<=session)\n                        fun(tasks,s+p,k,session,st,v);\n\n                    tasks.insert(tasks.begin()+i,p);\n                }\n            }\n            if(!flag){\n                fun(tasks,0,k+1,session,st,v);\n            }\n        }\n    }\n    \n    int minSessions(vector<int>& tasks, int sessionTime) {\n        set<pair<vector<int>,int>>st;\n        vector<int>v;\n        fun(tasks,0,0,sessionTime,st,v);\n        for(auto i:v)\n            cout<<i<<" ";\n        cout<<endl;\n        return *min_element(v.begin(),v.end());\n    }
[]