I really enjoyed this problem. I did not see the solution at first, but after it popped up, it was really simple.

HackerRank Kangaroo Solution

You have an empty sequence, and you will be given N queries. Each query is one of these three types:

HackerRank Non-Divisible Subset Solution

HackerRank Flatland Space Station Solution

class CustomStack: def __init__(self): self.stack = [] self.maxima = [] def push(self, value): self.stack.append(value) if not self.maxima or value = self.maxima[-1]: self.maxima.append(value) def printMax(self): print self.maxima[-1] def pop(self): value = self.stack.pop() if value == self.maxima[-1]: self.maxima.pop() def main(): cs = CustomStack() N = int(raw_input()) for _ in xrange(N): unknown = raw_input() command = unknown[0] if command == 1: cmd, value = map(int, unknown.split()) cs.push(value) elif command == 2: cs.pop() else: cs.printMax() if __name__ == __main__: main()

Keep two stacks. One for the actual values and one (non-strictly) increasing stack for keeping the maxima.

If you enjoyed this post, then make sure you subscribe to my Newsletter and/or Feed.

HackerRank Largest Rectangle Solution

onCodility SqlSegmentsSum Kalium 2015 Solution

onCodility Distinct Solution

HackerRank Simple Text Editor Solution

onCodility BinaryGap Solution

This entry was posted inCoding ChallengeHackerRankPython. Bookmark thepermalink.