C++ | 3ms | Greedy | Binary Search | DFS
we alway choose the max of the tasks to fill in the session.\nthen we use dfs to fill the remaining space in the session from the big to small,\nand we use Bina
we alway choose the max of the tasks to fill in the session.\nthen we use dfs to fill the remaining space in the session from the big to small,\nand we use Binary Search to find the max one that less than remaining space.\n3ms\n```\nclass Solution {\npublic:\n    int minSessions(vector<int>& tasks, int sessionTime) {\n        sort(tasks.begin(),tasks.end());\n        int ans=0;\n        int nowleft=sessionTime;\n\n        while (!tasks.empty()){\n            vector<int> path;\n            path.push_back(tasks.size()-1);\n            int n=nowleft-int(*tasks.rbegin());\n            dfs(tasks,tasks.size()-1,n,path,n,path);\n            for(auto i:path){\n                tasks.erase(tasks.begin()+i);\n            }\n            ans++;\n            nowleft=sessionTime;\n        }\n            \n        return(ans);\n        \n           \n    }\n    int lessmax(vector<int> tasks,int curr,int n){\n            int l=0;\n            int r=curr;\n            if (tasks[curr]<=n){\n               \n                return r-1;\n            }   \n            while (l<r){\n                int mid=(l+r)/2;\n                if (tasks[mid]<=n and tasks[mid+1]>n){\n                    return mid;\n                }\n                    \n                else if (tasks[mid]>n){\n                    \n                    r=mid;\n                }\n                    \n                else{\n                    l=mid;\n                }                    \n            } \n            return -1;\n        }\n    void dfs(vector<int> tasks,int curr,int nowleft,vector<int> nowpath,int &minleft,vector<int>& minpath){\n\n            int lm=lessmax(tasks,curr,nowleft);\n            if (lm<0){\n                if (nowleft<minleft){\n                    minleft=nowleft;\n                    minpath=nowpath;\n                }\n                return ;\n            }\n            for (int i=lm;i>=0;i--){\n                nowpath.push_back(i);\n                int tmp=nowleft-tasks[i];\n                if (tmp==0){\n                    minleft=0;\n                    minpath=nowpath;\n                    return;\n                }else{\n                    if (tmp<minleft){\n                        minleft=tmp;\n                        minpath=nowpath;\n                    }                    \n                    dfs(tasks,i,tmp,nowpath,minleft,minpath);\n                    if(minleft==0){\n                        return;\n                    }\n                }\n                nowpath.pop_back();\n            }\n        }\n};\n```
[]