3244. Shortest Distance After Road Addition Queries II | Weekly Leetcode 409

Поділитися
Вставка
  • Опубліковано 11 лис 2024

КОМЕНТАРІ • 18

  • @princenagar1686
    @princenagar1686 2 місяці тому

    Thanks that condition make this problem easy. Solved it after watching half video.

  • @Nishit_369
    @Nishit_369 3 місяці тому +2

    The constraints made me think of Snakes and Ladders. Q2 is the classic game where you are curently climbing a small ladder but you know that 3 or 4 steps ahead there was a bigger ladder that could shorten your journey from 0 to 100. On the other hand, Q3 is more like a modified game of SnL where no ladder paths overlap or maybe all ladders are sequentially placed except that they may start and end at same cells. Great Problem and great explanation.

  • @sudhadevi6692
    @sudhadevi6692 3 місяці тому +1

    Same as always Awesome lecture ❤

  • @souravshaw8904
    @souravshaw8904 3 місяці тому

    Great explanation 💯

  • @nikhilsoni2403
    @nikhilsoni2403 3 місяці тому +5

    You can actually do it in linear time by using a linked list and deleting nodes in the middle of two nodes between which a new edge is added because the points in between are useless now. Initially set the distance betweem the node 0 and node 'n-1' to be n-1. And as you delete each node in the linked list,You just decrement the distance counter. Since each node is deleted only once. The time complexity should be O(Q+N)

    • @krrishnichanii3046
      @krrishnichanii3046 3 місяці тому +1

      I also tried this but it is giving TLE , how did u delete the nodes in between in less than O(N) , because even for the intervals which are already deleted we have to check if they exist in the ll or not . Please explain

    • @NikhilSoni-f1j
      @NikhilSoni-f1j 3 місяці тому

      ​@@krrishnichanii3046 Actually i didnt use linked list, but used the same idea and did it without linked lists in linear time: C++ code :
      int pointsTo[100000];
      bool gone[100000];
      class Solution {
      public:
      vector shortestDistanceAfterQueries(int n, vector& queries) {
      int shortest = n - 1, left, right;
      vector answers(queries.size());
      for (int i = 1; i < n; ++i) {
      pointsTo[i - 1] = i;
      gone[i] = false;
      }
      for (int q = 0; q != queries.size(); ++q) {
      left = queries[q][0];
      right = queries[q][1];
      if (gone[left] || pointsTo[left] > right) {
      answers[q] = shortest;
      continue;
      }
      while (pointsTo[left] != right) {
      gone[pointsTo[left]] = true;
      pointsTo[left] = pointsTo[pointsTo[left]];
      --shortest;
      }
      answers[q] = shortest;
      }
      return answers;
      }
      };

    • @codingmohan
      @codingmohan  3 місяці тому +1

      Yes, nice catch!

    • @ruturaj_dm
      @ruturaj_dm 3 місяці тому

      Here is a simple solution using Map. Trying to mock the behavior of a LinkedList.
      public int[] shortestDistanceAfterQueries(int n, int[][] queries) {
      int[] ans = new int[queries.length];
      int iAns = 0;
      //Maintain currNode to nextNode map
      Map connectionMap = new HashMap();
      for (int i = 0; i < n-1; i++) {
      connectionMap.put(i, i+1);
      }
      for (int[] query : queries) {
      int u = query[0];
      int v = query[1];
      //Update connections
      if (connectionMap.containsKey(u)) {
      int temp = connectionMap.get(u);
      while (temp < v) {
      int next = connectionMap.get(temp);
      connectionMap.remove(temp);
      connectionMap.put(u, next);
      temp = next;
      }
      }

      ans[iAns++] = connectionMap.size();
      }
      return ans;
      }

  • @anonymous10906
    @anonymous10906 3 місяці тому +2

    please make solution to yesterday's 4th ques

  • @nikhilsoni2403
    @nikhilsoni2403 3 місяці тому +1

    21:54 i think its because we cannot iterate on a set or a map and delete elements from it at the same time. I think it changes the structure of the tree in the internal implementation or something like that, which causes a runtime error. I am not completely sure tho...Please correct me if i am wrong ..

  • @kashishchawla2754
    @kashishchawla2754 3 місяці тому +1

    pls make solution video yesterdays biweekly contest 4th question bhaiya:)

  • @sreeprasadsp
    @sreeprasadsp 3 місяці тому +1

    This is one of the underrated channels. Very good explanation. Also if you do
    while(it!=all.end() && *it

    • @Truysジャ
      @Truysジャ 3 місяці тому

      Where r u incrementing the pointer?

  • @adityaroychowdhury3709
    @adityaroychowdhury3709 3 місяці тому

    ngl, i actually thought of this approach, but noob me thought this will be an obvious TLE, so i just skipped this idea and went towards graphs.

    • @codingmohan
      @codingmohan  3 місяці тому +1

      Happens to all of us.
      Next time before tracking back, try to write the rough pseudocode and it would help avoid these kind of issues to some extent.