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​

Комментарии • 33

  • @PiyushYadav-pl9jm
    @PiyushYadav-pl9jm Месяц назад

    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.

    • @shashwat_tiwari_st
      @shashwat_tiwari_st  Месяц назад

      @@PiyushYadav-pl9jm That makes sense, I guess no changes in TC but it will reduce a few lines of code for sure.

    • @PiyushYadav-pl9jm
      @PiyushYadav-pl9jm Месяц назад

      @shashwat_tiwari_st yeah, and thanks, btw

  • @shashwat_tiwari_st
    @shashwat_tiwari_st  Месяц назад +2

    Please do like, if you understood the solution as well as the code 😊
    Like target for this video is 90.

  • @krutikadave4534
    @krutikadave4534 Месяц назад +1

    nicely done, thanks for emphasizing time & space complexity of the solutions.

  • @RohitKumar-dz8dh
    @RohitKumar-dz8dh Месяц назад +2

    Thanks 😊

  • @GirjeshSharma-zv3xo
    @GirjeshSharma-zv3xo Месяц назад +2

    love from malaysia♥

  • @Roomates338
    @Roomates338 Месяц назад +1

    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

    • @shashwat_tiwari_st
      @shashwat_tiwari_st  Месяц назад +1

      I also thought of creating one common class, like you have created helper array ✨️👏👏

  • @iloveislamichub
    @iloveislamichub Месяц назад

    Great you gained a sub

  • @aaryachoudhary7949
    @aaryachoudhary7949 Месяц назад

    Can you please take up the Leetcode 146. LRU Cache problem to explain in a video?

  • @AnshuKumar-qq6fr
    @AnshuKumar-qq6fr Месяц назад +1

    it is similar to asteroid collision

  • @krishnasrivaibhav2245
    @krishnasrivaibhav2245 Месяц назад

    Legends focus on both "They way he explains the problem" and The avengers logo frame on the wall behind him"🔥🔥🔥

  • @harsehajahuja2239
    @harsehajahuja2239 Месяц назад

    Pls continue graph series

  • @StudentjatinJatin
    @StudentjatinJatin Месяц назад +4

    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

    • @jatin9987
      @jatin9987 Месяц назад

      chor dena chahiye

    • @RohitKumar-dz8dh
      @RohitKumar-dz8dh Месяц назад +2

      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 😊

    • @saisree04
      @saisree04 Месяц назад +1

      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

    • @StudentjatinJatin
      @StudentjatinJatin Месяц назад +1

      @@RohitKumar-dz8dh Thank you bro tumahra comment na mujhe confidence toh dila diya

    • @StudentjatinJatin
      @StudentjatinJatin Месяц назад

      @@saisree04 yes bro

  • @DarkDragon-bz6qp
    @DarkDragon-bz6qp Месяц назад

    Asteroid Collision

  • @CharlieSkygazer
    @CharlieSkygazer Месяц назад +1

    why you kept index 0 at the end???

    • @shashwat_tiwari_st
      @shashwat_tiwari_st  Месяц назад

      sorted index on the basis of position array (increasing order)

    • @saisree04
      @saisree04 Месяц назад +1

      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).

  • @StudentjatinJatin
    @StudentjatinJatin Месяц назад

    bhaiya mana ya code c++ ma kiya to some test case pass ne ho ra ?..

  • @nileshchandra3317
    @nileshchandra3317 Месяц назад

    not working for second case

    • @shashwat_tiwari_st
      @shashwat_tiwari_st  Месяц назад

      You might have missed something, check your code once more.

  • @navneetsingh7209
    @navneetsingh7209 Месяц назад

    Sir please update your version
    Yup study model like 1990s
    Git gaakar r1 r2 r3 bol rhe ho ye kya tarika hai