2751. Robot Collisions | Stacks | Leetcode Daily Challlenge | DSA | Hindi
HTML-код
- Опубликовано: 22 авг 2024
- Problem Name:
2751. Robot Collisions
Problem Statement:
There are n 1-indexed robots, each having a position on a line, health, and movement direction.
You are given 0-indexed integer arrays positions, healths, and a string directions (directions[i] is either 'L' for left or 'R' for right). All integers in positions are unique.
All robots start moving on the line simultaneously at the same speed in their given directions. If two robots ever share the same position while moving, they will collide.
If two robots collide, the robot with lower health is removed from the line, and the health of the other robot decreases by one. The surviving robot continues in the same direction it was going. If both robots have the same health, they are both removed from the line.
Your task is to determine the health of the robots that survive the collisions, in the same order that the robots were given, i.e. final heath of robot 1 (if survived), final health of robot 2 (if survived), and so on. If there are no survivors, return an empty array.
Return an array containing the health of the remaining robots (in the order they were given in the input), after no further collisions can occur.
Note: The positions may be unsorted.
Problem Link:
leetcode.com/p...
Graph Playlist:
• Graph Data Structure S...
Java Plus DSA Placement Course Playlist:
• Java and DSA Course Pl...
Java Plus DSA Sheet:
docs.google.co...
Notes:
github.com/Tiw...
Telegram Link:
shashwattiwari...
Ultimate Recursion Series Playlist:
• Recursion and Backtrac...
Instagram Handle: (@shashwat_tiwari_st)
shashwattiwari...
Samsung Interview Experience:
• I cracked Samsung | SR...
Company Tags:
Facebook | Amazon | Microsoft | Netflix | Google | LinkedIn | Pega Systems | VMware | Adobe | Samsung
Timestamp:
0:00 - Introduction
#ShashwatTiwari #coding #problemsolving
We don't have to push any left moving robot to the stack. Also, We can just check at the start of the while loop that if the health of the left moving robot becomes 0 then we break out of the while loop.
@@PiyushYadav-pl9jm That makes sense, I guess no changes in TC but it will reduce a few lines of code for sure.
@shashwat_tiwari_st yeah, and thanks, btw
Please do like, if you understood the solution as well as the code 😊
Like target for this video is 90.
nicely done, thanks for emphasizing time & space complexity of the solutions.
Thanks 😊
love from malaysia♥
Following your videos daily sir,I was able to solve it on my own.Here's my solution.
class Solution {
public List survivedRobotsHealths(int[] positions, int[] healths, String directions) {
int len = positions.length;
int[][] helper = new int[len][4];
for (int i = 0; i < len; i++) {
helper[i][0] = positions[i];
helper[i][1] = healths[i];
helper[i][2] = directions.charAt(i) - 'A';
helper[i][3] = i + 1;
}
Arrays.sort(helper, new Comparator() {
public int compare(int[] a, int[] b) {
return Integer.compare(a[0], b[0]);
}
});
Stack stack = new Stack();
for (int i = 0; i < len; i++) {
if ((!stack.isEmpty() && helper[i][2] == 11) && helper[stack.peek()][2] == 17) {
while ((!stack.isEmpty() && helper[i][2] == 11) && helper[stack.peek()][2] == 17) {
if (helper[stack.peek()][1] > helper[i][1]) {
helper[stack.peek()][1] -= 1;
break;
}
else if (helper[stack.peek()][1] == helper[i][1]) {
stack.pop();
break;
}
else {
stack.pop();
helper[i][1] -= 1;
if (stack.isEmpty() || helper[stack.peek()][2] == 11) {
stack.push(i);
}
}
}
}
else {
stack.push(i);
}
}
int size = stack.size();
ArrayList result = new ArrayList();
int[][] ans = new int[size][2];
int i = 0;
while (!stack.isEmpty()) {
int index = stack.pop();
ans[i][0] = helper[index][1];
ans[i][1] = helper[index][3];
i++;
}
Arrays.sort(ans, new Comparator() {
public int compare(int[] a, int[] b) {
return Integer.compare(a[1], b[1]);
}
});
for(int j=0;j
I also thought of creating one common class, like you have created helper array ✨️👏👏
Great you gained a sub
Can you please take up the Leetcode 146. LRU Cache problem to explain in a video?
it is similar to asteroid collision
@@AnshuKumar-qq6fr 💯
Legends focus on both "They way he explains the problem" and The avengers logo frame on the wall behind him"🔥🔥🔥
Pls continue graph series
ma daily leetcode pa questions kar raha tha but do din sa problem solve ne hui or ab confidence low hai ab kya karna chaiye gay's any suggestion ? 🙃 me
chor dena chahiye
Be patience and keep coding 😊. Mere se bhi hota hai solution to dur problem ko decode hi nahi kar pata ki actual me problem hai kya , jab problem ko hum acche se read karte hai uske solution simulate karte hai tab thodi thodi idea ane lagta hai ki kese problem ko solve karna hai , aur sare efforts k baad bhi na ho to shashwat sir to hai hi 😊
Its okay if you cannot solve by yourself, first try to come up with what data structure you can use and how to approach the problem, if you still fail to arrive at the solution then it is okay to watch videos to understand how the problem actually works. Keep being consistent and you will start recognising the patterns. Consistency is the key
@@RohitKumar-dz8dh Thank you bro tumahra comment na mujhe confidence toh dila diya
@@saisree04 yes bro
Asteroid Collision
💯💫
why you kept index 0 at the end???
sorted index on the basis of position array (increasing order)
If positions has values [5, 2, 8, 1] and indices has values [0, 1, 2, 3], after sorting, indices will be [3, 1, 0, 2] because positions[3] (1) is the smallest, followed by positions[1] (2), positions[0] (5), and positions[2] (8).
bhaiya mana ya code c++ ma kiya to some test case pass ne ho ra ?..
not working for second case
You might have missed something, check your code once more.
Sir please update your version
Yup study model like 1990s
Git gaakar r1 r2 r3 bol rhe ho ye kya tarika hai
@@navneetsingh7209 thoda smjh me aaye aesa bolo...!