Leetcode - Search a 2D Matrix II (Python)

Sdílet
Vložit
  • čas přidán 22. 02. 2021
  • February 2021 Leetcode Challenge
    Leetcode - Search a 2D Matrix II #38
    Difficulty: Medium
  • Věda a technologie

Komentáře • 16

  • @benpurcell591
    @benpurcell591 Před 21 dnem

    Nice. I did it recursively using a binary search type thing, searching smaller matrices until result is found. Was more complicated but did get it done. Yours is simpler, glad a checked your channel

  • @felixreynoso7686
    @felixreynoso7686 Před rokem +2

    pretty amazing how simplicity can be so beautiful yet powerful, super clever approach.

  • @CEOofTheHood
    @CEOofTheHood Před 3 lety +7

    MY MIND IS BLOWN!!!!!. This algo works on search 2d matrix 1 as well. In my understanding, it works as long as the columns are sorted, if not we would have to check if the target exists in the range of each row, then binary search through it to find it.

    • @janmichaelaustria620
      @janmichaelaustria620 Před 3 lety +2

      You could also start from the upper right. But I don't think it would work if you started from upper left or lower right. At the lower left and upper right, it is certain that at least one element will be smaller or one element will be greater from your current spot - this isn't the case with the upper left and lower right.
      rows = len(matrix)
      cols = len(matrix[0])
      #start upper right
      i,j = 0, cols-1
      while 0

    • @sneezygibz6403
      @sneezygibz6403 Před 3 lety

      @@janmichaelaustria620 is time O(m + n) or m*n

    • @janmichaelaustria620
      @janmichaelaustria620 Před 3 lety

      @@sneezygibz6403 in the worst case we go all the way up (n) then all the way left (m). So upper bounded by O(m+n).

  • @lipinghu11
    @lipinghu11 Před 2 lety

    glad that I found your channel!

  • @peilin8297
    @peilin8297 Před 11 měsíci

    can't believe it, it just abruptly ends and solved the problem

  • @kolawoleabdulrahman
    @kolawoleabdulrahman Před 10 měsíci

    thank you for this

  • @choliu1918
    @choliu1918 Před 2 lety

    can I switch the order of the if statements? like I check if > then < then ==.

  • @edwardteach2
    @edwardteach2 Před 3 lety

    U a God

  • @sauravdeb8236
    @sauravdeb8236 Před 2 lety

    Could you please your thought when you first see this type of problem?

  • @mohamedgad4291
    @mohamedgad4291 Před rokem

    WOW

  • @pacomarmolejo3492
    @pacomarmolejo3492 Před 2 lety

    niceeee but why is it O(N+M) ??

    • @mohithkailash
      @mohithkailash Před rokem

      taking N as number of rows, M as number of columns, the worst case complexity occurs if target is at top right

  • @AnkitaNallana
    @AnkitaNallana Před měsícem

    FUCK. SO SIMPLE.