@@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)
@techdose4u4 күн бұрын
yea. AI is trash 😮💨
@rutikbhanushali9575 күн бұрын
Moving left pointer back to avoid duplicate was a bit tricky...
@techdose4u4 күн бұрын
yes
@ishanbhatt60674 күн бұрын
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 :)
@techdose4u4 күн бұрын
You are welcome :)
@MP-ny3ep5 күн бұрын
Very beautifully explained !
@techdose4u4 күн бұрын
Thanks :)
@jaatharsh4 күн бұрын
good explanation as always, many thanks
@techdose4u4 күн бұрын
Glad you liked it!
@asgarantony4 күн бұрын
Good explanation Sir.
@techdose4u4 күн бұрын
Thanks :)
@freecourseplatformenglish28294 күн бұрын
Excellent expalination, I too comeup with the exact approach but struggle to code it. Thanks man.
@techdose4u4 күн бұрын
nice :)
@freecourseplatformenglish2829Күн бұрын
@@techdose4u 💚💚💚
@ujjwal_75314 күн бұрын
wonderful explain
@techdose4u4 күн бұрын
Thanks :)
@smitshah21134 күн бұрын
Nice explanation. Thanks. Just one tiny bug: when moving back left, we need to ensure we are not moving out of the array
@techdose4u4 күн бұрын
but that condition wont arise coz we must have a break point otherwise the right pointer wouldnt stop!
@smitshah21134 күн бұрын
@@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 :)
@techdose4u4 күн бұрын
yep :)
@Abhishek-e6d2c5 күн бұрын
what an explanation!!!
@techdose4u4 күн бұрын
thanks :)
@devmahad4 күн бұрын
thanks :)
@techdose4u4 күн бұрын
welcome :)
@lakithakare73875 күн бұрын
Thank you
@techdose4u4 күн бұрын
welcome
@LinhHoang-ml1qo4 күн бұрын
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
@techdose4u4 күн бұрын
Anything is O(N) Thankfully we dont need to consider constants :)
@gireeswar185 күн бұрын
Nice explanation, may I know which blackboard you are using?
@pravyn3504 күн бұрын
This guy ❤
@techdose4u4 күн бұрын
:)
@vidhyarthisunav4 күн бұрын
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.
@techdose4u4 күн бұрын
👍🏼
@nicolaswolyniec13544 күн бұрын
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
@techdose4u4 күн бұрын
AI is useless 🥲
@algorithmsanddatastructure37015 күн бұрын
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
@techdose4u4 күн бұрын
yes. But the candidates may differ and so you will endup maintaining all frequencies :)
@algorithmsanddatastructure37014 күн бұрын
@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-05 күн бұрын
God damn, my head hurts hahaha
@techdose4u5 күн бұрын
🤣 brain teaser 🧠
@dieformusic17055 күн бұрын
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?
@techdose4u4 күн бұрын
Yea you can take priority queue but you will need 2 heaps. One for min and one for max
@vishalgadade15855 күн бұрын
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 ?
@techdose4u5 күн бұрын
can you please explain with an example
@aryan51coder2 күн бұрын
using deque makes it o(n) both time and space
@SzrIsTaken7865 күн бұрын
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??"
@techdose4u5 күн бұрын
yea, it was not your daily sliding window :o confusing
@vikasvoruganti74905 күн бұрын
Time to practice more sliding window problems 😢
@techdose4u4 күн бұрын
but this was an outlier :)
@sailendrachettri85214 күн бұрын
And i was just counting subarrays
@techdose4u4 күн бұрын
Need to subtract the redundant ones :)
@thetruebluesingaporeankid5 күн бұрын
could explain why the n*(n+1)/2 come about?
@techdose4u4 күн бұрын
You can enumerate and count. it will be this always:)
@AKProductionsTelugu4 күн бұрын
why don't you move the left pointer to right man its striaght forward and less complicated for explaining!
@firstyfirst4 күн бұрын
would maintaining a index whenever I update max help last wala update will be stored and my next L would be ind+1;
@techdose4u4 күн бұрын
The max may not be optimal. ex. 8,7,1 repeatLimit: 2 Last max will give 8 but you needed 7 :)
@eliomorningstar73925 күн бұрын
sir is it possible if you can explain the segment or fenwick tree approach for this problem?
@techdose4u5 күн бұрын
that will be overkill. Nothing better than 2 pointer. currently I am focusing on easy and optimal solutions. Overkill is not recommended :)
@nileshbhanot27765 күн бұрын
Your python does not submit on leetcode and provides wrong output. Kindly, please check
@techdose4u4 күн бұрын
omg How can AI take jobs if they can’t even convert codes 🥹
@satguy84814 күн бұрын
@nileshbhanot2776 betaa khud se code likh le cheating krne aa gya moo utha ker. Approach dekh code nhi
@nileshbhanot27764 күн бұрын
@@techdose4u Haha I tried to convert the cpp logic to python too but that also didn't work, i don't know why
@techdose4u4 күн бұрын
I think it will work. Is there any implementation issue ?
@nileshbhanot27764 күн бұрын
@@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-Billy5 күн бұрын
I was with you until you start talking about moving L pointer.
@techdose4u4 күн бұрын
You can rewatch and do dry run on examples to clear :)
@AlbinSabu-y2v5 күн бұрын
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.