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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n std::vector<int> survivedRobotsHealths(std::vector<int> const& positions, std::vector<int>& healths, std::string const& directions) {\n std::vector<std::size_t> sorted(std::size(positions));\n std::iota(std::begin(sorted), std::end(sorted), 0);\n std::ranges::sort(sorted, [&](auto const i, auto const j) {\n return positions[i] < positions[j];\n });\n std::vector<std::size_t> survived;\n for (std::size_t const i : sorted) {\n survived.emplace_back(i);\n while (std::size(survived) >= 2) {\n auto const n = std::size(survived);\n auto const i = survived[n - 2];\n auto const j = survived[n - 1];\n if (!(directions[i] == 'R' && directions[j] == 'L')) {\n break;\n }\n auto const minHealth = std::min(healths[i], healths[j]);\n if (healths[j] == minHealth || healths[j] == 1) {\n survived.erase(std::begin(survived) + (n - 1));\n } else {\n --healths[j];\n }\n if (healths[i] == minHealth || healths[i] == 1) {\n survived.erase(std::begin(survived) + (n - 2));\n } else {\n --healths[i];\n }\n }\n }\n std::vector<int> survivedHealths;\n std::ranges::sort(survived);\n for (std::size_t const i : survived) {\n survivedHealths.emplace_back(healths[i]);\n }\n return survivedHealths;\n }\n};",
"memory": "201116"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = positions.size();\n\n vector<pair<int, int>> posIdxPair(n);\n for (int i = 0; i < n; ++i) {\n posIdxPair[i] = {positions[i], i};\n }\n sort(posIdxPair.begin(), posIdxPair.end());\n\n vector<pair<int, int>> survivors;\n\n stack<pair<int, int>> st;\n for (int i = 0; i < n; ++i) {\n int robotIdx = posIdxPair[i].second;\n char dir = directions[robotIdx];\n if (dir == 'R') {\n st.push({robotIdx, healths[robotIdx]});\n continue;\n }\n\n bool survived = true;\n while (!st.empty()) {\n auto [colliderIdx, colliderHealth] = st.top();\n st.pop();\n\n if (colliderHealth < healths[robotIdx]) {\n healths[robotIdx]--;\n continue;\n }\n\n survived = false;\n if (colliderHealth == healths[robotIdx]) {\n break;\n } else if (colliderHealth > healths[robotIdx]) {\n st.push({colliderIdx, colliderHealth - 1});\n break;\n }\n }\n\n if (survived) {\n survivors.push_back({robotIdx, healths[robotIdx]});\n }\n }\n\n while (!st.empty()) {\n survivors.push_back(st.top());\n st.pop();\n }\n\n sort(survivors.begin(), survivors.end());\n vector<int> ans;\n for (auto [idx, val] : survivors) {\n ans.push_back(val);\n }\n\n return ans;\n }\n};",
"memory": "202433"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 1 | {
"code": "#include <vector>\n#include <algorithm>\n#include <stack>\n\nusing namespace std;\n\nstruct Robot {\n int pos;\n int health;\n char dir; // direction: 'L' or 'R'\n int index; // original index\n};\n\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = positions.size();\n vector<Robot> robots(n);\n \n // Initialize the robots with positions, healths, directions, and their original index\n for (int i = 0; i < n; ++i) {\n robots[i] = {positions[i], healths[i], directions[i], i};\n }\n \n // Sort robots by their positions\n sort(robots.begin(), robots.end(), [](const Robot &a, const Robot &b) {\n return a.pos < b.pos;\n });\n \n // Stack to simulate collisions\n stack<int> stk;\n \n for (int i = 0; i < n; ++i) {\n if (robots[i].dir == 'R') {\n stk.push(i);\n } else {\n while (!stk.empty() && robots[i].health > 0) {\n int j = stk.top(); // Index of the last robot moving to the right\n \n if (robots[j].health == robots[i].health) {\n // Both robots destroy each other\n robots[j].health = 0;\n robots[i].health = 0;\n stk.pop();\n } else if (robots[j].health > robots[i].health) {\n // Left-moving robot gets destroyed\n robots[j].health -= 1;\n robots[i].health = 0;\n } else {\n // Right-moving robot gets destroyed\n robots[i].health -= 1;\n robots[j].health = 0;\n stk.pop();\n }\n }\n }\n }\n \n // Collect the healths of surviving robots and sort them back to the original input order\n vector<int> result(n, 0);\n for (const auto& robot : robots) {\n if (robot.health > 0) {\n result[robot.index] = robot.health;\n }\n }\n \n // Remove the zeros (robots that were destroyed)\n result.erase(remove(result.begin(), result.end(), 0), result.end());\n \n return result;\n }\n};\n",
"memory": "202433"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = positions.size();\n vector<pair<int, int>> robots;\n for (int i = 0; i < n; i++) {\n robots.emplace_back(positions[i], i);\n }\n sort(robots.begin(), robots.end());\n\n stack<int> stk;\n for (int i = 0; i < n; i++) {\n auto [_, idx] = robots[i];\n if (directions[idx] == 'R') {\n stk.push(idx);\n continue;\n }\n\n while (stk.size()) {\n auto x = stk.top();\n // cout << \"Collision between \" << idx << \", \" << x << endl;\n stk.pop();\n if (healths[x] == healths[idx]) {\n // cout << \"A\\n\";\n healths[idx] = 0;\n healths[x] = 0;\n break;\n } else if (healths[x] > healths[idx]) {\n // cout << \"B\\n\";\n healths[x] -= 1;\n healths[idx] = 0;\n stk.push(x);\n break;\n } else {\n // cout << \"C\\n\";\n healths[idx] -= 1;\n healths[x] = 0;\n }\n }\n }\n\n vector<int> ans;\n ans.reserve(n);\n for (int i = 0; i < healths.size(); i++) {\n if (healths[i] > 0)\n ans.push_back(healths[i]);\n }\n return ans;\n }\n};",
"memory": "203751"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& health, string directions) {\n vector<pair<int,int>> indexMap;\n for(int i1=0;i1<positions.size();++i1){\n indexMap.push_back({positions[i1],i1});\n }\n sort(indexMap.begin(),indexMap.end());\n vector<int> indexStack;\n for(auto indexPair:indexMap){\n int currIndex=indexPair.second;\n indexStack.push_back(currIndex);\n while(1){\n if(indexStack.size()==1){\n break;\n }\n int prevIndex=indexStack[indexStack.size()-2];\n if(directions[prevIndex]=='R' && directions[currIndex]=='L'){\n if(health[prevIndex]>health[currIndex]){\n health[prevIndex]--;\n health[currIndex]=0;\n indexStack.pop_back();\n break;\n }\n else if(health[prevIndex]<health[currIndex]){\n health[prevIndex]=0;\n health[currIndex]--;\n indexStack[indexStack.size()-2]=indexStack[indexStack.size()-1];\n indexStack.pop_back();\n }\n else{\n health[prevIndex]=0;\n health[currIndex]=0;\n indexStack.pop_back();\n indexStack.pop_back();\n break;\n }\n }\n else{\n break;\n }\n }\n }\n vector<int> ans;\n ans.reserve(indexStack.size());\n for(int i1=0;i1<positions.size();++i1){\n if(health[i1]){\n ans.push_back(health[i1]);\n }\n }\n return ans;\n }\n};",
"memory": "203751"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<pair<int,int>>v1;\n for(int i=0;i<positions.size();i++)\n {\n v1.push_back({positions[i],i});\n }\n sort(v1.begin(), v1.end(), greater<>());\n\n vector<int> stack;\n for(auto& [pos, i]: v1){\n if (directions[i]=='L') stack.push_back(i);\n else{\n while(!stack.empty() && healths[i]>0){\n int j=stack.back();\n int x=healths[j]-healths[i];\n if (x>0) healths[j]--, healths[i]=0;\n else if (x<0) healths[j]=0, healths[i]--, stack.pop_back();\n else healths[i]=healths[j]=0, stack.pop_back();\n }\n }\n }\n vector<int> ans;\n for(int x: healths)\n if (x>0) ans.push_back(x);\n return ans;\n }\n};",
"memory": "205068"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions,\n vector<int>& healths, string direction) {\n\n vector<pair<int, int>> v;\n vector<int> ans;\n int np = positions.size();\n for (int i = 0; i < positions.size(); i++) {\n v.push_back(make_pair(positions[i], i));\n }\n sort(v.begin(), v.end());\n stack<int> st;\n st.push(v[0].second);\n for (int k = 1; k < v.size(); k++) {\n int i = v[k].second;\n if(!st.empty()){\n int temp = st.top();\n if (direction[i] == 'L' && direction[temp] == 'R') {\n if (healths[i] == healths[temp]) {\n st.pop();\n } else if (healths[i] > healths[temp]) {\n healths[i]--;\n st.pop();\n k--;\n } else if (healths[i] < healths[temp]) {\n healths[temp]--;\n }\n } else {\n st.push(i);\n }\n }\n else st.push(i);\n }\n while (!st.empty()) {\n ans.push_back(st.top());\n st.pop();\n }\n sort(ans.begin(), ans.end());\n for (int i = 0; i < ans.size(); i++) {\n ans[i] = healths[ans[i]];\n }\n return ans;\n }\n};",
"memory": "205068"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n stack<int>s;\n int n =positions.size();\n vector<pair<pair<int,int>,pair<int,char>>> r(n);\n vector<int>ans;\n vector<pair<int,int>>temp;\n\n for(int i =0;i<n;i++){\n r[i].first.first = positions[i];\n r[i].first.second = i;\n r[i].second.first = healths[i];\n r[i].second.second = directions[i];\n }\n sort(r.begin(),r.end());\n for(int i =0;i<n;i++){\n if(r[i].second.second == 'R'){\n s.push(i);\n }\n else{\n while(!s.empty() && r[s.top()].second.first < r[i].second.first){\n s.pop();\n r[i].second.first--;\n }\n if(!s.empty() && r[s.top()].second.first == r[i].second.first) s.pop();\n else if(s.empty()) temp.push_back({r[i].first.second, r[i].second.first});\n else{\n r[s.top()].second.first--;\n }\n }\n }\n while(!s.empty()){\n temp.push_back({r[s.top()].first.second, r[s.top()].second.first});\n s.pop();\n }\n sort(temp.begin(), temp.end());\n\n for(auto p:temp){\n ans.push_back(p.second);\n }\n return ans;\n }\n};",
"memory": "206386"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 1 | {
"code": "struct Robot {\n int i;\n int x;\n int health;\n char dir;\n};\n\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& pos, vector<int>& hs, string dirs) {\n int n = pos.size();\n vector<Robot> rs;\n rs.reserve(n);\n for (int i = 0; i < n; ++i) {\n rs.push_back({i, pos[i], hs[i], dirs[i]});\n }\n ranges::sort(rs, {}, &Robot::x);\n vector<Robot> st;\n st.reserve(n);\n for (auto r : rs) {\n bool dead = false;\n while (r.dir == 'L' && !st.empty() && st.back().dir == 'R') {\n int b_h = st.back().health;\n if (r.health == b_h) {\n st.pop_back();\n dead = true;\n break;\n } else if (r.health < b_h) {\n st.back().health--;\n dead = true;\n break;\n } else {\n r.health--;\n st.pop_back();\n }\n }\n if (!dead) {\n st.push_back(r);\n }\n }\n ranges::sort(st, {}, &Robot::i);\n vector<int> rhs;\n rhs.reserve(st.size());\n for (const auto& r : st) {\n rhs.push_back(r.health);\n }\n return rhs;\n }\n};",
"memory": "206386"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 1 | {
"code": "struct Robot {\n int i;\n int x;\n int health;\n char dir;\n};\n\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& pos, vector<int>& hs, string dirs) {\n int n = pos.size();\n vector<Robot> rs;\n rs.reserve(n);\n for (int i = 0; i < n; ++i) {\n rs.push_back({i, pos[i], hs[i], dirs[i]});\n }\n ranges::sort(rs, {}, &Robot::x);\n vector<Robot> st;\n for (auto r : rs) {\n bool dead = false;\n while (r.dir == 'L' && !st.empty() && st.back().dir == 'R') {\n int b_h = st.back().health;\n if (r.health == b_h) {\n st.pop_back();\n dead = true;\n break;\n } else if (r.health < b_h) {\n st.back().health--;\n dead = true;\n break;\n } else {\n r.health--;\n st.pop_back();\n }\n }\n if (!dead) {\n st.push_back(r);\n }\n }\n ranges::sort(st, {}, &Robot::i);\n vector<int> rhs;\n for (const auto& r : st) {\n rhs.push_back(r.health);\n }\n return rhs;\n }\n};",
"memory": "212973"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 1 | {
"code": "struct Robot {\n int i;\n int x;\n int health;\n char dir;\n};\n\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& pos, vector<int>& hs, string dirs) {\n int n = pos.size();\n vector<Robot> rs;\n rs.reserve(n);\n for (int i = 0; i < n; ++i) {\n rs.push_back({i, pos[i], hs[i], dirs[i]});\n }\n ranges::sort(rs, {}, &Robot::x);\n vector<Robot> st;\n for (auto r : rs) {\n bool dead = false;\n while (r.dir == 'L' && !st.empty() && st.back().dir == 'R') {\n int b_h = st.back().health;\n if (r.health == b_h) {\n st.pop_back();\n dead = true;\n break;\n } else if (r.health < b_h) {\n st.back().health--;\n dead = true;\n break;\n } else {\n r.health--;\n st.pop_back();\n }\n }\n if (!dead) {\n st.push_back(r);\n }\n }\n ranges::sort(st, {}, &Robot::i);\n vector<int> rhs;\n for (const auto& r : st) {\n rhs.push_back(r.health);\n }\n return rhs;\n }\n};",
"memory": "212973"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 1 | {
"code": "\nstruct Robot{\n int health;\n int position;\n char direction; \n int index; \n};\n\nbool comparator(Robot &a,Robot &b){\n return a.position<b.position;\n}\n\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n=positions.size();\n vector<int>ans;\n Robot robot[n];\n\n\n for(int i=0;i<n;i++){\n robot[i].health=healths[i];\n robot[i].position=positions[i];\n robot[i].direction=directions[i];\n robot[i].index=i;\n }\n sort(robot,robot+n,comparator); \n stack<Robot>st;\n\n //for(auto x:robot)cout<<x.position<<\" \"<<x.direction<<endl;\n for(int i=0;i<n;i++){\n if(robot[i].direction == 'R')st.push(robot[i]);\n else{\n while(!st.empty() &&\n st.top().direction == 'R' &&\n st.top().health < robot[i].health){\n st.pop();\n robot[i].health--;\n }\n if(!st.empty() && \n st.top().direction =='R' &&\n robot[i].direction == 'L' &&\n st.top().health==robot[i].health){\n cout<<st.top().direction<<\"2nd if\";\n st.pop();\n }\n else if(!st.empty() &&\n st.top().direction =='R' &&\n robot[i].direction == 'L' && \n st.top().health > robot[i].health){\n st.top().health--;\n }\n else if(st.empty() or st.top().direction == 'L'){\n cout<<robot[i].position<<\" \";\n st.push(robot[i]);\n } \n \n }\n }\n map<int,int>m;\n \n while(!st.empty()){\n //ans.push_back(st.top().health);\n m[st.top().position]=st.top().health;\n st.pop();\n }\n for(int i=0;i<n;i++){\n if(m.find(positions[i])!=m.end()){\n ans.push_back(m[positions[i]]);\n }\n }\n //reverse(ans.begin(),ans.end());\n return ans;\n }\n};\n",
"memory": "214291"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 1 | {
"code": "class Solution {\n public:\n struct robot {\n int originalIndex;\n int position;\n int health;\n char direction;\n } temp;\n\n static bool compareOriginalIndex(const robot& a, const robot& b) {\n return a.originalIndex < b.originalIndex;\n }\n\n static bool comparePosition(const robot& a, const robot& b) {\n return a.position < b.position;\n }\n\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<robot> allBot(positions.size()), survivedBot;\n for (int i = 0; i < positions.size(); ++i) {\n allBot[i] = {i, positions[i], healths[i], directions[i]};\n }\n\n sort(allBot.begin(), allBot.end(), comparePosition);\n\n stack<robot> sk;\n for (int i = 0; i < allBot.size(); ++i) {\n // cout << allBot[i].originalIndex << \" \" << allBot[i].position << \" \" << allBot[i].health << \" \" << allBot[i].direction << endl;\n // if (!sk.empty()) {\n // cout << sk.top().originalIndex << \" \" << sk.top().position << \" \" << sk.top().health << \" \" << sk.top().direction << endl;\n // }\n\n if (allBot[i].direction == 'R') {\n sk.push(allBot[i]);\n } else {\n // left-moving robots will collide with the right-moving robots on the left side\n while (!sk.empty() && sk.top().direction == 'R' && allBot[i].health > 0) {\n if (sk.top().health == allBot[i].health) { // both robots will be removed\n allBot[i].health = 0;\n sk.pop();\n } else if (sk.top().health < allBot[i].health) { // right-moving robot will be removed\n allBot[i].health -= 1;\n sk.pop();\n } else { // left-moving robot will be removed\n allBot[i].health = 0;\n sk.top().health -= 1;\n }\n }\n\n if ((sk.empty() || sk.top().direction == 'L') && allBot[i].health > 0) { // survived left-moving robot\n sk.push(allBot[i]);\n }\n }\n }\n\n while (!sk.empty()) {\n survivedBot.push_back(sk.top());\n sk.pop();\n }\n\n sort(survivedBot.begin(), survivedBot.end(), compareOriginalIndex);\n\n vector<int> survivedHealth(survivedBot.size());\n for (int i = 0; i < survivedBot.size(); ++i) {\n survivedHealth[i] = survivedBot[i].health;\n }\n return survivedHealth;\n }\n};",
"memory": "214291"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 1 | {
"code": "struct Robot\n{\n int posit;\n int health;\n char dir;\n int index;\n bool operator<(Robot const & b)\n {\n return this->posit<b.posit;\n }\n};\nbool judge(Robot a,Robot b)\n{\n return a.index<b.index;\n}\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n stack<int>left;\n vector<Robot>sa;\n int n=positions.size();\n for(int i=0;i<positions.size();i++)\n {\n sa.push_back({positions[i],healths[i],directions[i],i});\n }\n sort(sa.begin(),sa.end());\n for(int i=0;i<n;i++)\n {\n if(sa[i].dir=='R')\n left.push(i);\n else \n {\n while(!left.empty())\n {\n int top_left=left.top();\n left.pop();\n if(sa[top_left].health>sa[i].health)\n {\n sa[top_left].health-=1;\n left.push(top_left);\n sa[i].health=0;\n }\n else if(sa[top_left].health==sa[i].health)\n {\n sa[i].health=0;sa[top_left].health=0;\n }\n else \n {\n sa[top_left].health=0;\n sa[i].health-=1;\n continue;\n }\n break;\n }\n }\n }\n sort(sa.begin(),sa.end(),judge);\n vector<int>ans;\n for(int i=0;i<n;i++)\n {\n if(sa[i].health!=0)\n ans.push_back(sa[i].health);\n }\n return ans;\n }\n};",
"memory": "215608"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 1 | {
"code": "struct Robot\n{\n int posit;\n int health;\n char dir;\n int index;\n bool operator<(Robot const & b)\n {\n return this->posit<b.posit;\n }\n};\nbool judge(Robot a,Robot b)\n{\n return a.index<b.index;\n}\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n stack<int>left;\n vector<Robot>sa;\n int n=positions.size();\n for(int i=0;i<positions.size();i++)\n {\n sa.push_back({positions[i],healths[i],directions[i],i});\n }\n sort(sa.begin(),sa.end());\n for(int i=0;i<n;i++)\n {\n if(sa[i].dir=='R')\n left.push(i);\n else \n {\n while(!left.empty())\n {\n int top_left=left.top();\n left.pop();\n if(sa[top_left].health>sa[i].health)\n {\n sa[top_left].health-=1;\n left.push(top_left);\n sa[i].health=0;\n }\n else if(sa[top_left].health==sa[i].health)\n {\n sa[i].health=0;sa[top_left].health=0;\n }\n else \n {\n sa[top_left].health=0;\n sa[i].health-=1;\n continue;\n }\n break;\n }\n }\n }\n sort(sa.begin(),sa.end(),judge);\n vector<int>ans;\n for(int i=0;i<n;i++)\n {\n if(sa[i].health!=0)\n ans.push_back(sa[i].health);\n }\n return ans;\n }\n};",
"memory": "215608"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 1 | {
"code": "class Solution {\n typedef struct robot{\n int id;\n int pos;\n int health;\n char dir;\n }robot;\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n=positions.size();\n vector<robot> robots(n);\n for( int i=0;i<n;i++){\n robots[i].id=i+1;\n robots[i].pos=positions[i];\n robots[i].health=healths[i];\n robots[i].dir=directions[i];\n }\n map<int,int>mp;\n sort(robots.begin(),robots.end(),[](auto&a,auto&b){\n return a.pos < b.pos;\n });\n int i=0;\n stack<robot>st;\n for(i=0;i<n;){\n //cout<<robots[i].id<<\" \";\n if(robots[i].dir=='L'){\n if(st.empty()){\n i++;\n }\n else{\n robot right_most=st.top();\n st.pop();\n if(right_most.health > robots[i].health){\n right_most.health-=1;\n robots[i].health=0;\n st.push(right_most);\n i++;\n }\n else if(right_most.health < robots[i].health){\n robots[i].health-=1;\n }\n else if(right_most.health == robots[i].health){\n robots[i].health=0;\n i++;\n }\n }\n }\n else{\n st.push(robots[i]);\n i++;\n }\n }\n for(i=0;i<n;i++){\n if(robots[i].dir == 'L' && robots[i].health>0)\n {\n mp[robots[i].id]=robots[i].health;\n }\n }\n while(!st.empty()){\n mp[st.top().id] = st.top().health;\n st.pop();\n }\n vector<int>ansv;\n cout<<endl;\n for(auto it:mp){\n // cout<<it.first<<\" \";\n ansv.push_back(it.second);\n }\n return ansv;\n\n }\n};",
"memory": "216926"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions,\n vector<int>& healths, string directions) {\n vector<int> result;\n map<int, int> mp; // position -> health mapping for the surviving robots\n stack<pair<int, pair<int, char>>> st; // position -> (health, direction)\n int n = positions.size();\n vector<tuple<int, int, char>> robots(n);\n\n // Create the robots vector with positions, healths, and directions\n for (int i = 0; i < n; i++) {\n robots[i] = make_tuple(positions[i], healths[i], directions[i]);\n }\n\n // Sort robots by their positions\n sort(robots.begin(), robots.end());\n\n for (int i = 0; i < n; i++) {\n int pos = get<0>(robots[i]);\n int health = get<1>(robots[i]);\n char dir = get<2>(robots[i]);\n\n if (st.empty() || dir == 'R' || st.top().second.second == 'L') {\n st.push({pos, {health, dir}});\n } else {\n\n while (!st.empty() && st.top().second.second == 'R' &&\n st.top().second.first < health) {\n st.pop();\n health -= 1;\n }\n\n if (!st.empty() && st.top().second.first > health &&\n st.top().second.second == 'R') {\n st.top().second.first -= 1;\n } else if (!st.empty() && st.top().second.first == health &&\n st.top().second.second == 'R') {\n st.pop();\n } else {\n st.push({pos, {health, dir}});\n }\n }\n }\n\n while (!st.empty()) {\n mp[st.top().first] = st.top().second.first;\n st.pop();\n }\n\n for (int i = 0; i < n; i++) {\n if (mp.find(positions[i]) != mp.end()) {\n result.push_back(mp[positions[i]]);\n }\n }\n\n return result;\n }\n};",
"memory": "216926"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& health, string directions) {\n vector<pair<pair<int, int>, int>> v;\n for (int i = 0; i < positions.size(); i++) {\n v.push_back({{positions[i], health[i]}, i});\n }\n\n // Sort robots by their positions\n sort(v.begin(), v.end());\n\n vector<pair<pair<int, int>, int>> ans;\n for (auto it : v) {\n bool destroyed = false;\n\n // Process potential collisions\n while (!ans.empty() && directions[it.second] == 'L' && directions[ans.back().second] == 'R') {\n if (it.first.second > ans.back().first.second) {\n it.first.second -= 1; // Reduce health of the left-moving robot\n ans.pop_back(); // Right-moving robot is destroyed\n } else if (it.first.second < ans.back().first.second) {\n ans.back().first.second -= 1; // Reduce health of the right-moving robot\n destroyed = true; // Left-moving robot is destroyed\n break;\n } else {\n ans.pop_back(); // Both robots are destroyed\n destroyed = true;\n break;\n }\n }\n\n if (!destroyed) {\n ans.push_back(it);\n }\n }\n\n // Extract the health of surviving robots\n vector<int> result(positions.size(), -1); // Initialize with -1 to mark non-survivors\n for (auto it : ans) {\n result[it.second] = it.first.second;\n }\n\n // Remove non-survivors from the result\n result.erase(remove(result.begin(), result.end(), -1), result.end());\n\n return result;\n }\n};\n",
"memory": "218243"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "struct Robot\n{\n int posit;\n int health;\n char dir;\n int index;\n bool operator<(Robot const & b)\n {\n return this->posit<b.posit;\n }\n};\nbool judge(Robot a,Robot b)\n{\n return a.index<b.index;\n}\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n stack<int>left;\n vector<Robot>sa;\n int n=positions.size();\n vector<int>posi2index(n);\n for(int i=0;i<positions.size();i++)\n {\n sa.push_back({positions[i],healths[i],directions[i],i});\n }\n sort(sa.begin(),sa.end());\n for(int i=0;i<n;i++)\n {\n posi2index[sa[i].index]=i;\n if(sa[i].dir=='R')\n left.push(i);\n else \n {\n while(!left.empty())\n {\n int top_left=left.top();\n left.pop();\n if(sa[top_left].health>sa[i].health)\n {\n sa[top_left].health-=1;\n left.push(top_left);\n sa[i].health=0;\n }\n else if(sa[top_left].health==sa[i].health)\n {\n sa[i].health=0;sa[top_left].health=0;\n }\n else \n {\n sa[top_left].health=0;\n sa[i].health-=1;\n continue;\n }\n break;\n }\n }\n }\n // sort(sa.begin(),sa.end(),judge);\n vector<int>ans;\n for(int i=0;i<n;i++)\n {\n if(sa[posi2index[i]].health!=0)\n ans.push_back(sa[posi2index[i]].health);\n }\n return ans;\n }\n};",
"memory": "218243"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n class Robot{\n public:\n int position;\n int index;\n int health;\n char direction;\n Robot(int position,int index,int health,char direction){\n this->position = position;\n this->index = index;\n this->health = health;\n this->direction = direction;\n }\n };\n\n bool static compare(Robot &r1, Robot &r2){\n if(r1.position < r2.position){\n return true;\n }\n else if(r1.position > r2.position){\n return false;\n }\n return true;\n }\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<Robot> arr;\n for(int i=0;i<positions.size();i++){\n arr.push_back(Robot(positions[i],i,healths[i],directions[i]));\n }\n sort(arr.begin(),arr.end(),compare);\n int n = arr.size();\n stack<Robot *> q;\n for(int i=n-1;i>=0;i--){\n if(arr[i].direction == 'L'){\n q.push(&arr[i]);\n }\n else{\n if(q.size() == 0){\n continue;\n }\n else{\n while(q.size() > 0){\n Robot *curr = q.top();\n if(curr->health > arr[i].health){\n (curr->health)--;\n if(curr->health == 0){\n q.pop();\n }\n arr[i].health = 0;\n break;\n }\n else if(curr->health < arr[i].health){\n cout<<\"Hello \";\n curr->health = 0;\n (arr[i].health)--;\n q.pop();\n if(arr[i].health == 0){\n break;\n }\n }\n else{\n curr->health = 0;\n arr[i].health = 0;\n q.pop();\n break;\n }\n }\n }\n }\n }\n vector<int> res(n,0);\n for(int i=0;i<n;i++){\n res[arr[i].index] = arr[i].health;\n }\n vector<int> ans;\n for(int i=0;i<n;i++){\n if(res[i] > 0){\n ans.push_back(res[i]);\n }\n }\n return ans;\n }\n};",
"memory": "219561"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<pair<pair<int,int>,pair<int,char> > > arr;\n for(int i =0;i<positions.size();i++){\n arr.push_back(make_pair(make_pair(positions[i],i),make_pair(healths[i],directions[i])));\n \n\n\n }\n sort(arr.begin(),arr.end());\n for(auto it:arr){\n cout<<it.first.first<<\" \"<<it.second.first<<\" \"<<it.second.second<<endl;\n }\n stack<int>st;\n for(int i =0;i<arr.size();i++){\n if(arr[i].second.second == 'R'){\n st.push(i);\n }\n else{\n while(st.size() > 0 && arr[st.top()].second.first < arr[i].second.first){\n arr[i].second.first = arr[i].second.first -1;\n arr[st.top()].second.first = -1;\n st.pop();\n }\n while(st.size() > 0 && arr[st.top()].second.first > arr[i].second.first){\n arr[i].second.first = -1;\n arr[st.top()].second.first = arr[st.top()].second.first -1;\n \n\n // st.pop();\n break;\n }\n\n while(st.size() > 0 && arr[st.top()].second.first == arr[i].second.first){\n arr[i].second.first = -1;\n arr[st.top()].second.first = -1;\n\n st.pop();\n break;\n }\n \n\n\n }\n }\n vector<pair<int,int> >ans;\n\n for(int i =0;i<arr.size();i++){\n if(arr[i].second.first > 0){\n ans.push_back({arr[i].first.second,arr[i].second.first});\n }\n }\n sort(ans.begin(),ans.end());\n vector<int>temp;\n for(int i =0;i<ans.size();i++){\n temp.push_back(ans[i].second);\n }\n return temp;\n }\n};",
"memory": "219561"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n struct Robot{\n int index;\n int position;\n int health;\n char direction;\n };\n\n void handleCollisions(stack<Robot>& st, Robot& robot) {\n // collide with robots going right with less healths -> current robot wins\n while (!st.empty() && st.top().direction == 'R' && st.top().health < robot.health) {\n st.pop();\n robot.health--;\n }\n\n // collide with robot going right with equal or greater health\n if (!st.empty() && st.top().direction == 'R') { \n // equal health - both loses\n // greater health -> current robot loses\n if (st.top().health == robot.health)\n st.pop();\n else\n st.top().health--;\n } else { // no more robot going right \n st.push(robot);\n }\n }\n\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = positions.size();\n // attach index to robots\n vector<Robot> robots;\n for(int i=0; i<n; i++)\n robots.push_back({i, positions[i], healths[i], directions[i]});\n \n // sort robots based on position\n sort(robots.begin(), robots.end(), [](const auto &a, const auto &b){\n return a.position < b.position;\n });\n \n // Consider robots from left to right and handle collisions using stack\n stack<Robot> st;\n for(Robot& robot : robots){\n if(robot.direction == 'R')\n st.push(robot);\n else\n handleCollisions(st, robot);\n }\n\n // store remaining robots in stack in a vector\n robots.clear(); // reuse same robots vector\n while(!st.empty()){\n robots.push_back(st.top());\n st.pop();\n }\n\n // sort robot as per index as ans needs to be returned in same order as given\n sort(robots.begin(), robots.end(), [](const auto &a, const auto &b){\n return a.index < b.index;\n });\n\n // store health of robots in ans\n vector<int> ans;\n for(Robot robot : robots)\n ans.push_back(robot.health);\n\n return ans;\n }\n};",
"memory": "220878"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n struct ROBOT\n {\n int no;\n int pos;\n int hp;\n char dir;\n };\n\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<ROBOT> r;\n\n int n = positions.size();\n for(int i=0;i<n;i++)\n {\n r.push_back({i,positions[i],healths[i],directions[i]});\n }\n sort(r.begin(), r.end(), [](const ROBOT &a, const ROBOT &b) {return a.pos < b.pos;});\n\n stack<ROBOT> st;\n for(auto i:r)\n {\n if(st.empty())\n {\n st.push(i);\n }\n else if(st.top().dir == 'R' && i.dir == 'L')\n {\n\n if(st.top().hp == i.hp)\n {\n st.pop();\n }\n else if(st.top().hp > i.hp)\n {\n st.top().hp = st.top().hp-1;\n }\n else if(st.top().hp < i.hp)\n {\n int fg = 1;\n st.pop();\n i.hp = i.hp-1;\n if(st.empty())\n {\n st.push(i);\n continue;\n }\n while(st.top().dir == 'R')\n {\n \n if(st.top().hp == i.hp)\n {\n fg = 0;\n st.pop();\n break;\n }\n else if(st.top().hp > i.hp)\n {\n fg = 0;\n st.top().hp = st.top().hp - 1;\n break;\n }\n else if(st.top().hp < i.hp)\n {\n st.pop();\n i.hp = i.hp-1;\n }\n if(st.empty())\n {\n break;\n }\n }\n\n if(fg == 1)\n {\n st.push(i);\n }\n }\n\n }\n else\n {\n st.push(i);\n }\n\n }\n\n r.clear();\n while(!st.empty())\n {\n r.push_back(st.top());\n st.pop();\n }\n sort(r.begin(), r.end(), [](const ROBOT &a, const ROBOT &b) {return a.no < b.no ;});\n //sort(r.begin(), r.end(), [](const ROBOT &a, const ROBOT &b) {return a.pos < b.pos;});\n\n vector<int> res;\n for(auto i:r)\n {\n res.push_back(i.hp);\n }\n \n return res;\n }\n};",
"memory": "220878"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n cout.tie(nullptr);\n stack<pair<int,pair< char,int>>> st;\n int n = positions.size();\n vector<pair<int,int>> ans;\n vector<pair<int, pair<int,pair< char,int>>>> health;\n\n for (int i = 0; i < n; i++) {\n health.push_back({positions[i], {healths[i],{ directions[i],i+1}}});\n }\n\n sort(health.begin(), health.end());\n\n for (int i = 0; i < n; i++) {\n if (health[i].second.second.first == 'L' && st.empty()) {\n ans.push_back({health[i].second.second.second,health[i].second.first});\n } else {\n bool eliminated = false;\n if (health[i].second.second.first == 'L') {\n while (!st.empty() && st.top().second.first == 'R') {\n if (st.top().first > health[i].second.first) {\n st.top().first--;\n eliminated = true;\n break;\n } else if (st.top().first < health[i].second.first) {\n health[i].second.first--;\n st.pop();\n if (health[i].second.first == 0) {\n eliminated = true;\n break;\n }\n } else {\n st.pop();\n eliminated = true;\n break;\n }\n }\n if (!eliminated) {\n st.push({health[i].second.first, {'L',health[i].second.second.second}});\n }\n } else if (health[i].second.second.first == 'R') {\n st.push({health[i].second.first, {'R',health[i].second.second.second}});\n }\n }\n }\n\n while (!st.empty()) {\n ans.push_back({st.top().second.second,st.top().first});\n st.pop();\n }\n sort(ans.begin(),ans.end());\n vector<int> val;\n for(int i=0;i<ans.size();i++){\n val.push_back(ans[i].second);\n }\n return val;\n }\n};",
"memory": "222196"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<int> ans;\n vector<tuple<int, int, char, int>> vec;\n for(int i = 0; i<healths.size(); i++)\n {\n vec.push_back({positions[i], healths[i], directions[i], i});\n }\n sort(vec.begin(), vec.end());\n vector<pair<int, pair<int,char>>> st;\n pair<int, pair<int,char>> temp;\n for(int i=0; i<vec.size(); i++)\n {\n temp = make_pair(get<3>(vec[i]), make_pair(get<1>(vec[i]), get<2>(vec[i])));\n // cout << temp.second.first << \" \" << temp.second.second << '\\n';\n if(st.empty())\n st.push_back(temp);\n else if(!(st.back().second.second == 'R' && temp.second.second == 'L'))\n {\n st.push_back(temp);\n }\n else\n {\n if(st.back().second.first == temp.second.first)\n st.pop_back();\n else if(st.back().second.first > temp.second.first)\n st.back().second.first --;\n else{\n // cout << \"debug\";\n while(!st.empty() && st.back().second.second == 'R' && st.back().second.first < temp.second.first)\n {\n st.pop_back();\n temp.second.first--;\n }\n if(st.empty() || st.back().second.second == 'L')\n st.push_back(temp);\n else if(st.back().second.first > temp.second.first)\n st.back().second.first--;\n else\n st.pop_back();\n }\n }\n }\n sort(st.begin(), st.end());\n for(int i=0; i<st.size(); i++)\n {\n ans.push_back(st[i].second.first);\n }\n return ans;\n // int i = 0;\n // int dir =1;\n // int currMax = get<1>vec[0];\n // while(i+dir < vec.size() && i+dir >=0)\n // {\n // while(get<2>vec[i] == 'L')\n // i++;\n\n // if(get<1>vec[i] == get<1>vec[i-1])\n // {\n // get<1>vec[i] = get<1>vec[i-1] = 0;\n // i++;\n // }\n // else if(get<1>vec[i] < get<1>vec[i-1])\n // {\n // get<1>vec[i-1] -= 1;\n // get<1>vec[i] = 0;\n // i++;\n // }\n // if(get<1>vec[i] > get<1>vec[i-1])\n // {\n // while()\n // }\n // }\n }\n};",
"memory": "222196"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n void handleCollision(stack<pair<int, pair<int, pair<int, int>>>>& st, pair<int, pair<int, pair<int, int>>> curr) {\n while (!st.empty() && st.top().second.second.first == 'R' && st.top().second.first < curr.second.first) {\n st.pop();\n curr.second.first--;\n }\n if (!st.empty() && st.top().second.second.first == 'R') {\n if (st.top().second.first == curr.second.first) {\n st.pop();\n } else {\n st.top().second.first--;\n }\n } else {\n st.push(curr);\n }\n }\n\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<pair<int, pair<int, pair<int, int>>>> temp;\n int n = healths.size();\n for (int i = 0; i < n; i++) {\n temp.push_back({positions[i], {healths[i], {directions[i], i}}});\n }\n sort(temp.begin(), temp.end());\n \n stack<pair<int, pair<int, pair<int, int>>>> st;\n for (int i = 0; i < n; i++) {\n if (temp[i].second.second.first == 'R') {\n st.push(temp[i]);\n } else {\n handleCollision(st, temp[i]);\n }\n }\n \n vector<int> ans(n, 0);\n while (!st.empty()) {\n ans[st.top().second.second.second] = st.top().second.first;\n st.pop();\n }\n vector<int>anss;\n for(int i=0;i<ans.size();i++){\n if(ans[i]!=0)\n anss.push_back(ans[i]);\n }\n \n return anss;\n }\n};",
"memory": "223513"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "bool compare(const pair<int, pair<int, pair<int, char>>> a, const pair<int, pair<int, pair<int, char>>> b) {\n return a.first < b.first;\n}\nclass Solution {\npublic:\n\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n stack<pair<int,pair<int,char>>>pq;\n vector<pair<int,pair<int,pair<int,char>>>>vec;\n for(int i=0;i<directions.size();i++){\n vec.push_back({positions[i],{i,{healths[i],directions[i]}}});\n }\n sort(vec.begin(),vec.end(),compare);\n for(int i=0;i<directions.size();i++){\n if(pq.empty()){\n pair<int,pair<int,pair<int,char>>>current=vec[i];\n pq.push({current.second.first,{current.second.second.first,current.second.second.second}});\n }\n else{\n if(vec[i].second.second.second=='L'){\n int rightVal=vec[i].second.second.first;\n while(!pq.empty()&& pq.top().second.second=='R'){\n \n pair<int,pair<int,char>>cur=pq.top();\n pq.pop();\n if(cur.second.first==rightVal){\n rightVal=0;\n break;\n }\n else if(cur.second.first>rightVal){\n int var=cur.second.first-1;\n if(var){\n cur.second.first=var;\n rightVal=0;\n pq.push(cur);\n break;\n }\n }\n else{\n rightVal--;\n }\n }\n if(rightVal){\n pq.push({vec[i].second.first,{rightVal,'L'}});\n }\n }\n else{\n pair<int,pair<int,pair<int,char>>>current=vec[i];\n pq.push({current.second.first,{current.second.second.first,current.second.second.second}});\n }\n }\n }\n \n \n vector<pair<int,int>> ans;\n while(!pq.empty()){\n ans.push_back({pq.top().first,pq.top().second.first});\n pq.pop();\n }\n sort(ans.begin(),ans.end());\n vector<int>res;\n for(int i=0;i<ans.size();i++){\n res.push_back(ans[i].second);\n }\n return res;\n }\n};",
"memory": "223513"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\nstruct Robot{\n int p;\n int h;\n int d;\n \n int i;\n};\n \nstatic bool compare(const Robot &a,const Robot &b){\n return a.p < b.p;\n}\n vector<int> survivedRobotsHealths(vector<int>& pos, vector<int>& heal, string& direc) {\n int n=pos.size();\n vector<Robot>vals;\n for(int i=0;i<n;i++){\n vals.push_back({pos[i],heal[i],direc[i],i});\n }\n sort(vals.begin(), vals.end(), compare);\n vector<Robot>st;\n\n for(auto &robot:vals){\n\n if(robot.d=='R'){\n st.push_back(robot);\n continue;\n }\n bool gone=false;\n while(!st.empty() && st.back().h<=robot.h && st.back().d=='R'){\n if(st.back().h==robot.h){\n st.pop_back();\n gone=true;\n break;\n }\n robot.h--;\n st.pop_back();\n }\n if(!gone && !st.empty() && st.back().d=='R' && st.back().h>robot.h){\n st.back().h--;\n gone=true;\n }\n if(!gone){\n st.push_back(robot);\n }\n }\n sort(st.begin(), st.end(), [](const Robot& a, const Robot& b) {\n return a.i < b.i;\n });\n\n vector<int> result;\n for (const auto& robot : st) {\n result.push_back(robot.h);\n }\n\n \n \n return result;\n }\n};",
"memory": "224831"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string& directions) {\n ios_base::sync_with_stdio(false);\n cin.tie(NULL);\n cout.tie(NULL);\n vector<array<int, 4>> robots;\n stack<array<int, 4>> st;\n array<int, 4> temp;\n\n for(int i=0; i<positions.size(); i++)\n robots.push_back({positions[i], i+1, healths[i], directions[i]});\n sort(robots.begin(), robots.end());\n \n\n for(int i=0; i<robots.size(); i++) \n {\n if(st.empty())\n st.push(robots[i]);\n else if(st.top()[3] == 'R' && robots[i][3] == 'L')\n {\n while(1)\n {\n if(st.empty() || st.top()[3] != 'R')\n {\n st.push(robots[i]);\n break;\n }\n if(st.top()[2] < robots[i][2])\n {\n st.pop();\n robots[i][2]--; \n } \n else if(st.top()[2] > robots[i][2])\n {\n temp = st.top();\n st.pop();\n temp[2]--;\n st.push(temp);\n break;\n } \n else \n {\n st.pop();\n break;\n }\n }\n }\n else\n st.push(robots[i]);\n \n }\n vector<int> result;\n vector<pair<int,int>> posHealth;\n while(!st.empty())\n {\n posHealth.push_back({st.top()[1],st.top()[2]});\n st.pop();\n }\n sort(posHealth.begin(), posHealth.end());\n for(int i=0; i<posHealth.size(); i++)\n {\n result.push_back(posHealth[i].second);\n }\n return result;\n }\n};\n// positions = [3,5,2,6]\n// healths = [10,10,15,12]\n// directions = \"RLRL\"\n\n\n// [(3,10,R) ] (5,10,L)\n// // Direction should be opposite (Right at top of stack, and left as new entry)\n// // There should be no one between them\n// // so, after 3, 5 is coming then no one issue, just sort them \n\n// // If direction is opposite\n// // is both Health equal pop from stack\n// // is not pop from stack and store (health -1) of maxi",
"memory": "224831"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<pair<int, pair<char, int>>> kit;\n\n for(int i = 0; i < positions.size(); i++) {\n kit.push_back({positions[i], {directions[i], healths[i]}});\n }\n\n sort(kit.begin(), kit.end());\n\n stack<pair<int, pair<char, int>>> st;\n\n for(int i = 0; i < kit.size(); i++) {\n auto [pos, dir_health] = kit[i];\n char dir = dir_health.first;\n int health = dir_health.second;\n\n if(dir == 'R') {\n st.push(kit[i]);\n } else {\n while(!st.empty() && st.top().second.first == 'R') {\n auto [top_pos, top_dir_health] = st.top();\n int top_health = top_dir_health.second;\n\n if(top_health > health) {\n // Current robot 'L' is destroyed, reduce the health of 'R'\n st.top().second.second--;\n health = 0;\n break;\n } else if(top_health < health) {\n // 'R' is destroyed\n st.pop();\n health--;\n } else {\n // Both are destroyed\n st.pop();\n health = 0;\n break;\n }\n }\n\n if(health > 0) {\n st.push({pos, {dir, health}});\n }\n }\n }\n\n unordered_map<int, int> m;\n\n while(!st.empty()) {\n m[st.top().first] = st.top().second.second;\n st.pop();\n }\n\n vector<int> ans;\n\n for(auto pos : positions) {\n if(m.find(pos) != m.end()) {\n ans.push_back(m[pos]);\n }\n }\n\n return ans;\n }\n};\n",
"memory": "226148"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n ios_base::sync_with_stdio(false); \n cin.tie(nullptr); \n cout.tie(nullptr);\n struct Robot {\n int pos;\n int hp;\n char heading;\n };\n vector<Robot> info;\n\n for(int i = 0; i < positions.size(); i++){\n info.push_back({positions[i], healths[i], directions[i]});\n }\n sort(info.begin(), info.end(), [](const Robot& a, const Robot& b)\n { return a.pos < b.pos;});\n \n //greedy search for collision points\n stack<Robot> survivors;\n survivors.push(info[0]);\n int pointer = 1;\n while(pointer < info.size()){\n if((survivors.top().heading == 'R') && (info[pointer].heading == 'L')){\n if(survivors.top().hp == info[pointer].hp){\n survivors.pop();\n pointer++;\n } else if (survivors.top().hp < info[pointer].hp){\n survivors.pop();\n pointer = --info[pointer].hp ? pointer : ++pointer;\n } else {\n if(--survivors.top().hp == 0){\n survivors.pop();\n }\n pointer++;\n }\n } else {\n survivors.push(info[pointer]);\n pointer++;\n }\n if(survivors.empty() && pointer < info.size()){\n survivors.push(info[pointer]);\n pointer++;\n }\n }\n unordered_map<int, int> survivorHash;\n while(!survivors.empty()){\n survivorHash[survivors.top().pos] = survivors.top().hp;\n survivors.pop();\n }\n vector<int> result;\n for(auto& iter : positions){\n if(survivorHash.find(iter) != survivorHash.end()){\n result.push_back(survivorHash[iter]);\n }\n }\n \n // origPos.reserve(positions.size());\n // for(int i = 0; i < positions.size(); i++){\n // origPos[positions[i]] = i;\n // }\n \n // vector<vector<int>> posOrder;\n\n // while(!survivors.empty()){\n // posOrder.push_back({origPos[survivors.top().pos], survivors.top().hp});\n // survivors.pop();\n // }\n // sort(posOrder.begin(), posOrder.end());\n // vector<int> result;\n // for(auto& iter : posOrder){\n // result.push_back(iter[1]);\n // }\n return result;\n \n }\n};",
"memory": "226148"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<pair<int,pair<int,char>>> v;\n vector<int> ans;\n int n=positions.size();\n for(int i=0;i<n;i++){\n v.push_back({positions[i],{healths[i],directions[i]}});\n\n }\n sort(v.begin(),v.end());\n \n \n\n stack<pair<int,pair<int,char>>> s;\n\n for(int i=0;i<n;i++){\n \n if(s.empty()){\n s.push(v[i]);\n continue;\n\n }\n int h=s.top().second.first;\n char d=s.top().second.second;\n int p=s.top().first;\n int hc=v[i].second.first;\n char dc=v[i].second.second;\n int pc=v[i].first;\n \n if(d==dc||(dc=='R'&&d=='L')){\n s.push(v[i]);\n \n continue;\n \n }\n while(dc=='L'&&d=='R'){\n if(h==hc){\n \n s.pop();\n break;\n }\n else if(h>hc){\n s.pop();\n \n s.push({p,{h-1,d}});\n break;\n }\n else{\n s.pop();\n hc--;\n \n if(s.empty()){\n s.push({pc,{hc,dc}});\n // dc='A';\n }\n if(s.size()>0){\n h=s.top().second.first;\n d=s.top().second.second;\n p=s.top().first;\n }\n\n }\n\n \n\n }\n if(! (dc=='L'&&d=='R'))\n s.push({pc,{hc,dc}});\n\n\n \n }\n map<int,int> m;\n while(!s.empty()){\n int x=s.top().second.first;\n int y=s.top().first;\n m[y]=x;\n s.pop();\n }\n for(int i=0;i<n;i++){\n if(m.find(positions[i])!=m.end()){\n ans.push_back(m[positions[i]]);\n }\n }\n return ans;\n }\n};",
"memory": "227466"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\nvector<int> survivedRobotsHealths(vector<int> &positions, vector<int> &healths, string directions)\n{\n\n vector<int> st;\n unordered_map<int, int> mp;\n vector<int> ans;\n for (int i = 0; i < positions.size(); ++i)\n mp[positions[i]] = i;\n\n sort(positions.begin(), positions.end());\n\n for (auto &p : positions)\n {\n int i = mp[p];\n\n if (directions[i] == 'R')\n st.push_back(i);\n\n else\n {\n while (!st.empty() && directions[st.back()] == 'R' && healths[i] > 0)\n {\n int i2 = st.back();\n st.pop_back();\n\n if (healths[i] > healths[i2])\n {\n healths[i2] = 0;\n healths[i] -= 1;\n }\n else if (healths[i] < healths[i2])\n {\n healths[i] = 0;\n healths[i2] -= 1;\n st.push_back(i2);\n }\n\n else\n {\n healths[i] = 0;\n healths[i2] = 0;\n }\n }\n // if(healths[i] > 0)\n // st.push_back(i);\n }\n }\n\n for(auto &health : healths)\n {\n if(health > 0)\n ans.push_back(health);\n }\n\n return ans;\n}\n\n};",
"memory": "227466"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Robot {\n public:\n int id;\n int pos;\n int health;\n int direction;\n Robot(int id, int pos, int health, int direction){\n this->id = id;\n this->pos = pos;\n this->health = health;\n this->direction = direction;\n }\n};\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = positions.size();\n vector<Robot> v;\n for(int i = 0;i<n;i++){\n v.push_back(Robot(i, positions[i], healths[i], directions[i]));\n }\n auto comp = [](Robot a, Robot b){\n return a.pos < b.pos;\n };\n sort(v.begin(), v.end(), comp);\n stack<Robot> st;\n for(int i =0;i<n;i++){\n if(v[i].direction == 'R'){\n st.push(v[i]);\n } else {\n while(!st.empty() && st.top().direction == 'R'){\n if(st.top().health < v[i].health){\n v[i].health -= 1;\n st.pop();\n } else if(st.top().health > v[i].health) {\n st.top().health -= 1;\n v[i].health = 0;\n } else {\n v[i].health = 0;\n st.pop();\n }\n if(v[i].health == 0) break;\n }\n if(v[i].health) st.push(v[i]);\n }\n }\n vector<Robot> rem;\n while(!st.empty()){\n rem.push_back(st.top());\n st.pop();\n }\n auto comp2 = [](Robot a, Robot b){\n return a.id < b.id;\n };\n sort(rem.begin(), rem.end(), comp2);\n vector<int> ans;\n for(int i = 0;i<rem.size();i++){\n ans.push_back(rem[i].health);\n }\n return ans;\n }\n};",
"memory": "228783"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions,\n vector<int>& healths, string directions) {\n int n = positions.size();\n unordered_map<int, int> posToIdx;\n for (int i = 0; i < n; ++i) {\n posToIdx[positions[i]] = i;\n }\n\n sort(positions.begin(), positions.end());\n stack<int> stk;\n for(int pos:positions){\n int idx = posToIdx[pos];\n\n if(directions[idx] == 'R') stk.push(idx);\n else{\n while(!stk.empty() && healths[idx]){\n int idx2 = stk.top();\n\n if(healths[idx] > healths[idx2]){\n --healths[idx];\n healths[idx2] = 0;\n stk.pop();\n }\n else if(healths[idx2] > healths[idx]){\n --healths[idx2];\n healths[idx] = 0;\n }\n else{\n healths[idx] = healths[idx2] = 0;\n stk.pop();\n }\n }\n }\n }\n\n vector<int> finalHealths;\n for(int health:healths){\n if(health != 0) finalHealths.push_back(health);\n }\n\n return finalHealths;\n }\n};",
"memory": "228783"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = positions.size();\n vector<vector<int>> vec(n);\n for(int i=0;i<n;i++){\n vec[i] = {positions[i], i};\n }\n sort(vec.begin(), vec.end());\n stack<int> st;\n vector<int> ret(n, -1);\n for(int i=0;i<n;i++){\n int ind = vec[i][1];\n //cout<<ind<<\" \";\n int hel = healths[ind]; char dir = directions[ind];\n //cout<<st.top()<<\" \";\n if(dir == 'L'){\n while(st.empty() == false && healths[st.top()] < hel){\n st.pop(), hel--;\n }\n if(st.empty() == true)\n ret[ind] = hel;\n else if(hel == healths[st.top()]){\n st.pop();\n }\n else{\n healths[st.top()]--;\n }\n }else{\n st.push(ind);\n }\n }\n while(st.empty() == false){\n //cout<<st.top()<<\" \";\n ret[st.top()] = healths[st.top()];\n st.pop();\n }\n vector<int> ans;\n for(int x : ret)\n if(x > 0)\n ans.push_back(x);\n return ans;\n }\n};",
"memory": "230101"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n stack<int> s;\n int n=positions.size();\n map<int,pair<int,char> >m;\n for(int i=0;i<n;i++){\n m[positions[i]]={healths[i],directions[i]};\n }\n for(auto it:m){\n if(m[it.first].second=='R')s.push(it.first);\n else{\n while(!s.empty()){\n if(m[s.top()].first>m[it.first].first){\n m[s.top()].first--;\n m[it.first].first=0;\n break;\n }\n else if(m[s.top()].first==m[it.first].first) {\n m[s.top()].first=0;\n m[it.first].first=0;\n s.pop();\n\n break;\n }\n else{\n m[s.top()].first=0;\n m[it.first].first--;\n s.pop();\n }\n }\n }\n }\n vector<int> ans;\n for(int i=0;i<n;i++){\n if(m[positions[i]].first!=0){\n ans.push_back(m[positions[i]].first);\n }\n }\n return ans;\n }\n};",
"memory": "230101"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n struct robot{\n int pos;\n int health;\n char dir;\n int idx;\n bool dead = false;\n };\n static bool sortBots(robot& a, robot& b){\n return (a.pos < b.pos);\n }\n static bool sortBackBots(robot& a, robot& b){\n return (a.idx < b.idx);\n }\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int numBots = positions.size();\n robot curr;\n vector<robot> bots;\n for (int i=0; i<numBots; i++){\n curr = {positions[i], healths[i], directions[i], i};\n bots.push_back(curr);\n }\n sort(bots.begin(), bots.end(), sortBots);\n \n int startIdx=0;\n int endIdx=numBots-1;\n while (startIdx<numBots && bots[startIdx].dir=='L'){\n startIdx++;\n }\n while (endIdx>=0 && bots[endIdx].dir=='R'){\n endIdx--;\n }\n\n list<robot*> collisions;\n robot* currLeft;\n for (int i=startIdx; i<=endIdx; i++){\n if (bots[i].dir=='R'){\n collisions.push_back(&bots[i]);\n currLeft = collisions.back();\n }\n else{\n while (!collisions.empty() && currLeft->health<bots[i].health){\n bots[i].health--;\n currLeft->dead=true;\n collisions.pop_back();\n if (!collisions.empty()){\n currLeft = collisions.back();\n }\n }\n if (!collisions.empty() && currLeft->health==bots[i].health){\n currLeft->dead=true;\n bots[i].dead=true;\n collisions.pop_back();\n if (!collisions.empty()){\n currLeft = collisions.back();\n }\n }\n else if (!collisions.empty() && currLeft->health>bots[i].health){\n bots[i].dead=true;\n currLeft->health--;\n }\n }\n }\n //possible to also remove dead from bots immediatly when they die?\n sort(bots.begin(), bots.end(), sortBackBots);\n vector<int> ret;\n for (int i=0; i<numBots; i++){\n if (!bots[i].dead){\n ret.push_back(bots[i].health);\n }\n }\n return ret;\n }\n};",
"memory": "231418"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n ios::sync_with_stdio(false);\n cin.tie(nullptr);\n int n=positions.size();\n vector<int> ans;\n map<int, pair<int,int>> robs;\n map<int, pair<int,int>>::iterator left, right;\n priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> buf;\n for(int i=0; i<n; ++i) {\n robs[positions[i]]={healths[i], directions[i]=='L'? -1:+1};\n }\n for(right=robs.begin(), left=right++; right!=robs.end(); left=right++) {\n if(left->second.second==1 && right->second.second==-1) {\n buf.push({right->first-left->first, left->first});\n }\n }\n for(; buf.size(); ) {\n // cout<<\"[---]\"<<buf.top().first<<\",\"<<buf.top().second<<\"\\n\";\n left =robs.find(buf.top().second);\n right=robs.find(buf.top().first+buf.top().second);\n buf.pop();\n // cout<<\"[***]\"<<left->first<<\",\"<<left->second.first<<\",\"<<left->second.second<<\" | \"\n // <<right->first<<\",\"<<right->second.first<<\",\"<<right->second.second<<\"\\n\";\n if(left->second.first<right->second.first) {\n // cout<<\"[DELETE LEFT]: \"<<left->first<<\",\"<<left->second.first<<\",\"<<left->second.second<<\"\\n\";\n --right->second.first;\n left=robs.erase(left);\n if(left!=robs.begin() && (--left)->second.second==1) {\n // cout<<\"[ADD]\"<<left->first<<\",\"<<left->second.first<<\",\"<<left->second.second<<\" | \"\n // <<right->first<<\",\"<<right->second.first<<\",\"<<right->second.second<<\"\\n\";\n // cout<<\"[+++]\"<<right->first-left->first<<\",\"<<left->first<<\"\\n\";\n buf.push({right->first-left->first, left->first});\n }\n } else if(left->second.first>right->second.first) {\n // cout<<\"[DELETE RIGHT]: \"<<right->first<<\",\"<<right->second.first<<\",\"<<right->second.second<<\"\\n\";\n --left->second.first;\n right=robs.erase(right);\n if(right!=robs.end() && right->second.second==-1) {\n // cout<<\"[ADD]\"<<left->first<<\",\"<<left->second.first<<\",\"<<left->second.second<<\" | \"\n // <<right->first<<\",\"<<right->second.first<<\",\"<<right->second.second<<\"\\n\"; \n // cout<<\"[+++]\"<<right->first-left->first<<\",\"<<left->first<<\"\\n\";\n buf.push({right->first-left->first, left->first});\n }\n } else {\n // cout<<\"[DELETE LEFT]: \"<<left->first<<\",\"<<left->second.first<<\",\"<<left->second.second<<\"\\n\";\n // cout<<\"[DELETE RIGHT]: \"<<right->first<<\",\"<<right->second.first<<\",\"<<right->second.second<<\"\\n\";\n right=robs.erase(right);\n left=robs.erase(left);\n if(left!=robs.begin() && right!=robs.end() && (--left)->second.second==1 && right->second.second==-1) {\n // cout<<\"[ADD]\"<<left->first<<\",\"<<left->second.first<<\",\"<<left->second.second<<\" | \"\n // <<right->first<<\",\"<<right->second.first<<\",\"<<right->second.second<<\"\\n\"; \n // cout<<\"[+++]\"<<right->first-left->first<<\",\"<<left->first<<\"\\n\";\n buf.push({right->first-left->first, left->first});\n }\n } \n }\n for(int i=0; i<n; ++i) {\n if(robs.count(positions[i])) {\n ans.emplace_back(robs[positions[i]].first);\n }\n }\n return ans;\n }\n};",
"memory": "231418"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "const static auto startupOptimization = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions,\n vector<int>& healths, string directions) {\n const std::size_t size = positions.size();\n\n std::vector<Robot> robots;\n robots.reserve(size);\n for (std::size_t i = 0; i < size; ++i) {\n robots.emplace_back(i, positions[i], healths[i], directions[i]);\n }\n\n std::sort(robots.begin(), robots.end(),\n [](const Robot& left, const Robot& right) {\n return left.position < right.position;\n });\n\n std::vector<Robot> remainingRobots;\n for (auto& robot : robots) {\n if (robot.direction == Robot::Direction::Left) {\n while (robot.health > 0 && !remainingRobots.empty() &&\n remainingRobots.back().direction ==\n Robot::Direction::Right) {\n auto& sideRobot = remainingRobots.back();\n remainingRobots.pop_back();\n\n if (sideRobot.health == robot.health) {\n robot.health = 0;\n } else if (sideRobot.health > robot.health) {\n sideRobot.health--;\n robot.health = 0;\n if (sideRobot.health > 0) {\n remainingRobots.emplace_back(sideRobot);\n }\n } else {\n robot.health--;\n }\n }\n }\n\n if (robot.health > 0) {\n remainingRobots.emplace_back(robot);\n }\n }\n\n std::sort(remainingRobots.begin(), remainingRobots.end(),\n [](const Robot& left, const Robot& right) {\n return left.index < right.index;\n });\n\n std::vector<int> result;\n result.reserve(remainingRobots.size());\n for (const auto& robot : remainingRobots) {\n result.emplace_back(robot.health);\n }\n return result;\n }\n\nprivate:\n struct Robot {\n enum class Direction { Left, Right };\n\n std::size_t index;\n std::size_t position;\n std::size_t health;\n Direction direction;\n\n Robot(std::size_t _index, std::size_t _position, std::size_t _health,\n char _direction)\n : index(_index), position(_position), health(_health),\n direction(_direction == 'L' ? Direction::Left\n : Direction::Right) {}\n };\n};\n",
"memory": "232736"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "const static auto startupOptimization = []() {\n std::ios::sync_with_stdio(false);\n std::cin.tie(nullptr);\n std::cout.tie(nullptr);\n return 0;\n}();\n\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions,\n vector<int>& healths, string directions) {\n const std::size_t size = positions.size();\n\n std::vector<Robot> robots;\n robots.reserve(size);\n for (std::size_t i = 0; i < size; ++i) {\n robots.emplace_back(i, positions[i], healths[i], directions[i]);\n }\n\n std::sort(robots.begin(), robots.end(),\n [](const Robot& left, const Robot& right) {\n return left.position < right.position;\n });\n\n std::vector<Robot> remainingRobots;\n for (auto& robot : robots) {\n if (robot.direction == Robot::Direction::Left) {\n while (robot.health > 0 && !remainingRobots.empty() &&\n remainingRobots.back().direction ==\n Robot::Direction::Right) {\n auto& sideRobot = remainingRobots.back();\n remainingRobots.pop_back();\n\n if (sideRobot.health == robot.health) {\n robot.health = 0;\n } else if (sideRobot.health > robot.health) {\n sideRobot.health--;\n robot.health = 0;\n if (sideRobot.health > 0) {\n remainingRobots.emplace_back(std::move(sideRobot));\n }\n } else {\n robot.health--;\n }\n }\n }\n\n if (robot.health > 0) {\n remainingRobots.emplace_back(std::move(robot));\n }\n }\n\n std::sort(remainingRobots.begin(), remainingRobots.end(),\n [](const Robot& left, const Robot& right) {\n return left.index < right.index;\n });\n\n std::vector<int> result;\n result.reserve(remainingRobots.size());\n for (const auto& robot : remainingRobots) {\n result.emplace_back(robot.health);\n }\n return result;\n }\n\nprivate:\n struct Robot {\n enum class Direction { Left, Right };\n\n std::size_t index;\n std::size_t position;\n std::size_t health;\n Direction direction;\n\n Robot(std::size_t _index, std::size_t _position, std::size_t _health,\n char _direction)\n : index(_index), position(_position), health(_health),\n direction(_direction == 'L' ? Direction::Left\n : Direction::Right) {}\n };\n};\n",
"memory": "232736"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "/* clang-format off */\n#define cerr cout\nnamespace __DEBUG_UTIL__ { void print(const char *x) { cerr << x; } void print(bool x) { cerr << (x ? \"T\" : \"F\"); } void print(char x) { cerr << '\\'' << x << '\\''; } void print(signed short int x) { cerr << x; } void print(unsigned short int x) { cerr << x; } void print(signed int x) { cerr << x; } void print(unsigned int x) { cerr << x; } void print(signed long int x) { cerr << x; } void print(unsigned long int x) { cerr << x; } void print(signed long long int x) { cerr << x; } void print(unsigned long long int x) { cerr << x; } void print(float x) { cerr << x; } void print(double x) { cerr << x; } void print(long double x) { cerr << x; } void print(string x) { cerr << '\\\"' << x << '\\\"'; } template <size_t N> void print(bitset<N> x) { cerr << x; } void print(vector<bool> v) { int f = 0; cerr << '{'; for (auto &&i : v) cerr << (f++ ? \",\" : \"\") << (i ? \"T\" : \"F\"); cerr << \"}\"; } template <typename T> void print(T &&x); template <typename T> void print(vector<vector<T>> mat); template <typename T, size_t N, size_t M> void print(T (&mat)[N][M]); template <typename F, typename S> void print(pair<F, S> x); template <typename T, size_t N> struct Tuple; template <typename T> struct Tuple<T, 1>; template <typename... Args> void print(tuple<Args...> t); template <typename... T> void print(priority_queue<T...> pq); template <typename T> void print(stack<T> st); template <typename T> void print(queue<T> q); template <typename T> void print(T &&x) { int f = 0; cerr << '{'; for (auto &&i : x) cerr << (f++ ? \",\" : \"\"), print(i); cerr << \"}\"; } template <typename T> void print(vector<vector<T>> mat) { int f = 0; cerr << \"\\n~~~~~\\n\"; for (auto &&i : mat) { cerr << setw(2) << left << f++, print(i), cerr << \"\\n\"; } cerr << \"~~~~~\\n\"; } template <typename T, size_t N, size_t M> void print(T (&mat)[N][M]) { int f = 0; cerr << \"\\n~~~~~\\n\"; for (auto &&i : mat) { cerr << setw(2) << left << f++, print(i), cerr << \"\\n\"; } cerr << \"~~~~~\\n\"; } template <typename F, typename S> void print(pair<F, S> x) { cerr << '('; print(x.first); cerr << ','; print(x.second); cerr << ')'; } template <typename T, size_t N> struct Tuple { static void printTuple(T t) { Tuple<T, N - 1>::printTuple(t); cerr << \",\", print(get<N - 1>(t)); } }; template <typename T> struct Tuple<T, 1> { static void printTuple(T t) { print(get<0>(t)); } }; template <typename... Args> void print(tuple<Args...> t) { cerr << \"(\"; Tuple<decltype(t), sizeof...(Args)>::printTuple(t); cerr << \")\"; } template <typename... T> void print(priority_queue<T...> pq) { int f = 0; cerr << '{'; while (!pq.empty()) cerr << (f++ ? \",\" : \"\"), print(pq.top()), pq.pop(); cerr << \"}\"; } template <typename T> void print(stack<T> st) { int f = 0; cerr << '{'; while (!st.empty()) cerr << (f++ ? \",\" : \"\"), print(st.top()), st.pop(); cerr << \"}\"; } template <typename T> void print(queue<T> q) { int f = 0; cerr << '{'; while (!q.empty()) cerr << (f++ ? \",\" : \"\"), print(q.front()), q.pop(); cerr << \"}\"; } void printer(const char *) {} template <typename T, typename... V> void printer(const char *names, T &&head, V &&...tail) { int i = 0; for (int bracket = 0; names[i] != '\\0' and (names[i] != ',' or bracket > 0); i++) if (names[i] == '(' or names[i] == '<' or names[i] == '{') bracket++; else if (names[i] == ')' or names[i] == '>' or names[i] == '}') bracket--; cerr.write(names, i) << \" = \"; print(head); if (sizeof...(tail)) cerr << \" ||\", printer(names + i + 1, tail...); else cerr << \"]\\n\"; } }\n#ifdef ONLINE_JUDGE\n#define debug(...) cerr << __LINE__ << \": [\", __DEBUG_UTIL__::printer(#__VA_ARGS__, __VA_ARGS__)\n#else\n#define debug(...)\n#endif\n/* clang-format on */ class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions,\n vector<int>& healths, string directions) {\n set<pair<int, pair<int, int>>, greater<pair<int, pair<int, int>>>>\n right;\n vector<pair<int, pair<int, int>>> ans, left;\n for (int i = 0; i < positions.size(); i++) {\n if (directions[i] == 'L')\n left.push_back({positions[i], {healths[i], i}});\n else\n right.insert({positions[i], {healths[i], i}});\n }\n sort(left.rbegin(), left.rend());\n debug(right, left);\n while (left.size()) {\n auto [p, pr] = left.back();\n auto [h, i] = pr;\n auto it = right.lower_bound({p, {0, 0}});\n debug(left.back(), *it);\n left.pop_back();\n if (it == right.end()) {\n ans.push_back({p, pr});\n } else {\n auto [p2, pr2] = *it;\n auto [h2, i2] = pr2;\n right.erase(it);\n if (h > h2) {\n left.push_back({p, {h - 1, i}});\n } else if (h < h2) {\n right.insert({p2, {h2 - 1, i2}});\n }\n }\n }\n for (auto [p, pr] : right) {\n ans.push_back({p, pr});\n }\n sort(ans.begin(), ans.end(), [](const auto& a, const auto& b) {\n return a.second.second < b.second.second;\n });\n vector<int> ans2;\n for (auto [p, pr] : ans)\n ans2.push_back(pr.first);\n return ans2;\n }\n};",
"memory": "234053"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = positions.size(); \n vector<tuple<int,int,int,char>> v; \n stack<tuple<int,int,int,char>> st; \n \n for (int i = 0; i < n; i++) {\n v.push_back(make_tuple(positions[i], i, healths[i], directions[i])); \n }\n \n sort(v.begin(), v.end());\n \n for (auto x : v) {\n auto [pos, index, health, dir] = x;\n while (!st.empty()) {\n auto [top_pos, top_index, top_health, top_dir] = st.top();\n if (top_dir == 'R' && dir == 'L') {\n st.pop(); \n if (top_health > health) {\n st.push(make_tuple(top_pos, top_index, top_health - 1, top_dir));\n health = 0; \n break;\n } else if (top_health < health) {\n health -= 1;\n } else { \n health = 0;\n break;\n }\n } else {\n break;\n }\n }\n\n\n if (health > 0) {\n st.push(make_tuple(pos, index, health, dir));\n }\n }\n \n vector<int> res;\n map<int, int> m;\n \n while (!st.empty()) {\n auto [top_pos, top_index, top_health, top_dir] = st.top();\n m[top_index] = top_health;\n st.pop();\n }\n\n for (auto x : m) {\n res.push_back(x.second);\n }\n \n return res;\n }\n};\n",
"memory": "234053"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = positions.size();\n vector<tuple<int,int,char>> v(n);\n for (int i = 0; i < n; i++){\n get<0>(v[i]) = positions[i];\n get<1>(v[i]) = healths[i];\n get<2>(v[i]) = directions[i];\n }\n sort(v.begin(),v.end());\n stack<pair<int,int>> s; // <health,position> vice versa will also work\n unordered_map<int,int> m; // <position,health>\n for (int i = 0; i < n; i++){\n int health = get<1>(v[i]), position = get<0>(v[i]);\n char direction = get<2>(v[i]);\n if (direction == 'R'){\n s.push({health,position});\n } else {\n bool cnd = true; // to check if robot survived\n while (!s.empty()){\n if (health > s.top().first){\n health--;\n s.pop();\n } else if (health == s.top().first){\n s.pop();\n cnd = false; // robot destroyed\n break;\n } else {\n s.top().first--;\n cnd = false; // robot destroyed\n break;\n }\n }\n if (cnd)m[position] = health; // robot survived\n }\n }\n while (!s.empty()){ // putting survived robots' health in map\n m[s.top().second] = s.top().first;\n s.pop();\n }\n vector<int> ans;\n for (int i = 0; i < n; i++){\n if (m[positions[i]]){ // if this positioned robot survived\n ans.push_back(m[positions[i]]);\n }\n };\n return ans;\n }\n};",
"memory": "235371"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = positions.size();\n vector<tuple<int,int,char>> v(n);\n for (int i = 0; i < n; i++){\n get<0>(v[i]) = positions[i];\n get<1>(v[i]) = healths[i];\n get<2>(v[i]) = directions[i];\n }\n sort(v.begin(),v.end());\n stack<pair<int,int>> s; // <health,position> vice versa will also work\n unordered_map<int,int> m; // <position,health>\n for (int i = 0; i < n; i++){\n int health = get<1>(v[i]), position = get<0>(v[i]);\n char direction = get<2>(v[i]);\n if (direction == 'R'){\n s.push({health,position});\n } else {\n bool cnd = true; // to check if robot survived\n while (!s.empty()){\n if (health > s.top().first){\n health--;\n s.pop();\n } else if (health == s.top().first){\n s.pop();\n cnd = false; // robot destroyed\n break;\n } else {\n s.top().first--;\n cnd = false; // robot destroyed\n break;\n }\n }\n if (cnd)m[position] = health; // robot survived\n }\n }\n while (!s.empty()){ // putting survived robots' health in map\n m[s.top().second] = s.top().first;\n s.pop();\n }\n vector<int> ans;\n for (int i = 0; i < n; i++){\n if (m[positions[i]]){ // if this positioned robot survived\n ans.push_back(m[positions[i]]);\n }\n };\n return ans;\n }\n};",
"memory": "235371"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = positions.size();\n vector<pair<int, pair<int, char>>> robots(n);\n for (int i = 0; i < n; ++i) {\n robots[i] = make_pair(positions[i], make_pair(healths[i], directions[i]));\n }\n\n sort(robots.begin(), robots.end());\n\n unordered_map<int, int> ans;\n stack<pair<int, int>> stk; \n for (int i = n - 1; i >= 0; --i) {\n int pos = robots[i].first;\n int hp = robots[i].second.first;\n char dir = robots[i].second.second;\n if (dir == 'R') {\n while (!stk.empty() && stk.top().second < hp) {\n --hp;\n stk.pop();\n }\n if (!stk.empty() && stk.top().second == hp) {\n hp = 0;\n stk.pop();\n }\n if (hp != 0 && !stk.empty() && stk.top().second > hp) {\n hp = 0;\n stk.top().second--;\n }\n ans[pos] = hp;\n } else {\n stk.push({pos, hp});\n }\n }\n\n while (!stk.empty()) {\n ans[stk.top().first] = stk.top().second;\n stk.pop();\n }\n vector<int> res;\n for (int i = 0; i < n; ++i) {\n int pos = positions[i];\n if (ans[pos]) {\n res.push_back(ans[pos]);\n }\n }\n return res;\n }\n};",
"memory": "236688"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = positions.size();\n vector<tuple<int, int, char>> robots(n);\n for (int i = 0; i < n; ++i) {\n robots[i] = {positions[i], healths[i], directions[i]};\n }\n\n sort(robots.begin(), robots.end());\n\n unordered_map<int, int> ans;\n stack<pair<int, int>> stk; \n for (int i = n - 1; i >= 0; --i) {\n int pos, hp;\n char dir;\n tie(pos, hp, dir) = robots[i];\n if (dir == 'R') {\n while (!stk.empty() && stk.top().second < hp) {\n --hp;\n stk.pop();\n }\n if (!stk.empty() && stk.top().second == hp) {\n hp = 0;\n stk.pop();\n }\n if (hp != 0 && !stk.empty() && stk.top().second > hp) {\n hp = 0;\n stk.top().second--;\n }\n ans[pos] = hp;\n } else {\n stk.push({pos, hp});\n }\n }\n\n while (!stk.empty()) {\n ans[stk.top().first] = stk.top().second;\n stk.pop();\n }\n vector<int> res;\n for (int i = 0; i < n; ++i) {\n int pos = positions[i];\n if (ans[pos]) {\n res.push_back(ans[pos]);\n }\n }\n return res;\n }\n};",
"memory": "236688"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n stack<pair<int, int>> st;\n int n = directions.size();\n vector<int> ans(n, -1);\n\n unordered_map<int, int> rev;\n for(int i = 0; i < n; i++){\n rev[positions[i]] = i;\n }\n\n vector<pair<int, pair<int, char>>> robots(n);\n for(int i = 0; i < n; i++){\n robots[i] = {positions[i], {healths[i], directions[i]}};\n }\n\n sort(robots.begin(), robots.end());\n\n for(int i = 0; i < n; i++){\n while(!st.empty() && robots[i].second.second == 'L'){\n int currIdx = st.top().first;\n int currHealth = st.top().second;\n if(robots[i].second.first > currHealth){\n st.pop();\n robots[i].second.first--;\n }\n else if(robots[i].second.first == currHealth){\n st.pop();\n ans[rev[robots[i].first]] = -2;\n break;\n }\n else{\n st.pop();\n st.push({currIdx, currHealth - 1});\n break;\n }\n }\n\n if(st.empty() && robots[i].second.second == 'L' && ans[rev[robots[i].first]] != -2) ans[rev[robots[i].first]] = robots[i].second.first;\n else if(robots[i].second.second == 'R') st.push({i, robots[i].second.first});\n\n }\n\n while(!st.empty()){\n ans[rev[robots[st.top().first].first]] = st.top().second;\n st.pop();\n }\n\n vector<int> finalAns;\n\n for(auto it : ans){\n if(it >= 0) finalAns.push_back(it);\n }\n\n return finalAns;\n\n }\n};",
"memory": "238006"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = positions.size();\n vector<int> idx;\n unordered_map<int, int> mp;\n for (int i = 0; i < n; i++) {\n idx.push_back(i);\n mp[positions[i]] = i;\n }\n sort(idx.begin(), idx.end(), [&](int x, int y){\n return positions[x] < positions[y];\n });\n stack<int> p_st;\n stack<int> h_st;\n stack<char> d_st;\n for (int i = 0; i < n; i++) {\n int id = idx[i];\n int p = positions[id];\n int h = healths[id];\n char d = directions[id];\n bool push = true;\n while (push && !p_st.empty() && d_st.top() == 'R' && d == 'L') {\n int pop_p = p_st.top();\n int pop_h = h_st.top();\n char pop_d = d_st.top();\n p_st.pop();\n h_st.pop();\n d_st.pop();\n if (pop_h == h) {\n push = false;\n } else if (pop_h > h) {\n push = false;\n p_st.push(pop_p);\n h_st.push(pop_h - 1);\n d_st.push(pop_d);\n } else {\n h--;\n if (h == 0) {\n push = false;\n }\n }\n }\n if (push) {\n p_st.push(p);\n h_st.push(h);\n d_st.push(d);\n }\n }\n vector<int> ans(n, 0);\n while (!p_st.empty()) {\n int p = p_st.top();\n int h = h_st.top();\n p_st.pop();\n h_st.pop();\n ans[mp[p]] = h;\n }\n ans.erase(remove(ans.begin(), ans.end(), 0), ans.end());\n return ans;\n }\n};",
"memory": "238006"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& pos, vector<int>& he, string dir) {\n int n=pos.size();\n if(n==1){\n return he;\n }\n int cr=0,cl=0;\n \n //pos,heal,dir\n vector<pair<int,pair<int,char>>> p(n);\n for(int i=0;i<n;i++){\n if(dir[i]=='L') cl++;\n else cr++;\n p[i]={pos[i],{he[i],dir[i]}};\n }\n sort(p.begin(),p.end());\n // for(auto x:p){\n // cout<<x.first<<\" : \"<<x.second.first<<\" : \"<<x.second.second<<endl;\n // }\n stack<pair<int,pair<int,char>>> st;\n st.push(p[0]);\n \n for(int i=1;i<n;i++){\n if(st.empty()) {st.push(p[i]); continue;}\n auto x=st.top();\n //col\n if(x.second.second=='R' && p[i].second.second=='L'){\n if(x.second.first>=p[i].second.first){\n st.pop();\n if(x.second.first==p[i].second.first){\n continue;\n }\n x.second.first--;\n if(x.second.first>0) st.push(x);\n }\n else{\n st.pop();\n p[i].second.first--;\n bool rem=1;\n pair<int,pair<int,char>> y;\n while(!st.empty() && p[i].second.first>0){\n y=st.top();\n // cout<<\"now cmp \"<<y.second.first<<\" w \"<<p[i].second.first<<endl;\n if(y.second.second=='R'){\n if(y.second.first>=p[i].second.first){\n // cout<<\"cmp: \"<<y.second.first<<\" w \"<<p[i].second.first<<endl;\n st.pop();\n rem=0;\n if(y.second.first==p[i].second.first) {rem=0; break;}\n y.second.first--;\n if(y.second.first>0) st.push(y);\n break;\n }\n else{\n st.pop();\n rem=1;\n p[i].second.first--;\n if(p[i].second.first<=0){rem=0; break;}\n }\n }\n else break;\n } \n if(rem && p[i].second.first>0){\n st.push(p[i]);\n }\n } \n }\n //same dir / opp dir\n else{\n st.push(p[i]);\n }\n }\n vector<int> ans;\n // cout<<\"aft\\n\";\n int siz=st.size();\n unordered_map<int,int> m;\n while(!st.empty()){\n auto x=st.top();\n // cout<<x.first<<\" : \"<<x.second.first<<\" : \"<<x.second.second<<endl;\n m[x.first]=x.second.first;\n st.pop();\n }\n for(int i=0;i<n;i++){\n if(m[pos[i]]!=0)\n ans.push_back(m[pos[i]]);\n }\n return ans;\n }\n};",
"memory": "239323"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& pos, vector<int>& he, string dir) {\n int n=pos.size();\n if(n==1){\n return he;\n }\n int cr=0,cl=0;\n \n //pos,heal,dir\n vector<pair<int,pair<int,char>>> p(n);\n for(int i=0;i<n;i++){\n if(dir[i]=='L') cl++;\n else cr++;\n p[i]={pos[i],{he[i],dir[i]}};\n }\n sort(p.begin(),p.end());\n // for(auto x:p){\n // cout<<x.first<<\" : \"<<x.second.first<<\" : \"<<x.second.second<<endl;\n // }\n stack<pair<int,pair<int,char>>> st;\n st.push(p[0]);\n \n for(int i=1;i<n;i++){\n if(st.empty()) {st.push(p[i]); continue;}\n auto x=st.top();\n //col\n if(x.second.second=='R' && p[i].second.second=='L'){\n if(x.second.first>=p[i].second.first){\n st.pop();\n if(x.second.first==p[i].second.first){\n continue;\n }\n x.second.first--;\n if(x.second.first>0) st.push(x);\n }\n else{\n st.pop();\n p[i].second.first--;\n bool rem=1;\n pair<int,pair<int,char>> y;\n while(!st.empty() && p[i].second.first>0){\n y=st.top();\n // cout<<\"now cmp \"<<y.second.first<<\" w \"<<p[i].second.first<<endl;\n if(y.second.second=='R'){\n if(y.second.first>=p[i].second.first){\n // cout<<\"cmp: \"<<y.second.first<<\" w \"<<p[i].second.first<<endl;\n st.pop();\n rem=0;\n if(y.second.first==p[i].second.first) {rem=0; break;}\n y.second.first--;\n if(y.second.first>0) st.push(y);\n break;\n }\n else{\n st.pop();\n rem=1;\n p[i].second.first--;\n if(p[i].second.first<=0){rem=0; break;}\n }\n }\n else break;\n } \n if(rem && p[i].second.first>0){\n st.push(p[i]);\n }\n } \n }\n //same dir / opp dir\n else{\n st.push(p[i]);\n }\n }\n vector<int> ans;\n // cout<<\"aft\\n\";\n int siz=st.size();\n unordered_map<int,int> m;\n while(!st.empty()){\n auto x=st.top();\n // cout<<x.first<<\" : \"<<x.second.first<<\" : \"<<x.second.second<<endl;\n m[x.first]=x.second.first;\n st.pop();\n }\n for(int i=0;i<n;i++){\n if(m[pos[i]]!=0)\n ans.push_back(m[pos[i]]);\n }\n return ans;\n }\n};",
"memory": "239323"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n bool static cust(vector<int>&a,vector<int>&b){\n return a[1]<b[1];\n }\n vector<int> survivedRobotsHealths(vector<int>& l, vector<int>& h, string d) {\n int n=l.size();\n stack<vector<int>>st;\n vector<pair<int,int>>l1;\n for(int i=0;i<n;i++)l1.push_back({l[i],i});\n sort(l1.begin(),l1.end());\n vector<vector<int>>ans;\n for(int i=0;i<n;i++){\n int ind=l1[i].second;\n int loc=l1[i].first;\n \n if(d[ind]=='R')st.push({h[ind],ind});\n else{\n int val=h[ind];\n while(st.size()&&val>0){\n if(val==st.top()[0])val=0,st.pop();\n else if(val>st.top()[0])val--,st.pop();\n else{\n int temp=st.top()[0];\n int x=st.top()[1];\n st.pop();st.push({temp-1,x});\n val=0;\n } \n }\n if(val!=0)ans.push_back({val,ind});\n }\n }\n while(st.size())ans.push_back(st.top()),st.pop();\n sort(ans.begin(),ans.end(),cust);\n vector<int>a1;\n for(int i=0;i<ans.size();i++)a1.push_back(ans[i][0]);\n return a1;\n }\n};",
"memory": "240641"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& posn, vector<int>& health, string dirn) {\n unordered_map<int,int>index;\n int n=posn.size();\n for(int i=0;i<n;i++){\n index[posn[i]]=i;\n }//keeping track of original index with respect to position\n sort(posn.begin(),posn.end());\n vector<int>newHealth;\n vector<bool> newDirn;\n\n for(int i=0;i<n;i++){\n int temp=posn[i];\n int ind=index[temp];\n newHealth.push_back(health[ind]);\n if(dirn[ind]=='L')newDirn.push_back(1);\n else newDirn.push_back(0);\n }//making rearraging health and direction according to the new sorted position array\n\n stack<pair<int,int>>s;\n s.push({0,newHealth[0]});\n for(int i=1;i<n;i++){\n int h=newHealth[i];\n bool disha=newDirn[i];\n int index=i;\n if(s.empty()){//if stack is empty then no collision can happen\n s.push({index,h});\n continue;\n }\n int prevH=s.top().second;\n int prevIndex=s.top().first;\n bool prevDisha=newDirn[prevIndex];\n\n if(prevDisha==disha || (prevDisha==1 && disha==0)){//if travelling in same diection or opposite direction but in outward direction the no collision\n //no collision\n //cout<<\"No collition between \"<<index<<\" and \"<<prevIndex<<endl;\n s.push({index,h});\n continue;\n }\n else{\n //collision\n bool flag=false;\n while(!s.empty() && disha==1 && prevDisha==0){//jab tk stack mei koi bhi robot aisa h jo ki collision kr skta h ya phir stack empty nhi hota\n s.pop();\n flag=false;//ek case h jisme agr dono ki health same h to koi bhi nya push nhi krna sirf pop krna h last robot ko , to yeh track rkhne ke liye ki humme koi push krna h ye nhi iske liye flag use kra h \n //cout<<\"collision occured \"<<h<<\" \"<<prevH<<\" \";\n if(h==prevH)break;//no pushing\n\n else if(h>prevH && h-1>0){\n h--;//current robot strong h , aur uski health minus krke usse hi maintain krna h current pr\n flag=true;//pushing\n if(!s.empty()){//previous robot update krdo agr stack empty nhi h to\n prevIndex=s.top().first;\n prevH=s.top().second;\n prevDisha=newDirn[prevIndex];\n }\n }\n\n else{//previous robot strong h, aise mei previous robot ko current robot ke stats se replace krdo kyunki humme previous robot ki health -1 krke waps push krne h \n flag=true;//pushing\n h=prevH-1;\n disha=prevDisha;\n index=prevIndex;\n break;\n }\n }\n //cout<<\"pushing \"<<index<<endl;\n if(flag)s.push({index,h});\n } \n }\n vector<pair<int,int>>ans;\n while(!s.empty()){\n int ind=s.top().first;//new index of robot\n int key=posn[ind];//finding position of robot according to new index\n int originalIndex=index[key];//finding original index according to its index, mapped at the begining\n ans.push_back({originalIndex,s.top().second});\n s.pop();\n }\n sort(ans.begin(),ans.end());//sorting according to original index\n vector<int>result;\n for(auto i:ans){//storing updated health\n result.push_back(i.second);\n }\n return result;\n \n\n\n }\n};",
"memory": "240641"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class val{\n public:\n int pos;\n int heal;\n char dir;\n};\n\n\nclass Solution {\n\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n=healths.size();\n stack<val> s;\n map<int,int>m;\n vector<int>ans;\n vector<val> k(n);\n for(int i=0;i<n;i++){\n k[i].pos=positions[i];\n k[i].heal=healths[i];\n k[i].dir=directions[i];\n }\n sort(k.begin(),k.end(),[](val a,val b){\n return a.pos<b.pos;\n });\n for(int i=0;i<n;i++){\n if(!s.empty() && k[i].dir=='L' && s.top().dir=='R'){\n if(k[i].heal == s.top().heal){\n s.pop();\n }\n else{\n if(k[i].heal > s.top().heal){\n s.pop();\n k[i].heal--;\n i--;\n }\n else{\n s.top().heal--;\n }\n }\n }\n else{\n s.push(k[i]);\n }\n }\n while(!s.empty()){\n m[s.top().pos]=s.top().heal;\n s.pop();\n }\n for(int i=0;i<n;i++){\n if(m[positions[i]])\n ans.push_back(m[positions[i]]);\n }\n return ans;\n\n\n\n }\n};",
"memory": "241958"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n ios_base::sync_with_stdio(false); \n cin.tie(nullptr); \n cout.tie(nullptr);\n struct Robot {\n int pos;\n int hp;\n char heading;\n };\n vector<Robot> info;\n\n for(int i = 0; i < positions.size(); i++){\n info.push_back({positions[i], healths[i], directions[i]});\n }\n sort(info.begin(), info.end(), [](const Robot& a, const Robot& b)\n { return a.pos < b.pos;});\n //extract the position-ordered direction sequence\n for(int i = 0; i < directions.length(); i++){\n directions[i] = info[i].heading;\n }\n // for(auto& iter : info){\n // cout << \"pos:\" << iter.pos << \" health:\" << iter.hp << \" dir: \" << iter.heading << endl;\n // }\n //greedy search for collision points\n int head = 1;\n int tail = 0;\n int lastSurv = 0;\n while(head < directions.length()){\n if((directions[tail] == 'R') && (directions[head] == 'L')){\n if(info[tail].hp == info[head].hp){\n info[head].hp = 0;\n info[tail].hp = 0;\n while(tail >= 0 && info[tail].hp == 0){\n tail--;\n }\n if(tail == -1){\n tail = ++head;\n } \n head++;\n } else if (info[tail].hp < info[head].hp){\n info[head].hp--;\n info[tail].hp = 0;\n while(tail >= 0 && info[tail].hp == 0){\n tail--;\n }\n if(tail == -1){\n tail = ++head;\n } \n }else{\n info[head].hp = 0;\n info[tail].hp--;\n head++;\n }\n }else{\n tail = head;\n head++;\n }\n }\n //form a hash table\n unordered_map<int, int> pos_hp;\n for(auto& iter : info){\n pos_hp[iter.pos] = iter.hp;\n }\n int count = 0;\n for(auto& iter : positions){\n if(pos_hp[iter] != 0){\n healths[count] = pos_hp[iter];\n count++;\n }\n }\n healths.erase(healths.begin()+count, healths.end());\n\n\n return healths;\n \n }\n};",
"memory": "241958"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = healths.size();\n map<int,int> mp;\n for(int i=0;i<n;i++)\n {\n mp[positions[i]] = i+1;\n }\n vector<pair<int,pair<int,char> > > robot(n);\n for(int i=0;i<n;i++)\n {\n robot[i] = {positions[i],{healths[i],directions[i]}};\n }\n sort(robot.begin(),robot.end());\n vector<pair<int,int> > survive;\n stack<pair<int,pair<int,char> > > st;\n int idx = 0;\n while(idx < n)\n {\n int pos = robot[idx].first;\n int health = robot[idx].second.first;\n char dir = robot[idx].second.second;\n if(dir == 'L')\n {\n if(st.empty())\n {\n survive.push_back({pos,health});\n idx++;\n }\n else\n {\n while(!st.empty() && health>st.top().second.first)\n {\n st.pop();\n health--;\n }\n if(st.empty())\n {\n survive.push_back({pos,health});\n idx++;\n }\n else if(health==st.top().second.first)\n {\n st.pop();\n idx++;\n }\n else\n {\n st.top().second.first--;\n idx++;\n }\n }\n }\n else\n {\n st.push(robot[idx]);\n idx++;\n }\n }\n while(!st.empty())\n {\n int pos = st.top().first;\n int health = st.top().second.first;\n survive.push_back({pos,health});\n st.pop();\n }\n for(int i=0;i<survive.size();i++)\n {\n int pos = survive[i].first;\n survive[i].first = mp[pos];\n }\n sort(survive.begin(),survive.end());\n vector<int> result;\n for(int i=0;i<survive.size();i++)\n {\n result.push_back(survive[i].second);\n }\n return result;\n }\n};",
"memory": "243276"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = positions.size();\n for(int i=0;i<n;i++)\n {\n if(directions[i]=='L') healths[i]=-healths[i];\n }\n vector<pair<int,int>>v;\n unordered_map<int,int>f;\n for(int i=0;i<n;i++)\n {\n v.push_back({positions[i],healths[i]});\n f[positions[i]]=i;\n }\n sort(v.begin(),v.end());\n stack<int>st,st1;\n // st.push({v[n-1].second,v[n-1].first});\n st.push(v[n-1].second);\n st1.push(v[n-1].first);\n for(int i=n-2;i>=0;i--)\n {\n int flag = 0;\n // cout<<st.size()<<endl;\n // cout<<st1.size()<<endl;\n while(1)\n {\n if(st.empty())\n {\n st.push(v[i].second);\n st1.push(v[i].first);\n break;\n }\n if(v[i].second>0)\n {\n if(st.top()>0)\n {\n // st.push({v[i].second,v[i].first});\n st.push(v[i].second);\n // cout<<v[i].second<<\" \"<<v[i].first<<endl;\n st1.push(v[i].first);\n flag=0;\n break;\n }\n else\n {\n if(v[i].second>abs(st.top()))\n {\n st.pop();\n st1.pop();\n v[i].second-=1;\n flag=1;\n continue;\n }\n else if(v[i].second<abs(st.top()))\n {\n int x = abs(st.top());\n // int ind = st.top().second;\n st.pop();\n // st.push({-(x-1),ind});\n st.push(-(x-1));\n flag=0;\n break;\n }\n else\n {\n st.pop();\n st1.pop();\n flag=0;\n break;\n }\n }\n }\n else\n {\n if(st.top()<0)\n {\n // st.push({v[i].second,v[i].first});\n st.push(v[i].second);\n st1.push(v[i].first);\n flag=0;\n break;\n }\n else\n {\n // st.push({v[i].second,v[i].first});\n st.push(v[i].second);\n st1.push(v[i].first);\n flag=0;\n break;\n }\n }\n }\n if(flag==1)\n {\n // st.push({v[i].second,v[i].first});\n st.push(v[i].second);\n st1.push(v[i].first);\n }\n }\n // cout<<st.size()<<endl;\n // cout<<st1.size()<<endl;\n vector<pair<int,int>>temp;\n vector<int>ans1(n,0);\n while(!st.empty())\n {\n int x = st.top();\n int ind1 = st1.top();\n // cout<<x<<\" \"<<ind1<<endl;\n int id = f[ind1];\n ans1[id]=x;\n st.pop();\n st1.pop();\n }\n vector<int>ans;\n for(int i=0;i<n;i++)\n {\n if(ans1[i]!=0) ans.push_back(abs(ans1[i]));\n }\n return ans;\n return ans;\n }\n};",
"memory": "243276"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& position, vector<int>& health, string direction) {\n map<int, pair<int,char>> m;\n for(int i=0;i<position.size();i++)\n m[position[i]] = {health[i],direction[i]};\n\n stack<int> l,r;\n set<int> pos;\n\n for(auto it:m){\n int ch = it.second.first;\n int cp = it.first;\n\n if(it.second.second == 'L'){\n while(!r.empty() && ch){\n int rh = m[r.top()].first;\n if(rh<ch){\n r.pop();\n ch--;\n }\n else if(rh>ch){\n m[r.top()] = {rh-1,'R'};\n ch=0;\n }\n else{\n r.pop();\n ch = 0;\n }\n }\n m[cp] = {ch,'L'};\n if(ch)\n pos.insert(it.first);\n }\n else\n r.push(it.first);\n }\n\n while(!r.empty()){\n pos.insert(r.top());\n r.pop();\n }\n\n vector<int> ans;\n\n for(int i=0;i<position.size();i++){\n if(pos.find(position[i]) != pos.end()){\n ans.push_back(m[position[i]].first);\n }\n }\n\n return ans;\n }\n};",
"memory": "244593"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n map<int,int> robotToPosition;\n int n = positions.size();\n for (int i=0;i<n;i++)\n {\n robotToPosition[positions[i]]=i;\n }\n\n vector<pair<int,int>> positionHealth;\n\n for(int i=0;i<n;i++)\n { if(directions[i]=='L')\n positionHealth.push_back(make_pair(positions[i],-healths[i]));\n else\n positionHealth.push_back(make_pair(positions[i],healths[i]));\n }\n sort(positionHealth.begin(),positionHealth.end());\n stack<pair<int,int>> health1;\n int counter=0;\n vector<int> ans(n);\n for (int i=0;i<n;i++)\n {\n if(positionHealth[i].second>0)\n {\n health1.push(positionHealth[i]);\n }\n else\n {\n int val1 = -positionHealth[i].second;\n while(!health1.empty() && val1>0)\n {\n int topVal = health1.top().second;\n if(topVal<val1)\n {\n health1.pop();\n val1-=1;\n }\n else if(topVal==val1)\n {\n health1.pop();\n val1=0;\n break;\n }\n else\n {\n pair<int,int> temp=health1.top();\n health1.pop();\n temp.second-=1;\n health1.push(temp);\n val1=0;\n break;\n }\n }\n if(val1>0)\n {\n ans[robotToPosition[positionHealth[i].first]]=val1;\n }\n // counter++;\n }\n }\n while(!health1.empty())\n {\n auto temp12 = health1.top();\n health1.pop();\n ans[robotToPosition[temp12.first]]=temp12.second;\n }\n vector<int> finalans;\n for(int it:ans)\n if (it>0)\n finalans.push_back(it);\n return finalans;\n\n }\n};",
"memory": "244593"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n = positions.size();\n vector<pair<int, pair<int, char>>> robots; \n\n for (int i = 0; i < n; i++) {\n robots.push_back({positions[i], {healths[i], directions[i]}});\n } \n sort(robots.begin(), robots.end());\n stack<int> st; \n for (int i = 0; i < n; i++) {\n if (robots[i].second.second == 'R') {\n st.push(i);\n } else {\n while (!st.empty()) {\n if (robots[st.top()].second.first == robots[i].second.first){ \n robots[st.top()].first = -1;\n robots[i].first = -1;\n st.pop();\n break;\n } else if (robots[st.top()].second.first < robots[i].second.first) {\n robots[st.top()].first = -1;\n robots[i].second.first--;\n st.pop();\n } else {\n robots[i].first = -1;\n robots[st.top()].second.first--;\n break;\n }\n }\n }\n }\n\n unordered_map<int, int> mpp;\n vector<int> result;\n for (int i = 0; i < n; i++) {\n if (robots[i].first != -1) {\n mpp[robots[i].first] += robots[i].second.first;\n }\n }\n for (int i = 0; i < n; i++) {\n if (mpp[positions[i]]) {\n result.push_back(mpp[positions[i]]);\n }\n }\n return result;\n \n }\n};\n\n/*\n---------------------without sorting but fails to implement----------------------\n\n vector<int> v;\n for(int i = 0; i < directions.length(); i++){\n if(directions[i]=='R') v.push_back(positions[i]);\n else{\n int x = -1 * positions[i];\n v.push_back(x);\n }\n }\n\n vector<pair<int,int>> p;\n for(int i = 0; i < v.size(); i++){\n p.push_back({v[i],i});\n }\n\n stack<pair<int,int>> s;\n for(int i = 0; i < p.size(); i++){\n\n if(p[i].first > 0) s.push(p[i]);\n\n else{\n while(!s.empty() && healths[i]>0){\n int val = s.top().first;\n int j = s.top().second;\n s.pop();\n if(healths[i]==healths[i]){\n healths[i]=0;\n healths[j]=0;\n }\n else if(healths[i]>healths[j]){\n healths[i]-=1;\n healths[j]=0;\n }\n else{\n healths[i]=0;\n healths[j]-=1;\n s.push({val,j});\n }\n }\n }\n }\n\n vector<int> ans;\n for (int i = 0; i < positions.size(); i++) {\n if (healths[i] > 0) {\n ans.push_back(healths[i]);\n }\n }\n // reverse(ans.begin(),ans.end());\n\n return ans;\n*/",
"memory": "245911"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<pair<int,int>> rel;\n unordered_map<int,int> pos;\n for(int i=0; i< positions.size(); i++){\n rel.push_back({positions[i],i});\n pos[positions[i]] = i;\n }\n sort(rel.begin(), rel.end());\n stack<pair<int,int>> st1;\n stack<pair<int,int>> st2;\n for(auto val: rel) {\n if(directions[val.second] == 'R') {\n st1.push({healths[val.second],val.first});\n }\n if(directions[val.second] == 'L'){\n st2.push({healths[val.second], val.first});\n }\n while(!st1.empty() && !st2.empty() && st1.top().second < st2.top().second){\n if(st1.top().first > st2.top().first){\n st1.top().first = st1.top().first-1;\n st2.pop();\n }else if(st1.top().first < st2.top().first){\n st2.top().first = st2.top().first -1;\n st1.pop();\n }else{\n st1.pop();\n st2.pop();\n }\n }\n }\n vector<pair<int,int>> res;\n while(!st1.empty()){\n res.push_back({pos[st1.top().second],st1.top().first});\n st1.pop();\n }\n while(!st2.empty()){\n res.push_back({pos[st2.top().second],st2.top().first});\n st2.pop();\n }\n sort(res.begin(),res.end());\n vector<int> ans;\n for(auto x: res){\n ans.push_back(x.second);\n }\n return ans;\n\n }\n};",
"memory": "245911"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int n=positions.size();\n map<int,pair<int,char>> mp;\n for(int i=0;i<n;i++)\n {\n mp[positions[i]].first=healths[i];\n mp[positions[i]].second=directions[i];\n }\n\n stack<pair<int,pair<int,char>>> st;\n for(auto it:mp)\n {\n if(!st.empty()&&it.second.second=='L')\n {\n if(st.top().second.second=='R')\n {\n if(it.second.first<st.top().second.first)\n {\n st.top().second.first-=1;\n continue;\n }\n else if(st.top().second.first<it.second.first)\n {\n while(!st.empty()&&it.second.first>st.top().second.first &&st.top().second.second=='R')\n {\n it.second.first-=1;\n st.pop();\n }\n if(!st.empty()&&it.second.first==st.top().second.first && st.top().second.second=='R')\n {\n st.pop();\n continue;\n }\n\n if(!st.empty()&&it.second.first<st.top().second.first && st.top().second.second=='R')\n {\n st.top().second.first-=1;\n continue;\n }\n\n }\n\n else\n {\n st.pop();\n continue;\n }\n }\n }\n st.push(it);\n }\n\n vector<int> res;\n unordered_map<int,int> mp1;\n while(!st.empty())\n {\n mp1[st.top().first]=st.top().second.first;\n st.pop();\n }\n\n for(int i=0;i<n;i++)\n {\n if(mp1.find(positions[i])!=mp1.end())\n {\n res.push_back(mp1[positions[i]]);\n }\n }\n\n return res;\n }\n};",
"memory": "247228"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n stack<int> st;\n vector<vector<int>> v;\n for(int i=0;i<positions.size();i++){\n v.push_back({positions[i],healths[i],directions[i],i});\n }\n sort(v.begin(),v.end());\n for(int k=0;k<v.size();k++){\n int i=v[k][3];\n if(directions[i]=='R'){\n st.push(i);\n }\n else if(directions[i]=='L'){\n if(st.empty()){\n st.push(i);\n }\n else{\n bool m=1;\n while(!st.empty() && directions[st.top()]=='R'){\n int v1=st.top();\n if(healths[v1]<healths[i]){\n st.pop();\n healths[i]--;\n m=1;\n }\n else if(healths[v1]>healths[i]){\n st.pop();\n healths[v1]--;\n st.push(v1);\n m=0;\n break;\n }\n else{\n st.pop();\n m=0;\n break;\n }\n }\n if(m==1){\n st.push(i);\n }\n }\n }\n }\n vector<int> ans;\n while(!st.empty()){\n int a=st.top();\n ans.push_back(a);\n st.pop();\n }\n sort(ans.begin(),ans.end());\n for(int i=0;i<ans.size();i++){\n ans[i]=healths[ans[i]];\n }\n return ans;\n }\n};",
"memory": "247228"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 3 | {
"code": "#define ll int\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n ll n = directions.size();\n map<ll, ll> m; // {indx, health}\n stack<pair<ll, ll>> stk;\n vector<tuple<ll, ll, char>> v;\n for(ll i=0; i<n; i++)\n {\n v.push_back(make_tuple(positions[i], healths[i], directions[i]));\n }\n sort(v.begin(), v.end());\n for(auto it : v)\n {\n ll a, b, c;\n tie(a, b, c) = it;\n ll cur = b;\n if(c == 'L')\n {\n while(!stk.empty())\n {\n if(stk.top().first == b)\n {\n stk.pop();\n b = 0;\n break;\n }\n else if(stk.top().first > b)\n {\n auto pr = stk.top();\n stk.pop();\n pr.first--;\n stk.push(pr);\n b = 0;\n break;\n }\n else if(stk.top().first < b)\n {\n stk.pop();\n b--;\n }\n }\n if(b > 0)\n {\n m[a] = b;\n }\n }\n else\n {\n stk.push(make_pair(b, a));\n }\n }\n while(!stk.empty())\n {\n m[stk.top().second] = stk.top().first;\n stk.pop();\n }\n vector<ll> ans;\n for(ll i=0; i<n; i++)\n {\n if(m[positions[i]])\n {\n ans.push_back(m[positions[i]]);\n }\n }\n return ans;\n }\n};",
"memory": "248546"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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<tuple<int, int, char>> robots;\n stack<int> stk;\n unordered_map<int, int> umap;\n vector<pair<int, int>> robotPositions;\n bool keepRobot;\n int numRobotsRemaining;\n\n for (int i = 0; i < positions.size(); i++){\n umap[positions[i]] = i;\n robots.push_back({positions[i], healths[i], directions[i]});\n }\n \n sort(robots.begin(), robots.end());\n \n stk.push(0);\n \n for (int i = 1; i < robots.size(); i++){\n if (!stk.empty() && (get<2>(robots[stk.top()]) == 'R') && (get<2>(robots[i]) == 'L')){\n keepRobot = false;\n\n while (!stk.empty() && (get<2>(robots[stk.top()]) == 'R') && (get<2>(robots[i]) == 'L')){\n if (get<1>(robots[stk.top()]) == get<1>(robots[i])){\n stk.pop();\n keepRobot = false;\n break;\n } else if (get<1>(robots[stk.top()]) < get<1>(robots[i])){\n stk.pop();\n get<1>(robots[i])--;\n keepRobot = true;\n } else {\n get<1>(robots[stk.top()])--;\n keepRobot = false;\n break;\n }\n }\n\n if (keepRobot)\n stk.push(i);\n } else\n stk.push(i);\n }\n\n if (stk.empty())\n return {};\n \n numRobotsRemaining = stk.size();\n \n for (int i = 0; i < numRobotsRemaining; i++){\n robotPositions.push_back({umap[get<0>(robots[stk.top()])], get<1>(robots[stk.top()])});\n stk.pop();\n }\n\n sort(robotPositions.begin(), robotPositions.end());\n\n vector<int> result(robotPositions.size());\n\n for (int i = 0; i < robotPositions.size(); i++)\n result[i] = robotPositions[i].second;\n \n return result;\n }\n};",
"memory": "248546"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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 vector<vector<int>> vec;\n for(int i=0; i<positions.size(); i++)\n {\n vec.push_back({positions[i], healths[i], directions[i], i});\n }\n sort(vec.begin(), vec.end());\n \n \n stack<int> st;\n for(int i=0; i<vec.size(); i++)\n {\n if(vec[i][2] == 'R')\n {\n st.push(i);\n continue;\n }\n\n bool flag = false;\n while(!st.empty() && vec[st.top()][2] == 'R')\n {\n if(vec[st.top()][1] == vec[i][1])\n {\n vec[st.top()][1] = -1;\n vec[i][1] = -1;\n st.pop();\n flag = true;\n break;\n }\n else if(vec[i][1] > vec[st.top()][1])\n {\n vec[i][1] -=1;\n vec[st.top()][1] = -1;\n st.pop();\n }\n else\n {\n vec[st.top()][1] -=1;\n vec[i][1] = -1;\n flag = true;\n break;\n } \n }\n if(flag) continue;\n st.push(i);\n }\n\n vector<int>ans;\n vector<int> newhealth(healths.size());\n for(int i=0; i<vec.size(); i++)\n {\n newhealth[vec[i][3]] = vec[i][1];\n }\n for(int i=0; i<newhealth.size(); i++)\n {\n if(newhealth[i] != -1) ans.push_back(newhealth[i]);\n }\n return ans;\n }\n};",
"memory": "249863"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 3 | {
"code": "#include<bits/stdc++.h>\ntypedef long long ll;\n \n#define rep(i,a,b) for(ll i=a;i<b;i++)\n#define revrep(i,a,b) for(ll i=a;i>=b;i--)\n#define yes cout << \"YES\" << endl;\n#define no cout << \"NO\" << endl;\n#define vi vector<ll>\n#define pb push_back\n#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)\n#define num(a) ll a;cin>>a\nusing namespace std;\nclass Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n vector<int>o;\n map<ll,ll>m;\n stack<pair<ll,pair<char,ll>>>s;\n int ind=0;\n vector<pair<ll,pair<char,ll>>>v;\n for(auto value:positions){\n v.pb({value,{directions[ind],healths[ind]}});\n ind++;\n }\n sort(v.begin(),v.end());\n for(auto value:v){\n if(value.second.first=='R'){\n s.push({value.first,{value.second.first,value.second.second}});\n }\n else{\n ll flag=0;\n ll ok=value.second.second;\n while(true){\n if(s.empty()){\n break;\n }\n if(s.top().second.first=='L'){\n break;\n }\n if(s.top().second.second<ok){\n s.pop();\n ok--;\n continue;\n }\n if(s.top().second.second==ok){\n s.pop();\n flag=1;\n break;\n }\n if(s.top().second.second>ok){\n ll on=s.top().second.second;\n ll bn=s.top().first;\n ll check=s.top().second.first;\n s.pop();\n on--;\n s.push({bn,{check,on}});\n flag=1;\n break;\n\n }\n }\n if(!flag){\n s.push({value.first,{value.second.first,ok}});\n }\n }\n }\n while(!s.empty()){\n m[s.top().first]=s.top().second.second;\n s.pop();\n\n }\n for(auto value:positions){\n if(m.find(value)!=m.end()){\n o.pb(m[value]);\n }\n }\n return o;\n }\n};",
"memory": "249863"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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 deque<pair<int, int>> st;\n vector<int> ans;\n vector<vector<int>> v;\n vector<pair<int, int>> temp;\n for (int i = 0; i < positions.size(); i++) {\n v.push_back({positions[i], healths[i], directions[i], i});\n }\n\n sort(v.begin(), v.end());\n for (auto& vec : v) {\n if (vec[2] == 'R') {\n st.push_back({vec[3], vec[1]});\n continue;\n }\n\n int curr = vec[1];\n while (st.size() and st.back().second < curr) {\n curr--;\n st.pop_back();\n }\n\n if (st.size() and st.back().second == curr) {\n st.pop_back();\n continue;\n }\n\n if (st.size() and st.back().second > curr) {\n st.back().second--;\n continue;\n }\n\n temp.push_back({vec[3], curr});\n }\n\n while (st.size()) {\n temp.push_back(st.front());\n st.pop_front();\n }\n\n sort(temp.begin(), temp.end());\n for (auto& it : temp)\n ans.push_back(it.second);\n return ans;\n }\n};",
"memory": "251181"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& p, vector<int>& h, string d) {\n stack<int> st;\n int n = p.size();\n vector<pair<int, pair<int, char>>> com;\n map<int,int>mp;\n for (int i = 0; i < n; i++) {\n mp[p[i]] = i;\n com.push_back({p[i], {h[i], d[i]}});\n }\n sort(com.begin(), com.end());\n for (int i = 0; i < n; i++) {\n while (!st.empty() && com[i].second.second == 'L' && com[i].second.first > 0) {\n if (com[st.top()].second.first < com[i].second.first) {\n com[st.top()].second.first = 0;\n st.pop();\n com[i].second.first--;\n } else {\n if (com[st.top()].second.first == com[i].second.first) {\n com[st.top()].second.first = 0;\n com[i].second.first = 0;\n st.pop();\n } else {\n com[st.top()].second.first--;\n com[i].second.first = 0;\n }\n break;\n }\n }\n if (com[i].second.second == 'R') {\n st.push(i);\n }\n }\n vector<int> ans;\n vector<pair<int,int>>helper;\n for (auto it : com) {\n if (it.second.first > 0) {\n helper.push_back({mp[it.first],it.second.first});\n }\n }\n sort(helper.begin(),helper.end());\n for(int i=0;i<helper.size();i++){\n ans.push_back(helper[i].second);\n }\n return ans;\n }\n};",
"memory": "251181"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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 ios_base::sync_with_stdio(false); \n cin.tie(nullptr); \n cout.tie(nullptr);\n struct Robot {\n int pos;\n int hp;\n char heading;\n };\n vector<Robot> info;\n\n for(int i = 0; i < positions.size(); i++){\n info.push_back({positions[i], healths[i], directions[i]});\n }\n sort(info.begin(), info.end(), [](const Robot& a, const Robot& b)\n { return a.pos < b.pos;});\n \n //greedy search for collision points\n stack<Robot> survivors;\n survivors.push(info[0]);\n int pointer = 1;\n while(pointer < info.size()){\n if((survivors.top().heading == 'R') && (info[pointer].heading == 'L')){\n if(survivors.top().hp == info[pointer].hp){\n survivors.pop();\n pointer++;\n } else if (survivors.top().hp < info[pointer].hp){\n survivors.pop();\n pointer = --info[pointer].hp ? pointer : ++pointer;\n } else {\n if(--survivors.top().hp == 0){\n survivors.pop();\n }\n pointer++;\n }\n } else {\n survivors.push(info[pointer]);\n pointer++;\n }\n if(survivors.empty() && pointer < info.size()){\n survivors.push(info[pointer]);\n pointer++;\n }\n }\n unordered_map<int, int> origPos;\n origPos.reserve(positions.size());\n for(int i = 0; i < positions.size(); i++){\n origPos[positions[i]] = i;\n }\n \n vector<vector<int>> posOrder;\n\n while(!survivors.empty()){\n posOrder.push_back({origPos[survivors.top().pos], survivors.top().hp});\n survivors.pop();\n }\n sort(posOrder.begin(), posOrder.end());\n vector<int> result;\n for(auto& iter : posOrder){\n result.push_back(iter[1]);\n }\n return result;\n \n }\n};",
"memory": "257768"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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 ios_base::sync_with_stdio(false); \n cin.tie(nullptr); \n cout.tie(nullptr);\n struct Robot {\n int pos;\n int hp;\n char heading;\n };\n vector<Robot> info;\n\n for(int i = 0; i < positions.size(); i++){\n info.push_back({positions[i], healths[i], directions[i]});\n }\n sort(info.begin(), info.end(), [](const Robot& a, const Robot& b)\n { return a.pos < b.pos;});\n \n //greedy search for collision points\n stack<Robot> survivors;\n survivors.push(info[0]);\n int pointer = 1;\n while(pointer < info.size()){\n if((survivors.top().heading == 'R') && (info[pointer].heading == 'L')){\n if(survivors.top().hp == info[pointer].hp){\n survivors.pop();\n pointer++;\n } else if (survivors.top().hp < info[pointer].hp){\n survivors.pop();\n pointer = --info[pointer].hp ? pointer : ++pointer;\n } else {\n if(--survivors.top().hp == 0){\n survivors.pop();\n }\n pointer++;\n }\n } else {\n survivors.push(info[pointer]);\n pointer++;\n }\n if(survivors.empty() && pointer < info.size()){\n survivors.push(info[pointer]);\n pointer++;\n }\n }\n unordered_map<int, int> origPos;\n origPos.reserve(positions.size());\n for(int i = 0; i < positions.size(); i++){\n origPos[positions[i]] = i;\n }\n \n vector<vector<int>> posOrder;\n\n while(!survivors.empty()){\n posOrder.push_back({origPos[survivors.top().pos], survivors.top().hp});\n survivors.pop();\n }\n sort(posOrder.begin(), posOrder.end());\n vector<int> result;\n for(auto& iter : posOrder){\n result.push_back(iter[1]);\n }\n return result;\n \n }\n};",
"memory": "257768"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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(positions.size(), vector<int>(4));\n for(int i=0;i<positions.size();i++){\n v[i][0]=positions[i];\n }\n for(int i=0;i<healths.size();i++){\n v[i][1]=healths[i];\n }\n for(int i=0;i<directions.size();i++){\n if(directions[i]=='R'){\n v[i][2]=1;\n }\n else v[i][2]=0;\n v[i][3]=i+1;\n }\n\n sort(v.begin(), v.end());\n stack<vector<int>>st;\n for(int i=0;i<positions.size();i++){\n cout << v[i][2];\n if(st.size()!=0 and st.top()[0]==1 and v[i][2]==0){\n if(v[st.top()[1]][1]>v[i][1]){\n v[st.top()[1]][1]-=1;\n v[i][1]=0;\n }\n else if(v[st.top()[1]][1]<v[i][1]){\n while(!st.empty()){\n if(st.top()[0]==1 and v[i][2]==0){\n if(v[st.top()[1]][1]>v[i][1]){\n v[st.top()[1]][1]-=1;\n v[i][1]=0;\n break;\n }else if(v[st.top()[1]][1]<v[i][1]){\n v[st.top()[1]][1]=0;\n v[i][1]-=1;\n st.pop();\n }\n else{\n v[st.top()[1]][1]=0;\n v[i][1]=0;\n st.pop();\n break;\n }\n }\n else{\n break;\n }\n }\n\n }\n else{\n v[st.top()[1]][1]=0;\n v[i][1]=0;\n st.pop();\n\n }\n \n\n }\n else{\n st.push({v[i][2], i});\n }\n }\n map<int, int>mp;\n for(int i=0;i<positions.size();i++)\n {\n if(v[i][1]!=0)\n mp[v[i][3]]=v[i][1];\n }\n vector<int>ans;\n for(auto m:mp)\n {\n ans.push_back(m.second);\n }\n\n return ans;\n\n \n }\n};",
"memory": "259086"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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>> data;\n unordered_map<int, int> ans;\n stack<pair<int, int>> s;\n int i = 0;\n int j = healths.size()-1;\n for(int k=i; k<=j; k++){\n int d;\n if(directions[k] == 'L') d = 0;\n else d = 1;\n data.push_back({positions[k], healths[k], d});\n }\n sort(data.begin(), data.end());\n for(int k=i; k<=j; k++){\n if(data[k][2] == 0){\n if(s.size()>0){\n while(s.size() > 0 && data[k][1] > 0){\n auto at = s.top();\n int pos = at.first;\n int heal = at.second;\n s.pop();\n if(data[k][1] > heal){\n data[k][1]--;\n }\n else if(data[k][1] < heal){\n heal--;\n s.push({pos, heal});\n data[k][1] = 0;\n }\n else{\n data[k][1] = 0;\n }\n }\n if(s.empty() && data[k][1] > 0){\n ans[data[k][0]] = data[k][1];\n data[k][1] = 0;\n }\n }\n else{\n ans[data[k][0]] = data[k][1];\n data[k][1] = 0;\n }\n }\n else{\n s.push({data[k][0], data[k][1]});\n data[k][1] = 0;\n }\n }\n while(!s.empty()){\n auto at = s.top();\n ans[at.first] = at.second;\n s.pop();\n }\n for(int k=i; k<=j; k++){\n if(data[k][1] > 0){\n ans[data[k][0]] = data[k][1];\n }\n }\n vector<int> ret;\n for(int i=0; i<positions.size(); i++){\n if(ans.find(positions[i]) != ans.end()){\n ret.push_back(ans[positions[i]]);\n }\n }\n return ret;\n }\n};",
"memory": "259086"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& pos,vector<int>& h,string s) {\n stack<int>st;\n int n=h.size();\n map<int,int>mp,fin;\n for(int i=0;i<n;i++)\n {\n mp[pos[i]]=i;\n }\n vector<pair<int,pair<int,char>>>v;\n for(int i=0;i<n;i++)\n {\n v.push_back({pos[i],{h[i],s[i]}});\n }\n sort(v.begin(),v.end());\n for(int i=0;i<n;i++)\n {\n pos[i]=v[i].first;\n h[i]=v[i].second.first;\n s[i]=v[i].second.second;\n }\n vector<int>ans;\n for(int i=0;i<n;i++)\n {\n cout<<s[i];\n if(s[i]=='R')\n st.push(i);\n else \n {\n int f=0;\n while(!st.empty() && s[st.top()]=='R')\n {\n if(h[st.top()]==h[i])\n {\n st.pop();\n f=1;\n break;\n }\n else if(h[st.top()]>h[i])\n {\n f=1;\n h[st.top()]--;\n break;\n }\n else \n {\n st.pop();\n h[i]--;\n }\n }\n if(f==0)\n st.push(i);\n }\n }\n\n\n while(!st.empty())\n {\n fin[mp[pos[st.top()]]]=h[st.top()];\n st.pop();\n }\n for(auto i:fin)\n ans.push_back(i.second);\n\n\n return ans;\n }\n};",
"memory": "260403"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 3 | {
"code": "struct Robot{\npublic:\n int health;\n int pos;\n bool left;\n bool alive;\n\n Robot(int h, int p, bool l): health{h}, pos{p}, left{l}, alive{true} {}\n\n bool willCollide(Robot* r) const {\n if (left != r->left) {\n // check both coming near other or going away\n if (left && pos < r->pos) return false;\n if (right && pos > r->pos) return false;\n return true;\n }\n return false;\n }\n};\n\nclass Solution {\npublic:\n Robot* collide(Robot* r1, Robot* r2) {\n if (r1->health == r2->health) {\n r1->alive = false;\n r2->alive = false;\n return nullptr;\n }\n else if (r1->health > r2->health) {\n r2->alive = false;\n r1->health -= 1;\n return r1;\n }\n r1->alive = false;\n r2->health -= 1;\n return r2;\n }\n\n vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n int N = positions.size();\n vector<Robot*> robots(N);\n unordered_map<int, Robot*> ref;\n for (int i=0; i<N; i++) {\n robots[i] = new Robot(healths[i], positions[i], directions[i]=='L');\n ref[positions[i]] = robots[i];\n }\n\n sort(robots.begin(), robots.end(), [](Robot* r1, Robot* r2) {\n return r1->pos < r2->pos;\n });\n stack<Robot*> s;\n for (int i=0; i<N; i++) {\n Robot* r = robots[i];\n // check for collison\n while (!s.empty()) {\n Robot* t = s.top();\n //cout << \"Check if robot \" << t->pos << \" will collide with \" << r->pos << \"\\n\";\n if (!t->willCollide(r)) {\n //cout << \"No\\n\";\n break;\n }\n //cout << \"Yes\\n\";\n s.pop();\n r = collide(r, t);\n if (!r) break;\n }\n if (r) {\n //cout << \"Insert \" << r->pos << \"\\n\";\n s.push(r);\n }\n }\n vector<int> res;\n for (int i=0; i<N; i++) {\n Robot* r = ref[positions[i]];\n if (r->alive) res.push_back(r->health);\n }\n return res;\n }\n};",
"memory": "260403"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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 = positions.size();\n vector<vector<int>>temp(n, {0,0,0});\n for(int i=0;i<n;i++){\n temp[i][0] = positions[i];\n temp[i][1] = healths[i];\n temp[i][2] = directions[i];\n }\n sort(temp.begin(),temp.end());\n stack<int>st;\n for(int i=0;i<n;i++){\n if(st.size()==0){\n st.push(i);\n }else {\n while(st.size()>0){\n auto tp = st.top();\n if(temp[tp][2]=='R' && temp[i][2]=='L'){\n st.pop();\n if(temp[tp][1]>temp[i][1]){\n st.push(tp);\n temp[tp][1]--;\n temp[i][1]=0;\n break;\n }else if(temp[tp][1]<temp[i][1]){\n temp[tp][1]=0;\n temp[i][1]--;\n }else if(temp[tp][1]==temp[i][1]){\n temp[tp][1]=0;\n temp[i][1]=0;\n break;\n }\n } else {\n st.push(i);\n break;\n }\n }\n if(st.size()==0 && temp[i][1]!=0)st.push(i);\n }\n }\n unordered_map<int,int>mp;\n for(int i=0;i<n;i++){\n mp[positions[i]]=i;\n }\n for(int i=0;i<n;i++){\n healths[mp[temp[i][0]]]=temp[i][1];\n }\n vector<int>ans;\n for(auto a: healths){\n if(a!=0)ans.push_back(a);\n }\n return ans;\n }\n};",
"memory": "261721"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& pos, vector<int>& hs, string dirs) {\n int n = pos.size();\n vector<vector<int>>a(n, vector<int>(4));\n for(int i = 0; i < n; i++) {\n a[i][0] = pos[i];\n a[i][1] = hs[i];\n a[i][2] = dirs[i] == 'R' ? 1 : -1;\n a[i][3] = i;\n }\n sort(a.begin(), a.end(), [](vector<int>&l, vector<int>&r) {\n return l[0] < r[0];\n });\n vector<vector<int>>vec;\n stack<vector<int>> st;\n for(int i = 0; i < n; i++) {\n if(a[i][2] == 1) st.push({a[i][1], a[i][3]});\n else {\n int lrob = a[i][1], lind = a[i][3];\n while(!st.empty() and lrob) {\n int rrob = st.top()[0], rind = st.top()[1];\n st.pop();\n if(rrob > lrob) {\n lrob = 0;\n st.push({rrob-1, rind});\n } else if(rrob == lrob) {\n lrob = 0;\n } else {\n lrob--;\n }\n }\n if(st.empty() and lrob > 0) vec.push_back({lrob, lind});\n }\n }\n while(!st.empty()) {\n vector<int>x = st.top();\n st.pop();\n vec.push_back(x);\n }\n sort(vec.begin(), vec.end(), [](vector<int>&l,vector<int>&r){\n return l[1] < r[1];\n });\n vector<int>ans(vec.size());\n for(int i = 0; i < vec.size(); i++) {\n ans[i] = vec[i][0];\n }\n return ans;\n }\n};",
"memory": "263038"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& pos, vector<int>& h,\n string s) {\n int n = s.size();\n vector<vector<int>> v;\n for (int i = 0; i < n; i++) {\n v.push_back({pos[i], h[i], i});\n }\n sort(v.begin(), v.end());\n unordered_map<int, int> mp;\n stack<pair<pair<char, int>, int>> st;\n for (int i = 0; i < n; i++) {\n char dir = s[v[i][2]];\n int health = v[i][1];\n int ind = v[i][2];\n if (i == 0)\n st.push({{dir, health}, ind});\n else if (dir == 'R')\n st.push({{dir, health}, ind});\n else {\n while (!st.empty() && st.top().first.first == 'R') {\n pair<char, int> p = st.top().first;\n if (p.second < health) {\n health--;\n st.pop();\n } else if (p.second <= health) {\n {\n health = -1000;\n st.pop();\n break;\n }\n } else {\n p.second = p.second - 1;\n st.top().first.second = st.top().first.second - 1;\n health = -1000;\n break;\n }\n }\n if (health != -1000)\n st.push({{dir, health}, ind});\n }\n }\n while (!st.empty()) {\n pair<pair<char, int>, int> p = st.top();\n st.pop();\n mp[p.second] = p.first.second;\n }\n vector<int> ans;\n for (int i = 0; i < n; i++) {\n if (mp.find(i) != mp.end())\n ans.push_back(mp[i]);\n }\n return ans;\n }\n};",
"memory": "263038"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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, pair<int, char>> mp;\n\n for(int i = 0; i < positions.size(); i++) mp[positions[i]] = {healths[i], directions[i]};\n\n stack<int> st;\n\n for(auto i : mp)\n {\n int p = i.first;\n int h = i.second.first;\n char dir = i.second.second;\n\n bool include = true;\n while(!st.empty() && mp[st.top()].second == 'R' && dir == 'L')\n {\n if(mp[st.top()].first > h)\n {\n include = false;\n mp[st.top()].first -= 1;\n break;\n }\n else if(mp[st.top()].first == h)\n {\n include = false;\n st.pop();\n break;\n }\n else\n {\n h -= 1;\n st.pop();\n }\n }\n\n mp[p] = {h, dir};\n\n if(include) st.push(p);\n }\n\n unordered_map<int, bool> include;\n while(!st.empty())\n {\n include[st.top()] = true;\n st.pop();\n } \n\n vector<int> ans;\n\n for(int i = 0; i < positions.size(); i++)\n {\n if(include[positions[i]]) ans.push_back(mp[positions[i]].first);\n }\n\n return ans;\n }\n};",
"memory": "264356"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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>> robs;\n int n=positions.size();\n for(int i=0;i<n;i++)\n {\n if(directions[i]=='R')\n robs.push_back({positions[i],i,1});\n else\n robs.push_back({positions[i],i,0});\n }\n sort(robs.begin(),robs.end());\n stack<vector<int>> st;\n for(int i=0;i<n;i++)\n {\n if(robs[i][2]==1||st.empty()||st.top()[2]==0)\n {\n st.push(robs[i]);\n }\n else\n {\n while(st.size()&&st.top()[2]==1&&healths[robs[i][1]]>healths[st.top()[1]])\n {\n healths[st.top()[1]]=-1;\n healths[robs[i][1]]-=1;\n st.pop();\n }\n if(st.size()&&st.top()[2]==1&&healths[robs[i][1]]<healths[st.top()[1]])\n {\n healths[robs[i][1]]=-1;\n healths[st.top()[1]]-=1;\n }\n else if(st.size()&&st.top()[2]==1&&healths[robs[i][1]]==healths[st.top()[1]])\n {\n healths[robs[i][1]]=-1;\n healths[st.top()[1]]=-1;\n st.pop();\n }\n }\n }\n vector<int> ans;\n for(int i=0;i<n;i++)\n {\n if(healths[i]>0)\n ans.push_back(healths[i]);\n }\n return ans;\n\n }\n};",
"memory": "265673"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nvector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {\n\tvector<int>ans;\n\tset<pair<int, pair<int, int>>> L, R;\n\tfor (int i = 0; i < positions.size(); i++) {\n\t\tif (directions[i] == 'L') {\n\t\t\tR.insert(make_pair(positions[i], make_pair(healths[i], i)));\n\t\t}\n\t\telse {\n\t\t\tL.insert(make_pair(positions[i], make_pair(healths[i], i)));\n\t\t}\n\t}\n\n\tauto it = L.end();\n\tauto limit = L.rbegin().base();\n\tif (L.size() > 0) {\n\t\tit--;\n\t}\n\tint cnt = 0;\n\twhile (L.size() != 0 && R.size() != 0) {\n\t\tint posL = it->first;\n\t\tpair<int, int> p = it->second;\n\t\tint healthL = p.first;\n\t\tint roboNumberL = p.second;\n\t\tauto itR = R.lower_bound(make_pair(posL, make_pair(INT_MIN, INT_MIN)));\n\t\tif (itR == R.end()) {\n\t\t\t//db(\"s\", posL);\n\t\t\tif (it == L.begin())\n\t\t\t\tbreak;\n\t\t\tit--;\n\t\t\tcontinue;\n\t\t}\n\t\tint healthR = itR->second.first;\n\t\tint posR = itR->first;\n\t\tint roboNumberR = itR->second.second;\n\t\t// db(\"L\", itR->second.first, itR->second.second);\n\t\t// db(\"R\", it->second.first, it->second.second);\n\t\t// ans.push_back(itR->second.first); ans.push_back(itR->second.second);\n\t\t// ans.push_back(it->second.first); ans.push_back(it->second.second);\n\n\t\tif (healthL > healthR) {\n\t\t\tR.erase(itR);\n\t\t\tL.erase(it);\n\t\t\tpair<int, pair<int, int>>newL = make_pair(posL, make_pair(healthL - 1, roboNumberL));\n\t\t\tL.insert(newL);\n\t\t\tit = L.lower_bound(newL);\n\t\t}\n\t\telse if (healthL < healthR) {\n\t\t\tset<pair<int, pair<int, int>>> ::iterator it2 = it;\n\t\t\t// if (it2 == L.end()) {\n\t\t\t// \tans.push_back(11111);\n\t\t\t// }\n\t\t\tbool Tobreak = false;\n\t\t\tif (it == L.begin()) {\n\t\t\t\tTobreak = true;\n\t\t\t}\n\t\t\tif (L.size() != 0)\n\t\t\t\tit2--;\n\t\t\t// if (it2 == L.end()) {\n\t\t\t// \tans.push_back(11112);\n\t\t\t// }\n\t\t\t// if (it2 == limit) {\n\t\t\t// \tans.push_back(11115);\n\t\t\t// }\n\t\t\tL.erase(it);\n\t\t\tR.erase(itR);\n\t\t\tpair<int, pair<int, int>>newR = make_pair(posR, make_pair(healthR - 1, roboNumberR));\n\t\t\tR.insert(newR);\n\t\t\tit = it2;\n\t\t\tif (Tobreak)\n\t\t\t\tbreak;\n\t\t}\n\t\telse {\n\t\t\tset<pair<int, pair<int, int>>> ::iterator it2 = it;\n\t\t\tbool Tobreak = false;\n if (it == L.begin()) {\n\t\t\t\tTobreak = true;\n\t\t\t}\n\t\t\tif (L.size() != 0)\n\t\t\t\tit2--;\n\t\t\tL.erase(it);\n\t\t\tR.erase(itR);\n\t\t\tit = it2;\n\t\t\tif (Tobreak)\n\t\t\t\tbreak;\n\t\t}\n\t\t// if (cnt >= 0) {\n\t\t// \tans.push_back(L.size());\n\t\t// \tans.push_back(R.size());\n\t\t// \tif (it == L.end()) {\n\t\t// \t\tans.push_back(11113);\n\t\t// \t}\n\t\t// \tprint(ans);\n\t\t// \treturn ans;\n\t\t// }\n\t\tcnt++;\n\t}\n\n\tmap<int, int>mp;\n\tfor (auto x : L) {\n\t\tmp[x.second.second] = x.second.first;\n\t}\n\tfor (auto x : R) {\n\t\tmp[x.second.second] = x.second.first;\n\t}\n\tfor (auto x : mp) {\n\t\tans.push_back(x.second);\n\t}\n\t//print(ans);\n\treturn ans;\n}\n};",
"memory": "265673"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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=positions.size();\n unordered_map<int,int>m1;\n unordered_map<int,char>m2;\n vector<int>v;\n for(int i=0;i<n;i++){\n v.push_back(positions[i]);\n m1[positions[i]]=healths[i];\n m2[positions[i]]=directions[i];\n }\n sort(positions.begin(),positions.end());\n stack<int>st;\n for(int i=0;i<n;i++){\n if(m2[positions[i]]=='R') st.push(positions[i]);\n else {\n while(!st.empty() && m1[st.top()]<m1[positions[i]]){\n m1[st.top()]=0;\n st.pop();\n m1[positions[i]]--;\n }\n if(!st.empty() && m1[st.top()]==m1[positions[i]]) {\n m1[st.top()]=0;\n st.pop();\n m1[positions[i]]=0;\n }\n else if(!st.empty() && m1[st.top()]>m1[positions[i]]) {\n m1[st.top()]--;\n m1[positions[i]]=0;\n }\n }\n }\n int j=0;\n for(int i=0;i<n;i++){\n if(m1[v[i]]>0){\n v[j]=m1[v[i]];\n j++;\n } \n }\n for(int i=0;i<(n-j);i++) v.pop_back();\n return v;\n }\n};",
"memory": "266991"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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 const int n = directions.size();\n vector<int> res;\n vector<int> pos;\n unordered_map<int, int> pos_res;\n string dir;\n\n vector<vector<int>> robo(n);\n for(int i=0; i<n; i++)\n robo[i] = {positions[i], healths[i], directions[i]};\n \n sort(robo.begin(), robo.end());\n\n for(int i=0; i<n; )\n {\n if(dir.empty() || dir.back() == 'L')\n {\n dir.push_back(robo[i][2]);\n res.push_back(robo[i][1]);\n pos.push_back(robo[i][0]);\n pos_res[pos.back()] = res.back();\n i++;\n continue;\n }\n while(!dir.empty() && dir.back() == 'R')\n {\n if(robo[i][2] == 'L')\n {\n int lh = res.back();\n int rh = robo[i][1];\n if(lh == rh)\n {\n pos_res[pos.back()] = 0;\n dir.pop_back();\n res.pop_back();\n pos.pop_back();\n break;\n }\n else if (lh > rh)\n {\n res.back() -= 1;\n pos_res[pos.back()] = res.back();\n break;\n }\n else\n {\n pos_res[pos.back()] = 0;\n dir.pop_back();\n res.pop_back();\n pos.pop_back();\n robo[i][1] -= 1;\n if(dir.empty() || dir.back() == 'L')\n {\n dir.push_back(robo[i][2]);\n res.push_back(robo[i][1]);\n pos.push_back(robo[i][0]);\n pos_res[pos.back()] = res.back();\n break;\n }\n }\n }\n else\n {\n dir.push_back(robo[i][2]);\n res.push_back(robo[i][1]);\n pos.push_back(robo[i][0]);\n pos_res[pos.back()] = res.back();\n break;\n }\n }\n i++;\n }\n\n vector<int> ans;\n for(int i=0; i<n; i++)\n {\n if(pos_res[positions[i]] > 0)\n ans.push_back(pos_res[positions[i]]);\n }\n\n return ans;\n }\n};",
"memory": "266991"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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 = positions.size();\n vector<Robot> robots(n);\n for (int i=0; i<n; i++) {\n Robot r(positions[i], healths[i], directions[i]=='R');\n robots[i] = r;\n }\n\n std::sort(robots.begin(), robots.end(), \n [](const Robot &a, const Robot &b) { return a.pos < b.pos; });\n\n /*for (auto r: robots) {\n cout << r.pos << \", \" << r.health << \", \" << r.dir << \"\\n\";\n }*/\n\n stack<int> toR;\n for (int i=0; i<n; i++) {\n if (robots[i].dir) toR.push(i);\n else collision(robots, toR, i);\n }\n\n unordered_map<int, int> pos_to_health;\n for (auto robot: robots) {\n pos_to_health[robot.pos] = robot.health;\n }\n vector<int> result;\n for (auto p: positions) {\n if (pos_to_health[p] > 0) result.push_back(pos_to_health[p]);\n }\n return result;\n }\n\nprivate:\n\n struct Robot {\n int pos = -1;\n int health = -1;\n int dir = -1;\n Robot(int i, int j, int k): pos(i), health(j), dir(k) {}\n Robot() {}\n };\n\n void collision(vector<Robot>& robots, stack<int>& toR, int i) {\n if (toR.empty()) return;\n auto r_R = toR.top();\n // cout << robots[r_R].pos << \", \" << robots[r_R].health << \"\\n\";\n toR.pop();\n if (robots[r_R].health > robots[i].health) {\n robots[i].health = 0;\n robots[r_R].health--;\n toR.push(r_R);\n }\n else if (robots[r_R].health == robots[i].health) {\n robots[i].health = 0;\n robots[r_R].health = 0;\n }\n else {\n robots[r_R].health = 0;\n robots[i].health--;\n collision(robots, toR, i);\n }\n }\n};",
"memory": "268308"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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 = positions.size();\n vector<Robot> robots(n);\n for (int i=0; i<n; i++) {\n robots[i].pos = positions[i];\n robots[i].health = healths[i];\n robots[i].dir = directions[i] == 'R';\n }\n\n std::sort(robots.begin(), robots.end(), \n [](const Robot &a, const Robot &b) { return a.pos < b.pos; });\n\n stack<int> toR;\n for (int i=0; i<n; i++) {\n if (robots[i].dir) toR.push(i);\n else collision(robots, toR, i);\n }\n\n unordered_map<int, int> pos_to_health;\n for (auto robot: robots) {\n pos_to_health[robot.pos] = robot.health;\n }\n vector<int> result;\n for (auto p: positions) {\n if (pos_to_health[p] > 0) result.push_back(pos_to_health[p]);\n }\n return result;\n }\n\nprivate:\n\n struct Robot {\n int pos = -1;\n int health = -1;\n int dir = -1;\n Robot(int i, int j, int k): pos(i), health(j), dir(k) {}\n Robot() {}\n };\n\n void collision(vector<Robot>& robots, stack<int>& toR, int i) {\n if (toR.empty()) return;\n auto r_R = toR.top();\n toR.pop();\n if (robots[r_R].health > robots[i].health) {\n robots[i].health = 0;\n robots[r_R].health--;\n toR.push(r_R);\n }\n else if (robots[r_R].health == robots[i].health) {\n robots[i].health = 0;\n robots[r_R].health = 0;\n }\n else {\n robots[r_R].health = 0;\n robots[i].health--;\n if (robots[i].health) collision(robots, toR, i);\n }\n }\n};",
"memory": "268308"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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>& positions, vector<int>& healths, string directions) {\n\n stack<int> stc;// index\n vector<int> ret , ans;\n vector<vector<int>> robots;\n int n = positions.size();\n for (int i =0;i < n;i++) robots.push_back({positions[i] , healths[i] , i});\n\n sort(robots.begin(), robots.end());\n\n\n for (int i =0;i< n;i++)\n {\n auto rob = robots[i];\n int pos = rob[0];\n //int& hel = rob[1];\n int id = rob[2];\n if (directions[id] == 'R')\n stc.push(id);\n else\n {\n while (true)\n {\n if (stc.empty() == true) { ret.push_back(id); break;}\n auto topid = stc.top();\n if (healths[topid] == healths[id] ) { stc.pop(); break;}\n else if (healths[topid] < healths[id] ) { stc.pop(); healths[id] --;}\n else {healths[topid]-- ; break;}\n }\n }\n }\n\n vector<int> temp;\n while (stc.empty() == false)\n {\n ret.push_back(stc.top());\n stc.pop();\n }\n\n sort(ret.begin(), ret.end());\n for (auto r: ret) ans.push_back(healths[r]);\n\n // sort(ans.begin(), ans.end() , [](const vector<int>& v1 ,const vector<int>& v2 ) \n // {\n // return (v1[2] < v2[2]);\n // }\n // );\n return ans;\n }\n};",
"memory": "269626"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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 = positions.size();\n stack<int> s;\n map<int, int> healthMap, indexMap;\n // 0 -> 4, 1 -> 3\n vector<int> temp = positions;\n sort(begin(temp), end(temp));\n for (int i=0;i<n;i++) {\n int idx = lower_bound(begin(temp), end(temp), positions[i])-begin(temp);\n indexMap[idx] = i;\n //cout << i << \"what\"<<indexMap[i] << \" \";\n \n }\n //cout << endl;\n for (int j=0;j<n;j++) {\n int i = indexMap[j];\n int currentHealth = healths[i];\n //cout << i << \"iii\" << currentHealth << \" \"; \n if (directions[i] == 'R') {\n s.push(i); \n healthMap[i] = currentHealth;\n continue;\n }\n \n while (!s.empty() && currentHealth >= healthMap[s.top()]) {\n if (currentHealth == healthMap[s.top()]) {\n currentHealth = 0;\n healthMap[s.top()] = 0;\n }\n else {\n currentHealth -= 1;\n healthMap[s.top()] = 0;\n }\n s.pop();\n }\n \n if (currentHealth > 0) {\n if (s.empty()) healthMap[i] = currentHealth;\n else healthMap[s.top()]--;\n }\n }\n \n vector<int> ans;\n for (int i=0;i<n;i++) {\n if (healthMap[i] != 0) ans.push_back(healthMap[i]);\n }\n \n return ans;\n }\n};",
"memory": "269626"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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 map<int,int> ans;\n int n=positions.size();\n for(int i=0;i<n;i++){\n v.push_back({positions[i],healths[i],directions[i]});\n }\n sort(v.begin(),v.end());\n stack<vector<int>> st;\n for(int i=0;i<n;i++){\n if(i==0 && v[i][2]=='R'){\n st.push(v[i]);\n }\n else{\n if(v[i][2]=='L'){\n while(!st.empty() && st.top()[1]<v[i][1]){\n st.pop();\n v[i][1]--;\n }\n if(v[i][1]>0){\n if(!st.empty()){\n if(st.top()[1]==v[i][1]){\n st.pop();\n }\n else{\n st.top()[1]--;\n }\n }\n else{\n ans[v[i][0]]=v[i][1];\n }\n }\n }\n else{\n st.push(v[i]);\n }\n }\n }\n while(!st.empty()){\n ans[st.top()[0]]=st.top()[1];\n st.pop();\n }\n vector<int> a;\n for(int i=0;i<positions.size();i++){\n if(ans.find(positions[i])!=ans.end()){\n // if(ans[positions[i]]>0)\n a.push_back(ans[positions[i]]);\n }\n }\n return a;\n\n }\n};",
"memory": "270943"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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 vector<vector<int>> robots;\n stack<vector<int>> stk;\n\n for (int i = 0; i < positions.size(); i++) {\n robots.push_back({positions[i], i});\n }\n\n sort(robots.begin(), robots.end());\n for (auto& robot : robots) {\n while (!stk.empty() && directions[stk.top()[1]] > directions[robot[1]] &&\n healths[robot[1]] > 0) {\n if (healths[stk.top()[1]] < healths[robot[1]]) {\n healths[stk.top()[1]]=0;\n stk.pop();\n healths[robot[1]]--;\n } else if (healths[stk.top()[1]] > healths[robot[1]]) {\n healths[stk.top()[1]]--;\n healths[robot[1]] = 0;\n } else {\n healths[stk.top()[1]] = 0;\n healths[robot[1]] = 0;\n stk.pop();\n }\n }\n if (healths[robot[1]] != 0) {\n stk.push(robot);\n }\n }\n\n vector<int> ans;\n for (auto& h : healths) {\n if (h > 0)\n ans.push_back(h);\n }\n return ans;\n }\n};",
"memory": "270943"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& p, vector<int>& h, string d) {\n vector<vector<int>> arr;\n stack<pair<int, int>> st;\n int n = p.size();\n\n // Prepare the robot data and sort by position\n for (int i = 0; i < n; i++) {\n vector<int> temp = {p[i], h[i], i, d[i] == 'R' ? 0 : 1};\n arr.push_back(temp);\n }\n sort(arr.begin(), arr.end());\n\n // Process each robot\n for (int i = 0; i < n; i++) {\n pair<int, int> pr = {arr[i][1], arr[i][2]}; // (health, index)\n if (arr[i][3] == 0) { // Robot moving right\n st.push(pr);\n } else { // Robot moving left\n while (!st.empty() && pr.first > 0) {\n int a = st.top().first;\n int b = st.top().second;\n st.pop();\n if (a == pr.first) {\n pr.first = 0; // Both robots destroy each other\n arr[i][1] = 0;\n } else if (a > pr.first) {\n st.push({a - 1, b}); // Right-moving robot survives with decremented health\n pr.first = 0;\n arr[i][1] = 0;\n } else {\n pr.first--; // Left-moving robot survives with decremented health\n }\n }\n if (pr.first > 0) {\n arr[i][1] = pr.first; // Update the health of the surviving left-moving robot\n }\n }\n }\n\n // Collect remaining robots from the stack\n vector<pair<int, int>> v; // (index, health)\n vector<int> ans;\n while (!st.empty()) {\n v.push_back({st.top().second, st.top().first});\n st.pop();\n }\n \n // Collect surviving left-moving robots\n for (int i = 0; i < n; i++) {\n if (arr[i][3] == 1 && arr[i][1] > 0) {\n v.push_back({arr[i][2], arr[i][1]});\n }\n }\n\n // Sort surviving robots by their original indices\n sort(v.begin(), v.end());\n for (auto x : v) {\n ans.push_back(x.second);\n }\n return ans;\n }\n};\n",
"memory": "272261"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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 = positions.size();\n vector<int> dir(n);\n for(int i=0; i<n; ++i){\n if(directions[i]=='L') dir[i]=-1; // L--> -1\n else dir[i]=1; // R--> +1\n }\n\n vector<vector<int>> robo(n);\n for(int i=0; i<n; ++i)\n robo[i] = {positions[i], healths[i], dir[i], i};\n\n auto comp = [](vector<int>&v1, vector<int>& v2){\n return v1[0] < v2[0];\n };\n sort(begin(robo), end(robo), comp);\n\n stack<vector<int>> st;\n int i=0;\n\n for(i=0; i<n; ++i){\n while(!st.empty() && st.top()[2]>0 && robo[i][2]<0 && (robo[i][1]>0 && st.top()[1]>0)){\n int a = (st.top()[1] - robo[i][1]);\n if(a > 0){ // exclude current robo\n st.top()[1] -= 1; // reducing health of the top robo\n robo[i][1]=0;\n }\n else if(a < 0){ // exclude the top robo\n robo[i][1] -= 1; // reducing health of the curr robo\n st.pop();\n }\n else{ // exclude curr as well as top robo\n robo[i][1]=0;\n st.pop();\n }\n }\n if(robo[i][1]>0) st.push(robo[i]);\n }\n\n vector<vector<int>> ans;\n while(!st.empty()){\n ans.push_back({st.top()[1], st.top()[3]});\n st.pop();\n }\n auto comp2 = [](vector<int>&v1,vector<int>&v2){ \n return v1[1]<v2[1];\n };\n sort(begin(ans), end(ans), comp2);\n\n vector<int> vec;\n for(auto & it : ans)\n vec.push_back(it[0]);\n\n return vec;\n }\n};",
"memory": "272261"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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=positions.size();\n map<int ,pair<int,char>>mp;\n map<int,int>pos;\n for(int i=0;i<n;i++){\n mp[positions[i]]={healths[i],directions[i]};\n pos[positions[i]]=i;\n }\n vector<pair<int,int>>ans;\n stack<pair<int,int>>sta;\n for(auto it:mp){\n if(it.second.second=='R'){\n sta.push({it.second.first,pos[it.first]});\n }\n else{\n int val=it.second.first;\n int posi=pos[it.first];\n while(!sta.empty()&&(sta.top().first<=val)){\n if(sta.top().first==val){\n val=0;\n }\n else{\n val--;\n }\n sta.pop();\n }\n if(!sta.empty()){\n if(val!=0){\n int x=sta.top().first;\n int y=sta.top().second;\n x-=1;\n sta.pop();\n sta.push({x,y});\n }\n }\n else{\n if(val!=0){\n ans.push_back({posi,val});\n }\n }\n }\n }\n stack<pair<int,int>>sta2;\n while(!sta.empty()){\n ans.push_back({sta.top().second,sta.top().first});\n sta.pop();\n }\n sort(ans.begin(),ans.end());\n vector<int>res;\n for(auto it:ans){\n res.push_back(it.second);\n }\n return res;\n }\n};",
"memory": "273578"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\nbool comp(vector<int> a,vector<int> b){\n return a[0]<=b[0];\n}\n vector<int> survivedRobotsHealths(vector<int>& p, vector<int>& h, string d) {\n stack<vector<int>> s;\n int n=p.size();\n vector<vector<int>> v(n);\n for(int i=0;i<n;i++){\n if(d[i]=='L'){\n v[i]={p[i],h[i],-1,i};\n }\n else {\n v[i]={p[i],h[i],1,i};\n }\n }\n sort(v.begin(),v.end());\n \n for(int i=0;i<n;i++){\n int hh=v[i][1];\n int f=0;\n while(!s.empty()&&s.top()[2]>0&&v[i][2]<0){\n if(s.top()[1]==hh){\n s.pop();\n f=1;\n break;\n }\n auto pp=s.top();\n s.pop();\n if(pp[1]>hh){\n s.push({pp[0],pp[1]-1,pp[2],pp[3]});\n f=1;\n break;\n }\n else{\n hh=hh-1;\n }\n }\n if(f==0)\n s.push({v[i][0],hh,v[i][2],v[i][3]});\n }\n vector<int> vv(n,0);\n while(!s.empty()){\n vv[s.top()[3]]=(s.top()[1]);\n s.pop();\n }\n vector<int> ans;\n for(auto it: vv){\n if(it>0) ans.push_back(it);\n }\n return ans;\n }\n};",
"memory": "273578"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& pos, vector<int>& hp, string dir) {\n\n vector<int> ans (pos.size(), 0);\n unordered_map<int, int> index;\n\n for(int i=0;i<pos.size(); i++)\n index[pos[i]]=i;\n\n unordered_map<int, pair<int, char>> m;\n\n for(int i=0;i<pos.size(); i++)\n m[pos[i]]={hp[i], dir[i]};\n \n sort(pos.begin(), pos.end());\n\n stack<pair<int,int>> s;\n for( int i=0;i<pos.size(); i++)\n {\n pair<int,char> curr = m[pos[i]];\n if(curr.second=='R')\n s.push({curr.first, index[pos[i]]});\n else\n {\n int flag=1;\n while(!s.empty())\n {\n pair<int, int> top = s.top();\n s.pop();\n if(top.first > curr.first)\n {\n s.push({top.first-1, top.second});\n flag=0;\n break;\n }\n else if(top.first < curr.first)\n {\n curr.first--;\n }\n else\n {\n flag=0;\n break;\n }\n \n }\n if(flag)\n ans[index[pos[i]]]=curr.first;\n }\n }\n\n while(!s.empty())\n {\n pair<int, int> top = s.top();\n s.pop();\n cout<<top.first<<\" \"<<top.second<<\"\\n\";\n ans[top.second]=top.first;\n }\n\n vector<int> fin;\n for(int i=0;i<ans.size(); i++)\n {\n if(ans[i])\n fin.push_back(ans[i]);\n }\n \n return fin;\n }\n};",
"memory": "274896"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n vector<int> survivedRobotsHealths(vector<int>& pos, vector<int>& hp, string dir) {\n\n vector<int> ans (pos.size(), 0);\n unordered_map<int, int> index;\n\n for(int i=0;i<pos.size(); i++)\n index[pos[i]]=i;\n\n unordered_map<int, pair<int, char>> m;\n\n for(int i=0;i<pos.size(); i++)\n m[pos[i]]={hp[i], dir[i]};\n \n sort(pos.begin(), pos.end());\n\n stack<pair<int,int>> s;\n for( int i=0;i<pos.size(); i++)\n {\n pair<int,char> curr = m[pos[i]];\n if(curr.second=='R')\n s.push({curr.first, index[pos[i]]});\n else\n {\n int flag=1;\n while(!s.empty())\n {\n pair<int, int> top = s.top();\n s.pop();\n if(top.first > curr.first)\n {\n s.push({top.first-1, top.second});\n flag=0;\n break;\n }\n else if(top.first < curr.first)\n {\n curr.first--;\n }\n else\n {\n flag=0;\n break;\n }\n \n }\n if(flag)\n ans[index[pos[i]]]=curr.first;\n }\n }\n\n while(!s.empty())\n {\n pair<int, int> top = s.top();\n s.pop();\n ans[top.second]=top.first;\n }\n\n vector<int> fin;\n for(int i=0;i<ans.size(); i++)\n {\n if(ans[i])\n fin.push_back(ans[i]);\n }\n \n return fin;\n }\n};",
"memory": "274896"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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 else if(c.empty())\n return result;\n else{\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": "276213"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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 else if(c.empty())\n return result;\n else{\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": "276213"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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 = positions.size();\n vector<vector<int>> robots;\n\n for (int i = 0; i < n; ++i) {\n robots.push_back({positions[i], healths[i], directions[i], i});\n }\n\n sort(robots.begin(), robots.end());\n\n vector<vector<int>> stack;\n\n for (auto& robot : robots) {\n if (robot[2] == 'R' || stack.empty() || stack.back()[2] == 'L') {\n stack.push_back(robot);\n continue;\n }\n\n if (robot[2] == 'L') {\n bool add = true;\n while (!stack.empty() && stack.back()[2] == 'R' && add) {\n int last_health = stack.back()[1];\n if (robot[1] > last_health) {\n stack.pop_back();\n robot[1] -= 1;\n } else if (robot[1] < last_health) {\n stack.back()[1] -= 1;\n add = false;\n } else {\n stack.pop_back();\n add = false;\n }\n }\n\n if (add) {\n stack.push_back(robot);\n }\n }\n }\n\n vector<int> result;\n sort(stack.begin(), stack.end(), [](vector<int>& a, vector<int>& b) {\n return a[3] < b[3];\n });\n\n for (auto& robot : stack) {\n result.push_back(robot[1]);\n }\n return result;\n }\n};",
"memory": "277531"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</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 = positions.size();\n vector<vector<int>> robots;\n\n for (int i = 0; i < n; ++i) {\n robots.push_back({positions[i], healths[i], directions[i], i});\n }\n\n sort(robots.begin(), robots.end());\n\n vector<vector<int>> stack;\n\n for (auto& robot : robots) {\n if (robot[2] == 'R' || stack.empty() || stack.back()[2] == 'L') {\n stack.push_back(robot);\n continue;\n }\n\n if (robot[2] == 'L') {\n bool add = true;\n while (!stack.empty() && stack.back()[2] == 'R' && add) {\n int last_health = stack.back()[1];\n if (robot[1] > last_health) {\n stack.pop_back();\n robot[1] -= 1;\n } else if (robot[1] < last_health) {\n stack.back()[1] -= 1;\n add = false;\n } else {\n stack.pop_back();\n add = false;\n }\n }\n\n if (add) {\n stack.push_back(robot);\n }\n }\n }\n\n vector<int> result;\n sort(stack.begin(), stack.end(), [](vector<int>& a, vector<int>& b) {\n return a[3] < b[3];\n });\n\n for (auto& robot : stack) {\n result.push_back(robot[1]);\n }\n\n return result;\n }\n};\n\nauto init = []() {\n ios_base::sync_with_stdio(0);\n cin.tie(0);\n cout.tie(0);\n return 0;\n}();",
"memory": "277531"
} |
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>'L'</strong> for <strong>left</strong> or <strong>'R'</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;"> </div>
<p> </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 = "RRRRR"
<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 = "RLRL"
<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's health is smaller, it gets removed, and robot 3'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 = "RLRL"
<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> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= positions.length == healths.length == directions.length == n <= 10<sup>5</sup></code></li>
<li><code>1 <= positions[i], healths[i] <= 10<sup>9</sup></code></li>
<li><code>directions[i] == 'L'</code> or <code>directions[i] == 'R'</code></li>
<li>All values in <code>positions</code> are distinct</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static bool mycmp(pair<int,pair<int,int>> &a,pair<int,pair<int,int>> &b){\n return a.first<b.first;\n }\n vector<int> survivedRobotsHealths(vector<int>& pos, vector<int>& health,\n string s) {\n int n=pos.size();\n vector<pair<int,pair<int,int>>> vec;\n unordered_map<int,int> mp;\n for(int i=0;i<n;i++){\n vec.push_back({pos[i],{health[i],i}});\n if(s[i]=='L'){\n mp[pos[i]]=0;//left\n }\n else{\n mp[pos[i]]=1;//right\n }\n }\n sort(vec.begin(),vec.end(),mycmp);\n vector<pair<int,pair<int,int>>> nums;\n for(int i=0;i<n;i++){\n nums.push_back({vec[i].second.first,{mp[vec[i].first],vec[i].second.second}});\n }\n stack<pair<int,pair<int,int>>> st;\n for(int i=0;i<n;i++){\n int val=nums[i].first;\n int dir=nums[i].second.first;\n int ind=nums[i].second.second;\n if(st.empty()==true || dir==1){\n \n st.push({val,{dir,ind}});\n }\n else{\n while(st.empty()==false && dir==0 && st.top().second.first==1){\n int tempval=st.top().first;\n int tempdir=st.top().second.first;\n int tempind=st.top().second.second;\n st.pop();\n if(val>tempval){\n val-=1;\n }\n else if(val<tempval){\n val=0;\n st.push({tempval-1,{tempdir,tempind}});\n break;\n }\n else{\n val=0;\n break;\n }\n \n }\n if(val!=0){\n st.push({val,{dir,ind}});\n }\n }\n \n }\n vector<int> res;\n map<int,int> mpp;\n while(st.empty()==false){\n auto it=st.top();\n st.pop();\n mpp[it.second.second]=it.first;\n }\n for(auto it:mpp){\n res.push_back(it.second);\n }\n return res;\n }\n};",
"memory": "278848"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.