1896. Minimum Cost to Change the Final Value of Expression
Description
You are given a valid boolean expression as a string expression consisting of the characters '1','0','&' (bitwise AND operator),'|' (bitwise OR operator),'(', and ')'.
- For example,
"()1|1"and"(1)&()"are not valid while"1","(((1))|(0))", and"1|(0&(1))"are valid expressions.
Return the minimum cost to change the final value of the expression.
- For example, if
expression = "1|1|(0&0)&1", its value is1|1|(0&0)&1 = 1|1|0&1 = 1|0&1 = 1&1 = 1. We want to apply operations so that the new expression evaluates to0.
The cost of changing the final value of an expression is the number of operations performed on the expression. The types of operations are described as follows:
- Turn a
'1'into a'0'. - Turn a
'0'into a'1'. - Turn a
'&'into a'|'. - Turn a
'|'into a'&'.
Note: '&' does not take precedence over '|' in the order of calculation. Evaluate parentheses first, then in left-to-right order.
Example 1:
Input: expression = "1&(0|1)" Output: 1 Explanation: We can turn "1&(0|1)" into "1&(0&1)" by changing the '|' to a '&' using 1 operation. The new expression evaluates to 0.
Example 2:
Input: expression = "(0&0)&(0&0&0)" Output: 3 Explanation: We can turn "(0&0)&(0&0&0)" into "(0|1)|(0&0&0)" using 3 operations. The new expression evaluates to 1.
Example 3:
Input: expression = "(0|(1|0&1))" Output: 1 Explanation: We can turn "(0|(1|0&1))" into "(0|(0|0&1))" using 1 operation. The new expression evaluates to 0.
Constraints:
1 <= expression.length <= 105expressiononly contains'1','0','&','|','(', and')'- All parentheses are properly matched.
- There will be no empty parentheses (i.e:
"()"is not a substring ofexpression).
Solutions
Solution: Depth-First Search
- Time complexity: O(n)
- Space complexity: O(n)
JavaScript
js
/**
* @param {string} expression
* @return {number}
*/
const minOperationsToFlip = function (expression) {
const n = expression.length;
const stack = [];
const bracketMap = new Map();
for (let index = 0; index < n; index++) {
const char = expression[index];
if (char === '(') {
stack.push(index);
} else if (char === ')') {
bracketMap.set(index, stack.pop());
}
}
const getMinFlipCost = (start, end) => {
if (start === end) return [Number(expression[start]), 1];
const startBracket = bracketMap.get(end) ?? end;
if (start === startBracket) return getMinFlipCost(start + 1, end - 1);
const [value1, cost1] = getMinFlipCost(start, startBracket - 2);
const [value2, cost2] = getMinFlipCost(startBracket, end);
const operator = expression[startBracket - 1];
if (operator === '&') {
if (value1 === 0 && value2 === 0) {
return [0, Math.min(cost1 + 1, cost2 + 1, cost1 + cost2)];
}
if (value1 === 1 && value2 === 1) {
return [1, Math.min(cost1, cost2)];
}
return [0, 1];
} else {
if (value1 === 0 && value2 === 0) {
return [0, Math.min(cost1, cost2)];
}
if (value1 === 1 && value2 === 1) {
return [1, Math.min(cost1 + 1, cost2 + 1, cost1 + cost2)];
}
return [1, 1];
}
};
return getMinFlipCost(0, n - 1)[1];
};