ZigZag Conversion - Leetcode 6 - Python

Поделиться
HTML-код
  • Опубликовано: 27 окт 2024

Комментарии • 84

  • @NeetCode
    @NeetCode  Год назад +13

    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.

  • @thewisestguy1
    @thewisestguy1 Год назад +41

    this problem humbled me real quick 😅

  • @henryCcc8614
    @henryCcc8614 2 года назад +47

    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 7 месяцев назад +2

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

  • @samlee405
    @samlee405 3 года назад +53

    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 года назад +4

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

    • @kushagarsharma4783
      @kushagarsharma4783 4 месяца назад +2

      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 Год назад +47

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

    • @srinayclg390
      @srinayclg390 7 месяцев назад +3

      practice and luck

    • @TF2Shows
      @TF2Shows 7 месяцев назад +4

      15 minutes*, its usually 2 questions per 45.

    • @angelofdeath095
      @angelofdeath095 6 месяцев назад +2

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

    • @pretty17Libra
      @pretty17Libra 2 месяца назад +4

      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

  • @atifhu
    @atifhu Год назад +81

    liked this video, disliked the leetcode problem

  • @canxingbu8813
    @canxingbu8813 3 года назад +17

    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!

  • @vipulchaudhary_js
    @vipulchaudhary_js Год назад +11

    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;
    }
    };

  • @Tony-yn5rr
    @Tony-yn5rr 2 года назад +14

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

  • @pvrohanraj
    @pvrohanraj Год назад +2

    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

  • @name_surname_1337
    @name_surname_1337 Год назад +3

    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

  • @KeshavKumar69420
    @KeshavKumar69420 3 года назад +4

    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!

  • @asdasddas100
    @asdasddas100 3 года назад +14

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

  • @ratikdubey4375
    @ratikdubey4375 2 года назад +3

    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 года назад +4

      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

  • @diasposangare1154
    @diasposangare1154 6 месяцев назад +2

    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

  • @butoyighyslain171
    @butoyighyslain171 8 месяцев назад

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

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

  • @Antinormanisto
    @Antinormanisto 7 месяцев назад +4

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

  • @eltonlobo8697
    @eltonlobo8697 3 года назад +2

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

  • @VINAYKUMAR-ob6co
    @VINAYKUMAR-ob6co 3 года назад +4

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

  • @k.k.harjeeth5422
    @k.k.harjeeth5422 Год назад

    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

  • @siddireddyvignesh
    @siddireddyvignesh Год назад +1

    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

  • @nathaniepeter5707
    @nathaniepeter5707 3 месяца назад

    I saw this question and laughed at the discussion 😂

  • @Ba2sik
    @Ba2sik 2 месяца назад

    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.

  • @jktruimp
    @jktruimp 3 года назад

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

  • @SrujanS-lp4gh
    @SrujanS-lp4gh 4 месяца назад

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

  • @appikondasreepavankumar
    @appikondasreepavankumar 7 месяцев назад

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

  • @LaveshGarg
    @LaveshGarg Год назад

    Amazing Explanation man

  • @yongfulu8984
    @yongfulu8984 3 года назад +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 года назад +7

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

  • @lokeshsenthilkumar4522
    @lokeshsenthilkumar4522 3 года назад +1

    increment should be outside the loop

  • @jagrutitiwari2551
    @jagrutitiwari2551 5 месяцев назад +1

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

  • @Eria196
    @Eria196 2 года назад +3

    how does each element in res get to the right position yet they are generated by two different conditions

    • @Albertinin0
      @Albertinin0 Год назад +2

      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 Год назад +1

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

  • @dernayt2728
    @dernayt2728 2 года назад

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

  • @RohanPahwa-x8h
    @RohanPahwa-x8h 9 месяцев назад

    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?

  • @greyreynyn
    @greyreynyn 3 года назад +1

    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.

  • @fnfal113
    @fnfal113 3 месяца назад

    This problem gave me headaches real fast and imposter syndrome kicked in

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

  • @connies_slice_of_life
    @connies_slice_of_life 5 месяцев назад

    thank you so much!!

  • @alviahmed7388
    @alviahmed7388 Год назад

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

  • @floatingfortress721
    @floatingfortress721 Год назад

    Please do a dry run of the code

  • @sanwalfarooque2747
    @sanwalfarooque2747 2 года назад +3

    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.

  • @KamleshSharma-si2rq
    @KamleshSharma-si2rq 2 дня назад

    asked me in oracle interview with expected time 10 to 15 minutes, worst problem

  • @saikumar-yg2ju
    @saikumar-yg2ju 2 года назад +2

    How did you setup this dark theme on leetcode ?

    • @ADEDOYIN87
      @ADEDOYIN87 2 года назад

      Go to editor settings

  • @thepoonhound3003
    @thepoonhound3003 Месяц назад

    you should make a replica video to all of your videos except made for c++ users

  • @krateskim4169
    @krateskim4169 2 года назад

    i love this channel

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

  • @nanaamoako7493
    @nanaamoako7493 2 года назад +1

    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?

  • @ianhnizdo4787
    @ianhnizdo4787 Год назад +1

    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

  • @vimalslab3398
    @vimalslab3398 3 года назад

    amazing solution

  • @giridharanpasupathi
    @giridharanpasupathi 6 месяцев назад

    legendary answer bruhhhhhh

  • @thepriestofvaranasi
    @thepriestofvaranasi День назад +1

    One of the worst and useless problems out there. Adds nothing of value.

  • @rohananthwal2527
    @rohananthwal2527 7 месяцев назад

    u r smart bro

  • @sourabhsharma2746
    @sourabhsharma2746 Год назад

    So clean thought process👏

  • @ehmeddadashov9565
    @ehmeddadashov9565 11 месяцев назад

    Can someone explain the "if" part please?

  • @Adarsh-mn7pl
    @Adarsh-mn7pl Год назад +3

    I hate this question!!

  • @adityamahamuni7365
    @adityamahamuni7365 Год назад +1

    THANK-FUCKING-YOU!!!

  • @SoniaStalance
    @SoniaStalance Год назад

    TYSM

  • @twoPointers123
    @twoPointers123 Год назад

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

  • @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 года назад +1

      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
    @tsunningwah3471 6 месяцев назад

    zhina

  • @KadamMeera
    @KadamMeera 8 месяцев назад

    M

  • @ngangamhaolai4058
    @ngangamhaolai4058 8 месяцев назад

    thanks man helped a lot!