SUBARRAY SUM EQUALS K | LEETCODE 560 | PYTHON SOLUTION

Ғылым және технология

Channel Discord Community: / discord
Problem Link: leetcode.com/problems/subarra...
Today we are solving a god awful problem that is an absolute chore to explain: Subarray Sums Equals K (Leetcode 560). The code for this problem is very straightforward but the explanation is so difficult as you'll see... even I'm struggling to explain and that's helped by the terrible examples given for this problem.
TIMESTAMPS
00:00 Intro
00:08 Question Prompt
00:21 Basic Example
01:39 Solution Intuition
07:28 Coding
12:24 Explaining prefix_dict[0] = 1
14:40 Time/Space Complexity
15:30 Outro

Пікірлер: 18

  • @ajvercueil8111
    @ajvercueil81112 ай бұрын

    "prefix_dict[0] = 1" basically replaces "if prefix_sum == k: counter += 1" in our loop

  • @massimomonticelli6010
    @massimomonticelli60105 ай бұрын

    Using a defaultdict(int) makes the check if the key is in the dictionary unnecessary (line 15), as the defaultdict automatically initializes the value to 0 if the key does not exist.

  • @dnm9931
    @dnm993110 күн бұрын

    What a horrible question! Thanks so much for all you do , you are appreciated !

  • @codewithmarish
    @codewithmarish4 ай бұрын

    At first glance, I couldn't catch up but If we observe carefully it is similar to the two-sum problem and we need to know which point we just need to see if any of the pre-calculated prefixes match with current_sum - k Thanks for your explanation It helped a lot.

  • @YT.Nikolay
    @YT.Nikolay6 ай бұрын

    Ah wow!!! Never knew about this feature, nice to know :)

  • @crackfaang

    @crackfaang

    6 ай бұрын

    You've been along for so long you've more than earned the right to just request the content you want to see. Just DM me on discord

  • @koreandude
    @koreandude2 ай бұрын

    thanks for the awesome explanation! had a question, in the example, shouldn't it be prefixes = [1, 3, 9, 4], if nums = [1, 2, 7, -3]?

  • @tsimbaland2905

    @tsimbaland2905

    2 ай бұрын

    prefix sum says that each next number in prefix sum array should be the sum of the previous ones and the number itself. So, if prefixes = [1, 3, 9, 4], nums = [1, 2, 6, -5] if nums = [1, 2, 7, -3], prefixes = [1, 3, 10, 7] ( 1, 1+2, 1+2+7, 1+2+7-3)

  • @3rd_iimpact
    @3rd_iimpact5 ай бұрын

    It's nice to know that I'm not the only who has a hard time explaining this one, lol.

  • @crackfaang

    @crackfaang

    5 ай бұрын

    Welcome as a channel member mate!

  • @tanvirahmed7993
    @tanvirahmed79934 ай бұрын

    sumI - k = sumJ, where I < J, was the key

  • @dmytryemery
    @dmytryemery5 ай бұрын

    haha, I did this question sooo many times trying to understand prefix sum questions. I was able to code it up from memory, but was totally shook if I got asked it in an interview.. because explaining my thought process :D Seriously this question is hard af. Screw the medium rating.

  • @crackfaang

    @crackfaang

    5 ай бұрын

    Yea it's just one of those where you really need a pen and paper to hammer in the logic. Once it clicks you can understand the solution to all these types of questions but I agree it's really hard the first time seeing it.

  • @skh9749
    @skh97493 ай бұрын

    I think if sum(i) - sum(j) = 0 that means sum(i+1 to j) including element at j equals 0, right? In the example prefix sum of [1,2,3,-3] is [1,3,6,3] so sum(0..1)-sum(0..3) = 0 so sum(2..3) = 0 i=1, j=3 sum(i+1..j)

  • @HarbiDr
    @HarbiDr16 күн бұрын

    Emotional Damage @ 5m 31s

  • @ansupriyadarshi1456
    @ansupriyadarshi1456Ай бұрын

    You could have made that 100X clearer.

  • @crackfaang

    @crackfaang

    Ай бұрын

    Yea this is the shit explanation. My better one for the divisible/equals K problems is in the video for Subarray Sums Divisible By K. It's one of those where if you know how it works it all makes sense but explaining it is harder than learning it lol

Келесі