Тёмный

LeetCode Max Consecutive Ones III Solution Explained - Java 

Nick White
Подписаться 385 тыс.
Просмотров 36 тыс.
50% 1

The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
Join my free exclusive community built to empower programmers! - www.skool.com/...
Preparing For Your Coding Interviews? Use These Resources
--------------------
(My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.d...
AlgoCademy - algocademy.com...
Daily Coding Interview Questions - bit.ly/3xw1Sqz
10% Off Of The Best Web Hosting! - hostinger.com/...
Follow Me on X/Twitter - x.com/nickwhit...
Follow My Instagram - / nickwwhite
Other Social Media
----------------------------------------------
Discord - / discord
Twitch - / nickwhitettv
TikTok - / nickwhitetiktok
LinkedIn - / nicholas-w-white
Show Support
------------------------------------------------------------------------------
Patreon - / nick_white
PayPal - paypal.me/nick....
Become A Member - / @nickwhite
#coding #programming #softwareengineering

Опубликовано:

 

5 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 43   
@artur4427
@artur4427 8 месяцев назад
what helped me understand this is the realization that i and j are not the actual edges of the longest consecutive segment. the window never shrinks, so the max length is kept track of implicitly by the window's size.
@danielpark4204
@danielpark4204 4 года назад
took a while but i get it now, however I think its more intuitive to just cache the maxLen in a variable and shrink until K is positive again rather than caching the width throughout.
@aj9706
@aj9706 3 года назад
Yes absolutely
@tapanbasak1453
@tapanbasak1453 3 года назад
Can anyone please explain why i-j always gives the correct result without keeping track of the max subarray at all times.
@HoangLe-qr9si
@HoangLe-qr9si 3 года назад
i think that was handled by if k is positive then j will not increase
@user-le6ts6ci7h
@user-le6ts6ci7h 3 года назад
It is because , as long as you are able to get contiguous 1 , with maximum k flips of zero, the i value goes on increasing and j remains at the position so far. And once the k becomes negative( that is, k+1 zeros are flipped) then both j and i are incremented , meaning that j-i is still fixed in length and equal to the size of contiguous 1 found so far . So actually this maximum fixed size windows is moving throughout the array which is always expanding rather than contracting
@luz8011
@luz8011 2 года назад
@@user-le6ts6ci7h thank u for explaining it :)
@amansaxena4446
@amansaxena4446 Год назад
not making sense at all
@subhamshaw1726
@subhamshaw1726 3 года назад
that was really simple, great explanation
@mehdihassan93
@mehdihassan93 2 месяца назад
for people who are looking for python3 solution: l = 0 for r in range(len(nums)): if nums[r] == 0: k -= 1 if k < 0: if nums[l] == 0 : k += 1 l += 1 r += 1 return r - l
@vijayakumareyunni6010
@vijayakumareyunni6010 9 месяцев назад
Key thing to be explained is how the pointers are to be updated which was not explained clearly. He also got confused when coding and somehow it worked.
@SnehaBharathiReddy
@SnehaBharathiReddy 5 месяцев назад
Straight forward Explanation. Thanks Nick
@akshittyagi1784
@akshittyagi1784 3 года назад
2 mins and 30 seconds into the video, and Nick's like enough talk, let's just get straight to business😂😂 respect man👍
@miaohong601
@miaohong601 4 года назад
This is much easier to understand than the most upvoted in discussion, Thanks Nick!!
@MrThepratik
@MrThepratik 4 года назад
Getting my head around it
@emerylin6793
@emerylin6793 2 года назад
while the code is simple, you dont get the longest number of ones, which is not the right answer.
@gitanjalikumari9262
@gitanjalikumari9262 3 года назад
So nice solution 👍😊
@mohammedghabyen721
@mohammedghabyen721 2 года назад
public class Solution { public int LongestOnes(int[] nums, int k) { int left=0; int right=0; int ck = k; int maxNu = 0; while(right0) { ck--; right++; }else{ if(nums[left]==0) { ck++; left++; }else left++; } } maxNu = Math.Max(maxNu,right-left); } return maxNu; } }
@kakwancheng9493
@kakwancheng9493 2 года назад
This logic is really clear, thanks a lot!!!
@edwardnewgate2198
@edwardnewgate2198 5 лет назад
What about the test case "0011111000"- and with K as 2? In that case- j wouldn't be able to reach a 0. How would we get the answer in that case??
@chaoschao9432
@chaoschao9432 5 лет назад
j will reach 0, you may run the test case step by step.
@MrSaiyah007
@MrSaiyah007 4 года назад
@@chaoschao9432 it will not
@ArtInMotion6912
@ArtInMotion6912 4 года назад
Yup but the size is same both j and i increments so no effect on it
@ashutoshchauhan4928
@ashutoshchauhan4928 3 года назад
I had the same doubt, thanks for giviing the rationale behind it @brock lesner
@sarscov9854
@sarscov9854 3 года назад
it works. I put it in the compiler, gave me 7.
@aayush5474
@aayush5474 4 года назад
whats the timecompleity?
@mikea3264
@mikea3264 3 года назад
O(n) because there is just one loop going through the whole array.
@rajatsrivastava555
@rajatsrivastava555 3 года назад
clean and not fancy!!
@sujayshanbhag2055
@sujayshanbhag2055 Год назад
What happens if i reaches end before k becomes positive?
@sujayshanbhag2055
@sujayshanbhag2055 Год назад
I think this is more Intuitive : - int longestOnes(vector& nums, int k) { int last=0; int first=0; int n=nums.size(); int maxLen=0; while(last< n) { if(nums[last]==0) k--; while(k
@sujayshanbhag2055
@sujayshanbhag2055 Год назад
Intuition behind the code in the video: This is notes I made for me, but if it helps you, great!. Above code: Whenever the number of zeros taken exceeds k, move the starting point of window until we recover the 0. In video code: We don't run a loop to recover the 0. Whenever we take in a bad zero, we increment the start. By doing this whenever, we increment last, first gets incremented and we maintain the length. [We do not keep track of the actual sub array]. If we recover the 0 in process, only last will start to increment, increasing the length of the subarray.
@comedycentral4333
@comedycentral4333 3 года назад
What would be this test case "1110" if k = 1 the output will be 3 right, but the above code returning 4, can anyone please suggest me?
@user-le6ts6ci7h
@user-le6ts6ci7h 3 года назад
No , the answer would be 4. At the end of the loop i will be having a value 4, the reason of which it exits the loop. And j will be at 0 .So returns 4-0
@deepakbisht7764
@deepakbisht7764 2 года назад
4 is correct as k =1 so we can flip single 0 so if you flip last 0 the longest continuation will be 1111 so result should be 4 not 3...
@tejapvs
@tejapvs Год назад
@@user-le6ts6ci7h Got It, Thinking for a whie how he got 4, didnt observe he was increaing i
@ronaldabellano5643
@ronaldabellano5643 4 года назад
The solution seems easy.
@ladro_magro5737
@ladro_magro5737 5 месяцев назад
this guy doesnt understand the solution...it can be seen from his explanation. but one thing is sure, he memorized it well for this presentation :D
@gevor3338
@gevor3338 9 месяцев назад
you know what i mean?
@sanjeevdhewa9670
@sanjeevdhewa9670 4 года назад
genius
@BharatiSubramanian99217
@BharatiSubramanian99217 4 года назад
Oh! This guy is GOLD!!! Thankyou so much!
@mahmoudattia4837
@mahmoudattia4837 4 года назад
OH MY GOD!
@uputoorikishore9548
@uputoorikishore9548 8 месяцев назад
stop doing videos
Далее
LeetCode 33. Search in Rotated Sorted Array
9:30
Просмотров 98 тыс.
Self Taught Programmers... Listen Up.
11:21
Просмотров 1 млн
How I would learn Leetcode if I could start over
18:03
Просмотров 568 тыс.
LeetCode Open the Lock Solution Explained - Java
15:35