LeetCode 2751 | Robot Collisions | Java | Hard | Stack & Sorting Simulation | CodeByTushu
Description: π Welcome Back to CodeByTushu!
In todayβs video, we solve LeetCode 2751 β Robot Collisions using Stack and Sorting in Java.
We are given positions, healths, and directions for multiple robots moving along a one-dimensional line at the exact same speed. Whenever two robots moving toward each other collide, the robot with lower health is destroyed while the survivor loses one health unit, and if both share identical health, both are eliminated. Our goal is to determine the final healths of all surviving robots in the original order they were provided.
Since all robots move at identical speeds, collisions occur only when a right-moving robot meets a left-moving robot positioned to its right. We first sort the robot indices by their coordinate positions from left to right. We then iterate through the sorted robots using a stack to track active right-moving robots. When encountering a left-moving robot, we resolve collisions against the top of the stack until either the left-moving robot is destroyed, both robots eliminate each other, or the left-moving robot clears all opposing right-moving robots. Finally, we collect the surviving healths in the original input order. This simulation achieves an optimal time complexity of O(N log N) dominated by sorting, with O(N) auxiliary space.
π₯ Problem Highlights
β Spatial sorting of robot indices by position along the line
β Collision invariant observation where only R meets L can collide
β Monotonic stack simulation efficiently resolving sequential collisions
β In-place health decrement and mutual elimination handling
β Reconstructing final survivor healths in original input index sequence
β Optimal O(N log N) time complexity and O(N) auxiliary space
β±οΈ TIMESTAMPS
00:00 Intro & Daily Challenge Kickoff (98-Day Streak)
01:38 Problem Understanding & Collision Dynamics Walkthrough
03:11 Key Insights: Coordinate Sorting & Monotonic Stack Invariant
05:00 Optimal Algorithmic Strategy: 3-Phase Simulation
06:25 Clean Java Solution Walkthrough Line by Line
07:57 Dry Run & Step-by-Step Example 2 Trace (98-Day Milestone)
10:14 Time Complexity Analysis (O(N log N) - 25 ms, Beats 96.8%)
11:13 Space Complexity Analysis (O(N) Auxiliary Space)
11:54 Day 1 Wrap-up & Channel Resources
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/April/01-April/2751-Robot-Collisions
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/April/01-April/2751-Robot-Collisions/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/April/01-April/2751-Robot-Collisions/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/April/01-April/2751-Robot-Collisions/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/April/01-April/2751-Robot-Collisions/solution.js
π LeetCode Source Code
π Admin type the link
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Hard Problems
Java DSA Preparation
Stack Data Structure
Array Sorting Simulation
Monotonic Stack Patterns
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why can two robots moving in the same direction (both Left or both Right) never collide with each other?
1οΈβ£ Because all robots move simultaneously at the exact same speed
2οΈβ£ Because robots moving in the same direction repel each other
3οΈβ£ Because their coordinate positions are always non-adjacent
4οΈβ£ Because they only collide if their health values are unequal
#LeetCode2751 #LeetCode #RobotCollisions #Java #JavaDSA #Stack #Sorting #Simulation #DSA #CodingInterview #ProblemSolving #LeetCodeHard #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
In todayβs video, we solve LeetCode 2751 β Robot Collisions using Stack and Sorting in Java.
We are given positions, healths, and directions for multiple robots moving along a one-dimensional line at the exact same speed. Whenever two robots moving toward each other collide, the robot with lower health is destroyed while the survivor loses one health unit, and if both share identical health, both are eliminated. Our goal is to determine the final healths of all surviving robots in the original order they were provided.
Since all robots move at identical speeds, collisions occur only when a right-moving robot meets a left-moving robot positioned to its right. We first sort the robot indices by their coordinate positions from left to right. We then iterate through the sorted robots using a stack to track active right-moving robots. When encountering a left-moving robot, we resolve collisions against the top of the stack until either the left-moving robot is destroyed, both robots eliminate each other, or the left-moving robot clears all opposing right-moving robots. Finally, we collect the surviving healths in the original input order. This simulation achieves an optimal time complexity of O(N log N) dominated by sorting, with O(N) auxiliary space.
π₯ Problem Highlights
β Spatial sorting of robot indices by position along the line
β Collision invariant observation where only R meets L can collide
β Monotonic stack simulation efficiently resolving sequential collisions
β In-place health decrement and mutual elimination handling
β Reconstructing final survivor healths in original input index sequence
β Optimal O(N log N) time complexity and O(N) auxiliary space
β±οΈ TIMESTAMPS
00:00 Intro & Daily Challenge Kickoff (98-Day Streak)
01:38 Problem Understanding & Collision Dynamics Walkthrough
03:11 Key Insights: Coordinate Sorting & Monotonic Stack Invariant
05:00 Optimal Algorithmic Strategy: 3-Phase Simulation
06:25 Clean Java Solution Walkthrough Line by Line
07:57 Dry Run & Step-by-Step Example 2 Trace (98-Day Milestone)
10:14 Time Complexity Analysis (O(N log N) - 25 ms, Beats 96.8%)
11:13 Space Complexity Analysis (O(N) Auxiliary Space)
11:54 Day 1 Wrap-up & Channel Resources
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/April/01-April/2751-Robot-Collisions
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/April/01-April/2751-Robot-Collisions/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/April/01-April/2751-Robot-Collisions/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/April/01-April/2751-Robot-Collisions/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/April/01-April/2751-Robot-Collisions/solution.js
π LeetCode Source Code
π Admin type the link
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Hard Problems
Java DSA Preparation
Stack Data Structure
Array Sorting Simulation
Monotonic Stack Patterns
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why can two robots moving in the same direction (both Left or both Right) never collide with each other?
1οΈβ£ Because all robots move simultaneously at the exact same speed
2οΈβ£ Because robots moving in the same direction repel each other
3οΈβ£ Because their coordinate positions are always non-adjacent
4οΈβ£ Because they only collide if their health values are unequal
#LeetCode2751 #LeetCode #RobotCollisions #Java #JavaDSA #Stack #Sorting #Simulation #DSA #CodingInterview #ProblemSolving #LeetCodeHard #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description




