Continuous Subarrays | Leetcode 2762

  Рет қаралды 7,248

Techdose

Techdose

Күн бұрын

Пікірлер: 74
@sivapradeep977
@sivapradeep977 5 күн бұрын
Suddenly my brain is not braining😂😂
@techdose4u
@techdose4u 4 күн бұрын
This is funniest 🤣
@Anikait-h3d
@Anikait-h3d 4 күн бұрын
Loving the explanation Hating the problem
@techdose4u
@techdose4u 4 күн бұрын
hahahaaha 🤣
@heybeachMIN
@heybeachMIN 4 күн бұрын
@@techdose4u Hi, your python code is not right. There is a small error there, at the end after the loop "for" you need to add to win_size + 1, that is, win_size = r - l + 1. I'm not exactly sure, but maybe this is because we don't add to the count without the condition max - min > 2 and always in the end we need to add to count and there the count in the loop starts from zero, so + 1. Sry for engl)
@techdose4u
@techdose4u 4 күн бұрын
yea. AI is trash 😮‍💨
@rutikbhanushali957
@rutikbhanushali957 5 күн бұрын
Moving left pointer back to avoid duplicate was a bit tricky...
@techdose4u
@techdose4u 4 күн бұрын
yes
@ishanbhatt6067
@ishanbhatt6067 4 күн бұрын
great call to include the complexity analysis! i came up with a similar solution but rejected it because i thought moving left pointer back would cause the complexity to become N-squared! thanks :)
@techdose4u
@techdose4u 4 күн бұрын
You are welcome :)
@MP-ny3ep
@MP-ny3ep 5 күн бұрын
Very beautifully explained !
@techdose4u
@techdose4u 4 күн бұрын
Thanks :)
@jaatharsh
@jaatharsh 4 күн бұрын
good explanation as always, many thanks
@techdose4u
@techdose4u 4 күн бұрын
Glad you liked it!
@asgarantony
@asgarantony 4 күн бұрын
Good explanation Sir.
@techdose4u
@techdose4u 4 күн бұрын
Thanks :)
@freecourseplatformenglish2829
@freecourseplatformenglish2829 4 күн бұрын
Excellent expalination, I too comeup with the exact approach but struggle to code it. Thanks man.
@techdose4u
@techdose4u 4 күн бұрын
nice :)
@freecourseplatformenglish2829
@freecourseplatformenglish2829 Күн бұрын
@@techdose4u 💚💚💚
@ujjwal_7531
@ujjwal_7531 4 күн бұрын
wonderful explain
@techdose4u
@techdose4u 4 күн бұрын
Thanks :)
@smitshah2113
@smitshah2113 4 күн бұрын
Nice explanation. Thanks. Just one tiny bug: when moving back left, we need to ensure we are not moving out of the array
@techdose4u
@techdose4u 4 күн бұрын
but that condition wont arise coz we must have a break point otherwise the right pointer wouldnt stop!
@smitshah2113
@smitshah2113 4 күн бұрын
@@techdose4u Ah, yes... the fact that the right pointer did stop means that there are two indices for which the subarray is not continuous. Hence we would stop moving left before going out of bounds. Thanks :)
@techdose4u
@techdose4u 4 күн бұрын
yep :)
@Abhishek-e6d2c
@Abhishek-e6d2c 5 күн бұрын
what an explanation!!!
@techdose4u
@techdose4u 4 күн бұрын
thanks :)
@devmahad
@devmahad 4 күн бұрын
thanks :)
@techdose4u
@techdose4u 4 күн бұрын
welcome :)
@lakithakare7387
@lakithakare7387 5 күн бұрын
Thank you
@techdose4u
@techdose4u 4 күн бұрын
welcome
@LinhHoang-ml1qo
@LinhHoang-ml1qo 4 күн бұрын
Hi sir, I think exactly the time complexity is O(2N) in the worst case l and r is both come to the last of index. Please confirm if my comment is false.Thank you so much and have a good day
@techdose4u
@techdose4u 4 күн бұрын
Anything is O(N) Thankfully we dont need to consider constants :)
@gireeswar18
@gireeswar18 5 күн бұрын
Nice explanation, may I know which blackboard you are using?
@pravyn350
@pravyn350 4 күн бұрын
This guy ❤
@techdose4u
@techdose4u 4 күн бұрын
:)
@vidhyarthisunav
@vidhyarthisunav 4 күн бұрын
Your Python code in the link need minor enhancement. n = len(nums) should be used instead of right pointer to calculate the final number of subarrays to be addes outside the loop on line number 126. It should be win_size = n - left instead of win_size = right - left.
@techdose4u
@techdose4u 4 күн бұрын
👍🏼
@nicolaswolyniec1354
@nicolaswolyniec1354 4 күн бұрын
the python solution will give you a wrong answer. After finishing the loop, you can not use the right variable because the value is n-1 win_size = len(nums) - left count += (win_size * (win_size + 1)) // 2
@techdose4u
@techdose4u 4 күн бұрын
AI is useless 🥲
@algorithmsanddatastructure3701
@algorithmsanddatastructure3701 5 күн бұрын
i think we can adjust the left pointer also simply by maintaining the index and updating the index if the same number occuring multiple times
@techdose4u
@techdose4u 4 күн бұрын
yes. But the candidates may differ and so you will endup maintaining all frequencies :)
@algorithmsanddatastructure3701
@algorithmsanddatastructure3701 4 күн бұрын
@techdose4u i am thinking of maintaining the index of only max and min,so whenever difference is greater than the given number, it will be because max and min, so accordingly we can move the left pointer either to min or max which ever was present in the subarray
@BigStin-0
@BigStin-0 5 күн бұрын
God damn, my head hurts hahaha
@techdose4u
@techdose4u 5 күн бұрын
🤣 brain teaser 🧠
@dieformusic1705
@dieformusic1705 5 күн бұрын
sir can i use priority queues to maintain min and max values so as to avoid the left movement of L pointer where i can update L pointer to the index choosen from the priority queues after checking the condition?
@techdose4u
@techdose4u 4 күн бұрын
Yea you can take priority queue but you will need 2 heaps. One for min and one for max
@vishalgadade1585
@vishalgadade1585 5 күн бұрын
As you are moving pointer again to left when we found any fault Isn't it is disturbing cpu cycle? Because it is making knot of pointers during computation Can we optimize this knot ?
@techdose4u
@techdose4u 5 күн бұрын
can you please explain with an example
@aryan51coder
@aryan51coder 2 күн бұрын
using deque makes it o(n) both time and space
@SzrIsTaken786
@SzrIsTaken786 5 күн бұрын
Had the sliding window part down, the maximum and minimum threw me off a bit but the formula is the part where I'm thinking "how the heck??"
@techdose4u
@techdose4u 5 күн бұрын
yea, it was not your daily sliding window :o confusing
@vikasvoruganti7490
@vikasvoruganti7490 5 күн бұрын
Time to practice more sliding window problems 😢
@techdose4u
@techdose4u 4 күн бұрын
but this was an outlier :)
@sailendrachettri8521
@sailendrachettri8521 4 күн бұрын
And i was just counting subarrays
@techdose4u
@techdose4u 4 күн бұрын
Need to subtract the redundant ones :)
@thetruebluesingaporeankid
@thetruebluesingaporeankid 5 күн бұрын
could explain why the n*(n+1)/2 come about?
@techdose4u
@techdose4u 4 күн бұрын
You can enumerate and count. it will be this always:)
@AKProductionsTelugu
@AKProductionsTelugu 4 күн бұрын
why don't you move the left pointer to right man its striaght forward and less complicated for explaining!
@firstyfirst
@firstyfirst 4 күн бұрын
would maintaining a index whenever I update max help last wala update will be stored and my next L would be ind+1;
@techdose4u
@techdose4u 4 күн бұрын
The max may not be optimal. ex. 8,7,1 repeatLimit: 2 Last max will give 8 but you needed 7 :)
@eliomorningstar7392
@eliomorningstar7392 5 күн бұрын
sir is it possible if you can explain the segment or fenwick tree approach for this problem?
@techdose4u
@techdose4u 5 күн бұрын
that will be overkill. Nothing better than 2 pointer. currently I am focusing on easy and optimal solutions. Overkill is not recommended :)
@nileshbhanot2776
@nileshbhanot2776 5 күн бұрын
Your python does not submit on leetcode and provides wrong output. Kindly, please check
@techdose4u
@techdose4u 4 күн бұрын
omg How can AI take jobs if they can’t even convert codes 🥹
@satguy8481
@satguy8481 4 күн бұрын
@nileshbhanot2776 betaa khud se code likh le cheating krne aa gya moo utha ker. Approach dekh code nhi
@nileshbhanot2776
@nileshbhanot2776 4 күн бұрын
@@techdose4u Haha I tried to convert the cpp logic to python too but that also didn't work, i don't know why
@techdose4u
@techdose4u 4 күн бұрын
I think it will work. Is there any implementation issue ?
@nileshbhanot2776
@nileshbhanot2776 4 күн бұрын
@@techdose4u def continuousSubarrays(self, nums: List[int]) -> int: l, res = 0, 0 mini, maxi = float("inf"), float("-inf") for r in range(len(nums)): maxi = max(nums[r], maxi) mini = min(nums[r], mini) if maxi - mini > 2: n = r - l res += ((n * (n + 1)) // 2) l = r maxi = nums[r] mini = nums[r] while l > 0 and abs(nums[r] - nums[l - 1])
@B-Billy
@B-Billy 5 күн бұрын
I was with you until you start talking about moving L pointer.
@techdose4u
@techdose4u 4 күн бұрын
You can rewatch and do dry run on examples to clear :)
@AlbinSabu-y2v
@AlbinSabu-y2v 5 күн бұрын
This is first time I see the window is going like a swing. That made me confusing(especially the double count & subtracton🥲). If I had never seen this algo my dumb brain could never figure out the soln.
@techdose4u
@techdose4u 4 күн бұрын
This was an outlier so cant blame anyone!
Max Chunks To Make Sorted | Leetcode 769
16:43
Techdose
Рет қаралды 1,2 М.
Quando A Diferença De Altura É Muito Grande 😲😂
00:12
Mari Maria
Рет қаралды 45 МЛН
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 641 М.
META offer Anindita | Techdose Alumni | SDE-2
25:28
Techdose
Рет қаралды 3,5 М.
Leetcode 2762 - Continuous Subarrays
26:33
pygoko
Рет қаралды 37
2024's Biggest Breakthroughs in Math
15:13
Quanta Magazine
Рет қаралды 292 М.
I tested the 14 most OVERKILL Smartphone Gadgets
17:07
Mrwhosetheboss
Рет қаралды 1,9 МЛН
The Every UUID Website Explained
33:43
ThePrimeTime
Рет қаралды 220 М.
The Elo Rating System
22:13
j3m
Рет қаралды 12 М.
Coding Predictions 2025
12:25
Stefan Mischook
Рет қаралды 10 М.
The Hard Truth About Harvard and MIT
14:46
Samuel Bosch
Рет қаралды 13 М.