Range Sum of Sorted Subarray Sums - Leetcode 1508 - Python

Поділитися
Вставка
  • Опубліковано 22 жов 2024

КОМЕНТАРІ • 54

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

    love the advice on don't bother with this if you are preparing just for interviews. Thank you! I'd have probably lost a day on that editorial!
    Thank you NeetCode!

  • @grantwells16
    @grantwells16 2 місяці тому +37

    Neetcode have my kids

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

    just wanted to say your videos are extremely easy to understand. i've used your channel for hard daily leetcode questions in the past, and you make it alot easier. i liked your realistic comments in this video as well.

  • @bombrman1994
    @bombrman1994 13 годин тому

    i am preparing for interviews and i came across monotonic stack and queue questions and started banging my head against the wall learning these concepts and trying to solve the questions. I am wondering if Amazon/Google do ask these questions on the final loops especially the amazon final loop

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

    I made the subarray_sums, then heapified it, and then popped "right" elements, I summed up the last "left" elements and returned it.
    Overall for a time complexity of n^2 + right*log(n^2) = n^2*log(n) but if right is a small number it's more efficient

  • @parthdeshwal4419
    @parthdeshwal4419 2 місяці тому +1

    i spent 3-4 hrs on editorials and claude trying to understand and nothing made sense until now

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

    Hey Neetcode! Thank you for doing the daily Questions it really helps!

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

    Today's daily challange was asm !! i came to learn something new! thanks for explaining

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

    Hey @NeetCodeIO can you please do an episode on range module LC#715? Trying to understand segment trees and was looking for the Neetcode version. Best explained leetcode ever!! thank you so much for all you do!

  • @mayankpant5376
    @mayankpant5376 2 місяці тому +1

    a question, I understand we have to mod every sub array sum, but why are we modding the sub array sum again when adding it to the final result? this is not asked in the question.

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

      Actually we don't need to MOD when making subarray sum
      because question states the max value of n = 1000 and nums[i] = 100,
      so in worst case we can have all 1000 element values to 100 still the sum of largest subarray which is 1000 will not excess the 32bit int.
      1000(1001) / 2 = 500500,
      but when summing the subarray from left to right, in worst case we have to sum whole subarray i.e 500500 * n ^ 2, which can exceed the 32 bit int limit.

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

    I think there's a problem with your first solution. Using that modulo while creating the sums array would break the sort of the numbers were large enough. The only reason it works still is that the sub array sums will never become that large.

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

    There is also an approach to get n^2 solution. C++ has a convenient std::nth_element which easily removes the need for full sort. I'm sure counting sort also works here

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

      Can you explain the solution?

    • @HoneyBadger447
      @HoneyBadger447 2 місяці тому +1

      @@mahdiadib24 call nth_element twice, first to rearrange elements on [0, size) for "right-1"th, then on range [0, right) for "left-1"

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

    pretty simple problem once you understand the description and separate it into manageable chunks :)

  • @ashwingadve7522
    @ashwingadve7522 2 місяці тому +3

    Please cover contest as well.

  • @MykolaPavluchynskyi
    @MykolaPavluchynskyi 2 місяці тому +1

    "don't ask me how to come up with it. I think that's a question for God to be honest" - best moment of today's video. And I completely agree.

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

    I have done it with help of recursion.
    Time complexity = O(NlogN)
    Space complexity = O(N)
    Is it optimal?

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

      There is an O(N*log(M)) time, where M is the sum of all elements in the array, and O(1) space solution. It involves binary search (which you probably already implemented), and some math to calculate the required subarray sums on the fly instead of having the prefix sum array.

    • @6mahine_mein_google
      @6mahine_mein_google 2 місяці тому +1

      Hey, can u share ur solution please

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

      @@6mahine_mein_google Just browse the problem's solutions and search for sliding window, theres a few approaches. It might be tough to grasp at first so I'd recommend spending some time on them. Maybe even using an LLM to chat about the solution and understand what topics are involved.

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

      mate share the solution please

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

    I love you Mr. Code

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

    elegant ash heap solution

  • @BikerInsights
    @BikerInsights 2 місяці тому +1

    I first did the N²LogN then came to see the optimized approach by Neetcode and got the treat

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

    This problem is similar to merge k sorted list.

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

    9:32 2 + 6 = 6 lol

  • @nipunyadav989
    @nipunyadav989 2 місяці тому +1

    looks like Dijkstra

  • @vaibhavbansal1244
    @vaibhavbansal1244 2 місяці тому +18

    People are here for the binary search approach

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

      yes

    • @NeetCodeIO
      @NeetCodeIO  2 місяці тому +26

      I know many people will not like this, but the smartest thing to do is realize that there are more common patterns to focus on. Rather than the binary search + sliding window pattern which you will almost never see in an interview.
      If you are smart enough to understand that approach, you're smart enough to realize there are better ways to spend your time.

    • @ashaynaik7540
      @ashaynaik7540 2 місяці тому +1

      @@NeetCodeIO had that pattern asked for my interview yesterday and i gave n2logn approach and got rejected😢.

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

      True!

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

      @@ashaynaik7540 India ? I assume.

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

    I closed the editorial after seeing the sliding window + binary search soln

    • @rjarora
      @rjarora 2 місяці тому +1

      You shouldn't directly reveal the solution like this for those who haven't solved.

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

      @@rjarora I thought if they really want to solve on own only why will the come to the video at first point you didn't knew that's why you came know I hope you understand ,I just said my opinion or how should I tell my opinion then 🤷

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

      @@rjarora many are telling binary search as comment btw

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

      ​@@ashish4k07 i have done this que is O(n^2 nlogn) is it good?

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

      @@harishms5330 n^2logn is fine but n^3logn 🙃

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

    Binary Search ? :(

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

    First

  • @Mad_Monk29
    @Mad_Monk29 2 місяці тому +1

    i was here for boinary search solution not this onw

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

      Same buddy
      That went over my head so hard