Codility 'StoneWall' Solution

Martin Kysel · December 31, 2014

Short Problem Definition:

Cover “Manhattan skyline” using the minimum number of rectangles.

StoneWall

Complexity:

expected worst-case time complexity is O(N)

expected worst-case space complexity is O(N)

Execution:

The explanation to this challenge has been posted on the Codility web, you can read it here.

Solution:

def solution(H):
    block_cnt = 0

    stack = []

    for height in H:
        # remove all blocks that are bigger than my height
        while len(stack) != 0 and stack[-1] > height:
            stack.pop()

        if len(stack) != 0 and stack[-1] == height:
            # we already paid for this size
            pass
        else:
            # new block is required, push it's size to the stack
            block_cnt += 1
            stack.append(height)

    return block_cnt

Twitter, Facebook

To learn more about solving Coding Challenges in Python, I recommend these courses: Educative.io Python Algorithms, Educative.io Python Coding Interview.