Squares of Sorted Array (LeetCode 977) | Full Solution with examples & animation | Study Algorithms

แชร์
ฝัง
  • เผยแพร่เมื่อ 12 ก.ย. 2024

ความคิดเห็น • 31

  • @araneuskyuro
    @araneuskyuro ปีที่แล้ว +7

    Thank you so much bro!
    Been racking my brain since the past 48 hours on this problem.
    I appreciate the gradual and slow explanation with the brute force first.
    Subscribed!

    • @nikoo28
      @nikoo28  ปีที่แล้ว

      Welcome aboard !!

  • @omkarkhandalkar8869
    @omkarkhandalkar8869 2 ปีที่แล้ว +1

    waiting for such more approaches
    lots of love from Mumbai❣

  • @devbhattacharya153
    @devbhattacharya153 ปีที่แล้ว

    One of the best explanations on youtube... :)...Thanks a lot sir

  • @naveenaravindh7877
    @naveenaravindh7877 ปีที่แล้ว +1

    It helps.. you deserve subscriptions..👍🏻

  • @TheHariPutraOfficially
    @TheHariPutraOfficially 7 หลายเดือนก่อน +1

    Bhaiyya you look like my cousin brother
    ..that's why I feel connected to you....thanksss

  • @lakshmisruthi-z7t
    @lakshmisruthi-z7t 8 หลายเดือนก่อน +1

    great explanation :)

  • @AdityaPandey-eb7wq
    @AdityaPandey-eb7wq 2 ปีที่แล้ว +1

    nicely explained thanks..........

  • @mustafasabir12
    @mustafasabir12 11 หลายเดือนก่อน

    @9:20 Slight correction, The time complexity is O(2n) = O(n) because the array is traversed twice, once for squaring all the numbers and once for comparing for placement in new array.
    Also if there is a requirement for something similar in API terms then there can be a solution where you do not modify the original input and just use a single array. Because at any time you are only comparing 2 squares you need not create a separate array of squares, you can store left pointer, right pointer, left square and right square in 4 variables and then do the iteration and store it in the new array directly.
    class Solution:
    def sortedSquares(self, A: List[int]) -> List[int]:
    return_array = [0] * len(A)
    write_pointer = len(A) - 1
    left_read_pointer = 0
    right_read_pointer = len(A) - 1
    left_square = A[left_read_pointer] ** 2
    right_square = A[right_read_pointer] ** 2
    while write_pointer >= 0:
    if left_square > right_square:
    return_array[write_pointer] = left_square
    left_read_pointer += 1
    left_square = A[left_read_pointer] ** 2
    else:
    return_array[write_pointer] = right_square
    right_read_pointer -= 1
    right_square = A[right_read_pointer] ** 2
    write_pointer -= 1
    return return_array

    • @lalanabiridi8358
      @lalanabiridi8358 10 หลายเดือนก่อน

      we doint consider constants in O notation...so O(kn)=O(n)

    • @chandranshpandey1929
      @chandranshpandey1929 3 หลายเดือนก่อน

      can you do this ques in O(1) space complexity without using any extra space

  • @anishbishnoi29xD
    @anishbishnoi29xD 2 ปีที่แล้ว +1

    awesome bro. ♥️

  • @vlogsaryan2540
    @vlogsaryan2540 2 ปีที่แล้ว +1

    In the else loop in comment //increment tail pointer . I can't understand u do it by mistake or it's something else 😕

    • @nikoo28
      @nikoo28  2 ปีที่แล้ว +2

      So sorry for that...it should have been "//decrement tail pointer"

    • @vlogsaryan2540
      @vlogsaryan2540 2 ปีที่แล้ว

      @@nikoo28 no problem bhayia 👍

  • @KamranRasheed-os2yw
    @KamranRasheed-os2yw 6 หลายเดือนก่อน

    when you are getting the square of each integer in the array, could you also use the Math.pow method?

    • @nikoo28
      @nikoo28  6 หลายเดือนก่อน

      yes, you can

  • @haripriyaveluchamy9449
    @haripriyaveluchamy9449 ปีที่แล้ว +1

    thank u

  • @kirankumargs8265
    @kirankumargs8265 3 หลายเดือนก่อน

    We can ignore the traversal of input array to square the integers.
    int n = nums.length;
    int[] result = new int[n];
    int head = 0;
    int tail = n - 1;
    for (int i = n - 1; i >= 0; i--) {
    int headSquare = nums[head] * nums[head];
    int tailSquare = nums[tail] * nums[tail];
    if (headSquare > tailSquare) {
    result[i] = headSquare;
    head++;
    } else {
    result[i] = tailSquare;
    tail--;
    }
    }
    return result;

  • @parthh1112
    @parthh1112 6 หลายเดือนก่อน

    can we solve this without using extra space ?

    • @nikoo28
      @nikoo28  6 หลายเดือนก่อน

      you need the result array to store your squares. An inplace solution isn't possible

  • @knightclub6272
    @knightclub6272 2 ปีที่แล้ว +3

    sir can you also provide code of it

    • @nikoo28
      @nikoo28  2 ปีที่แล้ว +1

      Did you check the link in the description?

  • @shubhamagarwal7707
    @shubhamagarwal7707 ปีที่แล้ว

    Can it also be done without taking extra array to copy.

    • @araneuskyuro
      @araneuskyuro ปีที่แล้ว

      You could modify the initial array by using a temporary local variable to store values and then modify the initial array. Depends on your requirements.

    • @harshvardhansankpal716
      @harshvardhansankpal716 10 หลายเดือนก่อน

      @@araneuskyuro but even if u use temp variable it can store one value at a time. so i think we cannot escape the extra space thing.

    • @araneuskyuro
      @araneuskyuro 10 หลายเดือนก่อน

      @@harshvardhansankpal716 Oh right. Welp, sorry for the misinformation.

  • @CPS_XI
    @CPS_XI 5 หลายเดือนก่อน

    ❤💯

  • @noonecares-786
    @noonecares-786 ปีที่แล้ว

    please launch your dsa course....

    • @nikoo28
      @nikoo28  ปีที่แล้ว

      I will do that eventually, working on 1 video at a time and building concepts.