Ejemplo n.º 1
0
def nge(array):
    _nge = [-1 for i in array]
    s = Stack()

    for i in range(len(array)):
        element = array[i]

        while not s.is_empty() and element > array[s.peek()]:
            _nge[s.peek()] = i
            s.pop()

        s.push(i)

    return _nge
def compute_left_smaller_diffs(array):
    s = Stack()
    diff_array = [None for i in array]

    for i in range(len(array)):
        element = array[i]
        while not s.is_empty() and s.peek() > element:
            s.pop()

        if s.is_empty():
            diff_array[i] = 0
        else:
            diff_array[i] = s.peek()

        s.push(element)

    return diff_array
Ejemplo n.º 3
0
def calculate_span(stocks):
    spans = [-1 for i in stocks]
    s = Stack()
    s.push(-1)
    spans[0] = 1

    for i in range(len(stocks)):
        # pop until we find a taller element in stack
        while s.peek() != -1 and stocks[s.peek()] <= stocks[i]:
            s.pop()

        # calculate span
        spans[i] = i - s.peek()

        s.push(i)

    return spans
Ejemplo n.º 4
0
def print_common_nodes(root1, root2):
    s1 = Stack()
    s2 = Stack()

    while True:
        # push left nodes of trees to stack
        if root1:
            s1.push(root1)
            root1 = root1.left

        elif root2:
            s2.push(root2)
            root2 = root2.left

        elif not s1.is_empty() and not s2.is_empty():
            root1 = s1.peek()
            root2 = s2.peek()

            if root1.data == root2.data:
                # found match, check next in both trees
                print root1.data,
                s1.pop()
                s2.pop()

                root1 = root1.right
                root2 = root2.right

            elif root1.data < root2.data:
                # move to next node in first tree
                s1.pop()
                root1 = root1.right

                # set this to none or in the next iteration it gets
                # pushed to s2 again
                root2 = None

            else:
                s2.pop()
                root2 = root2.right
                root1 = None

        else:
            break
def find_largest_area(hist):
    s = Stack()
    n = len(hist)

    max_area = 0

    i = 0
    while i < n:

        # Push as long as stack top is smaller
        if s.is_empty() or hist[s.peek()] <= hist[i]:
            s.push(i)
            i += 1

        else:
            # pop one by one and calculate area considering, the popped
            # elements height as the smallest. notice we are not
            # incrementing i here. Hence we keep popping until we find
            # a smaller height
            s_top = s.pop()

            # width is from top to i, but not including i
            width = i if s.is_empty() else i - s.peek() - 1
            area_with_top_as_smallest = hist[s_top] * width

            if max_area < area_with_top_as_smallest:
                max_area = area_with_top_as_smallest

    # pop entire stack to consider all elements
    while not s.is_empty():

        s_top = s.pop()

        # here i is N, so width will be from top to N, not
        # including N
        width = i if s.is_empty() else i - s.peek() - 1
        area_with_top_as_smallest = hist[s_top] * width

        if max_area < area_with_top_as_smallest:
            max_area = area_with_top_as_smallest

    return max_area
Ejemplo n.º 6
0
def nge(array):
    s = Stack()

    for num in array:
        while not s.is_empty() and s.peek() < num:
            print s.pop(), '-', num

        s.push(num)

    while not s.is_empty():
        print s.pop(), '-', -1
Ejemplo n.º 7
0
def build_lowest_number_removing_k_digits(num, k):
    s = Stack()
    l = len(num) - k

    for c in num:
        if not s.is_empty() and int(s.peek()) > int(c) and k > 0:
            s.pop()
            k -= 1

        s.push(c)

    return ''.join(s.get_list()[:l])
def convert(exp):
    k = -1
    exp = list(exp)

    s = Stack()

    for i in range(len(exp)):

        if is_operand(exp[i]):
            k += 1
            exp[k] = exp[i]

        elif exp[i] == '(':
            s.push(exp[i])

        elif exp[i] == ')':
            while not s.is_empty() and s.peek() != '(':
                k += 1
                exp[k] = s.pop()

            if not s.is_empty() and s.peek() != '(':
                return -1

            else:
                s.pop()  # pop off the '('

        else:
            while not s.is_empty() and precedence(exp[i]) <= precedence(
                    s.peek()):
                k += 1
                exp[k] = s.pop()

            s.push(exp[i])

    while not s.is_empty():
        k += 1
        exp[k] = s.pop()

    return ''.join(exp)[0:k + 1]
Ejemplo n.º 9
0
def is_pre_order(array):
    s = Stack()
    root = -maxint

    for value in array:
        while not s.is_empty() and s.peek() < value:
            root = s.pop()

        if value < root:
            return False

        s.push(value)

    return True
Ejemplo n.º 10
0
def get_balanced_stack(string):
    s = Stack()

    for c in string:
        if c == '{' or s.is_empty():
            s.push(c)

        else:
            if s.peek() == '{':
                s.pop()

            else:
                s.push(c)

    return s
Ejemplo n.º 11
0
def dupe_found(string):
    s = Stack()
    for c in string:
        exp = ""
        if c != ')':
            s.push(c)
        else:
            while not s.is_empty() and s.peek() != '(':
                char = s.pop()
                exp += char

            if not is_exp(exp):
                return True
            s.pop()

    return False
Ejemplo n.º 12
0
class Queue2(object):
    def __init__(self):
        self.stack1 = Stack()
        self.stack2 = Stack()

    def push(self, element):
        self.stack1.push(element)

    def pop(self):
        if self.stack2.is_empty():
            while not self.stack1.is_empty():
                self.stack2.push(self.stack1.pop())

        return self.stack2.pop()

    def peek(self):
        if self.stack2.is_empty():
            while not self.stack1.is_empty():
                self.stack2.push(self.stack1.pop())

        return self.stack2.peek()
def find_max_len(string):
    n = len(string)
    result = 0

    s = Stack()
    s.push(-1)

    for i in range(n):
        if string[i] == '(':
            s.push(i)

        else:

            s.pop()

            if not s.is_empty():
                result = max(result, i - s.peek())

            else:
                s.push(i)

    return result