Robot Collisions | Made Easy | Dry Run | Leetcode 2751 | codestorywithMIK
Whatsapp Community Link : https://www.whatsapp.com/channel/0029...
This is the 20th Video of our Playlist "Stack : Popular Interview Problems" by codestorywithMIK
Similar Problem - Leetcode 735 - Asteroid Collision - • Asteroid Collision | Intuition | Dry ...
In this video we will try to solve a very good Stack based problem : Robot Collisions | Made Easy | Dry Run | Leetcode 2751 | codestorywithMIK
I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.
Problem Name : Robot Collisions | Made Easy | Dry Run | Leetcode 2751 | codestorywithMIK
Company Tags : Will update soon
My solutions on Github(C++ & JAVA) : https://github.com/MAZHARMIK/Intervie...
Leetcode Link : https://leetcode.com/problems/robot-c...
My DP Concepts Playlist : • Roadmap for DP | How to Start DP ? | ...
My Graph Concepts Playlist : • Graph Concepts & Qns - 1 : Graph will...
My Recursion Concepts Playlist : • Introduction | Recursion Concepts And...
My GitHub Repo for interview preparation : https://github.com/MAZHARMIK/Intervie...
Instagram : / codestorywithmik
Facebook : / 100090524295846
Twitter : / cswithmik
Subscribe to my channel : / @codestorywithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
Summary :
The given solution is designed to determine the health of robots that survive after potential collisions based on their positions, health levels, and directions. Here's a brief summary of the approach:
Initialization:
The function survivedRobotsHealths takes three inputs: positions, healths, and directions.
The size of the input is stored in n.
A vector indices of size n is initialized to contain values from 0 to n-1.
Sorting by Position:
The indices vector is sorted based on the robots' positions using a lambda function, which allows for accessing the positions via the indices.
Simulation of Robot Movements Using Stack:
A stack st is used to manage robots moving to the right ('R').
As the sorted indices are processed:
If a robot is moving to the right ('R'), its index is pushed onto the stack.
If a robot is moving to the left ('L'), it may collide with robots on the stack.
Collisions are resolved by comparing health values:
If the health of the robot moving left is greater, it continues with reduced health.
If the health of the robot moving right is greater, it remains on the stack with reduced health.
If healths are equal, both robots are destroyed.
Collecting Results:
After processing all collisions, the health values of surviving robots (health greater than 0) are collected into the result vector.
Complexity:
Time Complexity: O(n log n) due to the sorting step.
Space Complexity: O(n) for storing indices and using the stack.
The final result vector contains the health values of the surviving robots after all potential collisions have been resolved.
✨ Timelines✨
00:00 - Introduction
00:25 - Problem Explanation
07:09 - Thought Process
22:21 - Sorting + Lambda
27:26 - Coding on Leetcode
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024