Exemplo n.º 1
0
def LargestRectangleInHistogram(hist):
    s = Stack()
    i = 0
    max_area = 0

    # Scan through Histogram
    while i < len(hist):
        # Increasing ---> Current Hist > Prev Hist
        if s.isEmpty() or hist[i] >= hist[s.peek()]:
            s.push(i)
        # Decreased! ---> Current Hist < Prev Hist (Hisab lagavi do)
        else:
            # Pop hist till Decreasing stops
            while not s.isEmpty() and hist[s.peek()] > hist[i]:
                L = s.pop()
                height = hist[L]
                if s.isEmpty():
                    area = height * i
                else:
                    area = height * (i - s.peek() - 1)
                max_area = max(max_area, area)
            s.push(i)
        i += 1

    while not s.isEmpty():
        L = s.pop()
        height = hist[L]
        area = height * (i - L - 1)
        max_area = max(max_area, area)

    return max_area
Exemplo n.º 2
0
def sort_stack(src):
    result = Stack()

    while not src.isEmpty():
        item = src.pop()
        while not result.isEmpty() and item > result.peek():
            src.push(result.pop())
        result.push(item)
    return result
Exemplo n.º 3
0
def Nearest_Smallest(arr):
    result = [-1] * len(arr)
    s = Stack()
    s.push(-1)

    # Scan through entire Array
    for index in xrange(len(arr)):

        # Nearest value is lesser ? Choose it
        if arr[index] > s.peek():
            result[index] = s.peek()
        else:
            # Go till the prev nearest value that's lesser? Choose it
            while arr[index] > s.peek():
                s.pop()
            result[index] = s.peek()

        # Get ready for next iteration
        s.push(arr[index])

    return result
Exemplo n.º 4
0
def LargestRectangleInHistogram_optimized(hist):
    s = Stack()
    max_area = 0
    i = 0

    # Scan through Histogram
    while i < len(hist):

        # Increasing ---> Current Hist > Prev Hist
        if s.isEmpty() or hist[i] >= hist[s.peek()]:
            s.push(i)
            i += 1
        # Decreased! ---> Current Hist < Prev Hist (Hisab lagavi do)
        else:
            # Pop hist till Decreasing stops
            L = s.pop()

            # Calculate area
            if s.isEmpty():
                current_area = hist[L] * i
            else:
                current_area = hist[L] * (i - s.peek() - 1)

            # Hisab for max area
            max_area = max(max_area, current_area)

    # Consider all remaining histogram
    while not s.isEmpty():
        L = s.pop()

        # Calculate area
        if s.isEmpty():
            current_area = hist[L] * i
        else:
            current_area = hist[L] * (i - s.peek() - 1)

        # Hisab for max area
        max_area = max(max_area, current_area)

    return max_area
def main():
    str = "AppleKayaKnothingAvid divA"
    mystack = Stack()
    mystack.push(str[0])
    ele = []

    for i in xrange(1, len(str) - 1):
        if str[i + 1] == mystack.peek():
            ele.append(mystack.pop())
        else:
            mystack.push(str[i])

    total = mystack.size()
    end = len(str) - 1

    print "Palindrom strings:"
    for i in range(total):
        palindrom = ""
        temp = mystack.pop()
        while str[end] != temp:
            palindrom += str[end]
            end -= 1
        print palindrom
        end -= 1
def main():
    str = "what is avid diva when kayak agree"
    mystack = Stack()
    mystack.push(str[0])
    mymap = {}
    PalindromStartIndexList = []

    sizeOfPalindromStr = 0
    for i in xrange(1, len(str) - 1):
        if str[i + 1] == mystack.peek():
            # MATCH, Palindrom starts
            if sizeOfPalindromStr == 0:
                PalindromStartIndexList.append(i)
            mystack.pop()
            sizeOfPalindromStr += 1
        else:
            if len(PalindromStartIndexList) != 0:
                mymap[PalindromStartIndexList.pop()] = (sizeOfPalindromStr - 1)
            sizeOfPalindromStr = 0
            mystack.push(str[i])

    # get max from mymap accordig to size
    IndexForLargestPalindrom = max(mymap, key=mymap.get)
    SizeOfLargestPalindrom = mymap[IndexForLargestPalindrom]

    # Build largest palindrom string using index and size
    LargestPalindromStr = ""
    PartPalindromStr = ""

    for i in xrange(IndexForLargestPalindrom + 1,
                    IndexForLargestPalindrom + 1 + SizeOfLargestPalindrom):
        PartPalindromStr += str[i]

    LargestPalindromStr = PartPalindromStr[::-1] + str[
        IndexForLargestPalindrom] + PartPalindromStr
    print LargestPalindromStr