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
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
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
@@satwiktatikonda764 good points, appreciate the feedback!
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.
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++;
}
Congratulations for your own leetcode 🎉
Keep it going, you inspire me when i watch your videos with such great explanations!
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.
Exact thing I was looking for, also the size would be incorrect in this case
@@sharmagkachannel Yup, I think @NeetCodeIO is just wrong here, or at least has an incomplete/inaccurate problem description.
i guess you could add if statement to avoid user input error
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
Where did you submit it to in order for checking it against test cases?
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
did you got it right??
insert() is working like replace here. There is a bug in the insert code as per my understanding.
congrats neetcode
congrats broo
so 'insert' is not actually insert a new element at that index but 'replace' an existing one at given index...
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
@@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. 🫣
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
P R O M O S M 👉
Congratulations on your new leetcode platform
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
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.