We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b43b827 commit 68c9299Copy full SHA for 68c9299
Python/minimum-cost-to-change-the-final-value-of-expression.py
@@ -8,14 +8,11 @@ def minOperationsToFlip(self, expression):
8
:rtype: int
9
"""
10
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])])
+ right, left = operands.pop(), operands.pop()
+ operands.append(ops[operators.pop()](left, right))
18
+ ops = {'&':lambda x, y: [min(x[0], y[0]), min(x[1]+y[1], min(x[1], y[1])+1)],
+ '|':lambda x, y: [min(x[0]+y[0], min(x[0], y[0])+1), min(x[1], y[1])]}
19
precedence = {'&':0, '|':0}
20
operands, operators = [], []
21
for c in expression:
@@ -36,7 +33,7 @@ def compute(operands, operators):
36
33
compute(operands, operators)
37
34
return max(operands[-1])
38
35
39
-
+
40
# Time: O(n)
41
# Space: O(n)
42
class Solution2(object):
0 commit comments