ysjaitawat
BAN USERIt will print 2 6 8 10 13.
As the control will reach to the print statement only in the last iteration when j will be calculated 2 + 0 and each time it would have increased before print.
Inverse.Falcon, nice approach.
I would like to add one more thing to take care of multiple instance of same value.
As min is popped u will pop it from minStack as well, but if there is one more element down in the stack then min will return wrong value.
So if new element is same as min push min again to both the stacks.
@ramukumar as soon as min is popped out "min" will be out of sync.
Stack should always be LIFO. Tweaking is not an option.
- ysjaitawat September 16, 2010If tree is binary, an heap would sort the problem with array values
[root],[root->left],[root->right],[1-left],[1->right], ... 2n+1,2n+2
where n is any node left child will be in [2n+1] and right in [2n+n]
Using just two string it can be done.
string transformString(string original) //
{
string fresh;
string dups;
foreach(char ch in orig);
if (strchr(fresh, ch) == null) //search for the character in the string
strcat(fresh, ch); // Append character to string of unique char
else
strcat(dups, ch); // Append character to string of duplicate char
return(strcat(fresh,dups)); // Append string of duplicate to string of unique char
}
What if the string has all the character including alphanumeric and special characters?
- ysjaitawat September 15, 2010
Yes, and as soon as by pop() the minimum number is pooped from stack, how would you calculate minimum in o(1).
- ysjaitawat September 20, 2010