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

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

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

  • @GregHogg
    @GregHogg  4 месяца назад +3

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

  • @Kainum
    @Kainum 6 месяцев назад +1

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

  • @jyotirmaysingh4059
    @jyotirmaysingh4059 6 месяцев назад +8

    Step 1 take transpose
    Step 2 reverse row

  • @brandonblackwell1216
    @brandonblackwell1216 3 месяца назад +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.

  • @darkhorse4466
    @darkhorse4466 3 месяца назад +2

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

  • @ayrtoncoelho8925
    @ayrtoncoelho8925 2 месяца назад

    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

  • @DrDeuteron
    @DrDeuteron 5 месяцев назад

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

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

    You are the best man!!! thanks

  • @thatbomb2658
    @thatbomb2658 4 месяца назад +1

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

  • @teostorm1
    @teostorm1 2 месяца назад

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

  • @bibekkarki5196
    @bibekkarki5196 3 месяца назад +1

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

    • @kingfriizy5707
      @kingfriizy5707 2 месяца назад +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 2 месяца назад +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.

  • @JoeTan-nq4fq
    @JoeTan-nq4fq 2 месяца назад

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

  • @teostorm1
    @teostorm1 2 месяца назад +1

    I feel like vertical reflection would make more sense haha.

  • @DrDeuteron
    @DrDeuteron 5 месяцев назад +2

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

  • @hamids4550
    @hamids4550 7 дней назад

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

  • @mauricemurillo556
    @mauricemurillo556 6 месяцев назад

    any tips on how to learn algorithms and data structures?

  • @lifeisbeautiful9945
    @lifeisbeautiful9945 6 месяцев назад +2

    Amazing

  • @edudictivecoder464
    @edudictivecoder464 5 месяцев назад +2

    TCS question

    • @ARkhan-xw8ud
      @ARkhan-xw8ud 5 месяцев назад

      leetcode

    • @edudictivecoder464
      @edudictivecoder464 5 месяцев назад

      @@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