Python easy to understand


  • 0
    J

    The idea is to maintain a stack for the valid values and caluculate the scores of upcoming rounds using the values in the 'valid' stack

    class Solution(object):
        def calPoints(self, ops):
            """
            :type ops: List[str]
            :rtype: int
            """
            s = 0
            valid = []
            for op in ops:
                if (op == 'C'):
                    s -= valid[-1]
                    valid.pop()
                elif(op == 'D'):
                    s += 2*valid[-1]
                    valid.append(2*valid[-1])
                elif(op == '+'):
                    s += sum(valid[-2:])
                    valid.append(sum(valid[-2:]))
                else:
                    s += int(op)
                    valid.append(int(op))
            return s
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.