Design a Dynamic Array (Resizable Array)

แชร์
ฝัง
  • เผยแพร่เมื่อ 26 ก.ย. 2024
  • 🚀 Try the problem yourself: neetcode.io/pr...
    🥷 Discord: / discord
    🐦 Twitter: / neetcode1
    🐮 Support the channel: / neetcode
    ⭐ BLIND-75 PLAYLIST: • Two Sum - Leetcode 1 -...
    💡 DYNAMIC PROGRAMMING PLAYLIST: • House Robber - Leetco...
    #neetcode #leetcode #python

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

  • @NeetCodeIO
    @NeetCodeIO  ปีที่แล้ว +12

    Damn, who knew id get to write some neat code on my own site one day 😁
    🚀 You can try it here: neetcode.io/problems/dynamicArray
    Lmk if you have feedback. Got a lot of things already on my todo list

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

      Same variable is used for dynamic array capacity and element in the array i.e 'n' which is somewhat ambiguous
      And now where constraints for the prblm are mentioned
      These are not my complaints ,bcoz I am very Happy to see your leetcode
      But these modifications help us to solve problems without any confusion

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

      @@satwiktatikonda764 good points, appreciate the feedback!

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

      I think it would be helpful if we get a red background on the place there is an error rather than manually tracking the line number. Also, the error message can be multi lined, so when there is some error saying it happened ^, the location is correct.

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

      should we not increment the length inside insert method ?
      Your solution:
      // Insert value n at the i-th index
      public void insert(int i, int n) {
      arr[i] = n;
      }
      To this :
      my solution :
      public void insert(int i, int n) {
      array[i] = n;
      if(i >= size)
      size++;
      }

  • @satwiktatikonda764
    @satwiktatikonda764 ปีที่แล้ว +16

    Congratulations for your own leetcode 🎉

  • @МаринаВысотская-й2в
    @МаринаВысотская-й2в ปีที่แล้ว +3

    Keep it going, you inspire me when i watch your videos with such great explanations!

  • @philf4018
    @philf4018 3 หลายเดือนก่อน +2

    I think the description is missing something?
    If you can add elements with the insert function at any index, you can create an array like this: [0,1,0,0,4,0,0]
    And thats leads to wrong results in the pushback and popback functions. I.e. popback would return element 1 here because the size is 2.

    • @sharmagkachannel
      @sharmagkachannel 2 หลายเดือนก่อน

      Exact thing I was looking for, also the size would be incorrect in this case

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

      @@sharmagkachannel Yup, I think @NeetCodeIO is just wrong here, or at least has an incomplete/inaccurate problem description.

    • @austinharroun6019
      @austinharroun6019 24 วันที่ผ่านมา

      i guess you could add if statement to avoid user input error

  • @erickrds
    @erickrds 11 หลายเดือนก่อน +2

    Feedback: I "almost" got it, submitted and it wouldn't pass in 2 test cases, so I came here to see the solution and the problem was my interpretation. I read `empty array` and assumed an array with no elements instead of an array with empty elements, so that changed how I implemented the pushback and resize functions, which ended up being a simpler solution than what was supposed to be lol
    Edit: Just realized I could call resize under my verification of capacity size, now it passed
    Thank you for the explanation, I've learning a lot from your videos

    • @rafainfernal
      @rafainfernal 9 หลายเดือนก่อน

      Where did you submit it to in order for checking it against test cases?

  • @riccimel
    @riccimel 10 หลายเดือนก่อน +3

    man...wish I had understood it better.... feeling like crap now. Got study more
    I'll be back to say if I got it right
    tnx for your amazing job

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

      did you got it right??

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

    insert() is working like replace here. There is a bug in the insert code as per my understanding.

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

    congrats neetcode

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

    congrats broo

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

    so 'insert' is not actually insert a new element at that index but 'replace' an existing one at given index...

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

      I think that's generally how it works, e.g. if that index doesn't exist you would get an index out of bounds error in python

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

      @@NeetCodeIO umm ok. That part was little confusing to me when I was using Java. I was increasing the length as well while doing insert. 🫣

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

      same here--this got me as well, my interpretation was the same as yours & this was one of the last bugs i had to fix in my code before passing the 11 test cases @@Sranju23

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

    P R O M O S M 👉

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

    Congratulations on your new leetcode platform

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

    Why won't you actually "pop" the value in popback()?
    Make it a zero - for example before anything in popback() add this: self.arr[self.size] = 0

    • @rafainfernal
      @rafainfernal 9 หลายเดือนก่อน

      I guess you could do that, but it doesn't really matter. That's one more operation (very insignificant operation, sure). However, you know that you are getting an invalid number if you're getting anything at an index beyond the current size of the array - 1.