diff --git a/InSange/README.md b/InSange/README.md index 80a1712..c6c803d 100644 --- a/InSange/README.md +++ b/InSange/README.md @@ -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/ diff --git "a/InSange/\354\212\244\355\203\235/2751_Robot Collisions.cpp" "b/InSange/\354\212\244\355\203\235/2751_Robot Collisions.cpp" new file mode 100644 index 0000000..5aeb56a --- /dev/null +++ "b/InSange/\354\212\244\355\203\235/2751_Robot Collisions.cpp" @@ -0,0 +1,70 @@ +#include +#include +#include +#include + +using namespace std; + +class Solution { +public: + vector survivedRobotsHealths(vector& positions, vector& healths, string directions) { + vector ans; + + priority_queue, vector>, greater>> IndexArr; + stack> 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; + } +}; \ No newline at end of file