Skip to content

Commit 68c9299

Browse files
authored
Update minimum-cost-to-change-the-final-value-of-expression.py
1 parent b43b827 commit 68c9299

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

Python/minimum-cost-to-change-the-final-value-of-expression.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ def minOperationsToFlip(self, expression):
88
:rtype: int
99
"""
1010
def compute(operands, operators):
11-
right, left, op = operands.pop(), operands.pop(), operators.pop()
12-
if op == '&':
13-
operands.append([min(left[0], right[0]),
14-
min(left[1]+right[1], min(left[1], right[1])+1)])
15-
else:
16-
operands.append([min(left[0]+right[0], min(left[0], right[0])+1),
17-
min(left[1], right[1])])
11+
right, left = operands.pop(), operands.pop()
12+
operands.append(ops[operators.pop()](left, right))
1813

14+
ops = {'&':lambda x, y: [min(x[0], y[0]), min(x[1]+y[1], min(x[1], y[1])+1)],
15+
'|':lambda x, y: [min(x[0]+y[0], min(x[0], y[0])+1), min(x[1], y[1])]}
1916
precedence = {'&':0, '|':0}
2017
operands, operators = [], []
2118
for c in expression:
@@ -36,7 +33,7 @@ def compute(operands, operators):
3633
compute(operands, operators)
3734
return max(operands[-1])
3835

39-
36+
4037
# Time: O(n)
4138
# Space: O(n)
4239
class Solution2(object):

0 commit comments

Comments
 (0)