id
int64
1
3.58k
problem_description
stringlengths
516
21.8k
instruction
int64
0
3
solution_c
dict
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = healths.size();\n // pos_hea_dir_num\n vector<vector<int>> v;\n for(int i=0;i<n;i++)v.push_back({positions[i],healths[i],(directions[i] == 'R'),i+1});\n sort(v.begin(),v.end());\n stack<int> st;\n for(int i=n-1;i>=0;i--){\n int dir = v[i][2];\n if(dir == 1){\n if(st.empty())continue;\n else{\n while(st.size() and v[st.top()][1] < v[i][1]){\n v[st.top()][1] = 0;\n v[i][1]--;\n st.pop();\n }\n if(st.size() and v[st.top()][1] == v[i][1]){\n v[st.top()][1] = 0;\n st.pop();\n v[i][1] = 0;\n }\n else if(st.size() and v[st.top()][1] > v[i][1]){\n v[i][1] = 0;\n v[st.top()][1]--;\n }\n }\n }else{\n st.push(i);\n }\n }\n map<int,int> mp;\n for(auto a:v)if(a[1])mp[a[3]] = a[1];\n vector<int> ans;\n for(auto p:mp)ans.push_back(p.second);\n return ans;\n\n }\n};", "memory": "278848" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n \n stack<pair<int,int>> stk;\n\n unordered_map<int, int> originalPositions;\n for (int i = 0; i < positions.size(); i++) {\n originalPositions[positions[i]] = i;\n }\n\n\n vector<pair<int, int>> pos_hp;\n\n for (int i = 0; i < positions.size(); i++) {\n pos_hp.emplace_back(positions[i], healths[i]);\n }\n\n sort(pos_hp.begin(), pos_hp.end());\n\n unordered_map<int, char> dir;\n\n for (size_t i = 0; i < positions.size(); ++i) {\n dir[positions[i]] = directions[i];\n }\n\n\n vector<pair<int, int>> aftermath;\n for (auto pair : pos_hp) {\n\n if (stk.empty() && dir[pair.first] == 'L') {\n aftermath.push_back(pair);\n continue;\n }\n\n if (dir[pair.first] == 'R') {\n stk.push(pair);\n } else if (stk.top().second == pair.second) {\n stk.pop();\n continue;\n } else if (stk.top().second > pair.second) {\n stk.top().second--;\n } else {\n do {\n stk.pop();\n pair.second--;\n } while (!stk.empty() && stk.top().second < pair.second);\n\n if (!stk.empty() && stk.top().second > pair.second) stk.top().second--;\n else if (!stk.empty() && stk.top().second == pair.second) {\n stk.pop();\n } else aftermath.push_back(pair);\n }\n }\n\n while (!stk.empty()) {\n aftermath.push_back(stk.top());\n stk.pop();\n }\n\n for (int i = 0; i < aftermath.size(); i++) {\n aftermath[i] = {originalPositions[aftermath[i].first], aftermath[i].second};\n }\n\n sort(aftermath.begin(), aftermath.end());\n\n vector<int> result;\n for(int i = 0; i < aftermath.size(); i++) {\n result.push_back(aftermath[i].second);\n }\n\n return result;\n }\n};", "memory": "280166" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n \n stack<pair<int,int>> stk;\n\n unordered_map<int, int> originalPositions;\n for (int i = 0; i < positions.size(); i++) {\n originalPositions[positions[i]] = i;\n }\n\n\n vector<pair<int, int>> pos_hp;\n\n for (int i = 0; i < positions.size(); i++) {\n pos_hp.emplace_back(positions[i], healths[i]);\n }\n\n sort(pos_hp.begin(), pos_hp.end());\n\n unordered_map<int, char> dir;\n\n for (size_t i = 0; i < positions.size(); ++i) {\n dir[positions[i]] = directions[i];\n }\n\n\n vector<pair<int, int>> aftermath;\n for (auto pair : pos_hp) {\n\n if (stk.empty() && dir[pair.first] == 'L') {\n aftermath.push_back(pair);\n continue;\n }\n\n if (dir[pair.first] == 'R') {\n stk.push(pair);\n } else if (stk.top().second == pair.second) {\n stk.pop();\n continue;\n } else if (stk.top().second > pair.second) {\n stk.top().second--;\n } else {\n do {\n stk.pop();\n pair.second--;\n } while (!stk.empty() && stk.top().second < pair.second);\n\n if (!stk.empty() && stk.top().second > pair.second) stk.top().second--;\n else if (!stk.empty() && stk.top().second == pair.second) {\n stk.pop();\n } else aftermath.push_back(pair);\n }\n }\n\n while (!stk.empty()) {\n aftermath.push_back(stk.top());\n stk.pop();\n }\n\n for (int i = 0; i < aftermath.size(); i++) {\n aftermath[i] = {originalPositions[aftermath[i].first], aftermath[i].second};\n }\n\n sort(aftermath.begin(), aftermath.end());\n\n vector<int> result;\n for(int i = 0; i < aftermath.size(); i++) {\n result.push_back(aftermath[i].second);\n }\n\n return result;\n }\n};", "memory": "280166" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<vector<int>> v;\n for(int i=0;i<positions.size();i++){\n v.push_back({positions[i],healths[i],directions[i]=='R'?1:-1});\n }\n sort(v.begin(),v.end());\n stack<pair<int,int>> s;\n s.push(make_pair(v[0][0],v[0][1]*v[0][2]));\n for(int i=1;i<v.size();i++){\n auto m = v[i];\n if(v[i][2]>0){\n s.push(make_pair(v[i][0],v[i][1]));\n continue;\n }\n else{\n if(s.empty()){\n s.push(make_pair(v[i][0],v[i][1]*v[i][2]));\n continue;\n }\n while(!s.empty()){\n auto x = s.top();\n if(x.second<0) break;\n s.pop();\n // cout<<s.size()<<endl;\n // if(s.empty()) cout<<\"dg\"<<endl;\n if(x.second<m[1]){\n m[1]--;\n continue;\n }\n else{\n if(x.second != m[1]){\n // cout<<x.second<<endl;\n s.push(make_pair(x.first,x.second-1));\n }\n m[1] = 0;\n break;\n }\n }\n if(m[1] > 0){\n s.push(make_pair(v[i][0],m[1]*v[i][2]));\n }\n }\n }\n map<int,int> mp;\n while(!s.empty()){\n // cout<<s.top().second<<\" \";\n mp[s.top().first] = abs(s.top().second);\n s.pop();\n } \n vector<int> ans;\n for(auto p:positions){\n if(mp.find(p)!=mp.end()){\n // cout<<p<<\" \"<<mp[p]<<endl;\n ans.push_back(mp[p]);\n }\n }\n return ans;\n }\n};", "memory": "281483" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<vector<int>> robots;\n int n = positions.size();\n\n for(int i = 0; i < n; i++) robots.push_back({ positions[i], (directions[i] == 'L'), healths[i] });\n\n sort(robots.begin(), robots.end());\n unordered_map<int, int> umap;\n\n for(int i = 0; i < n; i++) umap[robots[i][0]] = i;\n\n stack<int> forward;\n\n for(int i = 0; i < n; i++) {\n if(robots[i][1] == 0) {\n forward.push(i);\n } else {\n while(!forward.empty() && robots[i][2] > robots[forward.top()][2]) {\n robots[forward.top()][2] = -1;\n forward.pop();\n robots[i][2]--;\n cout << \"1 \";\n }\n\n if(!forward.empty()) {\n if(robots[i][2] == robots[forward.top()][2]) {\n robots[forward.top()][2] = -1;\n forward.pop();\n cout << \"2\";\n } else {\n robots[forward.top()][2]--;\n cout << \"3\";\n }\n robots[i][2] = -1;\n }\n\n cout << endl;\n }\n }\n\n vector<int> out;\n\n for(auto p : positions) {\n if(robots[umap[p]][2] != -1) out.push_back(robots[umap[p]][2]);\n }\n\n return out;\n }\n};", "memory": "282801" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<vector<int>> robots;\n int n = positions.size();\n\n for(int i = 0; i < n; i++) robots.push_back({ positions[i], (directions[i] == 'L'), healths[i] });\n\n sort(robots.begin(), robots.end());\n unordered_map<int, int> umap;\n\n for(int i = 0; i < n; i++) umap[robots[i][0]] = i;\n\n stack<int> forward;\n\n for(int i = 0; i < n; i++) {\n if(robots[i][1] == 0) {\n forward.push(i);\n } else {\n while(!forward.empty() && robots[i][2] > robots[forward.top()][2]) {\n robots[forward.top()][2] = -1;\n forward.pop();\n robots[i][2]--;\n }\n\n if(!forward.empty()) {\n if(robots[i][2] == robots[forward.top()][2]) {\n robots[forward.top()][2] = -1;\n forward.pop();\n } else {\n robots[forward.top()][2]--;\n }\n robots[i][2] = -1;\n }\n\n }\n }\n\n vector<int> out;\n\n for(auto p : positions) {\n if(robots[umap[p]][2] != -1) out.push_back(robots[umap[p]][2]);\n }\n\n return out;\n }\n};", "memory": "282801" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions,\n vector<int>& healths, string directions) {\n\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n\n\n vector<vector<int>> robots;\n stack<pair<int, int>> c;\n\n vector<int> result;\n\n unordered_map<int, int> factor;\n\n\n for (int i = 0; i < positions.size(); i++) {\n\n robots.push_back({positions[i], healths[i], (directions[i])});\n }\n\n sort(robots.begin(), robots.end());\n\n int r = 0;\n\n while (r < robots.size()) {\n\n if (!c.empty()) {\n\n if (c.top().first != robots[r][2] and c.top().first != 'L') {\n\n if (robots[c.top().second][1] > robots[r][1]) {\n robots[c.top().second][1]--;\n r++;\n } else if (robots[c.top().second][1] < robots[r][1]) {\n robots[r][1]--;\n\n robots[c.top().second][1] = 0;\n c.pop();\n } else {\n robots[c.top().second][1] = 0;\n c.pop();\n robots[r][1] = 0;\n r++;\n }\n\n } else {\n\n c.push({robots[r][2], r});\n r++;\n }\n\n } else {\n c.push({robots[r][2], r});\n r++;\n }\n }\n\n // if (c.size() == positions.size())\n // return healths;\n\n while (!c.empty()) {\n\n factor[robots[c.top().second][0]] = robots[c.top().second][1];\n c.pop();\n }\n\n for (int j : positions) {\n\n if (factor[j] != 0) {\n result.push_back(factor[j]);\n }\n }\n\n return result;\n }\n};", "memory": "284118" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<vector<int>> memo; // index, pos, health, direction (1 - R, 0 - L);\n int n = positions.size();\n for (int i = 0; i < n; i++) {\n int dir = directions[i] == 'R' ? 1 : 0;\n memo.push_back({positions[i], healths[i], dir, i});\n }\n\n sort(memo.begin(), memo.end());\n\n stack<vector<int>> st; // pos, health\n for (int k = 0; k < n; k++) {\n int p = memo[k][0];\n int h = memo[k][1];\n int d = memo[k][2];\n int i = memo[k][3];\n if (d) {\n st.push({i, h, d});\n } else {\n int left_out = false;\n while (!st.empty()) {\n if (!st.top()[2]) break;\n if (h < st.top()[1]) {\n --st.top()[1];\n left_out = true;\n break;\n } else if (h == st.top()[1]) {\n st.pop();\n left_out = true;\n break;\n } else {\n st.pop();\n --h;\n }\n }\n\n if (!left_out) st.push({i, h, d});\n }\n }\n\n map<int, int> mp;\n while (!st.empty()) {\n mp[st.top()[0]] = st.top()[1];\n st.pop();\n }\n\n vector<int> ans;\n for (auto &it : mp) {\n ans.push_back(it.second);\n }\n\n return ans;\n }\n};\n\n///", "memory": "284118" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n stack<int> lstack;\n stack<int> rstack;\n int n = positions.size();\n vector<vector<int>> v;\n for(int i=0;i<n;i++){\n v.push_back({positions[i],healths[i],(directions[i]=='L'?1:2) });\n }\n sort(v.begin(),v.end());\n map<int,int> mp;\n for(int i=0;i<n;i++){\n mp[v[i][0]]=i;\n }\n for(int i=0;i<n;i++){\n if(v[i][2]==1){\n cout<<\"hi\\n\";\n int hl = v[i][1];\n\n while(rstack.size()>0){\n int id = rstack.top();\n rstack.pop();\n int hr = v[id][1];\n\n if(hl<hr){\n hr--;\n hl=0;\n v[id][1]=hr;\n if(hr!=0){\n rstack.push(id);\n }\n break;\n }else if (hl>hr){\n hl--;\n v[id][1]=0;\n }else{\n hl=0,\n hr=0;\n v[id][1]=0;\n break;\n }\n }\n\n if(hl!=0){\n lstack.push(i);\n }\n v[i][1]=hl;\n }else{\n rstack.push(i);\n }\n }\n vector<int> ans;\n for(int i=0;i<n;i++){\n int id = mp[positions[i]];\n if(v[id][1]!=0){\n ans.push_back(v[id][1]);\n }\n }\n return ans;\n }\n};", "memory": "285436" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& position, vector<int>& health , string directions) {\n int n = position.size() ;\n vector<int> ans(n) ;\n\n unordered_map<int , int> mp ;\n\n vector<vector<int>> robots ;\n for(int i=0 ; i<n ; i++){\n robots.push_back({position[i] , health[i] , directions[i]}) ;\n mp[position[i]] = i ;\n }\n \n sort(robots.begin() , robots.end()) ;\n stack<int> st ;\n \n for(int i=0 ; i<n ; i++){ \n if(robots[i][2] == 'L'){\n while(!st.empty() && robots[st.top()][1] > 0){\n int t = st.top() ; \n\n if(robots[t][1] == robots[i][1]){\n //cout<<\"Equal :\"<<robots[t][1]<<\" \"<<robots[i][1]<<endl ;\n robots[t][1] = robots[i][1] = 0 ;\n st.pop() ; break ;\n }else if(robots[t][1] > robots[i][1]){\n //cout<<\"Right More :\"<<robots[t][1]<<\" \"<<robots[i][1]<<endl ;\n robots[t][1]-- ;\n robots[i][1] = 0 ; \n break ; \n }else{\n //cout<<\"Left more :\"<<robots[t][1]<<\" \"<<robots[i][1]<<endl ;\n robots[i][1]-- ; \n robots[t][1] = 0;\n st.pop() ;\n } \n }\n }else{\n st.push(i) ;\n } \n }\n\n \n // cout<<\"Ans\"<<endl ;\n // for(auto p : robots){ cout<<p[0]<<\" \"<<p[1]<<\" \"<<p[2]<<endl ; } \n for(int i=0 ; i<n ; i++){\n if(robots[i][1] > 0){ \n ans[mp[robots[i][0]]] = robots[i][1] ;\n }\n }\n \n vector<int> res ;\n for(auto &r : ans){ \n if(r>0){res.push_back(r) ;}\n }\n\n return res ;\n }\n};", "memory": "285436" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<int> ans(positions.size(),0);\n vector<vector<int>> rob;\n map<int,int> mp;\n for(int i=0; i<positions.size(); i++){\n mp[positions[i]]=i;\n rob.push_back({positions[i],healths[i],directions[i]});\n }\n sort(rob.begin(),rob.end());\n stack<pair<int,int>> st;\n for(int i=0; i<rob.size(); i++){\n if(st.empty()){\n if(rob[i][2]=='R'){\n st.push({rob[i][1],rob[i][0]});\n }\n else{\n ans[mp[rob[i][0]]]=rob[i][1];\n }\n }\n else{\n if(rob[i][2]=='R'){\n st.push({rob[i][1],rob[i][0]});\n }\n else{\n while(!st.empty() && rob[i][1]>st.top().first){\n rob[i][1]-=1;\n st.pop();\n }\n if(st.empty()){\n if(rob[i][1]>0){\n ans[mp[rob[i][0]]]=rob[i][1];\n }\n }\n else{\n if(st.top().first==rob[i][1]){\n st.pop();\n }\n else{\n int h=st.top().first;\n int idx=st.top().second;\n h-=1;\n st.pop();\n st.push({h,idx});\n }\n }\n }\n }\n }\n while(!st.empty()){\n ans[mp[st.top().second]]=st.top().first;\n st.pop();\n }\n vector<int> v;\n for(auto it:ans){\n if(it!=0){\n v.push_back(it);\n }\n }\n return v;\n }\n};", "memory": "286753" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "class Solution {\n\npublic:\n static bool cmp(pair<int, int> a, pair<int, int> b) {\n return a.second < b.second;\n }\n\n vector<int> survivedRobotsHealths(vector<int>& positions,\n vector<int>& healths, string directions) {\n unordered_map<int, int> index;\n stack<pair<int, int>> st;\n int n = positions.size();\n vector<pair<int, int>> vec;\n\n unordered_map<int, int> mp;\n for (int i = 0; i < n; i++) {\n vec.push_back({positions[i], directions[i]});\n mp[positions[i]] = healths[i];\n index[positions[i]] = i;\n }\n sort(vec.begin(), vec.end());\n vector<pair<int, int>> ans;\n vector<pair<int, int>> temp;\n for (int i = 0; i < n; i++) {\n bool thissurvive = true;\n if (vec[i].second == 'L') {\n if (st.empty()) {\n temp.push_back({mp[vec[i].first], vec[i].first});\n continue;\n }\n while (!st.empty()) {\n if (st.top().first == mp[vec[i].first]) {\n st.pop();\n thissurvive = false;\n break;\n } else if (st.top().first > mp[vec[i].first]) {\n auto y = st.top();\n int ok = y.first - 1;\n int index = y.second;\n st.pop();\n st.push({ok, index});\n thissurvive = false;\n break;\n } else {\n st.pop();\n mp[vec[i].first] -= 1;\n }\n }\n if (thissurvive) {\n temp.push_back({mp[vec[i].first], vec[i].first});\n }\n }\n if (vec[i].second == 'R') {\n\n st.push({mp[vec[i].first], vec[i].first});\n }\n }\n\n while (!st.empty()) {\n ans.push_back(st.top());\n st.pop();\n }\n for (auto x : temp) {\n ans.push_back(x);\n }\n\n vector<pair<int, int>> finatemp;\n for (auto x : ans) {\n finatemp.push_back({x.first, index[x.second]});\n }\n\n sort(finatemp.begin(), finatemp.end(), cmp);\n vector<int> fina;\n for (auto x : finatemp) {\n fina.push_back(x.first);\n }\n return fina;\n }\n};", "memory": "286753" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "class Solution {\n \n \npublic:\n \n static bool cmp(pair<int,int>a,pair<int,int>b){\n return a.second<b.second;\n }\n \n\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n unordered_map<int, int> index;\n stack<pair<int,int>>st;\n int n=positions.size();\n vector<pair<int,int>>vec;\n \n unordered_map<int,int>mp;\n for(int i=0; i<n;i++){\n vec.push_back({positions[i],directions[i]});\n mp[positions[i]]=healths[i];\n index[positions[i]]=i;\n }\n sort(vec.begin(),vec.end());\n vector<pair<int,int>>ans;\n vector<pair<int,int>>temp;\n for(int i=0; i<n; i++){\n bool thissurvive=true;\n if(vec[i].second=='L'){\n if(st.empty()){\n temp.push_back({mp[vec[i].first],vec[i].first});\n continue;\n }\n while(!st.empty()){\n if(st.top().first==mp[vec[i].first]){\n st.pop();\n thissurvive=false;\n break;\n }\n else if(st.top().first>mp[vec[i].first]){\n auto y=st.top();\n int ok=y.first-1;\n int index=y.second;\n st.pop();\n st.push({ok,index});\n thissurvive=false;\n break;\n }\n else{\n st.pop();\n mp[vec[i].first]-=1;\n }\n }\n if(thissurvive){\n temp.push_back({mp[vec[i].first],vec[i].first});\n }\n }\n if(vec[i].second=='R' && thissurvive){\n \n st.push({mp[vec[i].first],vec[i].first});\n }\n\n\n }\n\n\nwhile(!st.empty()){\n ans.push_back(st.top());\n st.pop();\n}\nfor(auto x:temp){\n ans.push_back(x);\n}\n\nvector<pair<int,int>>finatemp;\nfor(auto x:ans){\n finatemp.push_back({x.first,index[x.second]});\n}\n\nsort(finatemp.begin(),finatemp.end(),cmp);\nvector<int>fina;\nfor(auto x:finatemp){\n fina.push_back(x.first);\n}\nreturn fina;\n }\n};", "memory": "288071" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<int>ans;\n vector<vector<int>>v;\n for (int i=0;i<positions.size();i++)\n {\n v.push_back({positions[i],healths[i],directions[i]=='L',i});\n }\n stack<vector<int>>left,right;\n sort(v.begin(),v.end());\n for (int i=0;i<positions.size();i++)\n {\n if (v[i][2]==0)\n {\n right.push({v[i][3],v[i][0],v[i][1]});\n }\n else \n {\n while (!right.empty() && right.top()[2]<v[i][1])\n {\n v[i][1]--;\n right.pop();\n }\n if (!right.empty())\n {\n if (right.top()[2]==v[i][1])right.pop();\n else right.top()[2]--;\n }\n else left.push({v[i][3],v[i][0],v[i][1]});\n }\n }\n vector<vector<int>>temp;\n while (!left.empty())\n {\n temp.push_back({left.top()[0],left.top()[2]});\n left.pop();\n }\n while (!right.empty())\n {\n temp.push_back({right.top()[0],right.top()[2]});\n right.pop();\n }\n sort(temp.begin(),temp.end());\n for (int i=0;i<temp.size();i++)ans.push_back(temp[i][1]);\n return ans;\n }\n};", "memory": "288071" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "class Solution {\npublic:\n \n vector<int> survivedRobotsHealths(vector<int>& pos, vector<int>& healths, string dirs) {\n int n = pos.size();\n vector<vector<int>> v;\n for (int i=0; i<n;i++){\n int p = pos[i], h = healths[i], dir = dirs[i]-'A';\n v.push_back({p,h,dir,i});\n }\n sort(v.begin(), v.end());\n\n stack<vector<int>> s;\n\n for (int i=0;i<n;i++){\n int p = v[i][0], h = v[i][1], dir = v[i][2], idx = v[i][3];\n\n if (char('A' + dir) == 'R') s.push({v[i]});\n else{\n \n while(s.size() && h > 0){\n if (('A' + s.top()[2]) != 'R') break;\n int hTop = s.top()[1];\n if (hTop < h){\n s.pop();\n h--;\n }\n else if (hTop == h){\n s.pop();\n h = 0;\n }\n else{\n h = 0;\n s.top()[1]--;\n }\n }\n if (h > 0) s.push({p,h,dir,idx});\n }\n }\n\n vector<vector<int>> tempAns;\n while(s.size()){\n tempAns.push_back(s.top());\n s.pop();\n }\n\n sort(tempAns.begin(), tempAns.end(), [](vector<int>& a, vector<int>& b){\n return a[3] < b[3];\n });\n\n vector<int> ans;\n for (auto& tA : tempAns){\n ans.push_back(tA[1]);\n }\n return ans;\n }\n};", "memory": "289388" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n unordered_map<long long,vector<long long>> t;\n vector<int> k=positions;\n sort(k.begin(),k.end());\n for(int i=0;i<positions.size();i++){\n if(directions[i]=='L'){\n t[positions[i]].push_back(healths[i]);\n t[positions[i]].push_back(-1);\n }\n else{\n t[positions[i]].push_back(healths[i]);\n t[positions[i]].push_back(1);\n }\n }\n stack<long long> r;\n vector<int> ans;\n for(int i=0;i<k.size();i++){\n if(t[k[i]][1]==1){\n r.push(k[i]);\n }\n else{\n if(r.empty()){\n t[k[i]][1]=0;\n }\n else{\n while(!r.empty()&&t[r.top()][0]<t[k[i]][0]){\n t[k[i]][0]--;\n r.pop();\n }\n if(r.empty()){\n t[k[i]][1]=0;\n }\n else{\n if(t[r.top()][0]==t[k[i]][0]){\n r.pop();\n }\n else\n t[r.top()][0]--;\n }\n }\n }\n }\n while(!r.empty()){\n t[r.top()][1]=0;\n r.pop();\n }\n for(int i=0;i<positions.size();i++){\n if(t[positions[i]][1]==0&&t[positions[i]][0]>0){\n ans.push_back(t[positions[i]][0]);\n }\n }\n return ans;\n }\n};", "memory": "289388" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "bool cmp(vector<int>v1,vector<int>v2)\n{\n\n return v1[0]<v2[0];\n}\nbool cmp1(vector<int>v1,vector<int>v2)\n{\n\n return v1[3]<v2[3];\n}\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = positions.size();\n \n // Create a vector of robots where each robot is represented by its position, health, direction, and index\n vector<vector<int>> robots(n, vector<int>(4));\n for (int i = 0; i < n; i++) {\n robots[i][0] = positions[i]; // Position\n robots[i][1] = healths[i]; // Health\n robots[i][2] = (directions[i] == 'L'); // Direction (0 for right, 1 for left)\n robots[i][3] = i; // Original index\n }\n \n // Sort robots by their positions\n sort(robots.begin(), robots.end(), [](const vector<int>& a, const vector<int>& b) {\n return a[0] < b[0];\n });\n \n stack<vector<int>> st;\n \n for (auto robot : robots) {\n while (!st.empty() && st.top()[2] == 0 && robot[2] == 1) {\n // Collision between right-moving (st.top()) and left-moving (robot)\n if (st.top()[1] > robot[1]) {\n st.top()[1]--; // Reduce health of the robot in the stack\n robot[1] = 0; // Remove current robot'\n break;\n } else if (st.top()[1] < robot[1]) {\n robot[1]--; // Reduce health of the current robot\n st.pop(); // Remove the robot from the stack\n } else {\n st.pop(); // Both robots have equal health and are removed\n robot[1] = 0; // Remove current robot]\n break;\n }\n }\n \n if (robot[1] > 0) { // Only push the robot if it survived\n st.push(robot);\n }\n }\n \n vector<vector<int>> result;\n while (!st.empty()) {\n result.push_back(st.top());\n st.pop();\n }\n \n sort(result.begin(), result.end(), [](const vector<int>& a, const vector<int>& b) {\n return a[3] < b[3];\n });\n\n vector<int>ans;\n for(int i=0;i<result.size();i++){\n ans.push_back(result[i][1]);\n }\n return ans;\n }\n};", "memory": "290706" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "bool cmp(pair<int, int>& a, pair<int, int>& b) {\n return a.second < b.second;\n}\n\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& pos, vector<int>& hlth, string dir) {\n int n = dir.size();\n stack<pair<int, int>> st;\n vector<pair<int, int>> ans;\n map<int, vector<int>> m;\n\n // Map positions to corresponding directions, health, and indices\n for (int i = 0; i < n; i++) {\n m[pos[i]] = {dir[i], hlth[i], i};\n }\n\n for (auto it : m) {\n int pos = it.second[2];\n int hlth = it.second[1];\n char dir = it.second[0];\n\n if (dir == 'R') {\n st.push({hlth, pos});\n } else { // dir = 'L'\n while (!st.empty() && hlth > 0) {\n auto a = st.top();\n st.pop();\n\n if (a.first == hlth) {\n hlth = 0; // Both robots destroy each other\n break;\n } else if (a.first > hlth) {\n a.first--; // Right robot survives with reduced health\n st.push(a);\n hlth = 0; // Left robot is destroyed\n } else {\n hlth--; // Left robot survives, but health is reduced\n }\n }\n\n if (hlth > 0) {\n ans.push_back({hlth, pos}); // No more collisions, robot survives\n }\n }\n }\n\n // Remaining robots in the stack are added to the answer\n while (!st.empty()) {\n ans.push_back({st.top().first, st.top().second});\n st.pop();\n }\n\n // Sort the results by their original positions\n sort(ans.begin(), ans.end(), cmp);\n vector<int> res;\n\n // Extract health values for surviving robots\n for (auto it : ans) {\n res.push_back(it.first);\n }\n\n return res;\n }\n};\n", "memory": "290706" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n map<int, int> health;\n map<int, string> direction;\n for(int i = 0;i < positions.size();i++) {\n health[positions[i]] = healths[i];\n direction[positions[i]] = directions[i];\n }\n vector<int> stack;\n for(auto i = health.begin();i != health.end();i++) {\n int pos = i->first;\n if(direction[pos] == \"R\") {\n stack.push_back(pos);\n } else {\n if(!stack.empty()) {\n int stackPos = stack.back();\n if(health[pos] == health[stackPos]) {\n health[pos] = 0;\n health[stackPos] = 0;\n stack.pop_back();\n } else if(health[pos] < health[stackPos]) {\n health[pos] = 0;\n health[stackPos]--;\n if(health[stackPos] == 0) { stack.pop_back(); }\n } else {\n health[pos]--;\n health[stackPos] = 0;\n stack.pop_back();\n i--;\n }\n }\n }\n }\n vector<int> ans;\n for(int i = 0;i < positions.size();i++) {\n int pos = positions[i];\n if(health[pos] != 0) {\n ans.push_back(health[pos]);\n }\n }\n return ans;\n }\n};", "memory": "292023" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n stack<int> s; // Stack to store indices of robots\n unordered_map<int, char> directionMap; // Map positions to directions\n unordered_map<int, int> healthMap; // Map positions to healths\n vector<int> result; // Vector to store the final result\n\n // Map positions to directions and healths\n for (int i = 0; i < positions.size(); i++) {\n directionMap[positions[i]] = directions[i];\n healthMap[positions[i]] = healths[i];\n }\n\n // Check if all robots are moving in the same direction\n bool allSameDirection = true;\n char firstDirection = directions[0];\n for (char dir : directions) {\n if (dir != firstDirection) {\n allSameDirection = false;\n break;\n }\n }\n\n if (allSameDirection) {\n // If all robots move in the same direction, return healths in original order\n return healths;\n }\n\n // Sort positions for processing\n vector<int> sortedPositions = positions;\n sort(sortedPositions.begin(), sortedPositions.end());\n\n // Map to get original index quickly\n unordered_map<int, int> indexMap;\n for (int i = 0; i < positions.size(); i++) {\n indexMap[positions[i]] = i;\n }\n\n for (int pos : sortedPositions) {\n int i = indexMap[pos];\n bool destroyed = false;\n\n while (!s.empty() && directionMap[positions[s.top()]] == 'R' && directionMap[pos] == 'L') {\n if (healthMap[positions[s.top()]] < healthMap[pos]) {\n healthMap[pos]--;\n s.pop();\n } else if (healthMap[pos] == healthMap[positions[s.top()]]) {\n s.pop();\n destroyed = true;\n break;\n } else {\n healthMap[positions[s.top()]]--;\n destroyed = true;\n break;\n }\n }\n\n if (!destroyed) {\n s.push(i);\n }\n }\n\n // Collect surviving robots' healths based on their original index\n vector<int> survivors;\n while (!s.empty()) {\n survivors.push_back(s.top());\n s.pop();\n }\n\n sort(survivors.begin(), survivors.end());\n\n // Add the health of the surviving robots to the result vector `result`\n for (int i : survivors) {\n result.push_back(healthMap[positions[i]]);\n }\n\n return result;\n }\n};\n", "memory": "292023" }
2,846
<p>There are <code>n</code> <strong>1-indexed</strong> robots, each having a position on a line, health, and movement direction.</p> <p>You are given <strong>0-indexed</strong> integer arrays <code>positions</code>, <code>healths</code>, and a string <code>directions</code> (<code>directions[i]</code> is either <strong>&#39;L&#39;</strong> for <strong>left</strong> or <strong>&#39;R&#39;</strong> for <strong>right</strong>). All integers in <code>positions</code> are <strong>unique</strong>.</p> <p>All robots start moving on the line<strong> simultaneously</strong> at the <strong>same speed </strong>in their given directions. If two robots ever share the same position while moving, they will <strong>collide</strong>.</p> <p>If two robots collide, the robot with <strong>lower health</strong> is <strong>removed</strong> from the line, and the health of the other robot <strong>decreases</strong> <strong>by one</strong>. The surviving robot continues in the <strong>same</strong> direction it was going. If both robots have the <strong>same</strong> health, they are both<strong> </strong>removed from the line.</p> <p>Your task is to determine the <strong>health</strong> of the robots that survive the collisions, in the same <strong>order </strong>that the robots were given,<strong> </strong>i.e. final health of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.</p> <p>Return <em>an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.</em></p> <p><strong>Note:</strong> The positions may be unsorted.</p> <div class="notranslate" style="all: initial;">&nbsp;</div> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <p><img height="169" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516011718-12.png" width="808" /></p> <pre> <strong>Input:</strong> positions = [5,4,3,2,1], healths = [2,17,9,15,10], directions = &quot;RRRRR&quot; <strong>Output:</strong> [2,17,9,15,10] <strong>Explanation:</strong> No collision occurs in this example, since all robots are moving in the same direction. So, the health of the robots in order from the first robot is returned, [2, 17, 9, 15, 10]. </pre> <p><strong class="example">Example 2:</strong></p> <p><img height="176" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516004433-7.png" width="717" /></p> <pre> <strong>Input:</strong> positions = [3,5,2,6], healths = [10,10,15,12], directions = &quot;RLRL&quot; <strong>Output:</strong> [14] <strong>Explanation:</strong> There are 2 collisions in this example. Firstly, robot 1 and robot 2 will collide, and since both have the same health, they will be removed from the line. Next, robot 3 and robot 4 will collide and since robot 4&#39;s health is smaller, it gets removed, and robot 3&#39;s health becomes 15 - 1 = 14. Only robot 3 remains, so we return [14]. </pre> <p><strong class="example">Example 3:</strong></p> <p><img height="172" src="https://assets.leetcode.com/uploads/2023/05/15/image-20230516005114-9.png" width="732" /></p> <pre> <strong>Input:</strong> positions = [1,2,5,6], healths = [10,10,11,11], directions = &quot;RLRL&quot; <strong>Output:</strong> [] <strong>Explanation:</strong> Robot 1 and robot 2 will collide and since both have the same health, they are both removed. Robot 3 and 4 will collide and since both have the same health, they are both removed. So, we return an empty array, [].</pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 &lt;= positions.length == healths.length == directions.length == n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= positions[i], healths[i] &lt;= 10<sup>9</sup></code></li> <li><code>directions[i] == &#39;L&#39;</code> or <code>directions[i] == &#39;R&#39;</code></li> <li>All values in <code>positions</code> are distinct</li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n // check for l\n int n=positions.size();\n vector<pair<vector<int>,char>>v;\n for(int i=0;i<n;i++){\n v.push_back({{positions[i],healths[i],i},directions[i]});\n }\n sort(v.begin(),v.end());\n\n for(int i=0;i<n;i++){\n healths[i]=v[i].first[1];\n directions[i]=v[i].second;\n }\n \n int next=0;\n vector<int>ans(n,0);\n int cur=0;\n int now=0;\n stack<int>s;\n for(auto i:directions) cout<<i<<\" \"; cout<<endl;\n for(auto i:healths) cout<<i<<\" \"; cout<<endl;\n for(int i=n-1;i>=0;i--){\n if(directions[i]=='R'){\n // cout<<s.size()<<endl;\n cur=healths[i];\n int pans=0;\n cout<<s.size()<<endl;\n while(!s.empty()){\n if(cur<=s.top()){\n if(cur==s.top())s.pop(); \n else s.top()--;\n cur=0; break;\n }\n cur--;\n if(cur<=0) break;\n s.pop();\n pans++;\n\n }\n ans[i]=cur;\n }\n else {s.push(healths[i]);}\n }\n\n next=0; cur=0;\n now=0;\n while(!s.empty()) s.pop();\n for(int i=0;i<n;i++){\n if(directions[i]=='L'){\n cur=healths[i];\n int pans=0;\n while(!s.empty()){\n if(cur<=s.top()){\n \n \n if(cur==s.top())s.pop();\n else s.top()--;\n cur=0; \n break;\n }\n cur--;\n \n if(cur<=0) break;\n s.pop();\n pans++;\n\n }\n ans[i]=cur;\n }\n else {s.push(healths[i]);}\n }\n vector<int>finalans;\n\n map<int,int>mp;\n for(int i=0;i<n;i++){\n if(ans[i]>0){\n mp[v[i].first[2]]=ans[i];\n }\n }\n for(auto i:mp) finalans.push_back(i.second);\n return finalans;\n }\n};", "memory": "293341" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
0
{ "code": "class Solution {\n public:\n int maxIceCream(vector<int>& costs, int coins) {\n ranges::sort(costs);\n\n for (int i = 0; i < costs.size(); ++i)\n if (coins >= costs[i])\n coins -= costs[i];\n else\n return i;\n\n return costs.size();\n }\n};", "memory": "79000" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
0
{ "code": "class Solution {\n public:\n int maxIceCream(vector<int>& costs, int coins) {\n ranges::sort(costs);\n\n for (int i = 0; i < costs.size(); ++i)\n if (coins >= costs[i])\n coins -= costs[i];\n else return i;\n return costs.size();\n }\n};", "memory": "79100" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n sort(costs.begin(),costs.end());\n int ans=0;\n for(int i=0;i<costs.size();i++)\n {\n if(coins>=costs[i])\n {\n coins-=costs[i];\n ans++;\n }\n }\n return ans ;\n }\n};", "memory": "80000" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n sort(costs.begin(),costs.end());\n int cnt = 0;\n for(int i=0;i<costs.size();i++){\n if(coins>=costs[i]){\n coins = coins - costs[i];\n cnt++;\n }\n }\n return cnt;\n }\n};", "memory": "80100" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n sort(costs.begin(),costs.end());\n int cnt = 0;\n for(int i=0;i<costs.size();i++){\n if(coins>=costs[i]){\n coins = coins - costs[i];\n cnt++;\n }\n }\n return cnt;\n }\n};", "memory": "80100" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n sort(begin(costs), end(costs));\n int n = costs.size(), count = 0;\n for(int i = 0; i < n; i++){\n if(coins < 0) return count;\n count += (costs[i] <= coins);\n coins -= costs[i];\n }\n return count;\n }\n};", "memory": "80200" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coforins) {\n sort(costs.begin(),costs.end());\n int c=0;\n for(auto i:costs)\n {\n if(i<=coforins)\n {\n c+=1;\n coforins-=i;\n }\n }\n return c;\n \n \n }\n};", "memory": "80200" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n int count=0;\n sort(costs.begin(),costs.end());\n for(int i=0;i<costs.size();i++){\n if(costs[i] <= coins){\n coins-=costs[i];\n count++;\n }\n else break;\n }\n return count;\n }\n};", "memory": "80300" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
0
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n int n = costs.size();\n sort(costs.begin(),costs.end());\n int i = 0;\n long long int cnt = 0;\n while(i<n){\n if(costs[i]<=coins){\n cnt++;\n }\n coins -= costs[i];\n if(coins <= 0){\n break;\n }\n i++;\n }\n return cnt;\n }\n};", "memory": "80300" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
2
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n int n = costs.size();\n sort(costs.begin(),costs.end());\n int count = 0;\n for(int i=0; i<n;i++){\n if(costs[i]<=coins){\n count++;\n coins -= costs[i];\n }\n if(coins==0) break;\n }\n return count;\n }\n};", "memory": "80400" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n int ans=0;\n sort(begin(costs),end(costs));\n\n for(int i=0;i<costs.size();i++){\n if(costs[i]<=coins){\n ans++;\n coins-=costs[i];\n }\n else break;\n }\n return ans;\n }\n};", "memory": "80500" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n sort(costs.begin(),costs.end());\n int count = 0;\n\n for(int i=0;i<costs.size();i++){\n if(coins >= costs[i]){\n count++;\n coins -= costs[i];;\n }\n else{\n break;\n }\n }\n return count;\n }\n};\nauto init = []() {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n return 'c';\n}();", "memory": "80600" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n int c=0;\n int c1=0;\n vector<int>res;\n sort(costs.begin(),costs.end());\n for(int i=0;i<costs.size();i++){\n if(costs[i]<coins and c<=coins){\n c=c+costs[i];\n res.push_back(c);\n // c1=c1+1;\n }\n }\n for(auto i:res){\n if(i<=coins){\n c1=c1+1;\n }\n }\n return c1;\n }\n};", "memory": "81900" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n //GREEDY - Always choose the lowest cost ice cream with budget remaining\n int maxCost = 0;\n int minCost = INT_MAX;\n\n for (int i=0; i<costs.size(); i++) {\n maxCost = max(maxCost, costs[i]);\n minCost = min(minCost, costs[i]);\n }\n\n vector<int> countArr(maxCost-minCost+1, 0);\n for (int i=0; i<costs.size(); i++) {\n countArr[costs[i]-minCost]++;\n }\n\n int numBars = 0;\n for (int i=0; i<countArr.size(); i++) {\n while (countArr[i] && (i+minCost)<=coins) {\n countArr[i]--;\n coins-=i+minCost;\n numBars++;\n }\n }\n\n return numBars;\n }\n};", "memory": "82300" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n //GREEDY - Always choose the lowest cost ice cream with budget remaining\n int maxCost = 0;\n int minCost = INT_MAX;\n\n //First extract range of count array \n for (int i=0; i<costs.size(); i++) {\n maxCost = max(maxCost, costs[i]);\n minCost = min(minCost, costs[i]);\n }\n\n //Create and populate the count array\n vector<int> countArr(maxCost-minCost+1, 0);\n for (int i=0; i<costs.size(); i++) {\n countArr[costs[i]-minCost]++;\n }\n\n //Keep track of cheapest bars from count array till out of money\n int numBars = 0;\n for (int i=0; i<countArr.size(); i++) {\n while (countArr[i] && (i+minCost)<=coins) {\n countArr[i]--;\n coins-=i+minCost;\n numBars++;\n }\n }\n\n return numBars;\n }\n};", "memory": "82400" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "int compare(const void* a, const void* b)\n{\n\tconst int* x = (int*) a;\n\tconst int* y = (int*) b;\n\n\tif (*x > *y)\n\t\treturn 1;\n\telse if (*x < *y)\n\t\treturn -1;\n\n\treturn 0;\n}\n\nclass Solution {\npublic:\n\n int maxIceCream(vector<int>& costs, int coins) {\n int price = 0;\n vector<int> icecreamsIndexes;\n int cheapest = 0;\n int i = 0;\n int ans = 0;\n\n int costsFR[costs.size()];\n\n copy(costs.begin(), costs.end(), costsFR);\n\n qsort(costsFR, costs.size(), sizeof(int), compare);\n\n while(i < costs.size() && price + costsFR[i] <= coins){\n price += costsFR[i];\n i++;\n ans++;\n }\n\n return ans;\n }\n};", "memory": "82700" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n int max_el = *max_element(costs.begin(), costs.end());\n vector<int> count(max_el + 1, 0);\n for (const int &cost : costs) {\n count[cost]++;\n }\n\n int cnt = 0;\n for (int i = 0; i <= max_el; ++i) {\n if (count[i] != 0) {\n const int num = min(count[i], coins / i);\n cnt += num;\n coins -= num * i;\n if (coins < i) return cnt;\n }\n }\n return cnt;\n }\n};", "memory": "82800" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n int max = INT_MIN;\n long long bars = 0;\n for(int i = 0; i < costs.size(); i++)\n {\n if(max < costs[i])\n max = costs[i];\n }\n\n vector<int> cs = vector<int>(max+1, 0);\n\n for(int j = 0; j < costs.size(); j++)\n {\n cs[costs[j]] += 1;\n }\n for(int k = 0; k < cs.size(); k++)\n {\n long long curr = cs[k];\n long long totalCost = curr * (long long)k;\n if(totalCost < coins)\n {\n coins -= totalCost;\n bars += curr;\n continue;\n }\n else if(totalCost == coins)\n {\n bars += curr;\n return bars;\n }\n else\n {\n bars += (coins/k);\n return bars;\n }\n }\n return (int)bars;\n }\n};", "memory": "83000" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n const auto mx = *max_element(costs.begin(), costs.end());\n vector<int> freq(mx+1);\n for (auto c : costs) {\n freq[c]++;\n }\n auto ans = 0;\n\n for (auto c=1;c<=mx;c++) {\n auto f = freq[c];\n for (auto i=0;i<f;i++) {\n if (coins >= c) {\n coins -= c;\n ans++;\n }\n }\n }\n\n return ans;\n }\n};", "memory": "83100" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n int result = 0;\n int highestNumOfCoins = *max_element(costs.begin(), costs.end());\n\n vector<int> countArray(highestNumOfCoins + 1);\n\n for(int cost : costs){\n countArray[cost]++;\n }\n\n\n for(int i = 1; i < countArray.size(); i++){\n //no ice cream is present\n if(countArray[i] == 0){\n continue;\n }\n\n if(coins < countArray[i]){\n break;\n }\n\n //count how many ice cream bars we can take at the current amount of coins\n //either all of them or a portion\n int countToBuy = min(countArray[i], coins / i);\n\n coins -= countToBuy * i;\n result += countToBuy;\n }\n return result;\n }\n};", "memory": "83200" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n int maxCount=*max_element(costs.begin(),costs.end());\n vector<int> countArray(maxCount+1,0);\n for(auto i: costs){\n countArray[i]++;\n }\n int maxice=0;\n for(int i=1;i<=maxCount && coins;i++){\n int buy=coins/i;\n cout<<\"i: \"<<i<<\" | buy: \"<<buy<<\" | maxice:\"<<maxice<<\" | coins:\"<<coins<<endl;\n if(buy && countArray[i]){\n if(buy > countArray[i]){\n coins-=countArray[i]*i;\n maxice+=countArray[i];\n }\n else{\n maxice+=buy;\n break;\n }\n }\n }\n return maxice;\n }\n};\n\n", "memory": "83300" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n std::ios::sync_with_stdio(0);\n int ans = 0;\n int n = costs.size();\n int maxCost = *max_element(costs.begin(), costs.end());\n vector<int> st(maxCost + 1, 0);\n for (int num : costs) {\n st[num]++;\n }\n for (int i = 1; i <= maxCost; i++) {\n if (i > coins) {\n break;\n }\n if (i > 0 && st[i] > 0 && i > (INT_MAX / st[i])) {\n ans += coins / i;\n break;\n }\n if (i * st[i] <= coins) {\n ans += st[i];\n coins -= i * st[i];\n } else {\n ans += coins / i;\n break;\n }\n }\n return ans;\n }\n};", "memory": "83400" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n int n = costs.size();\n int cnt = 0;\n priority_queue<int, vector<int>, greater<int>> pq(\n costs.begin(), costs.end()); // Floyeds heapify Algorithm\n while (coins > 0 && !pq.empty()) {\n if (pq.top() <= coins) {\n coins -= pq.top();\n pq.pop();\n cnt++;\n } else\n break;\n }\n return cnt;\n }\n};", "memory": "83500" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n priority_queue<int,vector<int>,greater<int>> heap(costs.begin(),costs.end());\n while(coins>0 && !heap.empty()){\n if(coins>=heap.top()){\n coins-=heap.top();\n heap.pop();\n }\n else return costs.size()-heap.size();\n }\n return costs.size()-heap.size();\n }\n};", "memory": "83600" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> countSort(vector<int> &input) {\n int N = input.size();\n int M = 0;\n\n for(int i = 0; i < N; i++) {\n M = max(M, input[i]);\n }\n\n vector<int> countArray(M + 1, 0);\n\n for(int i = 0; i < N; i++) {\n countArray[input[i]]++;\n }\n\n for(int i = 1; i <= M; i++) {\n countArray[i] += countArray[i - 1];\n }\n\n vector<int> output(N);\n for(int i = N - 1; i >= 0; i--) {\n output[countArray[input[i]] - 1] = input[i];\n countArray[input[i]]--;\n }\n\n return output;\n }\n\n int maxIceCream(vector<int>& costs, int coins) {\n vector<int> sorted = countSort(costs);\n int res = 0;\n\n for(int i : sorted) {\n if(coins - i >= 0) {\n coins -= i;\n res++;\n }\n else {\n break;\n }\n }\n\n return res;\n }\n};", "memory": "86200" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> countSort(vector<int>& inputArray){\n int N = inputArray.size();\n int M = *max_element(inputArray.begin(),inputArray.end());\n vector<int> countArray(M + 1, 0);\n for (int i = 0; i < N; i++)\n countArray[inputArray[i]]++;\n for (int i = 1; i <= M; i++)\n countArray[i] += countArray[i - 1];\n vector<int> outputArray(N);\n for (int i = N - 1; i >= 0; i--){\n outputArray[countArray[inputArray[i]] - 1] = inputArray[i];\n countArray[inputArray[i]]--;\n }\n return outputArray;\n }\n int maxIceCream(vector<int>& costs, int coins) {\n vector<int>ans=countSort(costs);\n int sum=0;\n int cnt=0;\n if(ans[0]>coins)return 0;\n for(int i=0;i<ans.size();i++){\n if(sum+ans[i]<=coins){\n cnt++;\n sum+=ans[i];\n }\n else if(sum>coins){\n break;\n }\n }\n return cnt;\n }\n};", "memory": "86300" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> countSort(vector<int> &c, int &m){\n vector<int> tmp(m+1, 0);\n for(int i : c){\n tmp[i]++;\n }\n int rem = 0;\n for(auto it = tmp.begin(); it != tmp.end(); it++){\n int t = *it;\n *it = rem;\n rem += t;\n }\n vector<int> sortedArr(c.size());\n for(int i : c){\n sortedArr[tmp[i]] = i;\n tmp[i]++;\n }\n\n return sortedArr;\n }\n int maxIceCream(vector<int>& costs, int coins) {\n int m = *max_element(costs.begin(), costs.end());\n vector<int> sortArr = countSort(costs, m);\n int cnt = 0, i = 0;\n while(coins > 0 && i < sortArr.size()){\n if(coins < sortArr[i]) break;\n if(sortArr[i] <= coins){\n coins -= sortArr[i];\n cnt++, i++;\n }\n }\n return cnt;\n }\n};", "memory": "86400" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n vector<int> countingSort(vector<int>&costs){\n int n = costs.size();\n int m = *max_element(costs.begin(),costs.end());\n vector<int>count(m+1,0);\n for(auto ele : costs)count[ele]++;\n for(int i = 1 ; i < m+1;i++)count[i]+=count[i-1];\n vector<int> finalarray(n,0);\n for(auto ele : costs){\n finalarray[count[ele]-1] = ele;\n count[ele]--;\n }\n return finalarray;\n }\n int maxIceCream(vector<int>& costs, int coins) {\n costs = countingSort(costs);\n int ans = 0 ;\n for(auto ele:costs){\n if(coins-ele>=0){\n coins-=ele;\n ans++;}\n else break;\n }\n return ans;\n\n }\n};", "memory": "86500" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n int n=costs.size();\n int ans=0;\n int maxi=0;\n for(int i=0;i<n;i++){\n maxi=max(maxi,costs[i]);\n }\n\n vector<int> count(maxi+1,0);\n\n for(int i=0;i<n;i++){\n count[costs[i]]++;\n }\n for(int i=1;i<=maxi;i++){\n count[i]+=count[i-1];\n }\n\n vector<int> output(n);\n for(int i=n-1;i>=0;i--){\n output[count[costs[i]]-1]=costs[i];\n count[costs[i]]--;\n }\n\n for(int i=0;i<n;i++){\n if(output[i]>coins) ans+=0;\n else if(output[i]<=coins){\n ans+=1;\n coins-=output[i];\n }\n }\n\n\n return ans;\n }\n};", "memory": "86600" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n sort(costs.begin(), costs.end());\n vector<long long> cst(costs.size(), 0);\n \n cst[0] = costs[0];\n for(int i=1; i<costs.size(); i++){\n cst[i] = costs[i] + cst[i-1];\n }\n\n return upper_bound(cst.begin(), cst.end(), coins) - cst.begin();\n }\n};", "memory": "86700" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) \n {\n int n=costs.size();\n sort(costs.begin(), costs.end());\n vector<long long> vec(n);\n vec[0]=costs[0];\n for(int i=1;i<n;i++) vec[i] = vec[i-1] + costs[i];\n\n long long lb = lower_bound(vec.begin(),vec.end(),1LL*coins) - vec.begin();\n if(lb==n) return n;\n return (vec[lb]==coins)? lb+1:lb;\n }\n};", "memory": "86800" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "#include <bits/stdc++.h>\nusing namespace std;\n#define lop(i, n) for (long long i = 0; i < n; i++)\n#define lopr(i, n) for (long long i = n - 1; i >= 0; i--)\n#define loop(i, a, b) for (long long i = a; i < b; i++)\n\nauto init = []()\n{\n ios::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 'c';\n}();\n\nclass Solution\n{\npublic:\n int maxIceCream(vector<int> &costs, int coins)\n {\n vector<long long> pref(costs.size() + 1, 0);\n sort(costs.begin(), costs.end());\n for (long long i = 1; i <= costs.size(); ++i)\n {\n pref[i] = pref[i - 1] + costs[i - 1];\n }\n return upper_bound(pref.begin(), pref.end(), coins) - pref.begin() - 1;\n }\n};", "memory": "86900" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n int maxIceCream(vector<int>& costs, int coins) {\n vector<int> vec(costs.size()); \n sort(costs.begin(), costs.end());\n\n for(int i = 0; i < costs.size(); ++i) {\n vec.push_back(costs[i]);\n }\n\n int cnt = 0;\n for(int i = 0; i < costs.size(); ++i) {\n if (costs[i] > coins) {\n return cnt;\n }\n if (costs[i] <= coins) {\n cnt++;\n coins -= costs[i];\n }\n }\n\n return cnt;\n }\n};", "memory": "90000" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n void countingSort(vector<int>& nums)\n {\n // step 1: find max number O(n)\n int max_num = 0;\n for(int i = 0; i < nums.size(); ++i)\n max_num = max(max_num, nums[i]);\n\n // step 2: map frequncies O(n)\n vector<int> freq(max_num + 1, 0);\n for(int i = 0; i < nums.size(); ++i)\n ++freq[nums[i]];\n \n // step 3: prefix sum on frequncies O(n)\n vector<int> prefix_sum(freq.size());\n prefix_sum[0] = freq[0];\n for(int i = 1; i < freq.size(); ++i)\n prefix_sum[i] = prefix_sum[i-1] + freq[i];\n \n // step 4: sort\n vector<int> sorted_nums(nums.size());\n for(int i = nums.size() - 1; i >= 0; --i)\n {\n sorted_nums[prefix_sum[nums[i]] - 1] = nums[i];\n --prefix_sum[nums[i]];\n }\n nums = sorted_nums;\n }\n\n int maxIceCream(vector<int>& costs, int coins) {\n int max_types = 0, coins_used = 0;\n countingSort(costs);\n for(int i = 0; i < costs.size(); ++i)\n {\n if(coins_used + costs[i] > coins)\n break;\n ++max_types;\n coins_used += costs[i];\n }\n return max_types;\n }\n};", "memory": "90500" }
1,961
<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.</p> <p>At the store, there are <code>n</code> ice cream bars. You are given an array <code>costs</code> of length <code>n</code>, where <code>costs[i]</code> is the price of the <code>i<sup>th</sup></code> ice cream bar in coins. The boy initially has <code>coins</code> coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;</p> <p><strong>Note:</strong> The boy can buy the ice cream bars in any order.</p> <p>Return <em>the <strong>maximum</strong> number of ice cream bars the boy can buy with </em><code>coins</code><em> coins.</em></p> <p>You must solve the problem by counting sort.</p> <p>&nbsp;</p> <p><strong class="example">Example 1:</strong></p> <pre> <strong>Input:</strong> costs = [1,3,2,4,1], coins = 7 <strong>Output:</strong> 4 <strong>Explanation: </strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7. </pre> <p><strong class="example">Example 2:</strong></p> <pre> <strong>Input:</strong> costs = [10,6,8,7,7,8], coins = 5 <strong>Output:</strong> 0 <strong>Explanation: </strong>The boy cannot afford any of the ice cream bars. </pre> <p><strong class="example">Example 3:</strong></p> <pre> <strong>Input:</strong> costs = [1,6,3,1,2,5], coins = 20 <strong>Output:</strong> 6 <strong>Explanation: </strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18. </pre> <p>&nbsp;</p> <p><strong>Constraints:</strong></p> <ul> <li><code>costs.length == n</code></li> <li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= costs[i] &lt;= 10<sup>5</sup></code></li> <li><code>1 &lt;= coins &lt;= 10<sup>8</sup></code></li> </ul>
3
{ "code": "class Solution {\npublic:\n\n void countingSort(vector<int>& nums)\n {\n // step 1: find max number O(n)\n int max_num = 0;\n for(int i = 0; i < nums.size(); ++i)\n max_num = max(max_num, nums[i]);\n\n // step 2: map frequncies O(n)\n vector<int> freq(max_num + 1, 0);\n for(int i = 0; i < nums.size(); ++i)\n ++freq[nums[i]];\n \n // step 3: prefix sum on frequncies O(n)\n vector<int> prefix_sum(freq.size());\n prefix_sum[0] = freq[0];\n for(int i = 1; i < freq.size(); ++i)\n prefix_sum[i] = prefix_sum[i-1] + freq[i];\n \n // step 4: sort\n vector<int> sorted_nums(nums.size());\n for(int i = nums.size() - 1; i >= 0; --i)\n {\n sorted_nums[prefix_sum[nums[i]] - 1] = nums[i];\n --prefix_sum[nums[i]];\n }\n nums = sorted_nums;\n }\n\n int maxIceCream(vector<int>& costs, int coins) {\n int max_types = 0, coins_used = 0;\n countingSort(costs);\n for(int i = 0; i < costs.size(); ++i)\n {\n if(coins_used + costs[i] > coins)\n break;\n ++max_types;\n coins_used += costs[i];\n }\n return max_types;\n }\n};", "memory": "90500" }
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>
0
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n int n = tasks.size();\n auto cpm_queue = [&tasks](int i, int j) {\n if (tasks[i][1] == tasks[j][1]) return i > j;\n return tasks[i][1] > tasks[j][1];\n };\n priority_queue<int, vector<int>, decltype(cpm_queue)> q(cpm_queue);\n\n auto cpm = [&tasks](int i, int j) {\n return tasks[i][0] < tasks[j][0];\n };\n vector<int> idxs(n);\n iota(idxs.begin(), idxs.end(), 0);\n\n sort(idxs.begin(), idxs.end(), cpm);\n int64_t time_cpu_avail = 0;\n \n vector<int> res(n);\n int idx = 0;\n int i = 0;\n while (idx < n) {\n if (q.empty()) time_cpu_avail = max(time_cpu_avail, (int64_t)tasks[idxs[i]][0]);\n while (i < n && tasks[idxs[i]][0] <= time_cpu_avail) {\n q.push(idxs[i]);\n ++i;\n }\n auto t = q.top();\n // cout << \"pop \" << t << \" \" << time_cpu_avail << endl;\n q.pop();\n res[idx++] = t;\n time_cpu_avail += tasks[t][1];\n }\n return res;\n }\n};", "memory": "116670" }
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>
0
{ "code": "class custom {\npublic:\n bool operator()(pair<int, int>& a, pair<int, int>& b) {\n if (a.first > b.first) {\n return true;\n } else if (a.first == b.first) {\n if (a.second > b.second) {\n return true;\n } else {\n return false;\n }\n } else {\n return false;\n }\n }\n};\nclass Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<int> hash(tasks.size());\n for (int i = 0; i < tasks.size(); i++) {\n hash[i] = i;\n }\n sort(hash.begin(), hash.end(), [&tasks](int i1, int i2) {\n if (tasks[i1][0] < tasks[i2][0]) {\n return true;\n }\n return false;\n });\n int enqueuetime = 1;\n priority_queue<pair<int, int>, vector<pair<int, int>>, custom> pq;\n int i = 0;\n int k = 0;\n while (i < tasks.size()) {\n while (!pq.empty() && enqueuetime < tasks[hash[i]][0]) {\n hash[k++] = pq.top().second;\n enqueuetime += pq.top().first;\n pq.pop();\n }\n while (enqueuetime < tasks[hash[i]][0]) {\n enqueuetime++;\n }\n while (i < hash.size() && enqueuetime >= tasks[hash[i]][0]) {\n pq.push({tasks[hash[i]][1], hash[i]});\n i++;\n }\n hash[k++] = pq.top().second;\n enqueuetime += pq.top().first;\n pq.pop();\n }\n while (!pq.empty()) {\n hash[k++] = pq.top().second;\n pq.pop();\n }\n return hash;\n }\n};", "memory": "116670" }
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>
0
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n // sort the task based on enqueueTime, then process time, then idx\n // priority queue to choose task (1. compare process time, 2. compare idx)\n // put the first task into pq\n // time = enqueue time of the first task\n // cpu handle the current task, finish -> calculate the finished time\n // while loop to put all tasks that enqueueTime < finished time into the pq\n // cpu start the next task\n\n auto compare1 = [&](int a, int b) {\n if (tasks[a][0] != tasks[b][0]) {\n return tasks[a][0] < tasks[b][0];\n } else if (tasks[a][1] != tasks[b][1]) {\n return tasks[a][1] < tasks[b][1];\n } else {\n return a < b;\n }\n };\n int n = tasks.size();\n vector<int> task_ids(n, 0);\n for (int i = 0; i < n; i++) {\n task_ids[i] = i;\n }\n sort(task_ids.begin(), task_ids.end(), compare1);\n\n auto compare2 = [&](int a, int b) {\n if (tasks[a][1] != tasks[b][1]) {\n return tasks[a][1] > tasks[b][1];\n } else {\n return a > b;\n }\n };\n priority_queue<int, vector<int>, decltype(compare2)> pq(compare2);\n\n long long time = 0;\n int i = 0;\n vector<int> rst;\n while (!pq.empty() || i < n) {\n // still tasks unfinished\n\n // put all tasks that can be enqueued now into the pq (enqueueTime <= time)\n while (i < n) {\n if (tasks[task_ids[i]][0] <= time) {\n pq.push(task_ids[i]);\n i++;\n } else {\n break;\n }\n }\n\n if (pq.empty()) {\n // unfinished tasks haven't been enqueued\n time = tasks[task_ids[i]][0]; // next task enqueue time\n continue;\n }\n\n // pick the task to work on\n int tid = pq.top();\n pq.pop();\n rst.push_back(tid);\n time += tasks[tid][1];\n }\n return rst;\n }\n};", "memory": "117611" }
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>
0
{ "code": "using vi = vector<int>;\nusing vvi = vector<vi>;\n\nclass Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vi res, idx(tasks.size());\n priority_queue<pair<int, int>> pq;\n iota(idx.begin(), idx.end(), 0);\n sort(idx.begin(), idx.end(), [&](int i, int j) { return tasks[i][0] < tasks[j][0]; });\n\n for (long i = 0, time = 1; i < idx.size() || !pq.empty();) {\n for (; i < idx.size() && tasks[idx[i]][0] <= time; i++) {\n pq.push({-tasks[idx[i]][1], -idx[i]});\n }\n if (!pq.empty()) {\n auto [procTime, ind] = pq.top();\n pq.pop();\n time += -procTime;\n res.push_back(-ind);\n } else {\n time = tasks[idx[i]][0];\n }\n }\n\n return res;\n }\n};", "memory": "118553" }
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>
0
{ "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 int n = tasks.size();\n \n // Create a vector of indices and sort it based on the enqueue time\n std::vector<int> indices(n);\n for (int i = 0; i < n; ++i) {\n indices[i] = i;\n }\n \n // Sort the indices based on the enqueue time, and then by index to maintain stability\n std::sort(indices.begin(), indices.end(), [&tasks](int a, int b) {\n if (tasks[a][0] == tasks[b][0]) {\n return a < b;\n }\n return tasks[a][0] < tasks[b][0];\n });\n\n std::vector<int> result;\n std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<>> pq;\n \n long long currentTime = 0;\n int taskIndex = 0;\n \n while (taskIndex < n || !pq.empty()) {\n if (pq.empty() && currentTime < tasks[indices[taskIndex]][0]) {\n // No tasks are available, so move time to the next task's enqueue time\n currentTime = tasks[indices[taskIndex]][0];\n }\n \n // Push all tasks whose enqueueTime <= currentTime to the priority queue\n while (taskIndex < n && tasks[indices[taskIndex]][0] <= currentTime) {\n int idx = indices[taskIndex];\n pq.emplace(tasks[idx][1], idx);\n taskIndex++;\n }\n \n if (!pq.empty()) {\n auto [processTime, idx] = pq.top();\n pq.pop();\n \n // Execute this task\n currentTime += processTime;\n result.push_back(idx);\n }\n }\n \n return result;\n }\n};\n", "memory": "119494" }
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>
0
{ "code": "class Solution {\nprivate:\n\n struct triple{\n int start;\n int duration;\n int index;\n\n triple(){\n start = 0;\n duration = 0;\n index = 0;\n }\n\n triple(int s, int d, int i){\n start = s;\n duration = d;\n index = i;\n }\n };\n\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n const int n = tasks.size();\n vector<triple> data(n);\n\n for(int i = 0; i < n; ++i){\n data[i].start = tasks[i][0];\n data[i].duration = tasks[i][1];\n data[i].index = i;\n }\n\n sort(data.begin(), data.end(), [](const triple& first, const triple& second){\n if(first.start != second.start){\n return first.start < second.start;\n }\n else if(first.duration != second.duration){\n return first.duration < second.duration;\n }\n else{\n return first.index < second.index;\n }\n });\n\n vector<int> output(n);\n int dataIndex = 0;\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq;\n long long currTime = 0;\n\n for(int i = 0; i < n; ++i){\n \n if(pq.empty() && (currTime < (long long)data[dataIndex].start)){\n currTime = (long long)data[dataIndex].start;\n }\n \n while((dataIndex < n) && ((long long)data[dataIndex].start <= currTime)){\n pq.push({data[dataIndex].duration, data[dataIndex].index});\n ++dataIndex;\n }\n\n output[i] = pq.top().second;\n currTime += pq.top().first;\n pq.pop();\n }\n\n\n return output;\n }\n};", "memory": "120435" }
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>
0
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n int n = tasks.size();\n vector<int> index;\n for (int i = 0; i < n; i++) {\n index.emplace_back(i);\n }\n sort(index.begin(), index.end(), [&](const int& left, const int& right) {\n if (tasks[left][0] == tasks[right][0]) {\n return tasks[left][1] < tasks[right][1];\n }\n return tasks[left][0] < tasks[right][0];\n });\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;\n\n vector<int> res;\n int first = index[0];\n res.push_back(first);\n\n int time = tasks[first][0] + tasks[first][1];\n int i = 1;\n while (i < n) {\n // CPU not work, add tasks to queue\n while (i < n && (pq.empty() || tasks[index[i]][0] <= time)) {\n pq.push({tasks[index[i]][1],index[i]});\n i++;\n }\n int startTime = pq.top().first;\n int idx = pq.top().second;\n res.push_back(idx);\n time = max(time,tasks[idx][0]) + startTime;\n pq.pop();\n }\n while (!pq.empty()) {\n res.push_back(pq.top().second);\n pq.pop();\n }\n return res;\n }\n};", "memory": "121376" }
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>
0
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n int n = tasks.size();\n vector<int> index;\n for (int i = 0; i < n; i++) {\n index.emplace_back(i);\n }\n sort(index.begin(), index.end(), [&](const int& left, const int& right) {\n if (tasks[left][0] == tasks[right][0]) {\n return tasks[left][1] < tasks[right][1];\n }\n return tasks[left][0] < tasks[right][0];\n });\n priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;\n\n vector<int> res;\n int first = index[0];\n res.push_back(first);\n\n int time = tasks[first][0] + tasks[first][1];\n int i = 1;\n while (i < n) {\n // CPU not work\n while (i < n && (pq.empty() || tasks[index[i]][0] <= time)) {\n pq.push({tasks[index[i]][1],index[i]});\n i++;\n }\n res.push_back(pq.top().second);\n time = max(time,tasks[pq.top().second][0]) + pq.top().first;\n pq.pop();\n }\n while (!pq.empty()) {\n res.push_back(pq.top().second);\n pq.pop();\n }\n return res;\n }\n};", "memory": "122318" }
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>
0
{ "code": "class Solution {\npublic:\n typedef pair<pair<int, int>, int> p; // { {enqueueTime, processingTime}, originalIndex }\n\n struct lambda {\n bool operator()(const p &p1, const p &p2) const {\n // Prioritize by processing time, then by original index\n if (p1.first.second == p2.first.second)\n return p1.second > p2.second;\n return p1.first.second > p2.first.second;\n }\n };\n\n vector<int> getOrder(vector<vector<int>>& tasks) {\n int n = tasks.size();\n vector<int> indices(n);\n iota(indices.begin(), indices.end(), 0); // Fill indices with 0, 1, ..., n-1\n\n // Sort indices based on tasks' enqueue time\n sort(indices.begin(), indices.end(), [&](int i, int j) {\n return tasks[i][0] < tasks[j][0];\n });\n\n priority_queue<p, vector<p>, lambda> pq;\n int i = 0; \n long long t = tasks[indices[0]][0]; // Start time\n\n // Add all tasks that are available at the start time\n while (i < n && tasks[indices[i]][0] == t) {\n pq.push({{tasks[indices[i]][0], tasks[indices[i]][1]}, indices[i]});\n i++;\n }\n\n vector<int> ans;\n while (!pq.empty() || i < n) {\n if (pq.empty() && i < n) {\n // Jump time to next available task\n t = tasks[indices[i]][0];\n while (i < n && tasks[indices[i]][0] == t) {\n pq.push({{tasks[indices[i]][0], tasks[indices[i]][1]}, indices[i]});\n i++;\n }\n }\n\n auto top = pq.top();\n pq.pop();\n ans.push_back(top.second); // Store the original index\n t += top.first.second; // Advance time by the processing time\n\n // Add all tasks that become available up to time `t`\n while (i < n && tasks[indices[i]][0] <= t) {\n pq.push({{tasks[indices[i]][0], tasks[indices[i]][1]}, indices[i]});\n i++;\n }\n }\n\n return ans;\n }\n};\n", "memory": "123259" }
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>
0
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n int n = tasks.size();\n std::vector<std::tuple<int, int, int>> tasks_sorted(n);\n for(int i = 0; i < n; i++) {\n tasks_sorted[i] = std::tuple<int, int, int>(tasks[i][0], tasks[i][1], i);\n }\n std::sort(tasks_sorted.begin(), tasks_sorted.end());\n\n //for (auto [enqueueTime,processingTime,c] : tasks_sorted) {\n // cout << \"enqueueTime: \" << enqueueTime << \", processingTime: \" << processingTime << \", ix: \" << c << endl;\n //}\n\n std::priority_queue<std::tuple<int, int>, std::vector<std::tuple<int, int>>> heapq;\n\n int curr_ix = 0;\n long curr_time = 0;\n vector<int> ans;\n\n while (curr_ix < n || !heapq.empty()) {\n if (!heapq.empty()) {\n auto [processingTime, ix] = heapq.top();\n heapq.pop();\n ans.push_back(ix * -1);\n curr_time += processingTime * -1;\n // cout << \"curr_time: \" << curr_time << \", ix:\" << ix * -1 << endl;\n }\n else {\n auto [enqueueTime, processingTime, ix] = tasks_sorted[curr_ix];\n heapq.push(std::tuple<int, int>(-1 * processingTime, -1 * ix));\n curr_ix++;\n curr_time = enqueueTime;\n // cout << \"idle.. take next.. \" << ix << endl;\n }\n\n while (curr_ix < n && std::get<0>(tasks_sorted[curr_ix]) <= curr_time) {\n auto [enqueueTime, processingTime, ix] = tasks_sorted[curr_ix];\n heapq.push(std::tuple<int, int>(-1 * processingTime, -1 * ix));\n curr_ix++;\n // cout << \"add all can be started.. \" << ix << endl;\n }\n\n\n }\n return ans;\n }\n};", "memory": "123259" }
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>
0
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<int> res;\n int n=tasks.size();\n vector<array<int,3>>sortedtask;\n for(int i=0;i<n;i++){\n sortedtask.push_back({tasks[i][0],tasks[i][1],i});\n\n }\n sort(begin(sortedtask),end(sortedtask));\n long long cur=0;\n int i=0;\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>>pq;\n while(i<n || !pq.empty()){\n if(pq.empty() && cur<sortedtask[i][0]){\n cur=sortedtask[i][0];\n }\n while(i<n && sortedtask[i][0]<=cur){\n pq.push({sortedtask[i][1],sortedtask[i][2]});\n i++;\n }\n \n\n \n \n pair<int,int> temp=pq.top();\n pq.pop();\n cur+=temp.first;\n res.push_back(temp.second);\n \n\n }\n return res;\n }\n};", "memory": "129848" }
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>
0
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n using tuple_int = tuple<int, int, int>;\n using pair_int = pair<int, int>;\n priority_queue<tuple_int, vector<tuple_int>, greater<tuple_int>> pq;\n for(int i = 0; i<tasks.size();i++){\n pq.push({tasks[i][0], tasks[i][1], i});\n }\n\n vector<int> res;\n priority_queue<pair_int, vector<pair_int>, greater<pair_int>> pq2;\n long start_time = 0;\n while(!pq.empty() || !pq2.empty()){\n if( pq2.empty()){\n auto [start, process, i] = pq.top();\n pq2.push({process, i});\n pq.pop();\n start_time = start;\n }\n auto [process, i] = pq2.top();\n pq2.pop();\n while(!pq.empty() && start_time+process >= get<0>(pq.top())){\n auto [_, process2, i2] = pq.top();\n pq.pop();\n pq2.push({process2, i2});\n }\n\n res.push_back(i);\n start_time+=process;\n }\n\n return res;\n }\n};", "memory": "129848" }
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>
0
{ "code": "class Solution {\npublic:\n typedef pair<int,int> P;\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<int> ans;\n vector<pair<int,pair<int,int>>> v;\n for (int i = 0; i < tasks.size(); i++) {\n v.push_back({tasks[i][0],{tasks[i][1],i}}); // {enque,process,index}\n }\n sort(v.begin(),v.end());\n priority_queue<P,vector<P>,greater<P>> pq;\n long long curr_time = 0;\n int idx = 0;\n while(idx < v.size() || !pq.empty()) {\n if (pq.empty() && curr_time < v[idx].first) {\n curr_time = v[idx].first; \n }\n while(idx < v.size() && curr_time >= v[idx].first) {\n pq.push({v[idx].second.first,v[idx].second.second});\n idx++;\n }\n auto curr_task = pq.top();\n pq.pop();\n curr_time += curr_task.first;\n ans.push_back(curr_task.second);\n }\n return ans;\n }\n};", "memory": "130789" }
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>
0
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n std::ios_base::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>q;\n\n long long int time,n = tasks.size(),i = 0;\n vector<pair<int,pair<int,int>>>v;\n for(int i=0;i<n;i++)\n {\n v.push_back({tasks[i][0],{i,tasks[i][1]}});\n }\n\n sort(v.begin(),v.end());\n int initial_time = v[0].first;\n\n while(i < n && v[i].first == initial_time)\n {\n q.push({v[i].second.second,v[i].second.first});\n i++;\n }\n\n time = initial_time;\n vector<int>ans;\n\n while(q.size())\n {\n int process_time = q.top().first;\n int index = q.top().second;\n q.pop();\n time += process_time;\n\n ans.push_back(index);\n \n while(i < n && v[i].first <= time)\n {\n q.push({v[i].second.second,v[i].second.first});\n i++;\n }\n\n if(!q.size())\n {\n if(i < n)\n { initial_time = v[i].first;\n time = v[i].first;\n\n while(i < n && v[i].first == initial_time)\n {\n q.push({v[i].second.second,v[i].second.first});\n i++;\n }\n }\n }\n }\n\n return ans;\n }\n};", "memory": "130789" }
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>
0
{ "code": "\nclass Solution {\npublic:\n struct Data {\n int et; // enqueue time\n int pt; // processing time\n int it; // index\n };\n vector<int> getOrder(vector<vector<int>>& tasks) {\n \n \n priority_queue<pair<int,int> , vector<pair<int,int>> ,greater<pair<int,int>>>pq;\n \n int i = 0;\n int n = tasks.size();\n vector<Data>arr;\n for( int i = 0 ; i < n ; ++ i ){\n arr.push_back({tasks[i][0] , tasks[i][1] , i});\n }\n sort( arr.begin() , arr.end() ,[](Data& a, Data& b) { return a.et < b.et; });\n \n vector<int>ans;\n int time = arr[0].et;\n while( i < n ){\n while( i < n and arr[i].et <= time ){\n pq.push({arr[i].pt , arr[i].it});\n i++;\n }\n if( pq.empty() == false ){\n time += pq.top().first;\n ans.push_back(pq.top().second);\n pq.pop();\n }else time = arr[i].et;\n \n }\n while( pq.empty() == false ){\n ans.push_back(pq.top().second);\n pq.pop();\n }\n return ans;\n\n }\n};", "memory": "131730" }
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>
0
{ "code": "class Solution \n{\nprivate:\n struct task\n {\n int id;\n int start;\n int burst_time;\n };\n static bool comp(task t1, task t2)\n {\n return t1.start<t2.start;\n }\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) \n {\n int n=tasks.size();\n vector<task> arr;\n vector<int> ans;\n for(int i=0;i<n;i++)\n {\n arr.push_back({i,tasks[i][0],tasks[i][1]});\n }\n sort(arr.begin(),arr.end(),comp);\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>> pq;\n long long curr_time=0;\n int i=0;\n while(i<n || !pq.empty())\n {\n if(i<n && pq.empty() && curr_time<arr[i].start)\n {\n curr_time=arr[i].start;\n }\n while(i<n && arr[i].start<=curr_time)\n {\n pq.push({arr[i].burst_time,arr[i].id});\n i++;\n }\n auto it=pq.top();\n pq.pop();\n int id=it.second;\n int execution_time=it.first;\n curr_time+=execution_time;\n ans.push_back(id);\n }\n return ans;\n }\n};", "memory": "131730" }
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>
0
{ "code": "class Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<int>res;\n priority_queue <pair<pair<int,int>,int>, vector<pair<pair<int,int>,int>>, greater<pair<pair<int,int>,int>>> q;\n priority_queue <pair<pair<int,int>,int>, vector<pair<pair<int,int>,int>>, greater<pair<pair<int,int>,int>>> pq;\n for(int i=0;i<tasks.size();i++){\n pq.push({{tasks[i][0],tasks[i][1]},i});\n }\n cout<<pq.top().first.first<<endl;\n long long time=0;\nwhile((!pq.empty()||!q.empty())){\n \n if(q.empty()==1&&time<pq.top().first.first){\n time=pq.top().first.first;\n }\n\nwhile(pq.empty()==0&&time>=pq.top().first.first){\n q.push({{pq.top().first.second,pq.top().second},pq.top().first.first});\n pq.pop();\n}\nif(!q.empty()){\n int ii=q.top().first.second;\n res.push_back(ii);\n // cout<<ii<<\" \";\n time=time+q.top().first.first;\n q.pop();\n}\n\n// while(q.empty()==0&&time==q.top().first.first){\n// int ii=q.top().first.second;\n// res.push_back(ii);\n// cout<<ii<<\" \";\n// q.pop();\n// }\n// time++;\n\n}\n\n\n return res;\n }\n};\n\n", "memory": "132671" }
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>
0
{ "code": "#define ppi pair<int,pair<int,int>>\nclass Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n int n=tasks.size(),mx=0;\n vector<pair<int,pair<int,int>>>v;\n for(int i=0;i<n;i++){\n v.push_back({tasks[i][0],{tasks[i][1],i}});\n }\n sort(v.begin(),v.end());\n long int val=0,i=0,x,y,z,temp;\n vector<int>ans;\n while(i<n){\n val=v[i].first;\n temp=val;\n priority_queue<ppi,vector<ppi>,greater<ppi>>q;\n while(i<n && v[i].first<=val){\n q.push({v[i].second.first,{v[i].second.second,v[i].first}});\n i++;\n }\n while(!q.empty()){\n x=q.top().first;\n y=q.top().second.first;\n z=q.top().second.second;\n q.pop();\n ans.push_back(y);\n val+=x;\n while(i<n && v[i].first<=val){\n q.push({v[i].second.first,{v[i].second.second,v[i].first}});\n i++;\n }\n }\n }\n return ans;\n }\n};", "memory": "132671" }
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>
1
{ "code": "class Solution {\npublic:\n // from λ‚¨μ˜ μ†”λ£¨μ…˜ γ…  \n vector<int> getOrder(vector<vector<int>>& tasks) {\n int n = tasks.size();\n for (int i = 0; i < n; ++i)\n tasks[i].push_back(i); //indexing all the tasks so they dont loose their identity\n sort(tasks.begin(),tasks.end());\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq; //min heap\n vector<int> res;\n \n int i = 0;\n long long cur_time = tasks[0][0]; // current time\n \n while (!pq.empty() || i < n) {\n while (i < n && cur_time >= tasks[i][0]) {\n //keep pushing tasks into queue while the enqueueing time of next task is lower than the current time!!\n pq.push({tasks[i][1],tasks[i][2]});\n i++;\n }\n \n if (pq.empty()) {\n //if queue is empty and still the outer loop is running...\n // then this means we still have tasks and also the queue is empty\n // ---> CPU is idle from 't'(current time) t0 tasks[i][0](next enqueueing time)!!\n cur_time = tasks[i][0];\n } else {\n //queue is not empty and also the next enqueueing time is greater than curr time!!\n // that means we do the task having shortest burst time\n // until our current time is equal of greater than the enqueueing time of next task\n pair<int,int> p = pq.top();\n pq.pop();\n res.push_back(p.second);\n cur_time += p.first; \n } \n }\n return res;\n }\n};", "memory": "133613" }
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>
1
{ "code": "class Solution {\npublic:\n // from λ‚¨μ˜ μ†”λ£¨μ…˜ γ…  \n vector<int> getOrder(vector<vector<int>>& tasks) {\n int n = tasks.size();\n for (int i = 0; i < n; ++i)\n tasks[i].push_back(i); //indexing all the tasks so they dont loose their identity\n sort(tasks.begin(),tasks.end());\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> pq; //min heap\n vector<int> res;\n \n int cur_item = 0;\n long long cur_time = tasks[0][0]; // current time\n \n while (!pq.empty() || cur_item < n) {\n while (cur_item < n && cur_time >= tasks[cur_item][0]) {\n //keep pushing tasks into queue while the enqueueing time of next task is lower than the current time!!\n pq.push({tasks[cur_item][1] /*duration*/,tasks[cur_item][2]/*idx*/});\n cur_item++;\n }\n \n if (pq.empty()) {\n // 더 할일이없닀\n // λ‹€μŒμœΌλ‘œ λΉ λ₯Έ μ‹œκ°„μœΌλ‘œ μ—…λŽƒ\n //if queue is empty and still the outer loop is running...\n // then this means we still have tasks and also the queue is empty\n // ---> CPU is idle from 't'(current time) t0 tasks[i][0](next enqueueing time)!!\n cur_time = tasks[cur_item][0];\n } else {\n // queue is not empty and also the next enqueueing time is greater than curr time!!\n // that means we do the task having shortest burst time\n // until our current time is equal of greater than the enqueueing time of next task\n pair<int,int> p = pq.top();\n pq.pop();\n res.push_back(p.second);\n cur_time += p.first; \n } \n }\n return res;\n }\n};", "memory": "133613" }
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 \n for(int i=0;i<tasks.size();i++){\n \n tasks[i].push_back(i);\n \n }\n sort(tasks.begin(),tasks.end());\n// \n vector<int>flag(tasks.size());\n long long start=1,i=0;\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n while(i<tasks.size()){\n \n for(;i<tasks.size()&&tasks[i][0]<=start;i++){\n pq.push({tasks[i][1],tasks[i][2]});\n }\n if(pq.empty()){\n ans.push_back(tasks[i][2]);\n start=tasks[i][1]+tasks[i][0];\n i++;\n }\n else{\n ans.push_back(pq.top().second);\n start+=pq.top().first;\n pq.pop();\n\n }\n \n }\n while(pq.size()){\n ans.push_back(pq.top().second);\n start+=pq.top().first;\n pq.pop();\n }\n return ans;\n }\n};", "memory": "134554" }
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 // monotonic queue queued<enqueueTime, processingTime, index>\n // min pq free <processing time, index>\n\n vector<array<int, 3>> 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": "134554" }
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 for(int i = 0; i < tasks.size(); i++) {\n tasks[i].push_back(i);\n }\n \n sort(tasks.begin(), tasks.end());\n vector<int> ans;\n long long t = tasks[0][0];\n int i = 0;\n int n=tasks.size();\n priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>> minH;\n while(i<tasks.size() && tasks[i][0]==t){\n minH.push({tasks[i][1],{tasks[i][2],tasks[i][0]}});\n i++;\n }\n while(!minH.empty()){\n auto it=minH.top();\n minH.pop();\n ans.push_back(it.second.first);\n t+=it.first;\n while(i<n && tasks[i][0]<=t){\n minH.push({tasks[i][1],{tasks[i][2],tasks[i][0]}});\n i++;\n }\n if(i<n && minH.empty()){\n t=tasks[i][0];\n while(i<n && tasks[i][0]==t){\n minH.push({tasks[i][1],{tasks[i][2],tasks[i][0]}});\n i++;\n }\n }\n }\n return ans;\n }\n};\n", "memory": "135495" }
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 for(int i = 0; i < tasks.size(); i++) {\n tasks[i].push_back(i);\n }\n \n sort(tasks.begin(), tasks.end());\n vector<int> ans;\n long long t = tasks[0][0];\n int i = 0;\n int n=tasks.size();\n priority_queue<pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>,greater<pair<int,pair<int,int>>>> minH;\n while(i<tasks.size() && tasks[i][0]==t){\n minH.push({tasks[i][1],{tasks[i][2],tasks[i][0]}});\n i++;\n }\n while(!minH.empty()){\n auto it=minH.top();\n minH.pop();\n ans.push_back(it.second.first);\n t+=it.first;\n while(i<n && tasks[i][0]<=t){\n minH.push({tasks[i][1],{tasks[i][2],tasks[i][0]}});\n i++;\n }\n if(i<n && minH.empty()){\n t=tasks[i][0];\n while(i<n && tasks[i][0]==t){\n minH.push({tasks[i][1],{tasks[i][2],tasks[i][0]}});\n i++;\n }\n }\n }\n return ans;\n }\n};\n", "memory": "135495" }
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 priority_queue<pair<int,int> ,vector<pair<int,int>> , greater<pair<int,int>> > pq;\n int n = tasks.size();\n for(int i = 0; i < n; i++) {\n tasks[i].push_back(i);\n }\n stable_sort(tasks.begin() , tasks.end());\n vector<int> ans;\n \n int time = tasks[0][0];\n int i = 0;\n while(i < n and time == tasks[i][0]) {\n pq.push({tasks[i][1] , tasks[i][2]});\n i++;\n }\n \n while(i < n) {\n while(i < n and time >= tasks[i][0]) {\n pq.push({tasks[i][1] , tasks[i][2]});\n i++;\n }\n \n if(!pq.empty()) {\n ans.push_back(pq.top().second);\n time += pq.top().first;\n pq.pop();\n } else {\n time = tasks[i][0];\n }\n }\n \n while(!pq.empty()) {\n ans.push_back(pq.top().second);\n pq.pop();\n }\n return ans;\n }\n};", "memory": "136436" }
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 Task {\n public:\n int index;\n int processing_time;\n int start_time;\n};\n\nclass Comp {\n public:\n bool operator()(Task* t1, Task* t2){\n if (t1->processing_time == t2->processing_time) return t1->index > t2->index;\n return t1->processing_time > t2->processing_time;\n }\n};\n\nclass Comp2 {\n public:\n bool operator()(Task* t1, Task* t2){\n return t1->start_time > t2->start_time;\n }\n};\n\nclass Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n priority_queue<Task*,vector<Task*>, Comp> p_queue;\n priority_queue<Task*,vector<Task*>, Comp2> start_queue;\n for(int i=0; i<tasks.size(); i++){\n Task* cur_task = new Task();\n cur_task->index = i;\n cur_task->start_time = tasks[i][0];\n cur_task->processing_time = tasks[i][1];\n start_queue.push(cur_task);\n }\n\n int i=0;\n vector<int> result;\n if (tasks.empty()) return result;\n int cur_time = start_queue.top()->start_time;\n while(!start_queue.empty()){\n Task* cur_task = start_queue.top();\n if (cur_task->start_time <= cur_time){\n p_queue.push(cur_task);\n start_queue.pop();\n continue;\n }\n if (p_queue.empty()){\n cur_time = start_queue.top()->start_time;\n continue;\n }\n else {\n Task* cur_task = p_queue.top();\n p_queue.pop();\n cur_time += cur_task->processing_time;\n result.push_back(cur_task->index);\n } \n }\n while(!p_queue.empty()){\n Task* cur_task = p_queue.top();\n p_queue.pop();\n result.push_back(cur_task->index);\n }\n return result;\n }\n};", "memory": "137378" }
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 for(int i=0; i<tasks.size(); i++){\n tasks[i].push_back(i);\n }\n sort(tasks.begin(),tasks.end(),[](const vector<int>& a, const vector<int>& b){\n return a[0] < b[0];\n });\n long long time = tasks[0][0];\n int i = 0;\n vector<int> result;\n priority_queue<pair<long long,int>, vector<pair<long long,int>>, greater<pair<long long,int>>> minHeap;\n while(!minHeap.empty() or i < tasks.size()){\n while(i < tasks.size() && tasks[i][0] <= time){\n minHeap.push({tasks[i][1], tasks[i][2]});\n i++;\n }\n if(minHeap.empty()){\n time = tasks[i][0];\n }\n else{\n auto[procTime, index] = minHeap.top();\n minHeap.pop();\n time += procTime;\n result.push_back(index); \n }\n }\n return result;\n }\n};\n", "memory": "137378" }
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>>& t) {\n int n=t.size();\n vector<int>ans;\n priority_queue<pair<long long,int>>pq;\n for(int i=0;i<n;i++)t[i].push_back(i);\n sort(t.begin(),t.end());\n long long current=0;\n long long index=0;\n while(!pq.empty() or index<n)\n {\n if(pq.empty() and current<t[index][0])current=t[index][0];\n while(index<n and current>=t[index][0])\n {\n pq.push({-1*t[index][1],-1*t[index][2]});\n index++;\n }\n auto it=pq.top();\n pq.pop();\n current+=(abs(it.first));\n ans.push_back(abs(it.second));\n }\n return ans;\n }\n};", "memory": "138319" }
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 for(int i=0; i<tasks.size(); i++){\n tasks[i].push_back(i);\n }\n sort(tasks.begin(),tasks.end(),[](const vector<int>& a, const vector<int>& b){\n return a[0] < b[0];\n });\n long long time = tasks[0][0];\n int i = 0;\n vector<int> result;\n priority_queue<pair<long long,int>, vector<pair<long long,int>>, greater<pair<long long,int>>> minHeap;\n while(!minHeap.empty() or i < tasks.size()){\n while(i < tasks.size() && tasks[i][0] <= time){\n minHeap.push({tasks[i][1], tasks[i][2]});\n i++;\n }\n if(minHeap.empty()){\n time = tasks[i][0];\n }\n else{\n auto[procTime, index] = minHeap.top();\n minHeap.pop();\n time += procTime;\n result.push_back(index); \n }\n }\n return result;\n }\n};\n", "memory": "138319" }
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 // sort(tasks.begin(), tasks.end());\n vector<int> ans, ind(tasks.size());\n iota(ind.begin(), ind.end(), 0);\n sort(ind.begin(), ind.end(), [&](int i1, int i2)->bool {\n return tasks[i1][0] < tasks[i2][0];\n });\n long long cur_time = 0, pos = 0;\n set<pair<int, int>> cur_tasks;\n while (1) {\n // cout << pos << ' ' << cur_time << '\\n';\n while (pos < tasks.size() && tasks[ind[pos]][0] <= cur_time) {\n cur_tasks.insert({tasks[ind[pos]][1], ind[pos]});\n ++pos;\n }\n if (cur_tasks.size()) {\n ans.push_back(cur_tasks.begin()->second);\n // cout << cur_tasks.size() << ' ' << cur_tasks.begin()->first << ' ' << cur_tasks.begin()->second << ' ' << ans.back() << '\\n';\n cur_time += cur_tasks.begin()->first;\n cur_tasks.erase(cur_tasks.begin());\n } else if (pos < tasks.size()) {\n cur_time = tasks[ind[pos]][0];\n } else {\n break;\n }\n }\n return ans;\n }\n};", "memory": "139260" }
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 int n=tasks.size();\n vector<tuple<long long,long long,int>> sortedTasks;\n for(int i=0; i<n; i++) {\n sortedTasks.push_back(make_tuple(tasks[i][0],tasks[i][1],i));\n }\n sort(sortedTasks.begin(),sortedTasks.end());\n\n vector<int> res;\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<>> minHeap;\n int i=0;\n long long curTime=0;\n while(i<n || !minHeap.empty()) {\n if(minHeap.empty()) {\n curTime=max(curTime,get<0>(sortedTasks[i]));\n }\n while(i<n && get<0>(sortedTasks[i])<=curTime) {\n minHeap.emplace(get<1>(sortedTasks[i]),get<2>(sortedTasks[i]));\n i++;\n }\n auto [burstTime,pid]=minHeap.top();\n minHeap.pop();\n curTime+=burstTime;\n res.push_back(pid);\n }\n return res;\n }\n};", "memory": "140201" }
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 typedef pair<int,int> P;\n\n vector<int> getOrder(vector<vector<int>>& tasks) {\n\n int n=tasks.size();\n \n vector<vector<int>> sortedTasks(n);\n\n for(int i=0;i<n;i++){\n sortedTasks[i]={tasks[i][0],tasks[i][1],i};\n }\n\n sort(sortedTasks.begin(),sortedTasks.end());\n\n long long curr_time=0; \n int idx=0;\n\n priority_queue<P,vector<P>,greater<P>> pq;\n\n vector<int> result;\n\n while(idx<n || !pq.empty()){\n\n if(pq.empty() && curr_time<sortedTasks[idx][0]){\n curr_time=sortedTasks[idx][0];\n }\n\n while(idx<n && sortedTasks[idx][0]<=curr_time){\n pq.push({sortedTasks[idx][1],sortedTasks[idx][2]});\n idx++;\n }\n\n pair<int,int> temp=pq.top();\n pq.pop();\n\n result.push_back(temp.second);\n\n curr_time+=temp.first;\n }\n\n return result;\n }\n};", "memory": "141143" }
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": "\n#define pp pair<int,int> \n\n#define pp pair<int,int>\nclass Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n int n = tasks.size();\n vector<vector<int>>v(n,vector<int>(3));\n for(int i = 0;i<n;i++){\n v[i][0]=tasks[i][0];\n v[i][1]=tasks[i][1];\n v[i][2]=i;\n }\n sort(v.begin(),v.end());\n vector<int>ans;\n priority_queue<pp,vector<pp>,greater<>>pq;\n long long int crr_time = 0;\n int i = 0;\n while(i<n || !pq.empty()){\n\n if(pq.empty() && crr_time<v[i][0]){\n crr_time = v[i][0];\n }\n while(i<n && v[i][0]<=crr_time){\n pq.push({v[i][1],v[i][2]});\n i++;\n }\n pp crr_task= pq.top();\n pq.pop();\n crr_time += crr_task.first;\n ans.push_back(crr_task.second);\n }\n return ans;\n }\n};", "memory": "141143" }
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>>& tasks2) {\n int n=tasks2.size();\n vector<vector<int>>tasks(n);\n for(int i=0;i<n;i++){\n tasks[i]={tasks2[i][0],tasks2[i][1],i};\n }\n sort(tasks.begin(),tasks.end());\n int i=0;\n vector<int>v;\n long long t=0;\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>p;\n while(i<n||!p.empty()){\n while(i<n&&tasks[i][0]<=t){\n p.push({tasks[i][1],tasks[i][2]});\n i++;\n }\n if(p.empty()){\n t=tasks[i][0];\n while(i<n&&tasks[i][0]==t){\n cout<<i<<\" \";\n p.push({tasks[i][1],tasks[i][2]});\n i++;\n }\n }\n cout<<endl;\n int a=p.top().first;\n int b=p.top().second;\n cout<<a<<\" \"<<b<<endl;\n p.pop();\n v.push_back(b);\n t+=(long long)a;\n }\n return v;\n }\n};", "memory": "142084" }
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 ll long long\nclass 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 sort(tasks.begin(),tasks.end());\n ll idx = 0 ;\n ll n = tasks.size();\n priority_queue<pair<ll,pair<ll,ll>>,vector<pair<ll,pair<ll,ll>>>,greater<pair<ll,pair<ll,ll>>> > pq;\n ll i=0;\n while(i<n and tasks[i][0] == tasks[0][0]){\n pq.push({tasks[i][1],{tasks[i][2],tasks[i][0]}});\n idx++; i++;\n }\n vector<int>order;\n ll d = tasks[0][0];\n while(!pq.empty()){\n auto it = pq.top(); pq.pop();\n ll start = it.second.first; ll time = it.first; ll index = it.second.first;\n order.push_back(index);\n // cout<<it.second<<\" \"<<it.first<<endl;\n d += time;\n while(idx<n and tasks[idx][0] <= d){\n pq.push({tasks[idx][1], {tasks[idx][2],tasks[idx][0]} });\n idx++;\n }\n // cout<<idx<<endl;\n if(pq.empty() and idx != n ){\n i = idx;\n while(i<n and tasks[i][0] == tasks[idx][0]){\n pq.push({tasks[i][1],{tasks[i][2],tasks[i][0]}});\n d = tasks[i][0];i++;\n }\n idx = i;\n }\n }\n return order; \n }\n};", "memory": "142084" }
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 typedef pair<long long, pair<long long, long long>> p;\n\n struct cmp {\n bool operator()(p a, p b) {\n if (a.first == b.first) {\n return a.second.first > b.second.first;\n } else {\n return a.first > b.first;\n }\n }\n };\n\n vector<int> getOrder(vector<vector<int>>& tasks) {\n // Append the original index to each task\n for (int i = 0; i < tasks.size(); i++) {\n tasks[i].push_back(i);\n }\n\n // Sort tasks based on their enqueue time\n sort(tasks.begin(), tasks.end());\n\n long long startTime = 0;\n long long j = 0;\n priority_queue<p, vector<p>, cmp> q; // Min-heap based on processing time\n\n vector<int> ans;\n\n while (j < tasks.size() || !q.empty()) {\n // Push all tasks that can be started at the current time\n while (j < tasks.size() && tasks[j][0] <= startTime) {\n q.push({tasks[j][1], {tasks[j][2], tasks[j][0]}});\n j++;\n }\n\n if (!q.empty()) {\n // Get the task with the shortest processing time\n long long processingTime = q.top().first;\n long long idx = q.top().second.first;\n long long enqueueTime = q.top().second.second;\n q.pop();\n ans.push_back(idx);\n startTime += processingTime;\n } else if (j < tasks.size()) {\n // If the heap is empty but there are still tasks left, move to the next available task\n startTime = tasks[j][0];\n }\n }\n\n return ans;\n }\n};\n", "memory": "143025" }
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 priority_queue< pair<int, int>, vector<pair<int, int>>, greater<> > pq; //min_heap\n vector<int> answer;\n\n vector<array<int,3>> taskOrdered;\n int n = tasks.size();\n\n for(int i = 0; i < n;i++){\n\n auto it = tasks[i];\n\n taskOrdered.push_back({it[0],it[1],i});\n }\n\n sort(taskOrdered.begin(), taskOrdered.end());\n\n\n int idx = 0;\n long long currTime = 0;\n\n\n while(idx < n || !pq.empty()){\n if(pq.empty() && currTime < taskOrdered[idx][0]){\n currTime = taskOrdered[idx][0];\n }\n\n while(idx < n && taskOrdered[idx][0] <= currTime){\n pq.push({taskOrdered[idx][1], taskOrdered[idx][2]});\n idx++;\n }\n\n auto lowestTimeTask = pq.top();\n answer.push_back(lowestTimeTask.second);\n pq.pop();\n\n currTime += lowestTimeTask.first ;\n }\n\n\n return answer;\n\n }\n};", "memory": "143025" }
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 vector<array<int,3>>sorted_tasks;\n int i=0;\n int n=tasks.size();\n for(auto taks:tasks)\n {\n int starttime=taks[0];\n int endtime=taks[1];\n int index=i;\n i++;\n sorted_tasks.push_back({starttime,endtime,index});\n }\n sort(begin(sorted_tasks),end(sorted_tasks));\n vector<int>result;\n long long curr=0;\n int index=0;\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n while(index<n || !pq.empty())\n {\n if (pq.empty() && curr < sorted_tasks[index][0]) {\n curr = sorted_tasks[index][0];\n }\n while(index<n && sorted_tasks[index][0]<=curr)\n {\n pq.push({sorted_tasks[index][1],sorted_tasks[index][2]});\n index++;\n }\n pair<int,int>curr_time=pq.top();\n pq.pop();\n curr+=curr_time.first;\n result.push_back(curr_time.second);\n\n\n \n\n }\n return result;\n\n\n \n \n \n }\n};", "memory": "143966" }
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 vector<array<int,3>>sorted_tasks;\n int i=0;\n int n=tasks.size();\n for(auto taks:tasks)\n {\n int starttime=taks[0];\n int endtime=taks[1];\n int index=i;\n i++;\n sorted_tasks.push_back({starttime,endtime,index});\n }\n sort(begin(sorted_tasks),end(sorted_tasks));\n vector<int>result;\n long long curr=0;\n int index=0;\n priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;\n while(index<n || !pq.empty())\n {\n if (pq.empty() && curr < sorted_tasks[index][0]) {\n curr = sorted_tasks[index][0];\n }\n while(index<n && sorted_tasks[index][0]<=curr)\n {\n pq.push({sorted_tasks[index][1],sorted_tasks[index][2]});\n index++;\n }\n pair<int,int>curr_time=pq.top();\n pq.pop();\n curr+=curr_time.first;\n result.push_back(curr_time.second);\n\n\n \n\n }\n return result;\n\n\n \n \n \n }\n};", "memory": "143966" }
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 struct Task {\n unsigned long long enque_time;\n unsigned long long processing_time;\n int idx;\n };\n\n class TaskHeap {\n private:\n vector<Task>& heap;\n bool (*is_smaller) (Task&a, Task& b);\n\n void sift_down(int root) {\n while (true) {\n int next = root;\n int left_child = 2*root + 1;\n int right_child = 2*root + 2;\n\n if (left_child < heap.size() && is_smaller(heap[left_child], heap[next])) {\n next = left_child;\n }\n\n if (right_child < heap.size() && is_smaller(heap[right_child], heap[next])) {\n next = right_child;\n }\n\n if (root == next)\n break;\n\n swap(heap[next], heap[root]);\n root = next;\n }\n }\n\n void sift_up(int root) {\n while (root > 0) {\n int parent = (root - 1) / 2;\n if (is_smaller(heap[root], heap[parent])) {\n swap(heap[root], heap[parent]);\n root = parent;\n }\n else {\n break;\n }\n }\n }\n\n public:\n TaskHeap(vector<Task>& buffer, bool (*comparison)(Task& a, Task&b)) : heap(buffer) {\n is_smaller = comparison;\n for (int r = (heap.size() / 2) - 1; r >= 0; --r) {\n sift_down(r);\n }\n }\n\n Task get_next() {\n return heap[0];\n }\n\n void pop() {\n swap(heap[0], heap[heap.size() - 1]);\n heap.pop_back();\n sift_down(0);\n }\n\n void push(Task t) {\n heap.push_back(t);\n sift_up(heap.size() - 1);\n }\n\n bool is_empty() {\n return heap.empty();\n }\n };\n\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<int> result;\n\n auto sort_by_time = [] (Task& a, Task& b) { return (a.processing_time < b.processing_time || a.processing_time == b.processing_time && a.idx < b.idx); };\n auto sort_by_enque_time = [] (Task& a, Task& b) { return (a.enque_time < b.enque_time); };\n\n vector<Task> buffer_1;\n for (int i = 0; i < tasks.size(); ++i)\n buffer_1.push_back({ (unsigned long long)tasks[i][0], (unsigned long long)tasks[i][1], i });\n\n TaskHeap enque_heap(buffer_1, sort_by_enque_time);\n unsigned long long time = 0;\n\n vector<Task> buffer_2;\n TaskHeap time_heap(buffer_2, sort_by_time);\n\n while (!enque_heap.is_empty() || !time_heap.is_empty()) {\n if (time_heap.is_empty()) {\n int prev_time = -1;\n while(!enque_heap.is_empty()) {\n Task t = enque_heap.get_next();\n if (prev_time == -1) {\n prev_time = t.enque_time;\n time_heap.push(t);\n enque_heap.pop();\n }\n else if (t.enque_time == prev_time || t.enque_time <= time) {\n time_heap.push(t);\n enque_heap.pop();\n }\n else {\n break;\n }\n }\n\n if (time < prev_time)\n time = prev_time;\n }\n else {\n while(!enque_heap.is_empty()) {\n Task t = enque_heap.get_next();\n if (t.enque_time <= time) {\n time_heap.push(t);\n enque_heap.pop();\n }\n else {\n break;\n }\n }\n }\n\n Task process = time_heap.get_next();\n time += process.processing_time;\n time_heap.pop();\n result.push_back(process.idx);\n }\n\n return result;\n }\n};\n", "memory": "150555" }
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 struct Task {\n unsigned long long enque_time;\n unsigned long long processing_time;\n int idx;\n };\n\n class TaskHeap {\n private:\n vector<Task>& heap;\n bool (*is_smaller) (Task&a, Task& b);\n\n void sift_down(int root) {\n while (true) {\n int next = root;\n int left_child = 2*root + 1;\n int right_child = 2*root + 2;\n\n if (left_child < heap.size() && is_smaller(heap[left_child], heap[next])) {\n next = left_child;\n }\n\n if (right_child < heap.size() && is_smaller(heap[right_child], heap[next])) {\n next = right_child;\n }\n\n if (root == next)\n break;\n\n swap(heap[next], heap[root]);\n root = next;\n }\n }\n\n void sift_up(int root) {\n while (root > 0) {\n int parent = (root - 1) / 2;\n if (is_smaller(heap[root], heap[parent])) {\n swap(heap[root], heap[parent]);\n root = parent;\n }\n else {\n break;\n }\n }\n }\n\n public:\n TaskHeap(vector<Task>& buffer, bool (*comparison)(Task& a, Task&b)) : heap(buffer) {\n is_smaller = comparison;\n for (int r = (heap.size() / 2) - 1; r >= 0; --r) {\n sift_down(r);\n }\n }\n\n Task& get_next() {\n return heap[0];\n }\n\n void pop() {\n swap(heap[0], heap[heap.size() - 1]);\n heap.pop_back();\n sift_down(0);\n }\n\n void push(Task& t) {\n heap.emplace_back(t.enque_time, t.processing_time, t.idx);\n sift_up(heap.size() - 1);\n }\n\n bool is_empty() {\n return heap.empty();\n }\n };\n\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<int> result;\n\n auto sort_by_time = [] (Task& a, Task& b) { return (a.processing_time < b.processing_time || a.processing_time == b.processing_time && a.idx < b.idx); };\n auto sort_by_enque_time = [] (Task& a, Task& b) { return (a.enque_time < b.enque_time); };\n\n vector<Task> buffer_1;\n for (int i = 0; i < tasks.size(); ++i)\n buffer_1.push_back({ (unsigned long long)tasks[i][0], (unsigned long long)tasks[i][1], i });\n\n TaskHeap enque_heap(buffer_1, sort_by_enque_time);\n unsigned long long time = 0;\n\n vector<Task> buffer_2;\n TaskHeap time_heap(buffer_2, sort_by_time);\n\n while (result.size() < tasks.size()) {\n if (time_heap.is_empty()) {\n int earliest_time = -1;\n\n while(!enque_heap.is_empty()) {\n Task& t = enque_heap.get_next();\n if (earliest_time == -1) {\n earliest_time = t.enque_time;\n time_heap.push(t);\n enque_heap.pop();\n }\n else if (t.enque_time == earliest_time || t.enque_time <= time) {\n time_heap.push(t);\n enque_heap.pop();\n }\n else {\n break;\n }\n }\n\n if (time < earliest_time)\n time = earliest_time;\n }\n else {\n while(!enque_heap.is_empty()) {\n Task& t = enque_heap.get_next();\n if (t.enque_time <= time) {\n time_heap.push(t);\n enque_heap.pop();\n }\n else {\n break;\n }\n }\n }\n\n Task process = time_heap.get_next();\n time_heap.pop();\n\n time += process.processing_time;\n result.push_back(process.idx);\n }\n\n return result;\n }\n};\n", "memory": "150555" }
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 struct Task {\n unsigned long long enque_time;\n unsigned long long processing_time;\n int idx;\n\n friend bool operator < (Task& a, Task& b) {\n return a.enque_time < b.enque_time;\n }\n };\n\n class TaskHeap {\n private:\n vector<Task>& heap;\n\n void sift_down(int root) {\n while (true) {\n int next = root;\n int left_child = 2*root + 1;\n int right_child = 2*root + 2;\n\n if (left_child < heap.size() && (heap[left_child].processing_time < heap[next].processing_time || heap[left_child].processing_time == heap[next].processing_time && heap[left_child].idx < heap[next].idx)) {\n next = left_child;\n }\n\n if (right_child < heap.size() && (heap[right_child].processing_time < heap[next].processing_time || heap[right_child].processing_time == heap[next].processing_time && heap[right_child].idx < heap[next].idx)) {\n next = right_child;\n }\n\n if (root == next)\n break;\n\n swap(heap[next], heap[root]);\n root = next;\n }\n }\n\n void sift_up(int root) {\n while (root > 0) {\n int parent = (root - 1) / 2;\n if (heap[root].processing_time < heap[parent].processing_time || heap[root].processing_time == heap[parent].processing_time && heap[root].idx < heap[parent].idx) {\n swap(heap[root], heap[parent]);\n root = parent;\n }\n else {\n break;\n }\n }\n }\n\n public:\n TaskHeap(vector<Task>& buffer) : heap(buffer) { }\n\n Task& get_next() {\n return heap[0];\n }\n\n void pop() {\n swap(heap[0], heap[heap.size() - 1]);\n heap.pop_back();\n sift_down(0);\n }\n\n void push(Task& t) {\n heap.emplace_back(t.enque_time, t.processing_time, t.idx);\n sift_up(heap.size() - 1);\n }\n\n bool is_empty() {\n return heap.empty();\n }\n };\n\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<int> result;\n\n vector<Task> buffer_1;\n for (int i = 0; i < tasks.size(); ++i)\n buffer_1.push_back({ (unsigned long long)tasks[i][0], (unsigned long long)tasks[i][1], i });\n sort(buffer_1.begin(), buffer_1.end());\n\n unsigned long long time = buffer_1[0].enque_time;\n long long idx = 0;\n\n vector<Task> buffer_2;\n TaskHeap time_heap(buffer_2);\n\n while (result.size() < tasks.size()) {\n if (time_heap.is_empty()) {\n int earliest_time = -1;\n bool was_empty = time_heap.is_empty();\n\n while(idx < buffer_1.size()) {\n Task& t = buffer_1[idx];\n if (earliest_time == -1)\n earliest_time = t.enque_time;\n\n if (was_empty && t.enque_time == earliest_time || t.enque_time <= time) {\n time_heap.push(t);\n ++idx;\n }\n else {\n break;\n }\n }\n\n if (time < earliest_time)\n time = earliest_time;\n }\n else {\n while(idx < buffer_1.size()) {\n Task& t = buffer_1[idx];\n if (t.enque_time <= time) {\n time_heap.push(t);\n ++idx;\n }\n else {\n break;\n }\n }\n }\n\n Task process = time_heap.get_next();\n time_heap.pop();\n\n time += process.processing_time;\n result.push_back(process.idx);\n }\n\n return result;\n }\n};\n", "memory": "151496" }
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 struct Task {\n unsigned long long enque_time;\n unsigned long long processing_time;\n int idx;\n\n friend bool operator < (Task& a, Task& b) {\n return a.enque_time < b.enque_time;\n }\n };\n\n class TaskHeap {\n private:\n vector<Task>& heap;\n\n void sift_down(int root) {\n while (true) {\n int next = root;\n int left_child = 2*root + 1;\n int right_child = 2*root + 2;\n\n if (left_child < heap.size() && (heap[left_child].processing_time < heap[next].processing_time || heap[left_child].processing_time == heap[next].processing_time && heap[left_child].idx < heap[next].idx)) {\n next = left_child;\n }\n\n if (right_child < heap.size() && (heap[right_child].processing_time < heap[next].processing_time || heap[right_child].processing_time == heap[next].processing_time && heap[right_child].idx < heap[next].idx)) {\n next = right_child;\n }\n\n if (root == next)\n break;\n\n swap(heap[next], heap[root]);\n root = next;\n }\n }\n\n void sift_up(int root) {\n while (root > 0) {\n int parent = (root - 1) / 2;\n if (heap[root].processing_time < heap[parent].processing_time || heap[root].processing_time == heap[parent].processing_time && heap[root].idx < heap[parent].idx) {\n swap(heap[root], heap[parent]);\n root = parent;\n }\n else {\n break;\n }\n }\n }\n\n public:\n TaskHeap(vector<Task>& buffer) : heap(buffer) { }\n\n Task& get_next() {\n return heap[0];\n }\n\n void pop() {\n swap(heap[0], heap[heap.size() - 1]);\n heap.pop_back();\n sift_down(0);\n }\n\n void push(Task& t) {\n heap.emplace_back(t.enque_time, t.processing_time, t.idx);\n sift_up(heap.size() - 1);\n }\n\n bool is_empty() {\n return heap.empty();\n }\n };\n\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n vector<int> result;\n\n vector<Task> buffer_1;\n for (int i = 0; i < tasks.size(); ++i)\n buffer_1.push_back({ (unsigned long long)tasks[i][0], (unsigned long long)tasks[i][1], i });\n sort(buffer_1.begin(), buffer_1.end());\n\n unsigned long long time = buffer_1[0].enque_time;\n long long idx = 0;\n\n vector<Task> buffer_2;\n TaskHeap time_heap(buffer_2);\n\n while (result.size() < tasks.size()) {\n if (idx < buffer_1.size()) {\n int earliest_time = buffer_1[idx].enque_time;\n if (time_heap.is_empty() && time < earliest_time)\n time = earliest_time;\n }\n\n while(idx < buffer_1.size()) {\n Task& t = buffer_1[idx];\n if (t.enque_time <= time) {\n time_heap.push(t);\n ++idx;\n }\n else {\n break;\n }\n }\n\n Task process = time_heap.get_next();\n time_heap.pop();\n\n time += process.processing_time;\n result.push_back(process.idx);\n }\n\n return result;\n }\n};\n", "memory": "151496" }
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": "typedef pair<long long, long long> ii;\ntypedef long long ll;\n\nclass Solution {\npublic:\n vector<int> getOrder(vector<vector<int>>& tasks) {\n int n = tasks.size();\n deque<tuple<ll, ll, ll>> vec;\n for(int i = 0; i < n; i++){\n ll x = tasks[i][0], y = tasks[i][1];\n vec.emplace_back(x, y, i);\n }\n sort(vec.begin(), vec.end());\n\n ll currentTime = get<0>(vec[0]);\n vector<int> ans;\n set<ii> queuedTasks;\n\n while(!vec.empty() || !queuedTasks.empty()) {\n // enqueing all the tasks that I can\n while(!vec.empty() && currentTime >= get<0>(vec[0])){\n queuedTasks.insert(\n ii(\n get<1>(vec[0]),\n get<2>(vec[0])\n )\n );\n vec.pop_front();\n }\n\n if (queuedTasks.empty()) {\n currentTime = get<0>(vec[0]);\n } else {\n ii p = *queuedTasks.begin();\n queuedTasks.erase(p);\n currentTime += p.first;\n ans.push_back(p.second);\n }\n }\n return ans;\n }\n};", "memory": "152438" }