LeetCode 525. Contiguous Array (Algorithm Explained)

  Рет қаралды 37,036

Nick White

Nick White

4 жыл бұрын

The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
Join my free exclusive community built to empower programmers! - www.skool.com/software-develo...
Preparing For Your Coding Interviews? Use These Resources
--------------------
(My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.dev/courses/nick
AlgoCademy - algocademy.com/?referral=nick...
Daily Coding Interview Questions - bit.ly/3xw1Sqz
10% Off Of The Best Web Hosting! - hostinger.com/nickwhite
Follow Me on X/Twitter - x.com/nickwhitereal
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/nickwwhite?locale.x...
Become A Member - / @nickwhite
#coding #programming #softwareengineering

Пікірлер: 53
@calfland79
@calfland79 4 жыл бұрын
It is definitely a medium problem. counts.put(0, -1) only this line could kill 90% people if they have never study this problem before.
@abe10
@abe10 4 жыл бұрын
Totally!!
@AdityaSharma-yg6mw
@AdityaSharma-yg6mw 3 жыл бұрын
yaaay! only (0,-1) part makes it hard
@valiibraimi195
@valiibraimi195 4 ай бұрын
Or JUST PUT for(let i = 0; i
@oscaropdyou
@oscaropdyou 4 жыл бұрын
If there is confusion on why count "0" is initialised with -1 index, this is because before we start, we start with count of 0. If we get lets say [0,1], then it would go to -1 and 0. And as we have initialised first index of 0 as -1. And length would be -1 to 1(current index when count reached 0).
@shreshtashetty2985
@shreshtashetty2985 2 жыл бұрын
Got it now! Thanks!
@Dan-tg3gs
@Dan-tg3gs 4 жыл бұрын
i feel that this solution was hard to come up with without already seeing some kind of pattern of this question before. When I saw the solution, I understood it, but I'm wondering if these types of questions will get intuitive with more practice? Is this considered DP?
@extravaganza69
@extravaganza69 4 жыл бұрын
I've got the same problem. Basically, many solutions to medium+ problems are not really trivial or intuitive as they usually are based on some external assumption or characteristic of data. I think it's almost impossible to get it right every time, but definitely, it's possible to develop some kind of intuition as there is a finite amount of categories these problems belong to. Stay cool and positive! Cheers
@BrijeshPatelEngineer
@BrijeshPatelEngineer 4 жыл бұрын
​@@extravaganza69 yeah i think finite amount of categories you mentioned should be documented somewhere. It would be helpful to look at initially when we get stuck !!! Agree, should stay cool. Revisit the problems every interval to keep hang of it.
@YNA64
@YNA64 2 жыл бұрын
Nick : "It's not that hard, it could be easy even." Also Nick Submissions : Multiple Rejection Cmon dude...
@derpina615
@derpina615 2 жыл бұрын
lmfao
@visheshmangla2650
@visheshmangla2650 4 жыл бұрын
Consider two indices i and j where i
@jhonnymoreira9551
@jhonnymoreira9551 2 ай бұрын
Thanks for clearing the problem and going over the solution. I was having a hard time understanding the concept of `contiguous` (non-native english speaker), and the longer example plus your code helped a lot.
@manjorysaran6582
@manjorysaran6582 4 жыл бұрын
u r really good! how did you practice and for how long before you could code like this?
@duyetpham7924
@duyetpham7924 4 жыл бұрын
i try many ways before view video, this is great. It is simple and power, thank you.
@vinayprabhakar6925
@vinayprabhakar6925 2 жыл бұрын
Can u plz tell why we added 0,-1 into our hashmap before going through the array didn't get that part
@abhishekkrishna5978
@abhishekkrishna5978 4 ай бұрын
this is a fabulous way of looking at the problem. i wouldn't have comeup with this solution.
@SubramanyaChakravarthy
@SubramanyaChakravarthy 4 жыл бұрын
Great explanation thank you
@abulaman8713
@abulaman8713 3 жыл бұрын
I thought of someway of substracting the number of zeros and ones to arrive at the solution...., Wow man, love your brains
@ahjiba
@ahjiba 4 жыл бұрын
initially thought of this problem as max contiguous subarray equals k, where k = 0 and all 0s are converted into -1, hope that helps anyone thinking through this problem
@DAN-iz9gi
@DAN-iz9gi 4 жыл бұрын
Thank you! Great explanation!
@minarezkalla4403
@minarezkalla4403 2 жыл бұрын
Thank you for amazing explaination
@Whaleshamu
@Whaleshamu 4 жыл бұрын
i understand that we are putting -1 for 0, coz we have not seen 0 yet. But why we are initially putting 0 inside map?
@shreejitnair2174
@shreejitnair2174 4 жыл бұрын
Think about a simple condition where your array is [0,1] this would not update maxlen when the count becomes 0. Adding this check gives you (index - (-1)) so your maxlen = 2 now. Don't think about adding this condition first. Think about a test case or a counter example that breaks your logic without this condition. Adding this condition would appear natural at that point.
@Mohit-nn9bt
@Mohit-nn9bt 3 жыл бұрын
I liked how you explained what is binary :-)
@ianchui
@ianchui 4 жыл бұрын
awesome work! helped me very much. I did not enjoy going through that animation at all lmaoo
@ishaanj8023
@ishaanj8023 4 жыл бұрын
Thank you
@lavanyam3224
@lavanyam3224 2 жыл бұрын
Brilliant!
@madhurbhargava
@madhurbhargava 4 жыл бұрын
Did not really get why this video was created if the whole idea was to just read the solution from the solutions section.
@AnandPandeyHere
@AnandPandeyHere 4 жыл бұрын
Watch mine.. I think you might get help
@madhurbhargava
@madhurbhargava 4 жыл бұрын
@@AnandPandeyHere ok...going to do that
@7urkm3n
@7urkm3n 4 жыл бұрын
Cuz, he cares more about views. Basically he said and read the same thing on the solutions, and copy the solution code.
@saicharan8675
@saicharan8675 4 жыл бұрын
You are the best
@RishiRaj-dl1mg
@RishiRaj-dl1mg 4 жыл бұрын
Can we solve this using bottom up dp???
@rezwanurrahman7090
@rezwanurrahman7090 3 жыл бұрын
I think, without hashmap dp will not be possible in O(n) time
@subhedarsanchay
@subhedarsanchay 4 жыл бұрын
Hey Nick, great video. Thanks for all of your hard work. Just have a minor suggestion. I have seen a bunch of your videos and you almost tend to size the problem by saying "oh this is an easy problem" or "this problem is super simple". This might discourage the viewer into thinking they can't even solve these easy problem. And believe me, most viewers who are watching your videos are the folks who could not solve this problem and hence are looking to understand the solution. I'd appreciate if you could stop doing that. Let me know if I am being too sensitive. Again, I am your youtube subscriber and thank you!
@SumitKumar-fn3gj
@SumitKumar-fn3gj 4 жыл бұрын
Yeah this is not easy question.
@yilinhuang4509
@yilinhuang4509 4 жыл бұрын
awesome
@KEVINMBARUA
@KEVINMBARUA 4 жыл бұрын
bruh! you are the shiznit! that is all!!!!!!
@officialspock
@officialspock 4 жыл бұрын
I don't understand the example, why is [0,1] maxlen is 2, and [0,1,0] maxlen is still 2, I had a look at the discussion and get more confused... I hope someone can explain this properly before I can formulate a logic to solve this. [0,0,0,1,1,2,1] = 6 // other numbers than 0 and 1 are ignored [0,1,0,1] = 4 // so this is 4, why? [0,1,0,1,1] = 4 // this is also 4... so there is some symetry condition?? [0,0,1,0,0,0,1,1] = 6 // this is 6?? I do not see why. I would have expected 4
@damudaran3765
@damudaran3765 4 жыл бұрын
i have the same confusion bruh . this bastard in this video explain shit . he always rush up never cares about the viewers understanding worst experience.
@anybody_y
@anybody_y 4 жыл бұрын
took a a lot of re-read the problem statement to clear up my confusion, to answer you: [0,0,0,1,1,2,1] i believe you meant [0,0,0,1,1,0,1] , it is 6 because starting from the 2nd index [0,0,1,1,0,1] it is contiguous because this subarray has exactly the same number of 0 as to 1, 3 each. [0,1,0,1] = 4 because the whole array has same number of 0s and 1s, so the length of the array (4) is the longest subarray [0,1,0,1,1] = 4 because up to the first 4 elements, the subarray has exactly the same number of 0s and 1s, therefore it is 4. [0,0,1,0,0,0,1,1] = 6, from the 3rd index to the end, it has the same number of 0s and 1s therefore it is 6.
@anybody_y
@anybody_y 4 жыл бұрын
@@damudaran3765 instead of blaming others of not explaining in the level of detail that you are expecting, you could be more helpful by explaining what exactly you are confused with and ask for help around that confusion. you need to help others to understand what you don't for others to help you. your comment come across to me that you have spent no effort of trying to understand your own confusion.
@hjr834
@hjr834 4 жыл бұрын
@@anybody_y great explanation man, I've missed something like that while going through the statement of the problem
@officialspock
@officialspock 4 жыл бұрын
@@anybody_y thanks for your explanation, so basically, contiguous means same count of 1 and 0? The longest contiguous same count of 1 and 0?
@kunalnegi18
@kunalnegi18 3 жыл бұрын
Return No of times 0 occurs ×2
@raihanar5323
@raihanar5323 2 жыл бұрын
Im the 25,000 th view , 500 th like
Contiguous Array - Leetcode 525 - Python
15:41
NeetCodeIO
Рет қаралды 20 М.
Self Taught Programmers... Listen Up.
11:21
Nick White
Рет қаралды 1 МЛН
Double Stacked Pizza @Lionfield @ChefRush
00:33
albert_cancook
Рет қаралды 120 МЛН
Получилось у Миланы?😂
00:13
ХАБИБ
Рет қаралды 6 МЛН
Викторина от МАМЫ 🆘 | WICSUR #shorts
00:58
Бискас
Рет қаралды 5 МЛН
LeetCode 238. Product of Array Except Self (Solution Explained)
14:49
Contiguous Array | LeetCode 30 day Challenge | LeetCode 525
16:44
Knowledge Center
Рет қаралды 8 М.
LeetCode 442. Find All Duplicates in an Array (Solution Explained)
12:37
Contiguous array | Leetcode #525
13:12
Techdose
Рет қаралды 52 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 633 М.
LeetCode 735. Asteroid Collision (Solution Explained)
14:24
Nick White
Рет қаралды 20 М.
LeetCode 36. Valid Sudoku (Algorithm Explained)
11:33
Nick White
Рет қаралды 98 М.
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
Vision Pro наконец-то доработали! Но не Apple!
0:40
ÉЖИ АКСЁНОВ
Рет қаралды 525 М.
Rate This Smartphone Cooler Set-up ⭐
0:10
Shakeuptech
Рет қаралды 7 МЛН