Super Hard Google OA | 60LPA | Graph Algorithmic Special Solution By Kumar K

Поделиться
HTML-код
  • Опубликовано: 26 дек 2024

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

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

    Superb teaching style 👌👌👌👌

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

    We can also use a "Decreasing monotonic double ended queue".
    Start iterating from the begining of the array,
    => If there is no element in the deque, append the element
    => If the element is smaller than the last element of deque, append it
    => If the element is bigger than the last element and if there is only one element, no need to add it to the queue
    => if the element is bigger than the last element and if there are more than one element in the queue, start from the start of queue and delete the elements in the queue if queue[i] < curElement until there is one element left in the queue
    => In this case dont append the curElement, as it is already bigger than the last element in the queue(As the queue is monotonically decreasing)
    => Finally if the queue has only one element return True, else return False.
    Time Complexity: O(N) (at max two passes - 2*N, one to the array and one to the deque itself).
    Space Complexity: O(N) (in the worst case, the deque can grow larger upto size N if all the elements are in descending order).
    ---------------------------------------------Sample code in python--------------------------------------------------------
    def can_reduce_to_single_node(arr):
    dq = deque()
    for node in arr:
    if not dq:
    dq.append(node)
    elif node < dq[-1]:
    dq.append(node)
    else:
    while len(dq) > 1 and dq[0] < node:
    dq.popleft()
    return len(dq) == 1

    • @kumarkdsa
      @kumarkdsa  Месяц назад +3

      Nice idea.

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

      @kumarkdsa thank you, btw ur way of simplifying the question is amazing 🤩

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

      @@mentanagavenkatasrinivas9245 Thankyou 😀😁

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

    Brilliant explanation sir 🔥🔥

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

    Brilliant Kumar K sir❤🎉🎉🎉❤

  • @Amansingh-in4sy
    @Amansingh-in4sy Месяц назад +1

    awesome sir 🔥

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

    Problem 🔥🔥🔥

  • @HimanshuSingh-iw2te
    @HimanshuSingh-iw2te Месяц назад

    intution - traversing from back and finding nge through monotonic stack . there can be at max only 1 element which has not nge if it is morethan 1 then no otherwise yes

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

    We can solve it using dsu as well union by rank or size and for optimising we can use path compression

  • @AmanSinghRawat-ug6fr
    @AmanSinghRawat-ug6fr Месяц назад

    Sir cant we solve this by just mainting index of max and min element and if the index of min is to the left of the index of max ans is always possible else not possible

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

      That does not work as explained in other comment below.

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

    Montonic Stack possible?

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

    Can we get the next greater element of all the elements in the array, and then check how many elements don't have a next greater element. If the number is 0, 1, or 2 then YES, else NO

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

    What i thought of was really simple
    [Claim] : We can check the the max_ele_idx and the min_ele_idx, if the min_ele_idx is after the max_ele_idx, the answer is always no
    can you suggest some counter example to prove it wrong (only if it is wrong)

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

      for ex [4 5 6 1 3 2 ] in the test case discused 1 was coming after 6, hence the answer is no

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

      Take example of [2, 5, 1, 4, 3], even though index of 5 < index of 1, we can still delete them. Take (2,5) delete 5, take (2, 4) delete 2, then (1, 4) delete 4 and finally (1,3) and delete 3

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

      @@prashantshukla133 oh thank you, i didn't think it in this way