Reddit
Question
·
2026 Q2
LC Hard - POTD
7 upvotes
4 replies
Question Details
[https://leetcode.com/problems/robot-collisions/?envType=daily-question&envId=2026-04-01](https://leetcode.com/problems/robot-collisions/?envType=daily-question&envId=2026-04-01) Happy to shar
Full Details
[https://leetcode.com/problems/robot-collisions/?envType=daily-question&envId=2026-04-01](https://leetcode.com/problems/robot-collisions/?envType=daily-question&envId=2026-04-01) Happy to share that I am back, and possibly, better than I ever was at DSA. I think MBA gave me a break and when I got back to solving DSA problems, I started enjoying them so much that since resuming coding 12 days back, I was able to solve 6 out of the 7 hard questions I attempted (only 1 medium problem made me scratch my brain). Anyway, today's POTD was a simple stack problem with just a tricky implementation (it was probably among the easier of the generally hard questions on the platform). Here is my code for the same: class Solution { public: static bool cmp(vector<int>&a, vector<int>&b){ return a[1] < b[1]; } static bool cmp2(pair<int,int>&a, pair<int,int>&b){ return a.first < b.first; } vector<int> survivedRobotsHealths(vector<int>& positions, vector<int>& healths, string directions) { vector<vector<int>>pos_health_directions; int n = positions.size(); unordered_map<char,int>umap; umap['L'] = 0; umap['R'] = 1; for (int i = 0; i < n; i++){ pos_health_directions.push_back({i,positions[i],healths[i],umap[directions[i]]}); } sort(pos_health_directions.begin(),pos_health_directions.end(),cmp); stack<vector<int>>st; // for (auto i: pos_health_directions) cout<<i[0]<<" "<<i[1]<<" "<<i[2]<<" "<<i[3]<<endl; for (int i = 0; i < n; i++){ if (st.empty()) st.push({pos_health_directions[i]}); else{ if (st.top()[3] == 1 && pos_health_directions[i][3] == 0){ bool x = false; while (!st.empty() && st.top()[3] == 1 && pos_health_directions[i][3] == 0){ if (st.top()[2] > pos_health_directions[i][2]) { st.top()[2] -= 1; x = true; break; } else{ if (st.top()[2] == pos_health_directions[i][2]){ st.pop(); x = true; break; } else{ st.pop(); pos_health_directions[i][2] -= 1; // st.push(pos_health_directions[i]); } } } if (!x) st.push(pos_health_directions[i]); } else{ st.push(pos_health_directions[i]); } } } vector<pair<int,int>>vTemp; while (!st.empty()) { vTemp.push_back({st.top()[0],st.top()[2]}); st.pop(); } sort(vTemp.begin(),vTemp.end(),cmp2); vector<int>res; for (auto i : vTemp) res.push_back(i.second); return res; } };
Free preview — Unlock all questions →
Topics
Strings
Stack Queue