Probably the best explanation on the whole KZbin, thank you man!
@crackfaang3 ай бұрын
Thanks for the kind words! I can't remember which video it was where I completely butchered the explanation for this class of question but I guess it wasn't this one. Definitely a hard topic to explain even though in my head it all makes sense
Here's an explanation that made sense to me : we use the existing array values and prefix sums to compute the sub array values (which is linear time) on the fly instead of computing all of them (which is quadratic time). It's kind of like two-sum once you get this intuition
@massimomonticelli601010 ай бұрын
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.
@dnm99314 ай бұрын
What a horrible question! Thanks so much for all you do , you are appreciated !
@codewithmarish8 ай бұрын
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.
@skh97497 ай бұрын
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)
@3rd_iimpact10 ай бұрын
It's nice to know that I'm not the only who has a hard time explaining this one, lol.
@crackfaang10 ай бұрын
Welcome as a channel member mate!
@dmytryemery10 ай бұрын
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.
@crackfaang10 ай бұрын
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.
@YT.Nikolay10 ай бұрын
Ah wow!!! Never knew about this feature, nice to know :)
@crackfaang10 ай бұрын
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
@koreandude6 ай бұрын
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]?
@tsimbaland29056 ай бұрын
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)
@tanvirahmed79938 ай бұрын
sumI - k = sumJ, where I < J, was the key
@marcgrab25 күн бұрын
You do not need this strange init. It is hard to grasp. Just add this as the second operation in the loop instead `res += int(prefix_sum == k)` also this if statement could be replaced by `res += prefix_dict.get(prefix_sumn-k, 0)`
@yingxu1694Ай бұрын
why not just res += 1?
@dum_chicken_biryani3 ай бұрын
Tell me one thing, how can someone solve this question in 20-25 min with optimal solution if they have not seen it before. Sorry but I was frustrated to understand this. Also this is from Chatgpt for more understanding. If we have a prefix sum up to index j (let's call it sum_j), and we've seen a prefix sum (sum_i) such that sum_j - sum_i = k, then the subarray between i+1 and j (inclusive) has a sum of k. Here's why: sum_i represents the sum of all elements from index 0 to i. sum_j represents the sum of all elements from index 0 to j. The difference (sum_j - sum_i) represents the sum of elements from index i+1 to j. Let's break it down: sum_i = nums[0] + nums[1] + ... + nums[i] sum_j = nums[0] + nums[1] + ... + nums[i] + nums[i+1] + ... + nums[j] sum_j - sum_i = (nums[0] + nums[1] + ... + nums[i] + nums[i+1] + ... + nums[j]) - (nums[0] + nums[1] + ... + nums[i]) sum_j - sum_i = nums[i+1] + ... + nums[j] So, when sum_j - sum_i = k, it means the sum of elements from index i+1 to j is equal to k. This is why in the implementation, we don't need to make any adjustments when we find a match. The current index represents the end of the subarray (j), and the hashmap gives us the count of prefix sums that, when subtracted from the current sum, yield k. Each of these represents a valid subarray ending at the current index.
@crackfaang3 ай бұрын
Unfortunately you really are expected to have seen the question before and memorised the solution. That or you've solved so many similar problems you can use prior experience to solve it. The system is broken, but at least for Meta you know the list of questions ahead of time (the LC list) so you can just practice practice practice the most frequently asked ones.
@dum_chicken_biryani3 ай бұрын
Thanks for making the videos. Yes I am trying to finish top 75 last 6 months meta questions and see if I can pass phone screening . Never did leet code . But it's the truth , we need to play this game of dsa and algos. Again thanks for making these videos .
@manoelramon82832 ай бұрын
@@crackfaang I have 32 years of experience and I was a manager at google. We hired a front end engineer that passed the interview with glory...few weeks later I realized this person did not know anything about front end. I asked to see the loop question.... all leetcode questions... I feel it is like pockets in pijamas ... almost but not totally, useless
@HarbiDr4 ай бұрын
Emotional Damage @ 5m 31s
@tamzidahmed97062 ай бұрын
idk why this kind of questions are ven asked when there is only one certain to solve this and you are fucked you have not seen the question before.
@kapilrules2 ай бұрын
i am so confused
@TheCuriosKip5 ай бұрын
You could have made that 100X clearer.
@crackfaang5 ай бұрын
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
@manoelramon82832 ай бұрын
[1,1,1] .. i can have element 0 and 1 =2, element 1 and 2 = 2 as you said.. but I also can add the element 0 and 2 and have 2... so 3 sub-arrays not 2. The description of this exercise sucks