Range Sum of Sorted Subarray Sums - Leetcode 1508 - Python

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

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

  • @business_central
    @business_central 3 หลายเดือนก่อน +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 3 หลายเดือนก่อน +37

    Neetcode have my kids

  • @ursalight
    @ursalight 3 หลายเดือนก่อน

    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 14 วันที่ผ่านมา

    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 3 หลายเดือนก่อน

    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

  • @adityamallik3064
    @adityamallik3064 3 หลายเดือนก่อน

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

  • @johnj171
    @johnj171 3 หลายเดือนก่อน

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

  • @MykolaPavluchynskyi
    @MykolaPavluchynskyi 3 หลายเดือนก่อน +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.

  • @sreenivaskrishna7351
    @sreenivaskrishna7351 3 หลายเดือนก่อน

    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!

  • @parthdeshwal4419
    @parthdeshwal4419 3 หลายเดือนก่อน +1

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

  • @sundew_ii
    @sundew_ii 3 หลายเดือนก่อน

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

  • @yosephe6342
    @yosephe6342 3 หลายเดือนก่อน

    I love you Mr. Code

  • @Codisrocks
    @Codisrocks 3 หลายเดือนก่อน

    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.

  • @mayankpant5376
    @mayankpant5376 3 หลายเดือนก่อน +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 3 หลายเดือนก่อน

      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.

  • @debangshudey5515
    @debangshudey5515 3 หลายเดือนก่อน

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

    • @dan_____
      @dan_____ 3 หลายเดือนก่อน

      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 3 หลายเดือนก่อน +1

      Hey, can u share ur solution please

    • @dan_____
      @dan_____ 3 หลายเดือนก่อน

      @@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 3 หลายเดือนก่อน

      mate share the solution please

  • @pastori2672
    @pastori2672 3 หลายเดือนก่อน

    elegant ash heap solution

  • @greatfate
    @greatfate 3 หลายเดือนก่อน +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 3 หลายเดือนก่อน

      Can you explain the solution?

    • @HoneyBadger447
      @HoneyBadger447 3 หลายเดือนก่อน +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"

  • @chrischika7026
    @chrischika7026 3 หลายเดือนก่อน +2

    9:32 2 + 6 = 6 lol

  • @ashwingadve7522
    @ashwingadve7522 3 หลายเดือนก่อน +3

    Please cover contest as well.

  • @BikerInsights
    @BikerInsights 3 หลายเดือนก่อน +1

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

  • @akashverma5756
    @akashverma5756 3 หลายเดือนก่อน

    This problem is similar to merge k sorted list.

  • @nipunyadav989
    @nipunyadav989 3 หลายเดือนก่อน +1

    looks like Dijkstra

  • @vaibhavbansal1244
    @vaibhavbansal1244 3 หลายเดือนก่อน +18

    People are here for the binary search approach

    • @kapilkhandelwal48
      @kapilkhandelwal48 3 หลายเดือนก่อน

      yes

    • @NeetCodeIO
      @NeetCodeIO  3 หลายเดือนก่อน +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 3 หลายเดือนก่อน +1

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

    • @shambhavikhare9850
      @shambhavikhare9850 3 หลายเดือนก่อน

      True!

    • @chrischika7026
      @chrischika7026 3 หลายเดือนก่อน

      @@ashaynaik7540 India ? I assume.

  • @ashish4k07
    @ashish4k07 3 หลายเดือนก่อน +2

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

    • @rjarora
      @rjarora 3 หลายเดือนก่อน +1

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

    • @ashish4k07
      @ashish4k07 3 หลายเดือนก่อน

      @@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 3 หลายเดือนก่อน

      @@rjarora many are telling binary search as comment btw

    • @harishms5330
      @harishms5330 3 หลายเดือนก่อน

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

    • @ashish4k07
      @ashish4k07 3 หลายเดือนก่อน

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

  • @taufiqrahman2363
    @taufiqrahman2363 3 หลายเดือนก่อน

    First

  • @Mad_Monk29
    @Mad_Monk29 3 หลายเดือนก่อน +1

    i was here for boinary search solution not this onw

    • @inferno4738
      @inferno4738 3 หลายเดือนก่อน

      Same buddy
      That went over my head so hard

  • @AndrewKoulogeorge
    @AndrewKoulogeorge 3 หลายเดือนก่อน

    Binary Search ? :(