ZigZag Conversion - Leetcode 6 - Python

Ғылым және технология

🚀 neetcode.io/ - A better way to prepare for Coding Interviews
🐦 Twitter: / neetcode1
🥷 Discord: / discord
🐮 Support the channel: / neetcode
⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
💡 CODING SOLUTIONS: • Coding Interview Solut...
💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
🌲 TREE PLAYLIST: • Invert Binary Tree - D...
💡 GRAPH PLAYLIST: • Course Schedule - Grap...
💡 BACKTRACKING PLAYLIST: • Word Search - Backtrac...
💡 LINKED LIST PLAYLIST: • Reverse Linked List - ...
💡 BINARY SEARCH PLAYLIST: • Binary Search
📚 STACK PLAYLIST: • Stack Problems
Problem Link: leetcode.com/problems/zigzag-...
0:00 - Read the problem
1:50 - Drawing Explanation
8:30 - Coding Explanation
leetcode 6
This question was identified as an interview question from here: github.com/xizhengszhang/Leet...
#coding #interview #python
Disclosure: Some of the links above may be affiliate links, from which I may earn a small commission.

Пікірлер: 73

  • @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

    7 ай бұрын

    Your explanation has built my logic design strongers.

  • @atifhu
    @atifhu Жыл бұрын

    liked this video, disliked the leetcode problem

  • @thewisestguy1
    @thewisestguy111 ай бұрын

    this problem humbled me real quick 😅

  • @smartsoothing776
    @smartsoothing7769 ай бұрын

    How can we come up with this type of solution in 45 min interview? 🤕

  • @srinayan390

    @srinayan390

    4 ай бұрын

    practice and luck

  • @TF2Shows

    @TF2Shows

    3 ай бұрын

    15 minutes*, its usually 2 questions per 45.

  • @angelofdeath095

    @angelofdeath095

    3 ай бұрын

    With Shit ton of practice....easy...

  • @samlee405
    @samlee4052 жыл бұрын

    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

    2 жыл бұрын

    I solved it with that method and while less space efficient it was faster then 90% of submissions surprisingly

  • @kushagarsharma4783

    @kushagarsharma4783

    Ай бұрын

    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

  • @henryCcc8614
    @henryCcc86142 жыл бұрын

    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

    3 ай бұрын

    Thats exactly how I did it the first time (and it worked, beats 47% time and 72% space complexity).

  • @Tony-yn5rr
    @Tony-yn5rr2 жыл бұрын

    If I had to answer this I would fail, just properly parsing the instructions felt difficult till I saw you draw the zig zag.

  • @canxingbu8813
    @canxingbu88132 жыл бұрын

    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!

  • @KeshavKumar69420
    @KeshavKumar694202 жыл бұрын

    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!

  • @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

  • @eltonlobo8697
    @eltonlobo86972 жыл бұрын

    Hi, Can you do Subsets 2? Couldn't find any good video for that problem. You explain every problem's solution nicely.

  • @butoyighyslain171
    @butoyighyslain1715 ай бұрын

    thanks Neetcode! I cant think of a use case when this would be needed in real life

  • @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.

  • @jktruimp
    @jktruimp2 жыл бұрын

    Thanks for your tutorial, I can't understand this question's solutions wrote by others until I watched this video.😂

  • @name_surname_1337
    @name_surname_13378 ай бұрын

    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

  • @thewisestguy1
    @thewisestguy111 ай бұрын

    Can you explain the algorithm of (numRows - 1) * 2?

  • @asdasddas100
    @asdasddas1002 жыл бұрын

    Hey I just did this one. I bet your solution is 300x more elegant though Edit: It is. :p

  • @cmpe2723
    @cmpe27232 жыл бұрын

    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.

  • @user-cq9dp2gy7j
    @user-cq9dp2gy7j6 ай бұрын

    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?

  • @ratikdubey4375
    @ratikdubey43752 жыл бұрын

    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

  • @LaveshGarg
    @LaveshGarg Жыл бұрын

    Amazing Explanation man

  • @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

  • @VINAYKUMAR-ob6co
    @VINAYKUMAR-ob6co2 жыл бұрын

    Hey can you do all top 100 questions of leetcode 🙂🙂

  • @dernayt2728
    @dernayt27282 жыл бұрын

    I was thinking on this problem since yesterday and just realised that I confused rows and columns

  • @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 int inc = 2 * (numRows-1); for (int i = r; i ans += s[i]; if (r > 0 && r ans += s[i + inc - 2*r]; } } } return ans; } };

  • @nathaniepeter5707
    @nathaniepeter570718 күн бұрын

    I saw this question and laughed at the discussion 😂

  • @greyreynyn
    @greyreynyn2 жыл бұрын

    oh shit i had something like this at my current job, i think i had to take the string and print the zigzag

  • @ehmeddadashov9565
    @ehmeddadashov95658 ай бұрын

    Can someone explain the "if" part please?

  • @krateskim4169
    @krateskim41692 жыл бұрын

    i love this channel

  • @Eria196
    @Eria1962 жыл бұрын

    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).

  • @Antinormanisto
    @Antinormanisto3 ай бұрын

    Someday i will understand how these geniuses find solutions By the way i didn't understand solution

  • @licokr
    @licokr4 ай бұрын

    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!

  • @saikumar-yg2ju
    @saikumar-yg2ju2 жыл бұрын

    How did you setup this dark theme on leetcode ?

  • @ADEDOYIN87

    @ADEDOYIN87

    Жыл бұрын

    Go to editor settings

  • @vimalslab3398
    @vimalslab33982 жыл бұрын

    amazing solution

  • @connies_slice_of_life
    @connies_slice_of_life2 ай бұрын

    thank you so much!!

  • @jagrutitiwari2551
    @jagrutitiwari25512 ай бұрын

    In the nrw UI they have hidden the dislikes count. I don't know why. That's so wrong.

  • @lokeshsenthilkumar4522
    @lokeshsenthilkumar45222 жыл бұрын

    increment should be outside the loop

  • @SoniaStalance
    @SoniaStalance Жыл бұрын

    TYSM

  • @rohananthwal2527
    @rohananthwal25273 ай бұрын

    u r smart bro

  • @sourabhsharma2746
    @sourabhsharma2746 Жыл бұрын

    So clean thought process👏

  • @giridharanpasupathi
    @giridharanpasupathi3 ай бұрын

    legendary answer bruhhhhhh

  • @diasposangare1154
    @diasposangare11543 ай бұрын

    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

  • @nanaamoako7493
    @nanaamoako7493 Жыл бұрын

    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?

  • @yongfulu8984
    @yongfulu89842 жыл бұрын

    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

    2 жыл бұрын

    Good question, I wanna make a video about that soon 😉

  • @SrujanS-lp4gh
    @SrujanS-lp4ghАй бұрын

    nice explanation, but the initution of this problem is quite difficult when we see it for the first time

  • @ngangamhaolai4058
    @ngangamhaolai40584 ай бұрын

    thanks man helped a lot!

  • @floatingfortress721
    @floatingfortress7218 ай бұрын

    Please do a dry run of the code

  • @alviahmed7388
    @alviahmed73889 ай бұрын

    Oh man how did you think up the solution to this???!!

  • @adityamahamuni7365
    @adityamahamuni7365 Жыл бұрын

    THANK-FUCKING-YOU!!!

  • @user-qm7ni7bx9g
    @user-qm7ni7bx9g4 ай бұрын

    increment == 0 case: increment = 2*(numRows-1) if 2*(numRows-1) > 0 else 1

  • @Adarsh-mn7pl
    @Adarsh-mn7pl Жыл бұрын

    I hate this question!!

  • @sanwalfarooque2747
    @sanwalfarooque27472 жыл бұрын

    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. :) :)

  • @tejatavva6009

    @tejatavva6009

    Жыл бұрын

    once post your solution brother, it would be very helpful :)

  • @HolyC-xs2zj

    @HolyC-xs2zj

    11 ай бұрын

    I dont understand how solve this task without nesting loops. Pls, give me hint or solution, share some knowledge with coders brotherhood, man.

  • @siddireddyvignesh
    @siddireddyvignesh10 ай бұрын

    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

  • @k.k.harjeeth5422
    @k.k.harjeeth54229 ай бұрын

    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

  • @twoPointers123
    @twoPointers1239 ай бұрын

    O(n.n/2)=======O(n^2) if i am wring correct me

  • @L8rCloud
    @L8rCloud2 жыл бұрын

    '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).

  • @bryanleow5024

    @bryanleow5024

    Жыл бұрын

    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.

  • @tsunningwah3471
    @tsunningwah34713 ай бұрын

    zhina

  • @KadamMeera
    @KadamMeera4 ай бұрын

    M

Келесі