Robot Collisions - Leetcode 2751 - Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ย. 2024

ความคิดเห็น • 52

  • @unstoppablecoder8110
    @unstoppablecoder8110 2 หลายเดือนก่อน +30

    Wohoo, Today i solved the problem myself
    It is a great achivement for me 😁

  • @K_Ritik
    @K_Ritik 2 หลายเดือนก่อน +22

    May god bless you bro making amazing videos for free

  • @SarweshParsewar
    @SarweshParsewar 2 หลายเดือนก่อน +10

    This was the first hard problem I was able to solve by myself completely feels great😄

    • @anandsahoo3711
      @anandsahoo3711 2 หลายเดือนก่อน

      dayum bro so proud of you

  • @ROHANMANNA-il7nt
    @ROHANMANNA-il7nt 2 หลายเดือนก่อน +3

    Thanks, for that easy explanation 🙂. Currently i am beginner , i was struct in this problem near mapping and stack but you cleared it very well .
    Again thanks for the explanation🙏🏻☺

  • @StellasAdi18
    @StellasAdi18 2 หลายเดือนก่อน

    This is master piece. How to make contrite problem lucid to follow.

  • @saranshthukral4021
    @saranshthukral4021 2 หลายเดือนก่อน +2

    @3:21 bro understands robot🤖🤖 now

  • @MP-ny3ep
    @MP-ny3ep 2 หลายเดือนก่อน +1

    Great explanation as always. Thank you

  • @notcountdankula
    @notcountdankula 2 หลายเดือนก่อน +1

    Isn't this kind of Asteroid collision problem?

  • @monismomin6759
    @monismomin6759 2 หลายเดือนก่อน +1

    awsome bro
    Thanks for great intuition

  • @solomonkinard8500
    @solomonkinard8500 หลายเดือนก่อน

    3:18 robot living its best life.

  • @ursalight
    @ursalight 2 หลายเดือนก่อน

    indices = list(range(n))
    indices.sort(key=lambda x: positions[x])
    is another way to keep track of indices without the dictionary work

  • @akshaanshsrivastava9817
    @akshaanshsrivastava9817 2 หลายเดือนก่อน +1

    Thanks Sir to make this video..

  • @greatfate
    @greatfate 2 หลายเดือนก่อน +1

    Should probably be a medium, not even a hard medium

  • @deepakjain4481
    @deepakjain4481 หลายเดือนก่อน

    its all about understand the question i guess solution is quite easy to imagine

  • @rk0i7
    @rk0i7 2 หลายเดือนก่อน

    Amazing video @neetcode
    Can please make a video on cat and mouse leetcode problem

  • @anonanon6596
    @anonanon6596 2 หลายเดือนก่อน +1

    Could you first explain the problem before giving the solution?
    That's what you usually do, and today I feel like I missed out on a problem I could have solved myself.

  • @satyamjha68
    @satyamjha68 2 หลายเดือนก่อน

    Solved it!

  • @VinayKumar-xs6el
    @VinayKumar-xs6el 2 หลายเดือนก่อน +1

    this is exact same as astroid collision

    • @notcountdankula
      @notcountdankula 2 หลายเดือนก่อน

      That's what I thought too

  • @MohamedMpenda
    @MohamedMpenda 2 หลายเดือนก่อน

    Naitaji kujiunga ❤❤❤❤

  • @gameacc6079
    @gameacc6079 2 หลายเดือนก่อน +2

    imagine if this question involved speed gg

    • @SkySentry7
      @SkySentry7 2 หลายเดือนก่อน +1

      that would have been a nightmare

    • @saibunny1253
      @saibunny1253 หลายเดือนก่อน

      Like how much?​@@SkySentry7

    • @SkySentry7
      @SkySentry7 หลายเดือนก่อน

      @@saibunny1253 how much what?

  • @n0ne0ne
    @n0ne0ne 2 หลายเดือนก่อน

    can we do this without sorting?

    • @Chandakshaythakur
      @Chandakshaythakur 2 หลายเดือนก่อน +2

      Na bro then it will give special error for some edge cases

    • @zweitekonto9654
      @zweitekonto9654 2 หลายเดือนก่อน

      it is possible, but you cannot use stack then. you'll have to use binary search, which will result in the same complexity.

  • @chiragsharma8905
    @chiragsharma8905 2 หลายเดือนก่อน

    Sir, I would recommend to use either Pseudo Code, or simpler Python( and not one liners or Python for coding interviews) as I think many viewers could be using other languages like Java, C++, etc. Introducing one liners will kind of make our experience less good.

    • @NeetCodeIO
      @NeetCodeIO  2 หลายเดือนก่อน +8

      I agree, but the one liners in this case are pretty trivial to translate to other languages. Since it's a leetcode hard, I'm pretty sure most viewers of this one would be able to write a loop to add elements to a map or list.
      There's a lot of value in having less verbose code. I don't wanna hold the python viewers back just because some people still use java (no offense to the java people)

    • @chiragsharma8905
      @chiragsharma8905 หลายเดือนก่อน

      @@NeetCodeIO Thanks for replying sir. These much one liners are okay, but please don't go further in this direction, where trouble starts to arise for other language users. I have no hate for Python, I just prefer strongly typed languages.
      I really appreciate your videos, and I am following you for a long time. Thank you for your amazing explanations😃.

  • @HimanshuTanwani
    @HimanshuTanwani 2 หลายเดือนก่อน

    My solution (code below), using the similar stack approach has the same TC ( O(nlogn) ) and SC ( O(N) ) but it TLE'd (Test Cases 2430 / 2433 passed). Do you think this will be accepted as a good solution in interview.
    ```
    class Solution:
    def survivedRobotsHealths(self, positions: List[int], healths: List[int], directions: str) -> List[int]:
    robots = sorted([list(x) for x in zip(positions, directions, healths)])
    stack = []
    for robot in robots:
    skip = False
    while stack and stack[-1][1] == 'R' and robot[1] == 'L':
    if stack[-1][2] > robot[2]:
    stack[-1][2] -= 1
    skip = True
    break
    elif stack[-1][2] < robot[2]:
    stack.pop()
    robot[2] -= 1
    elif stack[-1][2] == robot[2]:
    stack.pop()
    skip = True
    break
    if not skip:
    stack.append(robot)
    stack = sorted(stack, key=lambda x: positions.index(x[0]))
    return [x[2] for x in stack]
    ```

    • @SubhamKumar-eg1pw
      @SubhamKumar-eg1pw 2 หลายเดือนก่อน

      Did you use chatgpt here? 🤔
      I think tuple sorting is taking extra time here

    • @HimanshuTanwani
      @HimanshuTanwani 2 หลายเดือนก่อน

      @@SubhamKumar-eg1pw No ChatGPT

  • @K_Ritik
    @K_Ritik 2 หลายเดือนก่อน

    I'm first to comment,

    • @SkySentry7
      @SkySentry7 2 หลายเดือนก่อน +2

      Give this guy a medal lol

  • @singh_pratistha
    @singh_pratistha 2 หลายเดือนก่อน

    It still is not appropriate and doesnot pass this test case:
    Input
    positions =
    [1,40]
    healths =
    [10,11]
    directions =
    "RL"
    Use Testcase
    Output
    []
    Expected
    [10]

    • @NeetCodeIO
      @NeetCodeIO  2 หลายเดือนก่อน

      I mean my code passed in the video. Is it possible you have a typo somewhere?

  • @CuriousAnonDev
    @CuriousAnonDev 2 หลายเดือนก่อน

    i solved it without using stack
    (used binary search 💀)
    took 432ms (beats 10%) lmao

    • @musickilely
      @musickilely 2 หลายเดือนก่อน

      can you share how ?

    • @CuriousAnonDev
      @CuriousAnonDev 2 หลายเดือนก่อน

      @@musickilely sure
      1) make 2 lists, right and left robots and sort according to positions
      2) now, for every left robot, search a robot closest to it and moving in right direction, we do this using binary search, returning floor (position of robot moving right) of the target(position of robot moving left)
      3) then, we check the health and remove the robots from list or decrease health according to the condition
      4) at the end, put all the remaining robots in the hashmap
      5) and use hashmap to return list of remaining robots relative to their positions in input positions array
      here's the code for reference
      ```
      class Solution {
      public List survivedRobotsHealths(int[] positions, int[] healths, String directions) {
      int n = directions.length();
      ArrayList right = new ArrayList();
      ArrayList left = new ArrayList();
      for (int i = 0; i < n; i++) {
      char ch = directions.charAt(i);
      if (ch == 'R') {
      right.add(new int[] {positions[i], healths[i]});
      } else {
      left.add(new int[] {positions[i], healths[i]});
      }
      }
      right.sort((a, b) -> a[0] - b[0]);
      left.sort((a, b) -> a[0] - b[0]);
      // be at left idx
      // find floor of robots in right direction using bs
      // if right health < left health, remove right, and decrease left health, repeat process
      // if right health = left health, remove right, move left idx, repeat process
      // if right health > left health, move left idx, decrease right health, repeat process
      // at the end either right or left, any one will become empty, that is when you will have remaining robot in other list
      int leftIdx = 0;
      while (leftIdx < left.size() && !right.isEmpty()) {
      int[] left_robot = left.get(leftIdx);
      int left_pos = left_robot[0];
      int left_health = left_robot[1];
      int floor = floorBS(right, left_pos);
      if (floor == -1) {
      leftIdx++;
      continue;
      }
      int[] right_robot = right.get(floor);
      if (right_robot[1] < left_health) {
      // remove right
      right.remove(floor);
      // decrease left health
      left_robot[1] = left_health - 1;
      } else if (right_robot[1] == left_health) {
      // remove right
      right.remove(floor);
      // left_idx++
      // leftIdx++;
      left.remove(leftIdx);
      } else {
      // right robot health > left robot health
      // decrease right health
      right_robot[1] = right_robot[1] - 1;
      // left_idx++
      // leftIdx++;
      left.remove(leftIdx);
      }
      }
      return answer(positions, healths, left, right);
      }
      // return list as per positions
      static List answer(int[] positions, int[] healths, List left, List right) {
      HashMap map = new HashMap();
      List ans = new ArrayList();
      for (int[] robot : left) {
      map.put(robot[0], robot[1]);
      }
      for (int[] robot : right) {
      map.put(robot[0], robot[1]);
      }
      for (int position : positions) {
      if (map.containsKey(position)) {
      ans.add(map.get(position));
      }
      }
      return ans;
      }
      static int floorBS(ArrayList list, int target) {
      int s = 0;
      int e = list.size() - 1;
      int ans = -1;
      while (s target) {
      e = m - 1;
      } else {
      return m;
      }
      }
      return ans;
      }
      }
      ```

    • @Jedi1976
      @Jedi1976 2 หลายเดือนก่อน

      How bro

    • @CuriousAnonDev
      @CuriousAnonDev 2 หลายเดือนก่อน

      @@musickilely sure
      1) make 2 lists, right and left robots and sort according to positions
      2) now, for every left robot, search a robot closest to it and moving in right direction, we do this using binary search, returning floor (position of robot moving right) of the target(position of robot moving left)
      3) then, we check the health and remove the robots from list or decrease health according to the condition
      4) at the end, put all the remaining robots in the hashmap
      5) and use hashmap to return list of remaining robots relative to their positions in input positions array
      code for reference:
      ```
      class Solution {
      public List survivedRobotsHealths(int[] positions, int[] healths, String directions) {
      int n = directions.length();
      ArrayList right = new ArrayList();
      ArrayList left = new ArrayList();
      for (int i = 0; i < n; i++) {
      char ch = directions.charAt(i);
      if (ch == 'R') {
      right.add(new int[] {positions[i], healths[i]});
      } else {
      left.add(new int[] {positions[i], healths[i]});
      }
      }
      right.sort((a, b) -> a[0] - b[0]);
      left.sort((a, b) -> a[0] - b[0]);
      // be at left idx
      // find floor of robots in right direction using bs
      // if right health < left health, remove right, and decrease left health, repeat process
      // if right health = left health, remove right, move left idx, repeat process
      // if right health > left health, move left idx, decrease right health, repeat process
      // at the end either right or left, any one will become empty, that is when you will have remaining robot in other list
      int leftIdx = 0;
      while (leftIdx < left.size() && !right.isEmpty()) {
      int[] left_robot = left.get(leftIdx);
      int left_pos = left_robot[0];
      int left_health = left_robot[1];
      int floor = floorBS(right, left_pos);
      if (floor == -1) {
      leftIdx++;
      continue;
      }
      int[] right_robot = right.get(floor);
      if (right_robot[1] < left_health) {
      // remove right
      right.remove(floor);
      // decrease left health
      left_robot[1] = left_health - 1;
      } else if (right_robot[1] == left_health) {
      // remove right
      right.remove(floor);
      // left_idx++
      // leftIdx++;
      left.remove(leftIdx);
      } else {
      // right robot health > left robot health
      // decrease right health
      right_robot[1] = right_robot[1] - 1;
      // left_idx++
      // leftIdx++;
      left.remove(leftIdx);
      }
      }
      return answer(positions, healths, left, right);
      }
      // return list as per positions
      static List answer(int[] positions, int[] healths, List left, List right) {
      HashMap map = new HashMap();
      List ans = new ArrayList();
      for (int[] robot : left) {
      map.put(robot[0], robot[1]);
      }
      for (int[] robot : right) {
      map.put(robot[0], robot[1]);
      }
      for (int position : positions) {
      if (map.containsKey(position)) {
      ans.add(map.get(position));
      }
      }
      return ans;
      }
      static int floorBS(ArrayList list, int target) {
      int s = 0;
      int e = list.size() - 1;
      int ans = -1;
      while (s target) {
      e = m - 1;
      } else {
      return m;
      }
      }
      return ans;
      }
      }
      ```

  • @sparkle3024
    @sparkle3024 2 หลายเดือนก่อน

    why is the code not working, some one help me

  • @datchkh
    @datchkh 2 หลายเดือนก่อน +1

    Have you thought about doing Tutorial Videos with your camera on? I think the videos will be so much better like that

    • @JamesBond-mq7pd
      @JamesBond-mq7pd 2 หลายเดือนก่อน +10

      Nah. Extra space

    • @whyredvince
      @whyredvince 2 หลายเดือนก่อน +5

      Or GTA clips covering half the screen?

    • @datchkh
      @datchkh 2 หลายเดือนก่อน

      I feel like seeing one's face and body language while teaching would helpful but I guess that's just me

    • @zweitekonto9654
      @zweitekonto9654 2 หลายเดือนก่อน +2

      @@datchkh you should hire a tutor then.

    • @datchkh
      @datchkh 2 หลายเดือนก่อน

      @@zweitekonto9654 🤓🤓🤓🤓🤓🤓

  • @DontCare-sl1hq
    @DontCare-sl1hq 2 หลายเดือนก่อน

    Can anyone guide me where I'm making mistake and my answer is not printing out properly
    class Solution {
    public:
    vector survivedRobotsHealths(vector& positions, vector& healths, string directions) {
    unordered_mapmp;
    int n = positions.size();
    for(int i=0; i