Leetcode 83 Problem 3 - Consecutive Numbers Sum (829)

แชร์
ฝัง
  • เผยแพร่เมื่อ 16 ธ.ค. 2024

ความคิดเห็น • 18

  • @lethality3704
    @lethality3704 2 ปีที่แล้ว

    I like the part you explain step by step of your improvement, learned a lot

  • @louise1021
    @louise1021 3 ปีที่แล้ว +1

    I don't understand what the upper and lower bounds are. 15/3=5 then 5-3=2 and 5+3=8. What do those represent?

  • @qilianyu6370
    @qilianyu6370 6 ปีที่แล้ว +2

    Very clear explanation. Thank you!

  • @sapugreat
    @sapugreat 5 ปีที่แล้ว +1

    Thanks for the amazing explanation!!!

  • @chrislam1341
    @chrislam1341 4 ปีที่แล้ว

    why j/2 ? i dont see the reason..

  • @qingshenh5944
    @qingshenh5944 5 ปีที่แล้ว +1

    the second one is fantastic~

  • @_ashout
    @_ashout 5 ปีที่แล้ว +2

    I'm not quite following why ```N - (1 + 2 + ...n) % n == 0``` indicates that N can be formed with window of length n? Can you clarify why this is true?

    • @mahulparikh
      @mahulparikh 5 ปีที่แล้ว +28

      See if this helps.
      For a given integer N, any set of consecutive integers can only add up once (unique) to value N. Example, for number 5, there is only one (unique) set of two consecutive positive integers that add up to 5, i.e, 2 + 3. There is no other set of two consecutive positive integers that add up to 5 (1+2 or 3+4 not equal to 5, only 2+3 is equal to 5). This is the case for three, four, or any other set of consecutive positive integers.
      Now, how to check efficiently if N can be formed from a given set of consecutive positive integers (two, three, four, etc.)? The first integer that can be formed from sum of n consecutive positive integers is n (n+1) / 2, which is the formula for summation of n numbers. Example, for n = 5, the first integer that can be formed is 15 (1+2+3+4+5). The next integer that can be formed from sum of n consecutive positive integers is 15 + 5 = 20 (2+3+4+5+6). The next after that is 15 + 5 + 5 = 25(3+4+5+6+7), and so on. So to check if N can be formed from five consecutive integers in this example, I just have to check if N - (summation of n consecutive integers) is divisible by n. If it is, I can say that N can be formed from some set of 5 consecutive integers. For example, for N = 20 and n = 3, if 20 - (1+2+3), which is 14 is divisible by 3 (it's not), then this means that I cannot express 20 by any combination of three consecutive positive integers.

    • @AbhishekKumar-yv6ih
      @AbhishekKumar-yv6ih 4 ปีที่แล้ว +2

      @@mahulparikh Your explanation was better than the video. So Clear.

    • @jamesperalta35
      @jamesperalta35 4 ปีที่แล้ว +1

      @@mahulparikh Great explanation

    • @gaishiya7696
      @gaishiya7696 4 ปีที่แล้ว +1

      @@mahulparikh Thank you soooo much!! Best explanation sir

    • @learncodebypic
      @learncodebypic 3 ปีที่แล้ว +1

      @@mahulparikh thank you so much. lucid and thorough explanations.

  • @effy1219
    @effy1219 6 ปีที่แล้ว

    thanks for sharing!

  • @nawabalam5267
    @nawabalam5267 6 ปีที่แล้ว +1

    thank you..

    • @code_report
      @code_report  6 ปีที่แล้ว

      No problem! Glad I could help :)

  • @natrain5425
    @natrain5425 6 ปีที่แล้ว

    wonderful