Magic Squares In Grid - Leetcode 840 - Python

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

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

  • @yang5843
    @yang5843 3 месяца назад +25

    You didn't think the Leetcode problems could get worse, yet here we are.

  • @gmh14
    @gmh14 3 месяца назад +15

    this week is just the worst problems week, there's no category, and leetcode knows it

  • @NishaRathod-eu3rw
    @NishaRathod-eu3rw 3 месяца назад +29

    Didn't feel like doing this problem, but streak :(

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

    That second solution is awesome. Idk, I get excited seeing math solutions with these logic tricks. It's like finding a trap door. If this wasn't for leetcode, i'd be feeling more positively about this

  • @MP-ny3ep
    @MP-ny3ep 3 месяца назад +4

    Thank you so much for the daily leetcode.

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

    Me to me after going through 15:52: that’s cool, let’s settle for the FIRST OPTIMAL SOLUTION.

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

    the way you explained second approach seems a piece of cake now. Thank you very much🙌

  • @satyamjha68
    @satyamjha68 3 месяца назад +6

    Solved it!!

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

    Thanks for the explanation! But I don't think the question mentioned that the sum of each row/col/diag is 15.

    • @NeetCodeIO
      @NeetCodeIO  3 месяца назад +23

      Aw I forgot to explain that part, since sum(1..9) = 45, and each row has to have the same sum, it must be 15. Same for columns and diagonals

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

      @@NeetCodeIO nice observation

  • @hithambasheir3283
    @hithambasheir3283 3 месяца назад

    Got the intuition and did a slightly different solution, did a few optimizations (with less readability) in the isMagic method
    def numMagicSquaresInside(self, grid: List[List[int]]) -> int:
    if len(grid) < 3 or len(grid[0]) < 3:
    return 0
    def isMagic(r, c):
    cols = [0 for _ in range(3)]
    rows = [0 for _ in range(3)]
    diags = [0 for _ in range(2)]
    seen = set()
    for i in range(r, r+3):
    for j in range(c, c+3):
    if grid[i][j] in seen or not (1

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

    Solution 1: 😀
    Solution 2: 💀

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

    implementing the brute force approach gives
    0ms Beats 100.00% in java wow
    😂😂

  • @Graveness4920
    @Graveness4920 3 месяца назад

    simpler optimization is calc prefix sum of rows and cols then check sub matrix is 45 if yes run check for magic square

  • @elliottkau7089
    @elliottkau7089 3 месяца назад

    In the second solution, won't it fail on a grid that follows the rotational pattern but puts the odds in the corners and evens in the edges? Should there also be a second condition on pattern checking that makes sure [r-1][c] is odd?

  • @tunno4586
    @tunno4586 3 месяца назад

    Got a similar brute force Approach but nice explaination!

  • @rajdippal51
    @rajdippal51 3 месяца назад

    Second solution is just crazy 🤯🤯

  • @chien-yuyeh9386
    @chien-yuyeh9386 3 месяца назад

    🎉🎉 Nice one

  • @jad_c
    @jad_c 3 месяца назад +18

    this problem sucks

  • @galkk3
    @galkk3 3 месяца назад

    I liked the math solution, hard to think about it but it's pretty nice

  • @FahmiJemal-t7l
    @FahmiJemal-t7l 3 месяца назад

    we were challenged by this problem when we were a kid

  • @sundew_ii
    @sundew_ii 3 месяца назад +9

    two 20+ min solution vids in one week. god help us

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

      Yeah aren't the problems supposed to be easy at the beginning of the month

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

      I think the problem itself is easy enough if you stick to the first solution.
      A bit tedious but definitely much better than that Integer to English one...

  • @InquisitiveLittleSteps
    @InquisitiveLittleSteps 3 месяца назад

    Is the possible equal sum always 15? How can we arrive at that conclusion? Please Help...

  • @michaelroditis1952
    @michaelroditis1952 3 месяца назад

    today I got my first "beats 100%" with a runtime of 25 ms without watching this video!!

  • @roderickli6505
    @roderickli6505 3 месяца назад

    why do the rows cols and diag sum up to 15?

  • @THK070
    @THK070 3 месяца назад

    That math solution is pretty nice, but hard to come up with.
    Can anyone tell which tool neet uses for drawing explanation?

  • @varunallagh7462
    @varunallagh7462 3 месяца назад

    can u tell whee is it written that sum should be 15

  • @pastori2672
    @pastori2672 3 месяца назад

    i lost it when you started rotating and flipping the matrix xd (i am losing it)

  • @YahyaNaeem-m7r
    @YahyaNaeem-m7r 3 месяца назад

    Why you took 1 first instead of 2,3,4,5,6,7,8,9 and then made patterns on 14 ?

    • @NeetCodeIO
      @NeetCodeIO  3 месяца назад

      Just to go through them in order, it's arbitrary but easier to keep track of starting at 1.
      We want them to sum to 15, hence 15 - 1 = 14

  • @vim2741
    @vim2741 3 месяца назад

    do u have any coupon code for lifetime acess pls

  • @parthdeshwal4419
    @parthdeshwal4419 3 месяца назад

    i'm gonna settle with the first solution

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

    Raise your hands if your brain got rotated at 19:00 😂🙋‍♂️

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

    first time i'm the first viewer PogChamp