id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n\nclass Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n\n int n = tasks.size();\n for (int i = 0; i < n; i++) {\n tasks[i] = {tasks[i][0], tasks[i][1], i};\n }\n sort(tasks.begin(), tasks.end());\n\n int i = 0;\n set<pair<int, int>> s;\n int currTime = tasks[0][0];\n\n vector<int> order;\n for (int i = 0; i < n; i++) {\n if (tasks[i][0] <= currTime) {\n s.insert({tasks[i][1], tasks[i][2]});\n } else {\n while (currTime < tasks[i][0] && !s.empty()) {\n order.push_back((*s.begin()).second);\n currTime += (*s.begin()).first;\n s.erase(s.begin());\n }\n s.insert({tasks[i][1], tasks[i][2]});\n if (currTime < tasks[i][0]) {\n currTime = tasks[i][0];\n }\n }\n }\n while (!s.empty()) {\n order.push_back((*s.begin()).second);\n s.erase(s.begin());\n }\n return order;\n }\n};", "memory": "152438" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\nprivate:\n int ansIdx = 0;\n void processTask(priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>& execTime, vector<int>& order, long long& currTime) {\n auto currTask = execTime.top();\n execTime.pop();\n order[ansIdx++] = currTask.second;\n currTime += currTask.first;\n }\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<int> order(tasks.size());\n vector<vector<int>> taskWithIdx(tasks.size());\n int i = 0;\n for(auto task : tasks) {\n taskWithIdx[i] = {task[0], task[1], i};\n i++;\n }\n \n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> execTime; //pair of execTime and index\n\n sort(taskWithIdx.begin(), taskWithIdx.end());\n long long currTime = taskWithIdx[0][0];\n int idx = 0;\n \n while(idx < taskWithIdx.size()) {\n while(idx < taskWithIdx.size() && taskWithIdx[idx][0] <= currTime) {\n execTime.push({taskWithIdx[idx][1], taskWithIdx[idx][2]});\n idx++;\n }\n if(execTime.empty() && idx < taskWithIdx.size()) {\n currTime = taskWithIdx[idx][0];\n continue;\n }\n processTask(execTime, order, currTime);\n }\n while(!execTime.empty()) {\n processTask(execTime, order, currTime);\n }\n return order;\n }\n};", "memory": "153379" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<pair<pair<int, int>, int>>temp;\n for (int i = 0; i < tasks.size(); i++) {\n temp.push_back({{tasks[i][0], tasks[i][1]}, i});\n }\n sort(temp.begin(), temp.end());\n vector<int>ans;\n ans.push_back(temp[0].second);\n long long val = temp[0].first.first + temp[0].first.second;\n temp.erase(temp.begin());\n priority_queue <vector<int>, vector<vector<int>>, greater<>> pq;\n int i = 0;\n while(i < temp.size() || pq.size()) {\n // int j = i;\n if (pq.empty()) {\n val = max(val, (long long)temp[i].first.first);\n }\n while(i < temp.size() && temp[i].first.first <= val) {\n pq.push({temp[i].first.second, temp[i].second});\n i++;\n }\n val += pq.top()[0];\n ans.push_back(pq.top()[1]);\n pq.pop();\n // int j = 0;\n // for (int i = 0; i < temp.size(); i++) {\n // if (temp[i].first.first <= val) {\n // if (temp[i].first.second < temp[j].first.second) {\n // j = i;\n // } else if (temp[i].first.second == temp[j].first.second) {\n // if (temp[i].second < temp[j].second)\n // j = i;\n // }\n // } else\n // break;\n // }\n // val += temp[j].first.second;\n // ans.push_back(temp[j].second);\n // temp.erase(temp.begin() + j);\n }\n return ans;\n }\n};", "memory": "153379" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<int>ans;\n vector<vector<int>>temp;\n for(int i =0;i<tasks.size();i++){\n temp.push_back({tasks[i][0],tasks[i][1],i});\n }\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int, int>>>q;\n long long curr = 0;\n sort(temp.begin(),temp.end());\n int i =0;\n int n = tasks.size();\n while(i<tasks.size()||!q.empty()){\n if(q.empty()&&curr<temp[i][0]){\n curr = temp[i][0];\n }\n while(i<n&&curr>=temp[i][0]){\n q.push({temp[i][1],temp[i][2]});\n i++;\n }\n pair<int,int>p = q.top();\n q.pop();\n curr+=p.first;\n ans.push_back(p.second);\n }\n return ans;\n }\n};", "memory": "154320" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\nvector<int> getOrder(vector<vector<int>>& tasks) {\n int n = tasks.size();\n \n // Add the original index to each task.\n vector<vector<int>> indexedTasks;\n for (int i = 0; i < n; ++i) {\n indexedTasks.push_back({tasks[i][0], tasks[i][1], i});\n }\n \n // Sort tasks by enqueue time.\n sort(indexedTasks.begin(), indexedTasks.end());\n \n // Min-heap to store {processing time, original index}\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> minHeap;\n \n vector<int> result;\n long long time = 0; // Use long long to handle large numbers\n int i = 0;\n \n // Process the tasks.\n while (result.size() < n) {\n // Push all tasks that can be processed at the current time.\n while (i < n && indexedTasks[i][0] <= time) {\n minHeap.push({indexedTasks[i][1], indexedTasks[i][2]}); // {processingTime, index}\n ++i;\n }\n \n if (!minHeap.empty()) {\n // Get the task with the smallest processing time.\n auto [procTime, idx] = minHeap.top();\n minHeap.pop();\n \n // Process this task.\n result.push_back(idx);\n time += procTime; // Move time forward by the processing time of this task.\n } else {\n // If the heap is empty, move time forward to the next task's enqueue time.\n time = indexedTasks[i][0];\n }\n }\n \n return result;\n}\n};", "memory": "154320" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "#define p pair<int,int>\nclass Solution {\npublic:\n\n struct lamda{\n bool operator()(p &p1,p &p2){\n if(p1.first==p2.first) return p1.second>p2.second;\n return p1.first>p2.first;\n }\n };\n\n\n vector<int> getOrder(vector<vector<int>> tasks) {\n for(int i=0;i<tasks.size();i++){\n tasks[i].push_back(i);\n }\n vector<int>result;\n int n=tasks.size();\n sort(tasks.begin(),tasks.end(),[](vector<int>&a,vector<int>&b){\n return a[0]<b[0];\n });\n long long curr_time=0;\n\n priority_queue<p,vector<p>,lamda>pq;\n int i=0;\n while(i<n || !pq.empty()){\n if(pq.empty() && tasks[i][0]>curr_time){\n curr_time=tasks[i][0];\n }\n\n while(i<n && curr_time>=tasks[i][0]){\n pq.push({tasks[i][1],tasks[i][2]});\n i++;\n }\n\n p curr_task=pq.top();\n pq.pop();\n\n curr_time+=curr_task.first;\n result.push_back(curr_task.second);\n }\n\n return result;\n }\n};", "memory": "155261" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n\n int rowSize = tasks.size();\n int colSize = 3;\n\n vector<vector<int>> arr(rowSize, vector<int>(3, 0)); // startingTime, processingTime, idx\n\n for(int i = 0; i < tasks.size(); i++){\n vector<int> task = tasks[i];\n\n arr[i] = {task[0], task[1], i}; \n }\n\n // sort by startingTime\n sort(arr.begin(), arr.end()); \n\n // for(int i = 0; i < arr.size(); i++){\n // vector<int> getVector = arr[i];\n // cout << getVector[0] << \" \" << getVector[1] << endl;\n // }\n\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;;\n vector<int> result;\n\n long long current_time = 0;\n int idx = 0;\n long long currentTime = 0;\n\n while(idx < arr.size() || !pq.empty()){\n\n // 0 --> StartTime, 1 --> ProcessingTime, 2 --> index :: Array\n\n // First One Processing, \n if(pq.empty() && currentTime <= arr[idx][0]){\n currentTime = arr[idx][0]; // updated time\n }\n\n // Already has update myStartTime\n while(idx < arr.size() && arr[idx][0] <= currentTime){ \n pq.push({arr[idx][1], arr[idx][2]}); // processingTime, index\n idx++;\n }\n \n pair<int, int> getTop = pq.top();\n pq.pop();\n\n currentTime += getTop.first; // e.g its processing time was 2 hours my StartTime updated\n\n result.push_back(getTop.second);\n }\n\n\n return result;\n }\n};", "memory": "155261" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n int n = tasks.size();\n vector<int> ans ;\n\n for(int i=0 ; i<n ; i++){\n tasks[i].push_back(i);\n }\n\n sort(tasks.begin() , tasks.end());\n priority_queue<vector<int> , vector<vector<int>>, greater<vector<int>>> pq;\n \n int maxEnqTime = 0;\n int i = 0;\n\n while(ans.size() < tasks.size()){\n\n if(i < tasks.size() && pq.empty()){\n maxEnqTime = max(maxEnqTime , tasks[i][0]);\n }\n\n while(i < tasks.size() && maxEnqTime >= tasks[i][0]){\n pq.push({tasks[i][1] , tasks[i][2]});\n i++;\n }\n\n maxEnqTime = min(1000000000 , maxEnqTime + pq.top()[0]);\n ans.push_back(pq.top()[1]);\n pq.pop();\n }\n return ans;\n }\n};", "memory": "156203" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n struct cmp {\n bool operator()(vector<int>& l, vector<int>& r) {\n if (l[1] == r[1]) \n return l[2] > r[2]; // Larger index has lower priority\n return l[1] > r[1]; // Larger processing time has lower priority\n }\n };\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<int>ans;\n for(int i = 0; i < tasks.size(); i++){\n tasks[i].push_back(i);\n }\n priority_queue<vector<int>,vector<vector<int>>,cmp>pq;\n sort(tasks.begin(), tasks.end());\n int i = 0;\n long long t = tasks[0][0];\n while (i < tasks.size() || !pq.empty()){\n if (pq.empty()){\n t = max(t, (long long)tasks[i][0]);\n }\n while(i < tasks.size() && tasks[i][0] <= t){\n pq.push(tasks[i]); i++;\n }\n if (!pq.empty()){\n ans.push_back(pq.top()[2]);\n t += pq.top()[1];\n pq.pop();\n }\n }\n return std::move(ans);\n }\n};", "memory": "156203" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n for(int i=0; i<tasks.size(); ++i){\n tasks[i].push_back(i);\n }\n\n sort(tasks.begin(), tasks.end());\n\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> pq;\n int i =0;\n\n long long timer = tasks[0][0];\n \n while(i<tasks.size() && tasks[0][0] >= tasks[i][0]){\n pq.push({tasks[i][1], tasks[i][2]});\n i++;\n }\n\n // cout << pq.size() << endl;\n\n vector<int> ans;\n\n while(!pq.empty()){\n int processTime = pq.top()[0];\n int index = pq.top()[1];\n pq.pop();\n\n timer+=processTime;\n\n ans.push_back(index);\n\n if(i<tasks.size() && pq.empty() && timer < tasks[i][0]){\n timer = tasks[i][0];\n }\n\n while(i<tasks.size() && timer >= tasks[i][0]){\n pq.push({tasks[i][1], tasks[i][2]});\n i++;\n }\n }\n\n return ans;\n }\n};", "memory": "157144" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "static auto init_io = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\nclass Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n int n = tasks.size();\n for(int i = 0; i < n; i++)\n {\n tasks[i].push_back(i);\n }\n sort(tasks.begin(), tasks.end());\n\n vector<int> ans;\n long long curTime = 0;\n int i = 0;\n priority_queue<vector<int>, vector<vector<int>>, greater<>> minHeap;\n while( i < n )\n {\n if (1 && curTime < tasks[i][0] && minHeap.empty())\n curTime = tasks[i][0];\n\n while( i < n && tasks[i][0] <= curTime )//( auto& [time, idxes] :timeIdx)\n {\n minHeap.push({tasks[i][1], tasks[i][2], tasks[i][0]});\n i++;\n }\n\n ans.push_back(minHeap.top()[1]);\n curTime += minHeap.top()[0];\n minHeap.pop();\n }\n while(!minHeap.empty())\n {\n ans.push_back(minHeap.top()[1]);\n curTime += minHeap.top()[0];\n minHeap.pop();\n }\n return ans;\n }\n};", "memory": "157144" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n \n // monotonic queue queued<enqueueTime, processingTime, index>\n // min pq free <processing time, index>\n\n vector<vector<int>> sortedTasks;\n for (int i = 0; i < tasks.size(); i++)\n sortedTasks.push_back({tasks[i][0], tasks[i][1], i});\n\n sort(sortedTasks.begin(), sortedTasks.end());\n\n priority_queue<pair<long, int>, vector<pair<long, int>>, greater<>> free;\n vector<int> executions;\n long currTime = 0;\n int taskNum = 0;\n while (taskNum != sortedTasks.size() || free.size()) {\n // if free empty, free first queued & all w/ the same enqueue time\n if (free.empty()) {\n currTime = sortedTasks[taskNum][0];\n while (taskNum != sortedTasks.size() && sortedTasks[taskNum][0] == currTime) {\n free.push(make_pair(sortedTasks[taskNum][1], sortedTasks[taskNum][2]));\n taskNum++;\n }\n }\n\n // execute top process in free, set currTime += processingTime\n executions.push_back(free.top().second);\n currTime += free.top().first;\n free.pop();\n // free all processes in queue that have enqueueTime <= currTime\n while (taskNum != sortedTasks.size() && sortedTasks[taskNum][0] <= currTime) {\n free.push(make_pair(sortedTasks[taskNum][1], sortedTasks[taskNum][2]));\n taskNum++;\n }\n }\n return executions;\n }\n};", "memory": "158085" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "#include <vector>\n#include <queue>\n#include <algorithm>\n\nusing namespace std;\n\nclass cmp1 {\npublic:\n // Custom comparator for the priority queue (min-heap)\n bool operator()(const pair<long long, int>& a, const pair<long long, int>& b) {\n // Sort by processing time first, if equal, then by index\n if (a.first == b.first) {\n return a.second > b.second;\n }\n return a.first > b.first;\n }\n};\n\nclass Solution {\npublic:\n // Static comparator for sorting tasks by enqueue time\n static bool cmp(const vector<int>& a, const vector<int>& b) {\n return a[0] < b[0];\n }\n\n vector<int> getOrder(vector<vector<int>>& v) {\n vector<vector<int>> tasks;\n \n // Preserve the original index of tasks\n for (int i = 0; i < v.size(); i++) {\n tasks.push_back({v[i][0], v[i][1], i});\n }\n\n // Sort tasks by enqueue time\n sort(tasks.begin(), tasks.end(), cmp);\n\n long long timer = 1; // Use long long for time variables\n priority_queue<pair<long long, int>, vector<pair<long long, int>>, cmp1> pq;\n int i = 0;\n vector<int> result;\n\n // Main processing loop\n while (i < tasks.size() || !pq.empty()) {\n // Push all available tasks to the priority queue\n while (i < tasks.size() && tasks[i][0] <= timer) {\n pq.push({tasks[i][1], tasks[i][2]});\n i++;\n }\n\n if (!pq.empty()) {\n // Process the task with the shortest processing time\n pair<long long, int> task = pq.top();\n pq.pop();\n timer += task.first; // Safely add processing time\n result.push_back(task.second); // Store the index of the processed task\n } else if (i < tasks.size()) {\n // If no tasks are in the heap, jump the timer to the next task's enqueue time\n timer = tasks[i][0];\n }\n }\n\n return result;\n }\n};\n", "memory": "159026" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<vector<long>> tasksMap;\n for (int i = 0; i < tasks.size(); ++i) {\n tasksMap.push_back({tasks[i][0], tasks[i][1], i});\n }\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; // processing time, index\n sort(tasksMap.begin(), tasksMap.end());\n long currTime = tasksMap[0][0];\n vector<int> result;\n int pos = 0;\n while (pos < tasksMap.size() || !pq.empty()) {\n // cout << pos << \" \" << pq.size() << endl;\n while (pos < tasksMap.size() && tasksMap[pos][0] <= currTime) {\n pq.push({tasksMap[pos][1], tasksMap[pos][2]});\n pos++;\n }\n if (!pq.empty()) {\n auto top = pq.top();\n // cout << top.first << \" \" << top.second << endl;\n pq.pop();\n currTime += top.first;\n result.push_back(top.second);\n } else {\n if (pos < tasksMap.size()) {\n currTime = tasksMap[pos][0];\n }\n }\n }\n return result;\n }\n};", "memory": "159026" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n priority_queue <pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;\n\n int n = tasks.size();\n map<int, vector<pair<int, int>>> m;\n\n for (int i = 0; i < n; i++) {\n m[tasks[i][0]].push_back({tasks[i][1], i});\n }\n\n auto it = m.begin();\n long long time = it->first;\n\n for (auto kvp : it->second) {\n pq.push({kvp.first, kvp.second});\n }\n it++;\n\n vector<int> result;\n\n while(pq.size() > 0) {\n auto top = pq.top();\n pq.pop();\n result.push_back(top.second);\n time += top.first;\n\n while (it != m.end() && time >= it->first) {\n for (auto items : it->second) {\n pq.push({items.first, items.second});\n }\n it++;\n }\n\n if (pq.size() == 0 && it != m.end()) {\n for (auto item : it->second) {\n pq.push({item.first, item.second});\n }\n time = it->first;\n it++;\n }\n }\n\n return result;\n }\n};", "memory": "159968" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<pair<vector<int>, int>> taskI;\n for(int i = 0; i < tasks.size(); i++)\n taskI.push_back({tasks[i],i});\n sort(taskI.begin(), taskI.end());\n vector<int> ans;\n long long int ci = 0, ct = taskI[0].first[0];\n pair<int,int> currEl;\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;\n while(ans.size() != taskI.size()){\n while(ci < taskI.size() && taskI[ci].first[0] <= ct){\n pq.push({taskI[ci].first[1], taskI[ci].second});\n ci++;\n }\n if(pq.empty()){\n ct = taskI[ci].first[0];\n continue;\n }\n currEl = pq.top();\n pq.pop();\n ans.push_back(currEl.second);\n ct += currEl.first;\n }\n\n return ans;\n }\n};", "memory": "160909" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Compare {\npublic:\n bool operator()(vector<int>& a, vector<int>& b) {\n if(a[1] == b[1])\n return a[2] > b[2];\n return a[1] > b[1];\n }\n};\n\nbool vectorComparator(vector<int>& a, vector<int>& b) {\n return a[0] < b[0];\n}\n\nclass Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n if (tasks.empty())\n return {};\n for(int i = 0; i < tasks.size(); i++) {\n tasks[i].push_back(i);\n }\n sort(tasks.begin(), tasks.end(), vectorComparator);\n priority_queue<vector<int>, vector<vector<int>>, Compare> pq;\n int curr_time = tasks[0][0], task_itr = 0;\n vector<int> ans;\n\n while(task_itr < tasks.size()) {\n while(task_itr < tasks.size() && tasks[task_itr][0] <= curr_time) {\n pq.push(tasks[task_itr]);\n task_itr++;\n }\n if(pq.empty()) {\n if(task_itr < tasks.size())\n curr_time = tasks[task_itr][0];\n continue;\n }\n vector<int> curr_task = pq.top();\n ans.push_back(curr_task[2]);\n int time_elapsed = curr_time + curr_task[1];\n pq.pop();\n curr_time = time_elapsed;\n }\n while(!pq.empty()) {\n ans.push_back(pq.top()[2]);\n pq.pop();\n }\n return ans;\n }\n};", "memory": "160909" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) \n {\n vector<pair<vector<int>, int >> tasksI;\n\n for (unsigned i = 0; i < tasks.size(); ++i)\n {\n tasksI.push_back({ tasks.at(i), i });\n }\n\n std::sort(tasksI.begin(), tasksI.end());\n\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>pqueue;\n\n std::vector<int> processed;\n\n long long int cycle = tasksI.begin()->first.at(0);\n std::size_t i = 0;\n int currentTaskIndex = -1;\n\n auto tasksNo = tasksI.size();\n\n while (true)\n {\n // Check if there are elements to insert in queue\n while (i < tasksI.size() && cycle >= tasksI[i].first.at(0))\n {\n pqueue.push(std::make_pair(tasksI[i].first.at(1), tasksI[i].second));\n i++;\n }\n \n if (!pqueue.empty())\n {\n auto it = pqueue.top();\n \n cycle += it.first;\n processed.emplace_back(it.second);\n\n if (processed.size() == tasksNo)\n {\n break;\n }\n\n pqueue.pop();\n }\n else\n {\n // Queue is empty\n cycle = tasksI[i].first[0];\n }\n \n }\n\n return processed;\n}\n};", "memory": "161850" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n priority_queue<pair<int , int> , vector<pair<int , int>> , greater<pair<int , int>>>pq;\n vector<pair<vector<int> , int >>v; \n vector<int>ans; \n for(int i = 0;i<tasks.size();i++)\n {\n v.push_back({tasks[i] , i});\n }\n sort(v.begin() , v.end());\n long long int ind = 0 , i = 0;\n while(true)\n {\n if(i<v.size() && v[i].first[0] > ind && pq.empty())\n {\n ind = v[i].first[0];\n }\n while(i<v.size() && v[i].first[0] <= ind)\n {\n pq.push({v[i].first[1] , v[i].second});\n i++;\n }\n if(i == v.size())\n {\n while(!pq.empty())\n {\n auto it = pq.top();\n pq.pop();\n ans.push_back(it.second);\n }\n break;\n }\n else\n {\n auto it = pq.top();\n pq.pop();\n ind += it.first;\n ans.push_back(it.second);\n }\n // ind++;\n }\n return ans;\n }\n};", "memory": "161850" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<pair<int, pair<int, int> > > tasksOrdered;\n vector<int> ans;\n\t\tint index=0;\n\t\tfor(auto task: tasks) {\n tasksOrdered.push_back({task[0],{task[1],index++}});\n }\n sort(tasksOrdered.begin(),tasksOrdered.end());\n int time=tasksOrdered[0].first;\n set<pair<int, int> > timeAndIndexSet;\n set<pair<int, int> >::iterator it;\n timeAndIndexSet.insert({tasksOrdered[0].second.first,tasksOrdered[0].second.second});\n int tasksOrderedIndex=1;\n while(!timeAndIndexSet.empty()) {\n it=timeAndIndexSet.begin();\n int processTime=(*it).first;\n int taskIndex=(*it).second;\n if(time<1000000000)\n time+=processTime;\n timeAndIndexSet.erase(it);\n while(tasksOrderedIndex<tasksOrdered.size() && tasksOrdered[tasksOrderedIndex].first<=time) {\n timeAndIndexSet.insert({tasksOrdered[tasksOrderedIndex].second.first,tasksOrdered[tasksOrderedIndex].second.second});\n tasksOrderedIndex++;\n }\n ans.push_back(taskIndex);\n if(timeAndIndexSet.empty() && tasksOrderedIndex<tasksOrdered.size()) {\n timeAndIndexSet.insert({tasksOrdered[tasksOrderedIndex].second.first,tasksOrdered[tasksOrderedIndex].second.second});\n time=tasksOrdered[tasksOrderedIndex].first;\n tasksOrderedIndex++;\n }\n }\n return ans;\n }\n};", "memory": "162791" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n priority_queue<pair<pair<int , int> , int> , vector<pair<pair<int , int> , int>> , greater<pair<pair<int , int> , int>>>pq;\n vector<pair<vector<int> , int >>v; \n vector<int>ans; \n for(int i = 0;i<tasks.size();i++)\n {\n v.push_back({tasks[i] , i});\n }\n sort(v.begin() , v.end());\n long long int ind = 0 , i = 0;\n while(true)\n {\n if(i<v.size() && v[i].first[0] > ind && pq.empty())\n {\n ind = v[i].first[0];\n }\n while(i<v.size() && v[i].first[0] <= ind)\n {\n pq.push({{v[i].first[1] , v[i].second} , v[i].first[0]});\n i++;\n }\n if(i == v.size())\n {\n while(!pq.empty())\n {\n auto it = pq.top();\n pq.pop();\n ans.push_back(it.first.second);\n }\n break;\n }\n else\n {\n auto it = pq.top();\n pq.pop();\n ind += it.first.first;\n ans.push_back(it.first.second);\n }\n }\n return ans;\n }\n};", "memory": "163733" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) \n {\n priority_queue<pair<pair<int , int> , int> , vector<pair<pair<int , int> , int>> , greater<pair<pair<int , int> , int>>>pq;\n \n vector<pair<vector<int> , int >>v; \n vector<int>ans;\n\n for(int i = 0;i<tasks.size();i++)\n v.push_back({tasks[i] , i});\n\n sort(v.begin() , v.end());\n\n long long int ind = 0 , i = 0;\n\n while(true)\n {\n if(i<v.size() && v[i].first[0] > ind && pq.empty())\n ind = v[i].first[0];\n \n while(i<v.size() && v[i].first[0] <= ind)\n {\n pq.push({{v[i].first[1] , v[i].second} , v[i].first[0]});\n i++;\n }\n if(i == v.size())\n {\n while(!pq.empty())\n {\n auto it = pq.top();\n pq.pop();\n ans.push_back(it.first.second);\n }\n break;\n }\n else\n {\n auto it = pq.top();\n pq.pop();\n ind += it.first.first;\n ans.push_back(it.first.second);\n }\n }\n return ans;\n }\n};", "memory": "163733" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>> tasks) {\n int n = tasks.size();\n for (int i = 0; i < n; i++) tasks[i].push_back(i);\n tasks.push_back({(int) 2e9, (int) 2e9, (int) 2e9});\n std::priority_queue<pair<int, int>, vector<pair<int, int>>, std::greater<pair<int, int>>> q;\n sort(tasks.begin(), tasks.end());\n int t = tasks[0][0];\n vector<int> ans;\n int i = 0;\n while (i < n) {\n while (!q.empty() && t < tasks[i][0]) {\n cout << \"Pop \" << q.top().second << \" at a moment \" << t << '\\n';\n ans.push_back(q.top().second);\n t += q.top().first;\n q.pop();\n }\n int old_i = i;\n while (t >= tasks[i][0]) {\n q.push(pair(tasks[i][1], tasks[i][2]));\n cout << \"Push \" << tasks[i][0] << ' ' << tasks[i][1] << ' ' << tasks[i][2] << \" at a moment \" << t << '\\n';\n i++;\n }\n if (i == old_i)\n t = tasks[i][0];\n }\n while (!q.empty()) {\n ans.push_back(q.top().second);\n q.pop();\n }\n return ans;\n }\n};", "memory": "164674" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>> tasks) {\n int n = tasks.size();\n for (int i = 0; i < n; i++) tasks[i].push_back(i);\n tasks.push_back({(int) 2e9, (int) 2e9, (int) 2e9});\n std::priority_queue<pair<int, int>, vector<pair<int, int>>, std::greater<pair<int, int>>> q;\n sort(tasks.begin(), tasks.end());\n int t = tasks[0][0];\n vector<int> ans;\n int i = 0;\n while (i < n) {\n while (!q.empty() && t < tasks[i][0]) {\n cout << \"Pop \" << q.top().second << \" at a moment \" << t << '\\n';\n ans.push_back(q.top().second);\n t += q.top().first;\n q.pop();\n }\n int old_i = i;\n while (t >= tasks[i][0]) {\n q.push(pair(tasks[i][1], tasks[i][2]));\n cout << \"Push \" << tasks[i][0] << ' ' << tasks[i][1] << ' ' << tasks[i][2] << \" at a moment \" << t << '\\n';\n i++;\n }\n if (i == old_i)\n t = tasks[i][0];\n }\n while (!q.empty()) {\n ans.push_back(q.top().second);\n q.pop();\n }\n return ans;\n }\n};", "memory": "164674" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n static bool cmp(const vector<int>&arr,const vector<int>&brr)\n {\n if(arr[0]==brr[0])\n {\n return arr[1]<brr[1];\n }\n return arr[0]<brr[0];\n }\n\n vector<int> getOrder(vector<vector<int>>& tasks) \n {\n vector<int>result;\n vector<vector<int>>arr;\n\n for(int i=0;i<tasks.size();i++)\n {\n int at=tasks[i][0];\n int bt=tasks[i][1];\n arr.push_back({at,bt,i});\n }\n\n sort(arr.begin(),arr.end(),cmp);\n int n=arr.size();\n\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n\n for(auto it:arr)\n {\n cout<<it[0]<<\" \"<<it[1]<<\" \"<<it[2]<<endl;\n }\n long long curr=0;\n int i=0;\n\n while(i<n||!pq.empty())\n {\n if(pq.empty() && arr[i][0]>curr)\n {\n curr=arr[i][0];\n }\n\n while(i<n && arr[i][0]<=curr)\n {\n pq.push({arr[i][1],arr[i][2]});\n i++;\n }\n\n if(!pq.empty())\n {\n auto it=pq.top();\n int burst=it.first;\n int id=it.second;\n curr+=burst;\n result.push_back(id);\n pq.pop();\n }\n }\n\n for(auto it:result)\n {\n cout<<it<<\" \";\n }\n\n return result; \n }\n};", "memory": "165615" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n/**\n[2, 15], [12, 24], [16, 9], [18, 18], [19, 13], [21, 10], []\n[2, 15] - 17 - 6\n[12, 24], [16, 9]\n[16, 9] - 25 - 1\n[12, 24], [19, 13], [21, 10], [18, 18]\n[21, 10] - 31 - 2\n[12, 24], [19, 13], [18, 18]\n**/\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<pair<long long int, pair<long long int, int> > > intervals;\n multiset<pair<pair<long long int ,int>, long long int> > set1;\n for(int i = 0; i < tasks.size(); i++) {\n intervals.push_back({tasks[i][0], {tasks[i][0] + tasks[i][1], i}});\n }\n sort(intervals.begin(), intervals.end());\n pair<int, long long int> curr = {intervals[0].second.second, intervals[0].second.first};\n vector<int> ans;\n int i = 1;\n while( i < intervals.size()) {\n if(intervals[i].first <= curr.second) {\n set1.insert({{intervals[i].second.first - intervals[i].first, intervals[i].second.second}, intervals[i].second.first});\n //cout<<\"inserted \"<< \n i++;\n } else if(intervals[i].first > curr.second) {\n ans.push_back(curr.first);\n // cout<<\"set size \"<<set1.size()<<endl;\n if(set1.size() > 0) {\n pair<pair<long long int, int>, long long int> top = *(set1.begin());\n set1.erase(set1.begin());\n long long int curr_time = curr.second;\n curr = {top.first.second, top.first.first + curr_time};\n // cout<<\"set size \"<<set1.size()<<endl;\n } else {\n curr = {intervals[i].second.second, intervals[i].second.first};\n i++;\n }\n }\n }\n ans.push_back(curr.first);\n while(!set1.empty()) {\n pair<pair<long long int, int>, long long int> top = *(set1.begin());\n set1.erase(set1.begin());\n ans.push_back(top.first.second);\n }\n return ans;\n }\n};", "memory": "171263" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "#define ppi pair<pair<int,int>,int>\n#define pi pair<int,int>\n\n\nclass Compare\n{\npublic:\n bool operator()(ppi&p1,ppi&p2)\n {\n if(p1.first.second==p2.first.second)\n {\n return p1.second > p2.second;\n }\n return p1.first.second>p2.first.second;\n }\n\n};\n\nclass Solution {\npublic:\n\n static bool cmp(vector<int>&v1,vector<int>&v2)\n {\n if(v1[0]==v2[0])\n {\n return v1[1]<v2[1];\n }\n return v1[0]<v2[0];\n }\n vector<int> getOrder(vector<vector<int>>& tsk) {\n\n int n=tsk.size();\n vector<int>ans;\n map<pair<int,int>,vector<int>>mp;\n\n \n //storing idx possible duplicates\n for(int i=0;i<tsk.size();i++)\n {\n mp[{tsk[i][0],tsk[i][1]}].push_back(i);\n }\n\n\n sort(tsk.begin(),tsk.end(),cmp);//sort\n priority_queue<ppi,vector<ppi>,Compare>pq;//customise pq\n\n\n \n long long curr_time=0;\n pq.push({{tsk[0][0],tsk[0][1]},mp[{tsk[0][0],tsk[0][1]}][0]});\n curr_time=tsk[0][0];\n int idx=1;\n\n\n\n while(!pq.empty())\n {\n auto it=pq.top();\n pq.pop();\n ans.push_back(it.second);\n curr_time+=it.first.second;\n\n//after proceesing all pq elements,curr time still not greater so increse curr time ie time for which it was idle skipped\n if(pq.empty() && idx<tsk.size() && curr_time < tsk[idx][0] )\n {\n curr_time=tsk[idx][0];\n }\n \n//add all posssible process that can be execute\n while(idx<tsk.size() && tsk[idx][0]<=curr_time)\n {\n //add all occurences\n for(auto it:mp[{tsk[idx][0],tsk[idx][1]}])\n {\n pq.push({{tsk[idx][0],tsk[idx][1]},it});\n }\n idx+=mp[{tsk[idx][0],tsk[idx][1]}].size();//duplicates skip\n }\n }\n\n return ans; \n }\n};", "memory": "171263" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n const int n = tasks.size();\n vector<vector<int>> copyTask; \n \n for(int i = 0; i < n; ++i) {\n int start = tasks[i][0];\n int duration = tasks[i][1];\n \n copyTask.push_back({start, duration, i});\n }\n \n sort(copyTask.begin(), copyTask.end());\n \n auto compare = [](const vector<int> &a, const vector<int> &b) {\n if(a[1] == b[1]) {\n return a[2] > b[2];\n }\n return a[1] > b[1];\n };\n priority_queue<vector<int>, vector<vector<int>>, decltype(compare)> pq(compare);\n \n vector<int> res(n, -1);\n int index = 0, popped = 0;\n long currTime = copyTask[0][0];\n \n while(popped < n) {\n while(index < n && copyTask[index][0] <= currTime) {\n pq.push(copyTask[index]);\n index++;\n }\n \n if(pq.size()) {\n int duration = pq.top()[1];\n res[popped] = pq.top()[2];\n\n popped++;\n currTime += duration;\n\n pq.pop();\n }\n else {\n currTime = copyTask[index][0];\n }\n }\n \n return res;\n }\n};", "memory": "172204" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n const int n = tasks.size();\n vector<vector<int>> copyTask; \n \n for(int i = 0; i < n; ++i) {\n int start = tasks[i][0];\n int duration = tasks[i][1];\n \n copyTask.push_back({start, duration, i});\n }\n \n sort(copyTask.begin(), copyTask.end());\n \n auto compare = [](const vector<int> &a, const vector<int> &b) {\n if(a[1] == b[1]) {\n return a[2] > b[2];\n }\n return a[1] > b[1];\n };\n priority_queue<vector<int>, vector<vector<int>>, decltype(compare)> pq(compare);\n \n vector<int> res(n, -1);\n int index = 0, popped = 0;\n long currTime = copyTask[0][0];\n \n while(popped < n) {\n while(index < n && copyTask[index][0] <= currTime) {\n pq.push(copyTask[index]);\n index++;\n }\n \n if(pq.size()) {\n int duration = pq.top()[1];\n res[popped] = pq.top()[2];\n\n popped++;\n currTime += duration;\n\n pq.pop();\n }\n else {\n currTime = copyTask[index][0];\n }\n }\n \n return res;\n }\n};", "memory": "172204" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n const int n = tasks.size();\n vector<vector<int>> copyTask; \n \n for(int i = 0; i < n; ++i) {\n int start = tasks[i][0];\n int duration = tasks[i][1];\n \n copyTask.push_back({start, duration, i});\n }\n \n auto comparator = [](const vector<int> &a, const vector<int> &b) {\n if(a[1] == b[1]) {\n return a[2] < b[2];\n }\n return a[0] < b[0];\n };\n sort(copyTask.begin(), copyTask.end());\n \n auto compare = [](const vector<int> &a, const vector<int> &b) {\n if(a[1] == b[1]) {\n return a[2] > b[2];\n }\n return a[1] > b[1];\n };\n priority_queue<vector<int>, vector<vector<int>>, decltype(compare)> pq(compare);\n \n vector<int> res(n, -1);\n int index = 0, popped = 0;\n long currTime = copyTask[0][0];\n \n while(popped < n) {\n while(index < n && copyTask[index][0] <= currTime) {\n pq.push(copyTask[index]);\n index++;\n }\n \n if(pq.size()) {\n int duration = pq.top()[1];\n res[popped] = pq.top()[2];\n\n popped++;\n currTime += duration;\n\n pq.pop();\n }\n else {\n currTime = copyTask[index][0];\n }\n }\n \n return res;\n }\n};", "memory": "173145" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n const int n = tasks.size();\n vector<vector<int>> copyTask; \n \n for(int i = 0; i < n; ++i) {\n int start = tasks[i][0];\n int duration = tasks[i][1];\n \n copyTask.push_back({start, duration, i});\n }\n \n sort(copyTask.begin(), copyTask.end());\n \n \n auto compare = [](const vector<int> &a, const vector<int> &b) {\n if(a[1] == b[1]) {\n return a[2] > b[2];\n }\n return a[1] > b[1];\n };\n priority_queue<vector<int>, vector<vector<int>>, decltype(compare)> pq(compare);\n \n vector<int> res(n, -1);\n int index = 0, popped = 0;\n long currTime = copyTask[0][0];\n \n while(popped < n) {\n while(index < n && copyTask[index][0] <= currTime) {\n pq.push(copyTask[index]);\n index++;\n }\n \n if(pq.size()) {\n int duration = pq.top()[1];\n res[popped] = pq.top()[2];\n\n popped++;\n currTime += duration;\n\n pq.pop();\n }\n else {\n currTime = copyTask[index][0];\n }\n }\n \n return res;\n }\n};", "memory": "173145" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>> tasks)\n {\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> priority_tasks;\n \n vector<vector<int>> all_tasks;\n\n for(int idx = 0; idx < tasks.size(); idx++)\n {\n all_tasks.push_back({tasks[idx][0], tasks[idx][1], idx});\n }\n \n sort(all_tasks.begin(), all_tasks.end());\n\n vector<int> result;\n\n long long time = all_tasks[0][0];\n int it = 0;\n int n = all_tasks.size();\n \n while(it < n || !priority_tasks.empty())\n {\n while(it < n && all_tasks[it][0] <= time)\n {\n priority_tasks.push({all_tasks[it][1], all_tasks[it][2]});\n\n it++;\n }\n\n if (!priority_tasks.empty()) {\n time += priority_tasks.top().first;\n\n result.push_back(priority_tasks.top().second);\n\n priority_tasks.pop();\n }\n else\n {\n time = all_tasks[it][0];\n }\n }\n\n return result;\n }\n};", "memory": "174086" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<vector<int>> original;// start Time, Process Time, index\n priority_queue<vector<long long>, vector<vector<long long>>, greater<vector<long long>> > PQ; // Process Time, index. \n //sort(tasks.begin(),tasks.end());\n for(int i=0;i<tasks.size();i++) {\n original.push_back({tasks[i][0], tasks[i][1], i});\n }\n sort(original.begin(), original.end());\n for(int i=0;i<original.size();i++) cout<<original[i][2]<<endl;\n int nextStart = 0;\n vector<int> ans;\n int indexOfOrig = 0;\n while(indexOfOrig<original.size() || !PQ.empty()) {\n while(indexOfOrig<original.size() && nextStart >= original[indexOfOrig][0]) {\n PQ.push({original[indexOfOrig][1], original[indexOfOrig][2]});\n indexOfOrig++;\n }\n if(indexOfOrig<original.size() && PQ.empty()) {\n nextStart = max(original[indexOfOrig][0], nextStart) + original[indexOfOrig][1];\n ans.push_back(original[indexOfOrig][2]);\n indexOfOrig++;\n } else if(!PQ.empty()) {\n nextStart = nextStart + PQ.top()[0];\n ans.push_back(PQ.top()[1]);\n PQ.pop();\n }\n }\n return ans;\n }\n};", "memory": "175028" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n//enquetime,\n \n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<vector<int>> v;\n for(int i=0;i<tasks.size();i++)\n {\n v.push_back({tasks[i][0],tasks[i][1],i});\n }\n sort(v.begin(),v.end());\n priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>> > pq;\n int idx=0;\n long long currtime=0;\n vector<int> res;\n while(!pq.empty() || idx<tasks.size())\n {\n if(pq.empty() && currtime < v[idx][0])\n {\n currtime=v[idx][0];\n }\n while(idx < tasks.size() && currtime >=v[idx][0])\n {\n pq.push({v[idx][1],v[idx][2]});\n idx++;\n }\n currtime += pq.top()[0];\n res.push_back(pq.top()[1]);\n pq.pop();\n }\n return res;\n }\n};", "memory": "175969" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<vector<int>> sortedTasks;\n int n = tasks.size();\n for(int i=0;i<tasks.size();i++){\n sortedTasks.push_back({tasks[i][0], tasks[i][1], i});\n }\n sort(sortedTasks.begin(), sortedTasks.end());\n vector<int> res;\n priority_queue<vector<int>, vector<vector<int>>, greater<>> pq;\n int i = 0;\n long long startTime = sortedTasks[0][0];\n while(i<n || !pq.empty()){\n while(i<n && sortedTasks[i][0]<=startTime){\n pq.push({sortedTasks[i][1], sortedTasks[i][2]});\n i++;\n }\n\n if(pq.empty() && i<n){\n startTime = sortedTasks[i][0];\n }\n else{\n res.push_back(pq.top()[1]);\n startTime += pq.top()[0];\n pq.pop();\n }\n \n }\n return res;\n }\n};", "memory": "176910" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n static bool compare(const vector<int>& a, const vector<int>& b) {\n if (a[1] == b[1]) {\n return a[2] > b[2];\n }\n return a[1] > b[1];\n }\n vector<int> getOrder(vector<vector<int>>& tasks) {\n priority_queue<vector<int>, vector<vector<int>>, decltype(&compare)> q(compare);\n vector<int> out;\n vector<vector<int>> store;\n for(int i = 0;i<tasks.size();i++) {\n store.push_back({tasks[i][0], tasks[i][1], i});\n }\n sort(store.begin(), store.end(), [](const vector<int>& a, const vector<int>& b){\n return a[0] < b[0];\n });\n int n = tasks.size();\n int i = 0;\n long long stime = store[i][0];\n while (n>0) {\n bool tasks_added = false;\n // Push the next tasks with same start time task.\n while(i < tasks.size() && store[i][0] <= stime) {\n q.push(store[i++]);\n tasks_added = true;\n }\n // If no tasks to process at current time move on to the next task.\n if(!tasks_added && i < tasks.size() && q.size() == 0) {\n stime = store[i][0];\n }\n // Process tasks.\n if(q.size() > 0) {\n const vector<int>& val = q.top();\n out.push_back(val[2]);\n stime += val[1];\n q.pop();\n n--;\n }\n }\n return out;\n }\n};", "memory": "177851" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<vector<int>> original;// start Time, Process Time, index\n priority_queue<vector<long long>, vector<vector<long long>>, greater<vector<long long>> > PQ; // Process Time, index. \n //sort(tasks.begin(),tasks.end());\n for(int i=0;i<tasks.size();i++) {\n original.push_back({tasks[i][0], tasks[i][1], i});\n }\n sort(original.begin(), original.end());\n for(int i=0;i<original.size();i++) cout<<original[i][2]<<endl;\n int nextStart = 0;\n vector<int> ans;\n int indexOfOrig = 0;\n while(indexOfOrig<original.size() || !PQ.empty()) {\n while(indexOfOrig<original.size() && nextStart >= original[indexOfOrig][0]) {\n PQ.push({original[indexOfOrig][1], original[indexOfOrig][2]});\n indexOfOrig++;\n }\n if(indexOfOrig<original.size() && PQ.empty()) {\n nextStart = max(original[indexOfOrig][0], nextStart) + original[indexOfOrig][1];\n ans.push_back(original[indexOfOrig][2]);\n indexOfOrig++;\n } else if(!PQ.empty()) {\n nextStart = nextStart + PQ.top()[0];\n ans.push_back(PQ.top()[1]);\n PQ.pop();\n }\n }\n return ans;\n }\n};", "memory": "177851" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n int n = tasks.size();\n vector<vector<int>> t(n, vector<int>(3));\n for (int i = 0; i < n; i++) {\n t[i][0] = tasks[i][0];\n t[i][1] = tasks[i][1];\n t[i][2] = i;\n }\n sort(t.begin(), t.end());\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> pq;\n int i = 0;\n long long time = 0;\n vector<int> ans;\n\n while (i < n || !pq.empty()) {\n if (pq.empty()) {\n time = max(time, (long long)t[i][0]);\n }\n while (i < n && t[i][0] <= time) {\n pq.push({t[i][1], t[i][2], t[i][0]});\n i++;\n }\n auto it = pq.top();\n pq.pop();\n int p = it[0];\n int ind = it[1];\n int e = it[2];\n ans.push_back(ind);\n time += p;\n }\n\n return ans;\n }\n};", "memory": "178793" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n std::vector<int> getOrder(std::vector<std::vector<int>>& tasks) {\n int n = tasks.size(); //declares an integer n and initializes it with the number of tasks\n std::vector<std::vector<int>> indexedTasks(n);\n\n for (int i = 0; i < n; ++i) {\n indexedTasks[i] = {tasks[i][0], tasks[i][1], i};\n } // this loop goes through each task, and for each task, it creates a new vector containing the enqueue time, processing time, and the original index of the task, and stores this new vector in indexedTasks\n\n std::sort(indexedTasks.begin(), indexedTasks.end());\n //sorts indexedTasks in ascending order based on their enqueue time.\n\n std::priority_queue<std::vector<int>, std::vector<std::vector<int>>, std::greater<>> pq;\n //declares a priority queue pq that stores vectors of integers the priority queue is ordered by std::greater<> which means it’s a min-heap\n\n std::vector<int> result;\n int index = 0;\n long long current_time = 0; \n \n while (!pq.empty() || index < n) {\n while (index < n && indexedTasks[index][0] <= current_time) {\n pq.push({indexedTasks[index][1], indexedTasks[index][2]});\n index++;\n //inner loop pushes tasks whose enqueue time is less than or equal to current_time into the priority queue, and increments index\n }\n \n if (pq.empty()) {\n current_time = indexedTasks[index][0];\n continue;\n }\n // priority queue is empty, this code updates current_time to the enqueue time of the next task and skips the rest of the loop.\n \n auto task = pq.top();\n pq.pop();\n \n int processingTime = task[0];\n int originalIndex = task[1];\n \n current_time += processingTime; \n result.push_back(originalIndex);\n //update current_time by adding the processing time of the task, and add the original index of the task to result\n }\n \n return result;\n //returns result, which is the order of execution of the tasks.\n }\n};\n", "memory": "178793" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n \n // monotonic queue queued<enqueueTime, processingTime, index>\n // min pq free <processing time, index>\n\n vector<vector<int>> list;\n for (int i = 0; i < tasks.size(); i++)\n list.push_back({tasks[i][0], tasks[i][1], i});\n\n sort(list.begin(), list.end());\n queue<vector<int>> queued;\n for (const vector<int>& v : list) {\n queued.push(v);\n }\n\n priority_queue<pair<long, int>, deque<pair<long, int>>, greater<>> free;\n vector<int> executions;\n long currTime = 0;\n while (queued.size() + free.size()) {\n // if free empty, free first queued & all w/ the same enqueue time\n if (free.empty()) {\n currTime = queued.front()[0];\n while (queued.size() && queued.front()[0] == currTime) {\n free.push(make_pair(queued.front()[1], queued.front()[2]));\n queued.pop();\n }\n }\n\n // execute top process in free, set currTime += processingTime\n executions.push_back(free.top().second);\n currTime += free.top().first;\n free.pop();\n // free all processes in queue that have enqueueTime <= currTime\n while (queued.size() && queued.front()[0] <= currTime) {\n free.push(make_pair(queued.front()[1], queued.front()[2]));\n queued.pop();\n }\n }\n return executions;\n }\n};", "memory": "179734" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "\n\nclass Solution {\npublic:\n // Comparator for the priority queue (min-heap by the first element)\n \n\n vector<int> getOrder(vector<vector<int>>& tasks) {\n int n = tasks.size();\n vector<vector<int>> newy; \n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> q;\n vector<int> ans;\n\n // Prepare newy with indices\n for(int i = 0; i < n; i++) {\n vector<int> temp = tasks[i];\n temp.push_back(i);\n newy.push_back(temp);\n }\n\n // Sort based on the task start times\n sort(newy.begin(), newy.end());\n\n long long time = newy[0][0];\n int i = 0;\n\n // Process tasks based on their start times and durations\n while(!q.empty() || i < n) {\n // Add all tasks that can start at or before the current time\n while(i < n && time >= newy[i][0]) {\n q.push({newy[i][1], newy[i][2]});\n i++;\n }\n\n if(q.empty()) {\n // Move to the start time of the next task\n if (i < n) {\n time = newy[i][0];\n }\n } else {\n // Process the task with the smallest duration\n ans.push_back(q.top().second);\n time += q.top().first;\n q.pop();\n }\n }\n\n return ans;\n }\n};\n", "memory": "180675" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "\nclass Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<vector<int>>t;\n int i=0;\n for(auto task:tasks){\n \n task.push_back(i);\n t.push_back(task);\n i++;\n }\n sort(begin(t),end(t));\n long long time=0;\n int j=0;\n int n=tasks.size();\n vector<int>ans;\n priority_queue<pair<int,int>>pq;\n while(j<n|| !pq.empty()){\n while(j<n&&t[j][0]<=time){\n pq.push({-t[j][1],-t[j][2]});\n j++;\n }\n if(pq.empty()){\n time=t[j][0];\n continue;\n }\n auto top=pq.top();\n pq.pop();\n ans.push_back(-top.second);\n time+=-top.first;\n }\n return ans;\n }\n};", "memory": "181616" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n int n=tasks.size();\n vector<vector<int>>taskCp;\n for(int i=0;i<n;i++){\n auto v=tasks[i];\n v.push_back(i);\n taskCp.push_back(v);\n }\n sort(taskCp.begin(),taskCp.end());\n vector<int>ans;\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n int i=0;\n long time=0;\n while(i<n || pq.size()){\n while(i<n && taskCp[i][0]<=time){\n pq.push({taskCp[i][1],taskCp[i][2]});\n i++;\n }\n if(!pq.size()){\n pq.push({taskCp[i][1],taskCp[i][2]});\n time=taskCp[i][0];\n i++;\n }\n auto task=pq.top();\n pq.pop();\n time+=long(task.first);\n ans.push_back(task.second);\n \n }\n return ans;\n }\n};", "memory": "181616" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "struct cmp {\n constexpr bool operator()( vector<int> const& a, vector<int> const& b) const noexcept {\n return a[0] < b[0] || a[0] == b[0] && a[2] > b[2];\n }\n};\n\nclass Solution {\n\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n ios::sync_with_stdio(false);\n vector<vector<int>> tmp;\n for(int i = 0; i < tasks.size(); i++){\n tmp.push_back({ tasks[i][0], tasks[i][1], i });\n }\n sort(tmp.begin(), tmp.end(), [](vector<int> &a, vector<int> &b){\n return a[0] < b[0];\n });\n priority_queue<vector<int>, vector<vector<int>>, cmp> q;\n vector<int> ans;\n int t = tmp[0][0];\n int i = 0;\n while(i < tmp.size()){\n if(!q.empty()){\n auto cur = q.top(); q.pop();\n ans.push_back(cur[2]);\n t += -cur[0];\n } else {\n t = max(t, tmp[i][0]);\n }\n while(i < tmp.size() && tmp[i][0] <= t){\n q.push({ -tmp[i][1], tmp[i][2], tmp[i][2] });\n i++;\n }\n }\n while(!q.empty()){\n ans.push_back(q.top()[2]);\n q.pop();\n }\n return ans;\n }\n};", "memory": "182558" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n \n // monotonic queue queued<enqueueTime, processingTime, index>\n // min pq free <processing time, index>\n\n vector<vector<int>> list;\n for (int i = 0; i < tasks.size(); i++)\n list.push_back({tasks[i][0], tasks[i][1], i});\n\n sort(list.begin(), list.end());\n queue<vector<int>> queued;\n for (const vector<int>& v : list) {\n queued.push(v);\n }\n\n priority_queue<pair<long, int>, vector<pair<long, int>>, greater<>> free;\n vector<int> executions;\n long currTime = 0;\n while (queued.size() + free.size()) {\n // if free empty, free first queued & all w/ the same enqueue time\n if (free.empty()) {\n currTime = queued.front()[0];\n while (queued.size() && queued.front()[0] == currTime) {\n free.push(make_pair(queued.front()[1], queued.front()[2]));\n queued.pop();\n }\n }\n\n // execute top process in free, set currTime += processingTime\n executions.push_back(free.top().second);\n currTime += free.top().first;\n free.pop();\n // free all processes in queue that have enqueueTime <= currTime\n while (queued.size() && queued.front()[0] <= currTime) {\n free.push(make_pair(queued.front()[1], queued.front()[2]));\n queued.pop();\n }\n }\n return executions;\n }\n};", "memory": "182558" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n \n // monotonic queue queued<enqueueTime, processingTime, index>\n // min pq free <processing time, index>\n\n vector<vector<int>> list;\n for (int i = 0; i < tasks.size(); i++)\n list.push_back({tasks[i][0], tasks[i][1], i});\n\n sort(list.begin(), list.end());\n queue<vector<int>> queued;\n for (const vector<int>& v : list) {\n queued.push(v);\n }\n\n priority_queue<pair<long, int>, vector<pair<long, int>>, greater<>> free;\n vector<int> executions;\n long currTime = 0;\n while (queued.size() + free.size()) {\n // if free empty, free first queued & all w/ the same enqueue time\n if (free.empty()) {\n currTime = queued.front()[0];\n while (queued.size() && queued.front()[0] == currTime) {\n free.push(make_pair(queued.front()[1], queued.front()[2]));\n queued.pop();\n }\n }\n\n // execute top process in free, set currTime += processingTime\n executions.push_back(free.top().second);\n currTime += free.top().first;\n free.pop();\n // free all processes in queue that have enqueueTime <= currTime\n while (queued.size() && queued.front()[0] <= currTime) {\n free.push(make_pair(queued.front()[1], queued.front()[2]));\n queued.pop();\n }\n }\n return executions;\n }\n};", "memory": "183499" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n \n // monotonic queue queued<enqueueTime, processingTime, index>\n // min pq free <processing time, index>\n\n vector<vector<int>> list;\n for (int i = 0; i < tasks.size(); i++)\n list.push_back({tasks[i][0], tasks[i][1], i});\n\n sort(list.begin(), list.end());\n queue<vector<int>> queued;\n for (const vector<int>& v : list) {\n queued.push(v);\n }\n\n priority_queue<pair<long, int>, vector<pair<long, int>>, greater<>> free;\n vector<int> executions;\n long currTime = 0;\n while (queued.size() + free.size()) {\n // if free empty, free first queued & all w/ the same enqueue time\n if (free.empty()) {\n currTime = queued.front()[0];\n while (queued.size() && queued.front()[0] == currTime) {\n free.push(make_pair(queued.front()[1], queued.front()[2]));\n queued.pop();\n }\n }\n\n // execute top process in free, set currTime += processingTime\n executions.push_back(free.top().second);\n currTime += free.top().first;\n free.pop();\n // free all processes in queue that have enqueueTime <= currTime\n while (queued.size() && queued.front()[0] <= currTime) {\n free.push(make_pair(queued.front()[1], queued.front()[2]));\n queued.pop();\n }\n }\n return executions;\n }\n};", "memory": "184440" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n set<pair<int, int> > processingTimeAndIndexSet;\n set<pair<int, int> >::iterator it;\n vector<vector<int>> tasksWithIndex;\n for(int i=0;i<tasks.size();i++) {\n tasksWithIndex.push_back(tasks[i]);\n tasksWithIndex[i].push_back(i);\n }\n sort(tasksWithIndex.begin(), tasksWithIndex.end());\n int i=0;\n long long time=0;\n vector<int> ans;\n while(i<tasksWithIndex.size() || !processingTimeAndIndexSet.empty()) {\n if(processingTimeAndIndexSet.empty() && time<tasksWithIndex[i][0]) {\n processingTimeAndIndexSet.insert({tasksWithIndex[i][1],tasksWithIndex[i][2]});\n time=tasksWithIndex[i][0];\n i++;\n }\n while(i<tasksWithIndex.size() && tasksWithIndex[i][0]<=time) {\n processingTimeAndIndexSet.insert({tasksWithIndex[i][1],tasksWithIndex[i][2]});\n i++;\n }\n it=processingTimeAndIndexSet.begin();\n ans.push_back((*it).second);\n time+=(*it).first;\n processingTimeAndIndexSet.erase(it);\n }\n return ans;\n }\n\n};", "memory": "184440" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n static bool sortvec(vector<int>&a, vector<int>&b){\n if(a[0]!=b[0])\n return a[0]<b[0];\n else if(a[1]!=b[1])\n return a[1]<b[1];\n else\n return a[2]<b[2];\n }\n vector<int> getOrder(vector<vector<int>>& tasks) {\n for(int i=0;i<tasks.size();i++){\n tasks[i].push_back(i);\n }\n sort(tasks.begin(),tasks.end(),sortvec);\n // for(int i=0;i<tasks.size();i++){\n // cout<<tasks[i][0]<<\" \"<<tasks[i][1]<<\" \"<<tasks[i][2]<<endl;\n // }\n vector<int>ans;\n long long int time=tasks[0][0];\n int i=1;\n priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>>minh;\n minh.push({tasks[0][1],tasks[0][0],tasks[0][2]});\n while(!minh.empty()){\n int ptime=minh.top()[0];\n int ind=minh.top()[2];\n vector<vector<int>>temp;\n while(!minh.empty() && minh.top()[0]==ptime){\n ind=min(ind,minh.top()[2]);\n temp.push_back(minh.top());\n minh.pop();\n }\n for(int i=0;i<temp.size();i++){\n if((temp[i][2])!=ind){\n minh.push(temp[i]);\n }\n }\n ans.push_back(ind); \n if(i<tasks.size() && minh.empty() && time+ptime<tasks[i][0]){\n time=tasks[i][0];\n }\n else\n time=time+ptime;\n while(i<tasks.size()){\n if(tasks[i][0]<=time){\n minh.push({tasks[i][1],tasks[i][0],tasks[i][2]});\n i++;\n }\n else\n break;\n }\n }\n return ans;\n }\n \n\n\n};", "memory": "185381" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n static bool sortvec(vector<int>&a, vector<int>&b){\n if(a[0]!=b[0])\n return a[0]<b[0];\n else if(a[1]!=b[1])\n return a[1]<b[1];\n else\n return a[2]<b[2];\n }\n vector<int> getOrder(vector<vector<int>>& tasks) {\n for(int i=0;i<tasks.size();i++){\n tasks[i].push_back(i);\n }\n sort(tasks.begin(),tasks.end(),sortvec);\n for(int i=0;i<tasks.size();i++){\n cout<<tasks[i][0]<<\" \"<<tasks[i][1]<<\" \"<<tasks[i][2]<<endl;\n }\n vector<int>ans;\n //ans.push_back(0);\n long long int time=tasks[0][0];\n int i=1;\n priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>>minh;\n minh.push({tasks[0][1],tasks[0][0],tasks[0][2]});\n while(!minh.empty()){\n int ptime=minh.top()[0];\n int ind=minh.top()[2];\n vector<vector<int>>temp;\n //minh.pop();\n while(!minh.empty() && minh.top()[0]==ptime){\n ind=min(ind,minh.top()[2]);\n temp.push_back(minh.top());\n minh.pop();\n }\n for(int i=0;i<temp.size();i++){\n if((temp[i][2])!=ind){\n minh.push(temp[i]);\n }\n }\n ans.push_back(ind); \n if(i<tasks.size() && minh.empty() && time+ptime<tasks[i][0]){\n time=tasks[i][0];\n }\n else\n time=time+ptime;\n while(i<tasks.size()){\n if(tasks[i][0]<=time){\n minh.push({tasks[i][1],tasks[i][0],tasks[i][2]});\n i++;\n }\n else\n break;\n }\n }\n return ans;\n }\n \n\n\n};", "memory": "186323" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& mat) {\n\n int n = mat.size();\n vector<int> ans;\n\n priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> pq;\n\n vector<vector<int>> g;\n\n for(int i=0;i<n;i++) {\n g.push_back({mat[i][0], mat[i][1], i});\n }\n\n sort(g.begin(), g.end());\n\n int i=0;\n long long curr=0;\n\n while (1) {\n\n while (i< n && curr >= g[i][0]) {\n pq.push({g[i][1], g[i][2]});\n i++;\n }\n\n if(pq.empty() && i<n) {\n curr = g[i][0];\n pq.push({g[i][1], g[i][2]});\n i++;\n }\n\n if(pq.empty())\n break;\n\n vector<int> t = pq.top();\n pq.pop();\n\n \n\n int d = t[0];\n int ind = t[1];\n\n curr = curr + mat[ind][1];\n\n ans.push_back(ind);\n\n\n }\n\n return ans;\n \n }\n};", "memory": "187264" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& arr) {\n int n = arr.size(), ct = 0;\n for(auto &i : arr){\n i.push_back(ct);\n ct++;\n }\n vector<int>result;\n priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>>pq;\n priority_queue<vector<int>,vector<vector<int>>,greater<vector<int>>>pq2;\n for(int i = 0 ; i < n ; i++){\n pq.push(arr[i]);\n }\n int curr_time = pq.top()[0];\n while(!pq.empty()){\n if(pq.top()[0] > curr_time && pq2.empty()){\n curr_time = pq.top()[0];\n }\n while(!pq.empty() && pq.top()[0] <= curr_time){\n pq2.push({pq.top()[1],pq.top()[2],pq.top()[0]});\n pq.pop();\n }\n result.push_back(pq2.top()[1]);\n curr_time += pq2.top()[0];\n pq2.pop(); \n }\n while(!pq2.empty()){\n result.push_back(pq2.top()[1]);\n pq2.pop();\n }\n return result;\n }\n};", "memory": "188205" }
1,962
<p>You are given <code>n</code>​​​​​​ tasks labeled from <code>0</code> to <code>n - 1</code> represented by a 2D integer array <code>tasks</code>, where <code>tasks[i] = [enqueueTime<sub>i</sub>, processingTime<sub>i</sub>]</code> means that the <code>i<sup>​​​​​​th</sup></code>​​​​ task will be available to process at <code>enqueueTime<sub>i</sub></code> and will take <code>processingTime<sub>i</sub></code><sub> </sub>to finish processing.</p> <p>You have a single-threaded CPU that can process <strong>at most one</strong> task at a time and will act in the following way:</p> <ul> <li>If the CPU is idle and there are no available tasks to process, the CPU remains idle.</li> <li>If the CPU is idle and there are available tasks, the CPU will choose the one with the <strong>shortest processing time</strong>. If multiple tasks have the same shortest processing time, it will choose the task with the smallest index.</li> <li>Once a task is started, the CPU will <strong>process the entire task</strong> without stopping.</li> <li>The CPU can finish a task then start a new one instantly.</li> </ul> <p>Return <em>the order in which the CPU will process the tasks.</em></p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> tasks = [[1,2],[2,4],[3,2],[4,1]] <strong>Output:</strong> [0,2,3,1] <strong>Explanation: </strong>The events go as follows: - At time = 1, task 0 is available to process. Available tasks = {0}. - Also at time = 1, the idle CPU starts processing task 0. Available tasks = {}. - At time = 2, task 1 is available to process. Available tasks = {1}. - At time = 3, task 2 is available to process. Available tasks = {1, 2}. - Also at time = 3, the CPU finishes task 0 and starts processing task 2 as it is the shortest. Available tasks = {1}. - At time = 4, task 3 is available to process. Available tasks = {1, 3}. - At time = 5, the CPU finishes task 2 and starts processing task 3 as it is the shortest. Available tasks = {1}. - At time = 6, the CPU finishes task 3 and starts processing task 1. Available tasks = {}. - At time = 10, the CPU finishes task 1 and becomes idle. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> tasks = [[7,10],[7,12],[7,5],[7,4],[7,2]] <strong>Output:</strong> [4,3,2,0,1] <strong>Explanation</strong><strong>: </strong>The events go as follows: - At time = 7, all the tasks become available. Available tasks = {0,1,2,3,4}. - Also at time = 7, the idle CPU starts processing task 4. Available tasks = {0,1,2,3}. - At time = 9, the CPU finishes task 4 and starts processing task 3. Available tasks = {0,1,2}. - At time = 13, the CPU finishes task 3 and starts processing task 2. Available tasks = {0,1}. - At time = 18, the CPU finishes task 2 and starts processing task 0. Available tasks = {1}. - At time = 28, the CPU finishes task 0 and starts processing task 1. Available tasks = {}. - At time = 40, the CPU finishes task 1 and becomes idle. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>tasks.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= enqueueTime<sub>i</sub>, processingTime<sub>i</sub> &lt;= 10<sup>9</sup></code></li> </ul>
3
{ "code": "#include <vector>\n#include <queue>\n#include <algorithm>\n\nclass Solution {\npublic:\n std::vector<int> getOrder(std::vector<std::vector<int>>& tasks) {\n // {startTime, processTime, index}\n std::vector<std::vector<int>> tasks_vec;\n for (int i = 0; i < tasks.size(); i++) \n {\n tasks_vec.push_back({tasks[i][0], tasks[i][1], i});\n }\n \n // sort tasks by startTime\n sort(tasks_vec.begin(), tasks_vec.end());\n \n // Min-heap to manage tasks based on processing time\n priority_queue<vector<long long>,vector<vector<long long>>,greater<vector<long long>>> pq;\n vector<int> ans;\n long long nextStart = 0;\n int idx = 0;\n int n = tasks_vec.size();\n \n while (idx < n || !pq.empty()) \n {\n // store the tasks processing time and index int the pq\n // Add all tasks that can start at or before nextStart time to the priority queue\n while (idx < n && tasks_vec[idx][0] <= nextStart) \n {\n pq.push({tasks_vec[idx][1], tasks_vec[idx][2]});\n idx++;\n }\n \n if (pq.empty()) \n {\n // If the priority queue is empty, move to the next task's start time\n nextStart = tasks_vec[idx][0];\n continue;\n }\n \n // Get the task with the smallest processing time\n auto task = pq.top();\n pq.pop();\n \n // Add the task index to the result\n ans.push_back(task[1]);\n \n // Update the nextStart time to the current time + task's processing time\n nextStart += task[0];\n }\n return ans;\n }\n};\n", "memory": "189146" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\n public:\n int maxFrequency(vector<int>& nums, int k) {\n int ans = 0;\n long sum = 0;\n\n ranges::sort(nums);\n\n for (int l = 0, r = 0; r < nums.size(); ++r) {\n sum += nums[r];\n while (sum + k < static_cast<long>(nums[r]) * (r - l + 1))\n sum -= nums[l++];\n ans = max(ans, r - l + 1);\n }\n\n return ans;\n }\n};", "memory": "104500" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\n public:\n int maxFrequency(vector<int>& nums, int k) {\n int ans = 0;\n long sum = 0;\n\n ranges::sort(nums);\n\n for (int l = 0, r = 0; r < nums.size(); ++r) {\n sum += nums[r];\n while (sum + k < static_cast<long>(nums[r]) * (r - l + 1))\n sum -= nums[l++];\n ans = max(ans, r - l + 1);\n }\n\n return ans;\n }\n};", "memory": "104600" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\n public:\n int maxFrequency(vector<int>& nums, int k) {\n int ans = 0;\n long sum = 0;\n\n ranges::sort(nums);\n\n for (int l = 0, r = 0; r < nums.size(); ++r) {\n sum += nums[r];\n while (sum + k < static_cast<long>(nums[r]) * (r - l + 1))\n sum -= nums[l++];\n ans = max(ans, r - l + 1);\n }\n\n return ans;\n }\n};", "memory": "104600" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\n public:\n int maxFrequency(vector<int>& nums, int k) {\n int ans = 0;\n long sum = 0;\n\n ranges::sort(nums);\n\n for (int l = 0, r = 0; r < nums.size(); ++r) {\n sum += nums[r];\n while (sum + k < static_cast<long>(nums[r]) * (r - l + 1))\n sum -= nums[l++];\n ans = max(ans, r - l + 1);\n }\n\n return ans;\n }\n};", "memory": "104700" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int ans = 0;\n long sum = 0;\n\n ranges::sort(nums);\n\n for (int l = 0, r = 0; r < nums.size(); ++r) {\n sum += nums[r];\n while (sum + k < static_cast<long>(nums[r]) * (r - l + 1))\n sum -= nums[l++];\n ans = max(ans, r - l + 1);\n }\n\n return ans;\n }\n \n};", "memory": "104800" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\n void merge(vector<int> &nums,int low,int mid,int high){\n int i=low,j=mid+1;\n // cout<<high<<low;\n int temp[high-low+1];\n int k=0;\n while(i<=mid && j<=high){\n if(nums[i]<nums[j])temp[k++]=nums[i++];\n else temp[k++]=nums[j++];\n }\n while(i<=mid)temp[k++]=nums[i++];\n while(j<=high)temp[k++]=nums[j++];\n k=0;\n for(i=low;i<=high;i++)nums[i]=temp[k++];\n }\n void mergeSort(vector<int>& nums,int low,int high){\n if(low>=high)return;\n int mid = (low+high)/2;\n mergeSort(nums,low,mid);\n mergeSort(nums,mid+1,high);\n merge(nums,low,mid,high);\n }\n void debug(vector<int>& nums){\n for(int i:nums)cout<<i<<\" \";\n cout<<\"\\n\";\n }\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n long n = nums.size();\n mergeSort(nums,0,n-1);\n long l=0;\n long maxWindow=0;\n long total =0;\n for(long r=0;r<n;r++){\n total+=nums[r];\n if(nums[r]*(r-l+1)>total+k){\n total-=nums[l++];\n }\n maxWindow= max(maxWindow,(r-l+1));\n }\n return maxWindow;\n }\n};", "memory": "105000" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int prefix[100000+1] = {0};\n for(auto x: nums){\n prefix[x]++;\n }\n int count=0;\n int maxi = 0;\n int operations = 0;\n int left =0;\n int leftcount=0;\n int curr=0;\n for(int i=1;i<100001;i++){\n operations+=count;\n operations+=prefix[i-1];\n curr+=prefix[i];\n count+=prefix[i-1];\n while(operations>k){\n while(leftcount>0&&operations>k){\n leftcount--;\n curr--;\n count--;\n operations-=(i-left);\n }\n if(operations>k){\n left++;\n leftcount = prefix[left];\n }\n }\n maxi= max(curr,maxi);\n\n }\n return maxi;\n }\n};", "memory": "105100" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "//https://hyper-meta.blogspot.com/\n#pragma GCC target(\"avx,mmx,sse2,sse3,sse4\")\nauto _=[]()noexcept{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);return 0;}();\nint fre[100001];\nclass Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n const int sz=nums.size();\n if(sz>1500){\n memset(fre,0,sizeof(fre));\n for(auto e:nums) fre[e]++;\n int i=1;\n while(fre[i]==0) ++i;\n int pre=i,r=fre[pre],wl=i;\n long long inc=0,pcnt=r;\n i++;\n while(1){\n while(i<=100000&&fre[i]==0)++i;\n if(i==100001) break;\n int cur=i;\n inc+=(cur-pre)*pcnt;\n while(inc>k){\n while(fre[wl]==0) wl++;\n --fre[wl];\n inc-=cur-wl;\n --pcnt;\n }\n pcnt+=fre[i++];\n r=max(r,(int)pcnt);\n pre=cur;\n }\n return r;\n }\n sort(nums.begin(),nums.end());\n int i=1,pre=nums[0];\n while(i<sz&&nums[i]==pre) ++i;\n int r=i,wl=0;\n long long inc=0,pcnt=i;\n while(i<sz){\n int cur=nums[i++];\n inc+=(cur-pre)*pcnt;\n while(inc>k) {inc-=cur-nums[wl++];--pcnt;}\n pcnt++;\n while(i<sz&&nums[i]==cur){++i;++pcnt;}\n r=max(r,i-wl);\n pre=cur;\n }\n return r;\n }\n};", "memory": "105200" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "//https://hyper-meta.blogspot.com/\n#pragma GCC target(\"avx,mmx,sse2,sse3,sse4\")\nauto _=[]()noexcept{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);return 0;}();\nint fre[100001];\nclass Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n const int sz=nums.size();\n if(sz>1500){\n memset(fre,0,sizeof(fre));\n for(auto e:nums) fre[e]++;\n int i=1;\n while(fre[i]==0) ++i;\n int pre=i,r=fre[pre],wl=i;\n long long inc=0,pcnt=r;\n i++;\n while(1){\n while(i<=100000&&fre[i]==0)++i;\n if(i==100001) break;\n int cur=i;\n inc+=(cur-pre)*pcnt;\n while(inc>k){\n while(fre[wl]==0) wl++;\n --fre[wl];\n inc-=cur-wl;\n --pcnt;\n }\n pcnt+=fre[i++];\n r=max(r,(int)pcnt);\n pre=cur;\n }\n return r;\n }\n sort(nums.begin(),nums.end());\n int i=1,pre=nums[0];\n while(i<sz&&nums[i]==pre) ++i;\n int r=i,wl=0;\n long long inc=0,pcnt=i;\n while(i<sz){\n int cur=nums[i++];\n inc+=(cur-pre)*pcnt;\n while(inc>k) {inc-=cur-nums[wl++];--pcnt;}\n pcnt++;\n while(i<sz&&nums[i]==cur){++i;++pcnt;}\n r=max(r,i-wl);\n pre=cur;\n }\n return r;\n }\n};", "memory": "105400" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& v, long long k) {\n sort(v.begin(), v.end());\n long long total = 0;\n long long left = 0;int ans = 0;\n\n for (long long right = 0; right < v.size(); right++) {\n total += v[right];\n\n //we should see after mutiply that if small then answer would be that\n while (v[right] * (right - left + 1) > total + k) {\n total -= v[left];\n left++;\n }\n\n ans = max(ans,(int) ( right - left + 1));\n }\n\n return ans;\n }\n};\n", "memory": "105500" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n bool possible(vector<int>& nums,int mid, int k)\n { \n long long int windowsum=0,totalsum=0;\n for(int i=0;i<mid;++i)\n windowsum+=nums[i];\n \n totalsum=1LL*nums[mid-1]*mid;\n \n if(totalsum-windowsum<=k)\n return 1;\n \n int j=0;\n for(int i=mid;i<nums.size();++i)\n {\n windowsum-=nums[j];\n windowsum+=nums[i];\n totalsum=totalsum=1LL*nums[i]*mid;\n \n if(totalsum-windowsum<=k)\n return 1;\n j++;\n }\n \n return 0;\n }\n \n int maxFrequency(vector<int>& nums, int k) {\n int l=1,h=nums.size(),ans = 0;\n sort(nums.begin(),nums.end());\n \n while(l<=h)\n {\n int mid=l+(h-l)/2;\n if(possible(nums,mid,k))\n {\n ans=mid;\n l=mid+1;\n } \n \n else \n h=mid-1;\n }\n \n return ans;\n }\n};", "memory": "105600" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n \n sort(nums.begin(), nums.end());\n\n int currAns = 1, maxAns = 1, prev = 0;\n long long op = 0;\n for(int i=1; i<nums.size(); ++i) {\n \n int diff = nums[i] - nums[i-1];\n op = op + (long long)(i - prev)*(long long)diff;\n\n if (op <= k) {\n ++ currAns;\n } else {\n maxAns = max(currAns, maxAns);\n while (prev < i && op > k) {\n op = op - (nums[i] - nums[prev]);\n ++prev;\n --currAns;\n }\n currAns += 1;\n }\n }\n\n return max(currAns, maxAns);\n }\n};", "memory": "105600" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>&v,int k) {\n sort(v.begin(), v.end());\n int n = v.size();\n long long currSum = 0;\n long long currTargetSum = 0;\n long long temp = 0;\n int ans = 0;\n for(int l = 0, r = 0; r < n; r++) {\n currSum += v[r];\n currTargetSum = (long long)(r - l + 1) * v[r];\n temp = currTargetSum - currSum;\n while(l < r && temp > k) {\n currSum -= v[l];\n temp -= (v[r] - v[l]);\n l++;\n }\n if (temp <= k) ans = max(ans, r - l + 1);\n }\n return ans;\n }\n};", "memory": "105700" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n int currLeft = 0;\n long double currSum = 0;\n int maxFreq = 1;\n\n for (int currRight = 0; currRight < nums.size(); currRight++) {\n currSum += nums[currRight];\n while (nums[currRight] * (long double)(currRight - currLeft + 1) - currSum > k) {\n currSum -= nums[currLeft];\n currLeft++; \n }\n \n maxFreq = max(maxFreq, currRight - currLeft + 1);\n }\n \n return maxFreq;\n }\n};\n", "memory": "105700" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n long long total = 0;\n int max_freq = 1;\n int i = 0; // Left pointer for the sliding window\n // Step 2: Use sliding window approach\n for (int j = 1; j < nums.size(); ++j) {\n total += (long long)(nums[j] - nums[j - 1]) * (j - i);\n // If the total cost exceeds k, shrink the window from the left\n while (total > k) {\n total -= nums[j] - nums[i]; // Remove the cost of nums[i]\n i++; // Move the left pointer to the right\n }\n max_freq = max(max_freq, j - i + 1);\n }\n return max_freq;\n }\n};", "memory": "105800" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maxFrequency(std::vector<int>& nums, int k) {\n std::sort(nums.begin(), nums.end());\n \n // Initialize the sliding window pointers and the total increment cost\n int left = 0;\n int64_t totalIncrement = 0; // Use int64_t for handling large sums\n int maxFrequency = 0;\n\n // Traverse the array with the right pointer\n for (int right = 0; right < nums.size(); ++right) {\n // Calculate the total increments needed to make all elements\n // from left to right equal to nums[right]\n if (right > 0) {\n totalIncrement += (static_cast<int64_t>(nums[right] - nums [right - 1])) * (right - left);\n }\n\n // Adjust the window if the total increment exceeds k\n while (totalIncrement > k) {\n totalIncrement -= nums[right] - nums[left];\n ++left;\n }\n\n // Update the maximum frequency\n maxFrequency = std::max(maxFrequency, right - left + 1);\n }\n\n return maxFrequency;\n }\n \n};", "memory": "105800" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n // sort(nums.begin(), nums.end());\n // int max_freq = 1;\n // for (int i = 0; i < nums.size(); i++) {\n // int temp = k, freq = 1;\n // for (int j = i + 1; j < nums.size() && temp >= 0; j++) {\n // int diff = (nums[j] - nums[j - 1]) * (j - i);\n // if (diff <= temp) {\n // freq++;\n // temp -= diff;\n // } else {\n // break;\n // }\n // }\n // max_freq = max(freq, max_freq);\n // }\n // return max_freq;\n\n sort(nums.begin(), nums.end());\n\n long long total = 0; // Total difference cost\n int max_freq = 1; // At least one element has a frequency of 1\n int i = 0; // Left pointer for the sliding window\n\n // Step 2: Use sliding window approach\n for (int j = 1; j < nums.size(); ++j) {\n // Add the cost of making nums[i...j-1] into nums[j]\n total += (long long)(nums[j] - nums[j - 1]) * (j - i);\n\n // If the total cost exceeds k, shrink the window from the left\n while (total > k) {\n total -= nums[j] - nums[i]; // Remove the cost of nums[i]\n i++; // Move the left pointer to the right\n }\n\n // Update the maximum frequency\n max_freq = max(max_freq, j - i + 1);\n }\n\n return max_freq;\n }\n};", "memory": "105900" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n int left = 0, right = 0;\n long res = 0, total = 0;\n\n while (right < nums.size()) {\n total += nums[right];\n\n while (nums[right] * static_cast<long>(right - left + 1) > total + k) {\n total -= nums[left];\n left += 1;\n }\n\n res = max(res, static_cast<long>(right - left + 1));\n right += 1;\n }\n\n return static_cast<int>(res);\n }\n};", "memory": "105900" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n \n long long total = 0;\n int left = 0, result = 1;\n \n // Sliding window approach\n for (int right = 0; right < nums.size(); ++right) {\n total += nums[right];\n \n // Check if we can make all elements in the window equal to nums[right]\n while ((long long)(right - left + 1) * nums[right] - total > k) {\n total -= nums[left];\n left++;\n }\n \n // Update the result with the maximum window size (right - left + 1)\n result = max(result, right - left + 1);\n }\n \n return result;\n }\n};\nstatic const int speedup = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();", "memory": "106000" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int count[100001]={0};\n \n for(int i=0;i<nums.size();i++)\n {\n count[nums[i]]++;\n }\n \n sort(nums.begin(),nums.end());\n \n int i=count[nums[0]];\n int max=i;\n \n \n int freq;\n for(;i<nums.size();)\n {\n freq=count[nums[i]];\n int m=k;\n for(int j=i-1;j>=0;j--)\n {\n if(nums[i]-nums[j]<=m)\n {\n m-=(nums[i]-nums[j]);\n freq++;\n }\n else\n {\n break;\n }\n }\n if(max<freq)\n {\n max=freq;\n }\n i+=count[nums[i]];\n \n\n }\n \n return max;\n\n\n }\n};", "memory": "106100" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int count[100001]={0};\n \n for(int i=0;i<nums.size();i++)\n {\n count[nums[i]]++;\n }\n \n sort(nums.begin(),nums.end());\n \n int i=count[nums[0]];\n int max=i;\n if(i==nums.size())\n {\n return nums.size();\n }\n \n int freq;\n for(;i<nums.size();)\n {\n freq=count[nums[i]];\n int m=k;\n for(int j=i-1;j>=0;j--)\n {\n if(nums[i]-nums[j]<=m)\n {\n m-=(nums[i]-nums[j]);\n freq++;\n }\n else\n {\n break;\n }\n }\n if(max<freq)\n {\n max=freq;\n }\n i+=count[nums[i]];\n \n\n }\n cout<<count[1];\n return max;\n\n\n }\n};", "memory": "106200" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int count[100001]={0};\n \n for(int i=0;i<nums.size();i++)\n {\n count[nums[i]]++;\n }\n \n sort(nums.begin(),nums.end());\n int i=0;\n while(i<nums.size()-1&&nums[i]==nums[i+1])\n {\n i++;\n\n }\n int max=i+1;\n if(i==nums.size()-1)\n {\n return nums.size();\n }\n i++;\n int freq;\n for(;i<nums.size();i++)\n {\n freq=count[nums[i]];\n int m=k;\n for(int j=i-1;j>=0;j--)\n {\n if(nums[i]-nums[j]<=m)\n {\n m-=(nums[i]-nums[j]);\n freq++;\n }\n else\n {\n break;\n }\n }\n if(max<freq)\n {\n max=freq;\n }\n while(i<nums.size()-1&&nums[i]==nums[i+1])\n {\n i++;\n\n }\n\n }\n return max;\n\n\n }\n};", "memory": "106300" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int count[100001]={0};\n \n for(int i=0;i<nums.size();i++)\n {\n count[nums[i]]++;\n }\n \n sort(nums.begin(),nums.end());\n \n int i=count[nums[0]];\n int max=i;\n if(i==nums.size())\n {\n return nums.size();\n }\n \n int freq;\n for(;i<nums.size();)\n {\n freq=count[nums[i]];\n int m=k;\n for(int j=i-1;j>=0;j--)\n {\n if(nums[i]-nums[j]<=m)\n {\n m-=(nums[i]-nums[j]);\n freq++;\n }\n else\n {\n break;\n }\n }\n if(max<freq)\n {\n max=freq;\n }\n i+=count[nums[i]];\n \n\n }\n cout<<count[1];\n return max;\n\n\n }\n};", "memory": "106300" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& v, int k) {\n sort(v.begin(), v.end());\n int n = v.size();\n long long int prefix[n + 1];\n memset(prefix, 0, sizeof(prefix));\n prefix[0] = v[0];\n for (int i = 1; i < n; i++) {\n prefix[i] = prefix[i - 1] + (long long)v[i];\n }\n\n int ans = 1;\n for (int i = n - 1; i >= 0; i--) {\n // lets assume that we want to make all elements equal to v[i]\n \n int l = 1, r = i;\n while (r >= l) {\n int mid = l + (r - l) / 2;\n int start = i - 1;\n int end = (i - 1 - mid);\n long long int req = prefix[start] - (end >= 0 ? prefix[end] : 0);\n long long int temp = (long long)mid * (long long)v[i] - req;\n if (temp > k) {\n r = mid - 1;\n } else {\n l = mid + 1;\n ans = max(ans, (mid + 1));\n }\n }\n // cout<<\"---->\"<<i<<\"---->\"<<ans<<endl;\n }\n return ans;\n }\n};", "memory": "106500" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int n = nums.size();\n sort(nums.begin(), nums.end());\n long long s[n + 1];\n s[0] = 0;\n for (int i = 1; i <= n; ++i) {\n s[i] = s[i - 1] + nums[i - 1];\n }\n int l = 1, r = n;\n auto check = [&](int m) {\n for (int i = m; i <= n; ++i) {\n if (1LL * nums[i - 1] * m - (s[i] - s[i - m]) <= k) {\n return true;\n }\n }\n return false;\n };\n while (l < r) {\n int mid = (l + r + 1) >> 1;\n if (check(mid)) {\n l = mid;\n } else {\n r = mid - 1;\n }\n }\n return l;\n }\n\n};", "memory": "106600" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int n = nums.size();\n sort(nums.begin(), nums.end());\n long long s[n + 1];\n s[0] = 0;\n for (int i = 1; i <= n; ++i) {\n s[i] = s[i - 1] + nums[i - 1];\n }\n int l = 1, r = n;\n auto check = [&](int m) {\n for (int i = m; i <= n; ++i) {\n if (1LL * nums[i - 1] * m - (s[i] - s[i - m]) <= k) {\n return true;\n }\n }\n return false;\n };\n while (l < r) {\n int mid = (l + r + 1) >> 1;\n if (check(mid)) {\n l = mid;\n } else {\n r = mid - 1;\n }\n }\n return l;\n }\n};", "memory": "106700" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int n = nums.size();\n sort(nums.begin(), nums.end());\n long long s[n + 1];\n s[0] = 0;\n for (int i = 1; i <= n; ++i) {\n s[i] = s[i - 1] + nums[i - 1];\n }\n int l = 1, r = n;\n auto check = [&](int m) {\n for (int i = m; i <= n; ++i) {\n if (1LL * nums[i - 1] * m - (s[i] - s[i - m]) <= k) {\n return true;\n }\n }\n return false;\n };\n while (l < r) {\n int mid = (l + r + 1) >> 1;\n if (check(mid)) {\n l = mid;\n } else {\n r = mid - 1;\n }\n }\n return l;\n }\n};", "memory": "106700" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int n = nums.size();\n sort(nums.begin(), nums.end());\n long long s[n + 1];\n s[0] = 0;\n for (int i = 1; i <= n; ++i) {\n s[i] = s[i - 1] + nums[i - 1];\n }\n int l = 1, r = n;\n auto check = [&](int m) {\n for (int i = m; i <= n; ++i) {\n if (1LL * nums[i - 1] * m - (s[i] - s[i - m]) <= k) {\n return true;\n }\n }\n return false;\n };\n while (l < r) {\n int mid = (l + r + 1) >> 1;\n if (check(mid)) {\n l = mid;\n } else {\n r = mid - 1;\n }\n }\n return l;\n }\n};", "memory": "106800" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int minNum = *min_element(nums.begin(), nums.end());\n int maxNum = *max_element(nums.begin(), nums.end());\n int range = maxNum - minNum + 1;\n vector<int> frequency(range, 0);\n \n for (int num : nums) {\n frequency[num - minNum]++;\n }\n \n int windowStart = 0, operationsUsed = 0, maxFreq = 0, currentCount = 0, windowStartCount = frequency[0];\n for (int windowEnd = 0; windowEnd < range; ++windowEnd) {\n operationsUsed += currentCount;\n currentCount += frequency[windowEnd];\n while (operationsUsed > k) {\n while (windowStartCount == 0) windowStartCount = frequency[++windowStart];\n operationsUsed -= windowEnd - windowStart;\n windowStartCount--;\n currentCount--;\n }\n maxFreq = max(maxFreq, currentCount);\n }\n return maxFreq;\n }\n};\nstatic const int speedup = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();", "memory": "106900" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "//50-ms\nclass Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int minNum = *min_element(nums.begin(), nums.end());\n int maxNum = *max_element(nums.begin(), nums.end());\n int range = maxNum - minNum + 1;\n vector<int> frequency(range, 0);\n \n for (int num : nums) {\n frequency[num - minNum]++;\n }\n \n int windowStart = 0, operationsUsed = 0, maxFreq = 0, currentCount = 0, windowStartCount = frequency[0];\n for (int windowEnd = 0; windowEnd < range; ++windowEnd) {\n operationsUsed += currentCount;\n currentCount += frequency[windowEnd];\n while (operationsUsed > k) {\n while (windowStartCount == 0) windowStartCount = frequency[++windowStart];\n operationsUsed -= windowEnd - windowStart;\n windowStartCount--;\n currentCount--;\n }\n maxFreq = max(maxFreq, currentCount);\n }\n return maxFreq;\n }\n};\nstatic const int speedup = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\n\n//https://leetcode.com/problems/frequency-of-the-most-frequent-element/submissions/1365045529/", "memory": "107000" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "//50-ms\nclass Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int minNum = *min_element(nums.begin(), nums.end());\n int maxNum = *max_element(nums.begin(), nums.end());\n int range = maxNum - minNum + 1;\n vector<int> frequency(range, 0);\n \n for (int num : nums) {\n frequency[num - minNum]++;\n }\n \n int windowStart = 0, operationsUsed = 0, maxFreq = 0, currentCount = 0, windowStartCount = frequency[0];\n for (int windowEnd = 0; windowEnd < range; ++windowEnd) {\n operationsUsed += currentCount;\n currentCount += frequency[windowEnd];\n while (operationsUsed > k) {\n while (windowStartCount == 0) windowStartCount = frequency[++windowStart];\n operationsUsed -= windowEnd - windowStart;\n windowStartCount--;\n currentCount--;\n }\n maxFreq = max(maxFreq, currentCount);\n }\n return maxFreq;\n }\n};\nstatic const int speedup = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();\n\n\n//https://leetcode.com/problems/frequency-of-the-most-frequent-element/submissions/1365045529/", "memory": "107000" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int minNum = *min_element(nums.begin(), nums.end());\n int maxNum = *max_element(nums.begin(), nums.end());\n int range = maxNum - minNum + 1;\n vector<int> frequency(range, 0);\n \n for (int num : nums) {\n frequency[num - minNum]++;\n }\n \n int windowStart = 0, operationsUsed = 0, maxFreq = 0, currentCount = 0, windowStartCount = frequency[0];\n for (int windowEnd = 0; windowEnd < range; ++windowEnd) {\n operationsUsed += currentCount;\n currentCount += frequency[windowEnd];\n while (operationsUsed > k) {\n while (windowStartCount == 0) windowStartCount = frequency[++windowStart];\n operationsUsed -= windowEnd - windowStart;\n windowStartCount--;\n currentCount--;\n }\n maxFreq = max(maxFreq, currentCount);\n }\n return maxFreq;\n }\n};\nstatic const int speedup = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();", "memory": "107100" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int minNum = *min_element(nums.begin(), nums.end());\n int maxNum = *max_element(nums.begin(), nums.end());\n int range = maxNum - minNum + 1;\n vector<int> frequency(range, 0);\n \n for (int num : nums) {\n frequency[num - minNum]++;\n }\n \n int windowStart = 0, operationsUsed = 0, maxFreq = 0, currentCount = 0, windowStartCount = frequency[0];\n for (int windowEnd = 0; windowEnd < range; ++windowEnd) {\n operationsUsed += currentCount;\n currentCount += frequency[windowEnd];\n while (operationsUsed > k) {\n while (windowStartCount == 0) windowStartCount = frequency[++windowStart];\n operationsUsed -= windowEnd - windowStart;\n windowStartCount--;\n currentCount--;\n }\n maxFreq = max(maxFreq, currentCount);\n }\n return maxFreq;\n }\n};\nstatic const int speedup = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();", "memory": "107200" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int minNum = *min_element(nums.begin(), nums.end());\n int maxNum = *max_element(nums.begin(), nums.end());\n int range = maxNum - minNum + 1;\n vector<int> frequency(range, 0);\n \n for (int num : nums) {\n frequency[num - minNum]++;\n }\n \n int windowStart = 0, operationsUsed = 0, maxFreq = 0, currentCount = 0, windowStartCount = frequency[0];\n for (int windowEnd = 0; windowEnd < range; ++windowEnd) {\n operationsUsed += currentCount;\n currentCount += frequency[windowEnd];\n while (operationsUsed > k) {\n while (windowStartCount == 0) windowStartCount = frequency[++windowStart];\n operationsUsed -= windowEnd - windowStart;\n windowStartCount--;\n currentCount--;\n }\n maxFreq = max(maxFreq, currentCount);\n }\n return maxFreq;\n }\n};\nstatic const int speedup = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 0;\n}();", "memory": "107300" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n sort(nums.rbegin(), nums.rend());\n int l = 0, r = 0;\n int t = 0;\n int ans = 0;\n while(r<nums.size()){\n \n int x = nums[l] - nums[r];\n t += x;\n while(t > k){\n l++;\n t -= (nums[l-1] - nums[l]) * (r-l+1);\n x = nums[l] - nums[r];\n }\n ans = max(ans, (r-l+1));\n r++;\n }\n return ans;\n }\n};", "memory": "107800" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\n #define ll long long\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n ios::sync_with_stdio(false);\n cin.tie(0);\n\n sort(nums.rbegin() , nums.rend());\n\n ll cnt = 1;\n ll val = k;\n int n = nums.size();\n ll ans = 1, start = 0, end = 1;\n\n // for(auto i : nums) cout<<i;\n // cout<<'\\n';\n\n\n while(end < n) {\n if( val >= (nums[start] - nums[end]) ) {\n cnt++;\n ans = max(ans , cnt);\n val -= (nums[start] - nums[end]);\n end++;\n } else {\n start++;\n end = start+1;\n ans = max(ans , cnt);\n cnt = 1;\n val = k;\n }\n if(end >= n) break;\n if(nums[start] == nums[end]) start++;\n // cout<<nums[start]<<\" \"<<nums[end]<<\" \"<<cnt<<\" \"<<ans<<'\\n';\n }\n\n ans = max(ans , cnt);\n\n return ans;\n }\n};", "memory": "107900" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n sort(nums.rbegin(), nums.rend());\n int l = 0, r = 0;\n int t = 0;\n int ans = 0;\n while(r<nums.size()){\n \n int x = nums[l] - nums[r];\n t += x;\n while(t > k){\n l++;\n t -= (nums[l-1] - nums[l]) * (r-l+1);\n x = nums[l] - nums[r];\n }\n ans = max(ans, (r-l+1));\n r++;\n }\n return ans;\n }\n};", "memory": "108000" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\n #define ll long long\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n sort(nums.rbegin() , nums.rend());\n\n ll cnt = 1;\n ll val = k;\n int n = nums.size();\n ll ans = 1, start = 0, end = 1;\n\n while(end < n) {\n if( val >= (nums[start] - nums[end]) ) {\n cnt++;\n ans = max(ans , cnt);\n val -= (nums[start] - nums[end]);\n end++;\n } else {\n start++;\n end = start+1;\n ans = max(ans , cnt);\n cnt = 1;\n val = k;\n }\n if(end >= n) break;\n if(nums[start] == nums[end]) start++;\n }\n\n ans = max(ans , cnt);\n return ans;\n }\n};", "memory": "108100" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n queue<pair<int, int>> queue;\n int queuecount = 0;\n long kused = 0;\n int ans = 0;\n int n = nums.size();\n for (int x = 0;;) {\n int y = x + 1;\n int num = nums[x];\n for (;y < n && nums[y] == num; y++);\n int count = y - x;\n ans = max(ans, count + queuecount);\n if (y == n) {\n return ans;\n }\n x = y;\n queue.push({num, count});\n queuecount += count;\n int num2 = nums[x];\n kused += queuecount * (long)(num2 - num);\n if (kused <= k) {\n continue;\n }\n for (;!queue.empty();queue.pop()) {\n auto [num, count] = queue.front();\n int numdif = num2 - num;\n kused -= count * (long)numdif;\n queuecount -= count;\n if (kused > k) {\n continue;\n }\n int cnt = (k - kused) / numdif; \n queuecount += cnt;\n queue.front() = {num, cnt};\n kused += cnt * numdif;\n break;\n }\n }\n }\n};", "memory": "108200" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n sort(nums.begin(), nums.end());\n vector<int> d(nums.size(), 0);\n for (int i=nums.size()-1; i>0; i--)\n {\n d[i]=nums[i]-nums[i-1];\n }\n long l=0, r=0, temp=0, ans=1;\n while (r<nums.size())\n {\n if (temp<=k)\n {\n ans=max(ans, r-l+1);\n r++;\n if (r==nums.size()) break;\n temp+=(r-l)*d[r];\n }\n else\n {\n temp-=(nums[r]-nums[l]);\n l++;\n }\n }\n return ans;\n }\n};", "memory": "110500" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n int n=nums.size();\n if(n==1) {\n return 1;\n }\n vector<int> num=nums;\n sort(num.begin(),num.end());\n long long sum=num[0];\n int mx=1;\n int left=0,right=1;\n while(right<n) {\n long long val=num[right];\n val*=(right-left);\n //cout<<val<<\" \"<<sum<<endl;\n if(val-sum<=k) {\n mx=max(right-left+1,mx);\n sum+=num[right];\n right++;\n }\n else {\n sum-=num[left];\n left++;\n }\n }\n return mx;\n }\n};", "memory": "110600" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n deque<int> dq;\n sort(nums.begin(), nums.end());\n dq.push_back(nums[0]);\n int answer = 1;\n long long operations = 0;\n for (int i = 1; i < nums.size(); i++)\n {\n int previous = dq.back();\n dq.push_back(nums[i]);\n\n operations += (dq.size() - 1) * (nums[i] - previous);\n\n while (operations > k)\n {\n operations -= (dq.back() - dq.front());\n dq.pop_front();\n }\n answer = answer > dq.size() ? answer : dq.size();\n }\n return answer;\n }\n};", "memory": "111400" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n\n queue<int>bfsQueue;\n sort(nums.begin(), nums.end());\n int maxFreq = 1;\n bfsQueue.push(nums[nums.size()-1]);\n int i = nums.size()-2;\n while(i >= 0) {\n if(bfsQueue.front() - nums[i] <= k) {\n bfsQueue.push(nums[i]);\n k -= bfsQueue.front() - nums[i];\n i--;\n maxFreq = max(maxFreq, int(bfsQueue.size()));\n continue;\n }\n while(bfsQueue.front() - nums[i] > k) {\n int poppedElement = bfsQueue.front();\n bfsQueue.pop();\n k += abs(poppedElement - bfsQueue.front()) * bfsQueue.size();\n }\n }\n return maxFreq;\n }\n};", "memory": "111500" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n deque<int> dq;\n sort(nums.begin(), nums.end());\n dq.push_back(nums[0]);\n int answer = 1;\n long long operations = 0;\n for (int i = 1; i < nums.size(); i++)\n {\n //addition of new element\n // add element\n int previous = dq.back();\n dq.push_back(nums[i]);\n // increase number of operations\n operations += (dq.size() - 1) * (nums[i] - previous);\n // removal on the basis of a condition\n // while number of operations is greater than k, remove the first element\n while (operations > k)\n {\n int front = dq.front();\n dq.pop_front();\n operations -= (dq.back() - front);\n }\n answer = answer > dq.size() ? answer : dq.size();\n }\n return answer;\n }\n};", "memory": "111600" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(vector<int>& nums, int k) {\n queue<int> dq;\n sort(nums.begin(), nums.end());\n dq.push(nums[0]);\n int previous = nums[0];\n int answer = 1;\n long long operations = 0;\n for (int i = 1; i < nums.size(); i++)\n {\n dq.push(nums[i]);\n\n operations += (dq.size() - 1) * (nums[i] - previous);\n previous = nums[i];\n\n while (operations > k)\n {\n operations -= (nums[i] - dq.front());\n dq.pop();\n }\n answer = answer > dq.size() ? answer : dq.size();\n }\n return answer;\n }\n};", "memory": "111600" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxFrequency(std::vector<int>& nums, int k) {\n countingSort(nums); // Sort the array using counting sort\n int start = 0; // Start index of the current subarray\n long long subarraySum = 0; // Sum of elements in the current subarray\n int maxFrequency = 1; // Initialize the maximum frequency to 1\n for (int i = 0; i < nums.size(); i++) {\n int subarrayLength = i - start + 1; // Length of the current subarray\n long long subarrayProduct = (long long)nums[i] * subarrayLength; // Use long long to avoid overflow\n subarraySum += nums[i]; // Add the current element to the subarray sum\n\n // Adjust the subarray to satisfy the condition (product - sum <= k)\n while (subarrayProduct - subarraySum > k) {\n subarraySum -= nums[start]; // Remove the leftmost element from the subarray\n start++; // Move the start index to the right\n subarrayLength--; // Decrease the length of the subarray\n subarrayProduct = (long long)nums[i] * subarrayLength; // Recalculate the product\n }\n\n // Update the maximum frequency based on the current subarray length\n maxFrequency = std::max(maxFrequency, subarrayLength);\n }\n return maxFrequency;\n }\n\nprivate:\n void countingSort(std::vector<int>& nums) {\n int maxElement = INT_MIN;\n for (int num : nums) {\n maxElement = max(maxElement, num);\n }\n std::vector<int> countMap(maxElement + 1, 0);\n for (int num : nums) {\n countMap[num]++;\n }\n int index = 0;\n for (int i = 0; i <= maxElement; i++) {\n while (countMap[i]-- > 0) {\n nums[index++] = i;\n }\n }\n }\n};", "memory": "113500" }
1,966
<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p> <p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p> <p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> nums = [1,2,4], k = 5 <strong>Output:</strong> 3<strong> Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.</pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> nums = [1,4,8,13], k = 5 <strong>Output:</strong> 2 <strong>Explanation:</strong> There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> nums = [3,9,6], k = 2 <strong>Output:</strong> 1 </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li> </ul>
3
{ "code": "// Idea: Greedy, Prefix Sum, Binary Search, Sorting\nclass Solution {\npublic:\n // _BinarySearch\n int maxFrequency(vector<int>& nums, int k) {\n std::ranges::sort(nums);\n const auto& a = nums;\n const int n = a.size();\n // create the prefix sum array, for quick range-sum\n vector<long long> ps(n);\n // Bug: partial_sum()'s accumulator is int32, so it cannot handle int64.\n // partial_sum(a.begin(), a.end(), ps.begin(),\n // [](long long accum, int e) { return accum + e; });\n // cout << \"n: \" << n << \", first: \" << a.front() << \", last: \" <<\n // a.back()\n // << \", total: \" << ps.back() << endl;\n // for (int i = 0; i < n - 1; ++i) {\n // if (ps[i] >= ps[i + 1]) {\n // cout << \"wrong: \" << ps[i] << \" vs \" << ps[i + 1] << endl;\n // }\n // }\n for (long long i = 0, sum = 0; i < n; ++i) {\n sum += a[i];\n ps[i] = sum;\n }\n int res = 1;\n for (int i = n - 1; i >= 1; --i) {\n // check if the whole range [0, i] is possible\n // {\n // long long existing = ps[i];\n // long long required = (long long)a[i] * (i + 1);\n // if (existing + k >= required) {\n // cout << \"can make [0, \" << i << \"]\" << endl;\n // return i + 1;\n // } else {\n // cout << \"lacked \" << required - existing + k\n // << \" to make [0, \" << i << \"]\" << endl;\n // }\n // }\n // binary search in [0, i]\n int low = 0;\n int high = i;\n int best = i;\n while (low <= high) {\n int mid = (low + high) / 2;\n long long existing = ps[i] - (mid > 0 ? ps[mid - 1] : 0);\n long long required = (long long)a[i] * (i - mid + 1);\n if (existing + k >= required) { // can make it\n // cout << \"low: \" << low << \", high: \" << high\n // << \", mid: \" << mid << \", existing: \" <<\n // existing\n // << \", required: \" << required << \", Good\" <<\n // endl;\n best = min(best, mid);\n high = mid - 1;\n } else {\n // cout << \"i: \" << i << \", low: \" << low << \", high: \" <<\n // high\n // << \", mid: \" << mid << \", existing: \" << existing\n // << \", required: \" << required\n // << \", failed: diff: \" << required - existing - k\n // << endl;\n low = mid + 1;\n }\n }\n // [best ... i] can be made the same as a[i]\n res = max(res, i - best + 1);\n }\n return res;\n }\n // sliding window\n int maxFrequency_SlidingWindow(vector<int>& A, long k) {\n int i = 0, j = 0;\n sort(A.begin(), A.end());\n for (j = 0; j < A.size(); ++j) {\n k += A[j];\n if (k < (long)A[j] * (j - i + 1))\n k -= A[i++];\n }\n return j - i;\n }\n};", "memory": "113600" }