Rotate Image - Leetcode 48 - Arrays & Strings (Python)

Поділитися
Вставка
  • Опубліковано 14 січ 2025

КОМЕНТАРІ • 25

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

    Master Data Structures & Algorithms For FREE at AlgoMap.io!

  • @Kainum
    @Kainum 7 місяців тому +1

    I love this series. Helps in understanding DSA a bit optimized way.

  • @brandonblackwell1216
    @brandonblackwell1216 4 місяці тому +1

    When transposing the inner loop (i+1, n) definitely takes some extra thought. Wouldn't have been able to come up with that in an interview setting.

  • @jyotirmaysingh4059
    @jyotirmaysingh4059 7 місяців тому +8

    Step 1 take transpose
    Step 2 reverse row

  • @darkhorse4466
    @darkhorse4466 4 місяці тому +2

    What is the intuition behind this approach ? If not intuition, is it just by observation ?

  • @zerotwo917
    @zerotwo917 27 днів тому

    Another way of looking at this would be all the elements of the first column would become the first row but in the reverse order and the same goes for all the columns
    n=len(matrix)
    ans =[]
    for _ in range(n):
    ans.append([0*n]*n)
    i=0
    m=n
    while i

  • @bibekkarki5196
    @bibekkarki5196 4 місяці тому +1

    In transpose part why the loop for J runs from (i+1 to n) instead of ( only n)

    • @kingfriizy5707
      @kingfriizy5707 4 місяці тому +2

      Because we would end up swapping elements twice leaving the board essentially unchanged. (i,j) would swap with (j,i) but when u get to (j,I) you’re swapping it again with (i,j).

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

      I solved by implementing it to n and adding a if statment "if i>= j:". If you think about it visualy, the (i+1 to n) or "if i>= j" is always selecting a element that is after the main diagonal and swapping with one before.

  • @DrDeuteron
    @DrDeuteron 6 місяців тому

    Also, from the transposed matrix mT:
    >>>list(map(operator.methodcaller('reverse'), mT))
    does it inplace.

  • @thatbomb2658
    @thatbomb2658 5 місяців тому +1

    couldn't we also do matrix[i].reverse() in the second loop?

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

    You are the best man!!! thanks

  • @mauricemurillo556
    @mauricemurillo556 7 місяців тому

    any tips on how to learn algorithms and data structures?

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

    To conceptualize this, I tried flipping my notebook without rotating it. In every case this worked.

  • @lifeisbeautiful9945
    @lifeisbeautiful9945 7 місяців тому +2

    Amazing

  • @JoeTan-nq4fq
    @JoeTan-nq4fq 3 місяці тому

    Is this considered in-place modification -
    for idx, lst in enumerate(map(reversed, zip(*matrix))):
    matrix[idx] = list(lst)

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

    Without knowing this, what I found out is that you can rotate elements by "levels". Imagine the matrix as an onion, the first level would be the outermost layer consisting of the first and last row, and first and last column. So after rotating every element in the first level, you are left with an inner matrix of size N-1. So all you need to do is repeat the same process N // 2 times. N being the matrix' size.
    Here's the code:
    ```
    level = 1
    N = len(matrix)
    while level

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

    I feel like vertical reflection would make more sense haha.

  • @DrDeuteron
    @DrDeuteron 6 місяців тому +2

    matrix = list(map(list, map(reversed, zip(*matrix))))

  • @hamids4550
    @hamids4550 Місяць тому

    coming up with these answers are definitely not easy, whoever solves these is cracked lol

  • @edudictivecoder464
    @edudictivecoder464 6 місяців тому +2

    TCS question

    • @ARkhan-xw8ud
      @ARkhan-xw8ud 6 місяців тому

      leetcode

    • @edudictivecoder464
      @edudictivecoder464 6 місяців тому

      @@ARkhan-xw8ud bro I am sorry for Mis typed but this question was asked in TCS 2023 September and there is sort colors problem on leetcode that same question was asked too as passenger luggage risk problem