![]() ![]() 1) initialize a queue holding the free slots from 0 to N - 1.Ģ) initialize the heads of all the stacks to -1 (the stacks are empty)ģ) POP: given a stack, set the head of that stack to the next element Here is a short description, space complexity O(N) where N is the size of the array. The only thing you would have to do is to properly set the pointers (or indexes, after all, we are using an array). Storing such a queue might be considered overhead, still, the space complexity would remain the same. ![]() Everytime you push or pop a value, you pop or push a free slot, respectively. For instance, you would know in which slot you would add your next value (independently from the stack you are pushing to). If you store a queue with the free slots available, you have everything you need. However, you could implement this the same way as implementing K stacks in 1 array. As the stacks grow, you know the array is full when the heads of each stack are adjacent. For the particular case of 2 stacks in one array, I like the solution of starting one stack at the first index and the second one at the last index. I guess there are many solutions to this problem. gaining experience) help me become better at this? In general, how should I 'think' about these problems? Or can only really intelligent people solve these types of problems? Will tackling/solving problems like these (i.e. Am I on the right track? Can someone give my possible pointers for solving this problem? I have been banging my head on the question for quite a while now. If the elements of the stack are (key,value) pairs, the key being the stack number, then to pop an element I would have to search, in the worst case, i or (n-i) times - which will be O(n) (feel free to correct me if I am wrong here), which would not be O(1). The problem with this approach is I will not be able to POP-S1 or POP-S2 reliably as there is no way of 'remembering' which element belongs to which stack. if stack S1 is full when a new element is trying to be pushed in, then push that element into stack S2 and vice versa). Let array A implement two stacks: S1 and S2.įor the PUSH-S1 and PUSH-S2 operations, if the stack is 'full' then start pushing elements into the other stack (eg. The solution that I have come up with so far is: ![]() The PUSH and POP operations should run in O(1) time. Here is what its states:Įxplain how to implement two stacks in one array A in such a way that neither stack overflows unless the total number of elements in both stacks together is n. I am trying to solve Excercise 10.1-2 from Chapter 10 Elementary Data Structures from CLRS Second Edition. I am trying to solve the problems and exercises given in the book by myself. I am reading Introduction to Algorithms - the famous CLRS text to become a better programmer. I want to begin by saying that this is NOT a homework question. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |