Skip to content

Commit

Permalink
Merge pull request #74 from AlgoLeadMe/20-InSange
Browse files Browse the repository at this point in the history
20-InSange
  • Loading branch information
InSange authored Aug 4, 2024
2 parents cadb852 + 72f0e8a commit d36d10d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
5 changes: 4 additions & 1 deletion InSange/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
| 16μ°¨μ‹œ | 2024.06.28 | κ·Έλž˜ν”„ | [Maximum_Total_Importance_ofRoads](https://leetcode.com/problems/maximum-total-importance-of-roads/) | [#16](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/63)]
| 17μ°¨μ‹œ | 2024.06.28 | λ¬Έμžμ—΄ | [Zigzag Conversion](https://leetcode.com/problems/zigzag-conversion/) | [#17](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/64)]
| 18μ°¨μ‹œ | 2024.07.10 | λ¬Έμžμ—΄ | [Crawler Log Folder](https://leetcode.com/problems/crawler-log-folder/) | [#18](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/64)]
| 19μ°¨μ‹œ | 2024.07.13 | μŠ€νƒ | [Crawler Log Folder](https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/) | [#19](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/64)]
| 19μ°¨μ‹œ | 2024.07.13 | μŠ€νƒ | [Crawler Log Folder](https://leetcode.com/problems/reverse-substrings-between-each-pair-of-parentheses/) | [#19](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/72)]
| 20μ°¨μ‹œ | 2024.07.17 | μŠ€νƒ | [Robot Collisions](https://leetcode.com/problems/robot-collisions/) | [#20](https://github.com/AlgoLeadMe/AlgoLeadMe-8/pull/74)]
---

https://leetcode.com/problems/robot-collisions/
70 changes: 70 additions & 0 deletions InSange/μŠ€νƒ/2751_Robot Collisions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <iostream>
#include <vector>
#include <queue>
#include <stack>

using namespace std;

class Solution {
public:
vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) {
vector<int> ans;

priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> IndexArr;
stack<pair<int, int>> Rst;

for (int i = 0; i < positions.size(); i++)
{
IndexArr.push({ positions[i], i });
}

while (!IndexArr.empty())
{
int val = IndexArr.top().first;
int index = IndexArr.top().second;

cout << val << ", " << index << "\n";
IndexArr.pop();

if (directions[index] == 'L')
{
while (!Rst.empty())
{
int stVal = Rst.top().first;
int stIndex = Rst.top().second;

if (healths[stIndex] == healths[index])
{
healths[stIndex] = 0;
healths[index] = 0;
Rst.pop();
break;
}
else if (healths[stIndex] > healths[index])
{
healths[index] = 0;
healths[stIndex]--;
break;
}
else
{
healths[stIndex] = 0;
healths[index]--;
Rst.pop();
}
}
}
else
{
Rst.push({ val, index });
}
}

for (int health : healths)
{
if (health) ans.push_back(health);
}

return ans;
}
};

0 comments on commit d36d10d

Please sign in to comment.