We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I just realized the algorism which I thought first. Actually, I did not believe that does not work. Even thought I read many code in order to solve my mystery , I could not understand.. I found so many people tried to solve the problem just like me..
If anyone try to poll out smaller number between 2 stacks,then he or she will fall into a trap.
ex:
wrong Algorism < pop() the smaller top of the 2 stacks>
max sum : 5
Stack A : 3 1 1 1 1
Stack B : 2 2 7 0
Top is from the begining.
Stack A : 3 1 1 1 1
Stack B : 2 2 7 0
^
smaller top (2<3)
So stack B.pop()
maxCnt : 1
sum : 2
Stack A : 3 1 1 1 1
Stack B : 2 7 0
^
smaller top (2<3)
So stack B.pop()
maxCnt : 2
sum : 4
and then? It is over because maxSum is 5 .
There is no more operation. So maxCnt is 2...
Stack A : 3 1 1 1 1
Stack B : 2 2 7 0
However, look at stack A only.
then we can pop() 3times.
3, 1, 1 (maxSum = 5 )
then maxCnt is 3.
That is why we can not use only smaller top value.
I am not sure but I hope it could be helpful for someone thought like me.
asdasdasd
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Game of Two Stacks
You are viewing a single comment's thread. Return to all comments →
I just realized the algorism which I thought first. Actually, I did not believe that does not work. Even thought I read many code in order to solve my mystery , I could not understand.. I found so many people tried to solve the problem just like me.. If anyone try to poll out smaller number between 2 stacks,then he or she will fall into a trap.
ex: wrong Algorism < pop() the smaller top of the 2 stacks>
max sum : 5 Stack A : 3 1 1 1 1 Stack B : 2 2 7 0
Top is from the begining. Stack A : 3 1 1 1 1
Stack B : 2 2 7 0
maxCnt : 1 sum : 2
Stack A : 3 1 1 1 1
Stack B : 2 7 0
maxCnt : 2 sum : 4
and then? It is over because maxSum is 5 . There is no more operation. So maxCnt is 2...
Stack A : 3 1 1 1 1 Stack B : 2 2 7 0
However, look at stack A only. then we can pop() 3times. 3, 1, 1 (maxSum = 5 ) then maxCnt is 3.
That is why we can not use only smaller top value.
I am not sure but I hope it could be helpful for someone thought like me.
asdasdasd