Тёмный

ZigZag Conversion - Leetcode 6 - Python 

NeetCode
Подписаться 836 тыс.
Просмотров 104 тыс.
50% 1

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

 

14 окт 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 81   
@NeetCode
@NeetCode Год назад
I created a second channel where I post the daily LC solutions 👉 www.youtube.com/@NeetCodeIO In case you're interested!
@mirzasohailhussain
@mirzasohailhussain 10 месяцев назад
Your explanation has built my logic design strongers.
@henryCcc8614
@henryCcc8614 2 года назад
Using += on strings creates new strings on each iteration in linear time and space. Might I suggest using a list and appending each character (constant amortised time/space), then converting to string before returning.
@TF2Shows
@TF2Shows 6 месяцев назад
Thats exactly how I did it the first time (and it worked, beats 47% time and 72% space complexity).
@thewisestguy1
@thewisestguy1 Год назад
this problem humbled me real quick 😅
@samlee405
@samlee405 3 года назад
While less elegant, I feel like the solution to this problem where you assign a list for each row and move up and down while assigning each letter from the input to a given list is a bit more intuitive. It's less space efficient but I think it's good to learn as well
@christopherquiroga2769
@christopherquiroga2769 3 года назад
I solved it with that method and while less space efficient it was faster then 90% of submissions surprisingly
@kushagarsharma4783
@kushagarsharma4783 4 месяца назад
Even better use an array of size equal to string, then at each index store the level e.g 0,1,2,3... ,then create a hashmap for each level adding the strings , then add all the values to ans
@smartsoothing776
@smartsoothing776 Год назад
How can we come up with this type of solution in 45 min interview? 🤕
@srinayclg390
@srinayclg390 6 месяцев назад
practice and luck
@TF2Shows
@TF2Shows 6 месяцев назад
15 minutes*, its usually 2 questions per 45.
@angelofdeath095
@angelofdeath095 6 месяцев назад
With Shit ton of practice....easy...
@pretty17Libra
@pretty17Libra 2 месяца назад
if you were lucky enough to have done this problem in advance and got someone who chose this exact or slight variation of this problem or if you're Einstein, probability of the latter quite abysmal, no shade - just a reality, thanks for coming to my TedxTalk
@canxingbu8813
@canxingbu8813 3 года назад
Hi, I think if you move "increment = 2 * (numRows -1)" out of for loop may be better to understand. Still a really great solution! Thank you!
@Tony-yn5rr
@Tony-yn5rr 2 года назад
If I had to answer this I would fail, just properly parsing the instructions felt difficult till I saw you draw the zig zag.
@atifhu
@atifhu Год назад
liked this video, disliked the leetcode problem
@vipulchaudhary_js
@vipulchaudhary_js Год назад
class Solution { public: string convert(string s, int numRows) { if (numRows == 1) return s; string ans = ""; for (int r = 0; r < numRows; r++) { int inc = 2 * (numRows-1); for (int i = r; i < s.length(); i += inc) { ans += s[i]; if (r > 0 && r < numRows-1 && i + inc - 2 * r < s.length()) { ans += s[i + inc - 2*r]; } } } return ans; } };
@name_surname_1337
@name_surname_1337 11 месяцев назад
it's pretty funny though that instead of a straightforward simulation you wrote the formula that you know. good luck to come up with this formula in an interview
@asdasddas100
@asdasddas100 3 года назад
Hey I just did this one. I bet your solution is 300x more elegant though Edit: It is. :p
@pvrohanraj
@pvrohanraj Год назад
I Think this solution is really what comes to my head over the strings one, i couldn't break it down somehow. Thank you for the explanation
@KeshavKumar69420
@KeshavKumar69420 3 года назад
Dang, what timing, I was searching for your solution of this question today and couldn't find it. This question is little confusing to understand. Thanks for uploading!
@ratikdubey4375
@ratikdubey4375 2 года назад
In the Medium Playlist, 75-88 are the same as 89-102, I think you've added them twice by mistake. Thanks for coming in clutch with all your videos man, really do appreciate it.
@NeetCode
@NeetCode 2 года назад
Good catch, just fixed it
@Max-ht2hk
@Max-ht2hk 2 месяца назад
Alternative solution (very similar solution): Each time the size of the the vertical plus diagonal is 2n-2 (excluding the top of the top diagonal) So we can consider numbers mod 2n-2. So 0 mod 2n-2 are the peaks 1 mod 2n-2 and -1 mod 2n-2 are the next row 2 mod 2n-2 and -2 mod 2n-2 are the next row and so on
@butoyighyslain171
@butoyighyslain171 7 месяцев назад
thanks Neetcode! I cant think of a use case when this would be needed in real life
@diasposangare1154
@diasposangare1154 6 месяцев назад
i spend more than 5h on this problem and after i said to myself let your ego and go watch the solution i don't know what wrong with me maybe i didn't approch a problem the best way
@licokr
@licokr 7 месяцев назад
Awesome... I think I got a little bit of tricked by the section 'pointer'. I made two vairables and switch the flag in every step and increase it by the first variable or the second variable depending on the flag. But this solution is much eaiser to read and simple... Thanks for uploading this video!
@eltonlobo8697
@eltonlobo8697 3 года назад
Hi, Can you do Subsets 2? Couldn't find any good video for that problem. You explain every problem's solution nicely.
@Antinormanisto
@Antinormanisto 6 месяцев назад
Someday i will understand how these geniuses find solutions By the way i didn't understand solution
@prajwalh8823
@prajwalh8823 26 дней назад
lol
@VINAYKUMAR-ob6co
@VINAYKUMAR-ob6co 3 года назад
Hey can you do all top 100 questions of leetcode 🙂🙂
@Eria196
@Eria196 2 года назад
how does each element in res get to the right position yet they are generated by two different conditions
@Albertinin0
@Albertinin0 Год назад
I think I got your question, and the thing is: we're getting the current character on `res += s[i];` and we check if there is a character between the current one, and the next one (the next one is the one that we jump every time).
@thewisestguy1
@thewisestguy1 Год назад
Can you explain the algorithm of (numRows - 1) * 2?
@Ba2sik
@Ba2sik Месяц назад
Your solution seems quite unintuitive honestly 😅 I thought of doing something like this, where we simply appending to the corresponding array index each iteration let i = 0; let down = true; for (const letter of s) { arr[i].push(letter); if (i === numRows - 1) down = false; if (i === 0) down = true; i += down ? 1 : -1; } And at the end we concat all of them.
@jagrutitiwari2551
@jagrutitiwari2551 5 месяцев назад
In the nrw UI they have hidden the dislikes count. I don't know why. That's so wrong.
@RohanPahwa-x8h
@RohanPahwa-x8h 8 месяцев назад
I am not good at building algorithms just like you created for the above question. If question like this come I might be able to solve but how can I build algorithm building activity in myself?
@yongfulu8984
@yongfulu8984 3 года назад
Hi bro, one of your video starting with: "I am still unemployed, so lets leetcode" I am wondering if you are employed now
@NeetCode
@NeetCode 3 года назад
Good question, I wanna make a video about that soon 😉
@nikhilshetty9201
@nikhilshetty9201 Год назад
Hey, i had a bit of confusion still can you help me out in explanation for 2nd and 3rd row you used formula as increment-2*rownumber, but in code you used + i I didn't catch that from where that "i" come from, so can you explain bit about it.
@nathaniepeter5707
@nathaniepeter5707 3 месяца назад
I saw this question and laughed at the discussion 😂
@appikondasreepavankumar
@appikondasreepavankumar 7 месяцев назад
increment == 0 case: increment = 2*(numRows-1) if 2*(numRows-1) > 0 else 1
@siddireddyvignesh
@siddireddyvignesh Год назад
class Solution(object): def convert(self, s, numRows): """ :type s: str :type numRows: int :rtype: str """ if numRows==1 : return s n=k=0 l=[] for i in range(numRows): l.append([]) j=1 for i in s: l[n].append(i) if n==numRows-1: j=-1 elif n==0: j=1 n+=j r="" for i in range(numRows): r+="".join(l[i]) return r
@lokeshsenthilkumar4522
@lokeshsenthilkumar4522 3 года назад
increment should be outside the loop
@jktruimp
@jktruimp 3 года назад
Thanks for your tutorial, I can't understand this question's solutions wrote by others until I watched this video.😂
@k.k.harjeeth5422
@k.k.harjeeth5422 11 месяцев назад
class Solution: def convert(self, s: str, numRows: int) -> str: n=len(s) mat=[[""]*n for i in range(numRows)] p,j=0,0 while(p
@sanwalfarooque2747
@sanwalfarooque2747 2 года назад
I did this in 1 while loop (no nesting) single iteration, super simple. I came here after my submission like I always do. I think your solution is a bit complicated. Anyway keep up the good work. :) :)
@jaishreeram-o9b47
@jaishreeram-o9b47 Год назад
once post your solution brother, it would be very helpful :)
@HolyC-xs2zj
@HolyC-xs2zj Год назад
I dont understand how solve this task without nesting loops. Pls, give me hint or solution, share some knowledge with coders brotherhood, man.
@saikumar-yg2ju
@saikumar-yg2ju 2 года назад
How did you setup this dark theme on leetcode ?
@ADEDOYIN87
@ADEDOYIN87 2 года назад
Go to editor settings
@SrujanS-lp4gh
@SrujanS-lp4gh 3 месяца назад
nice explanation, but the initution of this problem is quite difficult when we see it for the first time
@cmpe2723
@cmpe2723 2 года назад
Hey! Your videos are great! Can you please solve leetcode problem 68 text justification ? It is a hard problem and has a 89.13% frequency of being asked.
@dernayt2728
@dernayt2728 2 года назад
I was thinking on this problem since yesterday and just realised that I confused rows and columns
@greyreynyn
@greyreynyn 3 года назад
oh shit i had something like this at my current job, i think i had to take the string and print the zigzag
@courageware
@courageware 2 месяца назад
Generally focusing on the shape of the V, focusing on the number decreasing by 2 every time we go down. Way to explain this in a circular way there bud. Anyway good video aside from that.
@LaveshGarg
@LaveshGarg Год назад
Amazing Explanation man
@nanaamoako7493
@nanaamoako7493 2 года назад
The sequence is 1 2 3 4 3 2 1 2 3 4… given the number of rows is 4, but can someone help me generate that sequence using a loop statement?
@fnfal113
@fnfal113 2 месяца назад
This problem gave me headaches real fast and imposter syndrome kicked in
@alviahmed7388
@alviahmed7388 11 месяцев назад
Oh man how did you think up the solution to this???!!
@ianhnizdo4787
@ianhnizdo4787 Год назад
Tried this code with JavaScript based on Python and it just throws an error. Can anyone give me a clue as to what I'm missing cause I'm stumped. var convert = function(s, n) { if(n == 1) return s; let res = ''; for(let i=0; i
@thepoonhound3003
@thepoonhound3003 28 дней назад
you should make a replica video to all of your videos except made for c++ users
@floatingfortress721
@floatingfortress721 11 месяцев назад
Please do a dry run of the code
@connies_slice_of_life
@connies_slice_of_life 5 месяцев назад
thank you so much!!
@krateskim4169
@krateskim4169 2 года назад
i love this channel
@giridharanpasupathi
@giridharanpasupathi 6 месяцев назад
legendary answer bruhhhhhh
@ehmeddadashov9565
@ehmeddadashov9565 10 месяцев назад
Can someone explain the "if" part please?
@sourabhsharma2746
@sourabhsharma2746 Год назад
So clean thought process👏
@vimalslab3398
@vimalslab3398 3 года назад
amazing solution
@rohananthwal2527
@rohananthwal2527 6 месяцев назад
u r smart bro
@SoniaStalance
@SoniaStalance Год назад
TYSM
@adityamahamuni7365
@adityamahamuni7365 Год назад
THANK-FUCKING-YOU!!!
@Adarsh-mn7pl
@Adarsh-mn7pl Год назад
I hate this question!!
@L8rCloud
@L8rCloud 2 года назад
'L's and 'V's .....WTF? Honestly is this what goes on in your head??? I see x numbers of arrays and a couple of incremental/decremental processes to solve this simple problem (I prefer to user the term puzzle).
@randomystick
@randomystick 2 года назад
that uses extra O(s) space complexity and O(2s) time complexity total, which depending on your interviewer might not be what they want. I just got this problem today in an interview and coded your solution, but at the end of it they told me they wanted a one-pass solution which required the method shown above.
@twoPointers123
@twoPointers123 11 месяцев назад
O(n.n/2)=======O(n^2) if i am wring correct me
@akashbabutammali4478
@akashbabutammali4478 Месяц назад
It's o(n)
@tsunningwah3471
@tsunningwah3471 6 месяцев назад
zhina
@KadamMeera
@KadamMeera 7 месяцев назад
M
@ngangamhaolai4058
@ngangamhaolai4058 7 месяцев назад
thanks man helped a lot!
Далее
Text Justification - Leetcode 68 - Python
17:52
Просмотров 26 тыс.
Decode String - Leetcode 394 - Python
16:26
Просмотров 88 тыс.
Solving Wordle using information theory
30:38
Просмотров 10 млн
How I would learn Leetcode if I could start over
18:03
Просмотров 598 тыс.
Big-O Notation - For Coding Interviews
20:38
Просмотров 490 тыс.
Reverse Linked List II - Leetcode 92 - Python
16:03
Просмотров 82 тыс.