forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create 1896.Minimum-Cost-to-Change-the-Final-Value-of-Expression.cpp
- Loading branch information
1 parent
aa36c9c
commit d817a42
Showing
1 changed file
with
90 additions
and
0 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
...e-Final-Value-of-Expression/1896.Minimum-Cost-to-Change-the-Final-Value-of-Expression.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
class Solution { | ||
public: | ||
int evalVal(char op, pair<int,int>&a, pair<int,int>&b) | ||
{ | ||
if (op == '#') | ||
return b.first; | ||
else if (op == '&') | ||
return a.first & b.first; | ||
else | ||
return a.first | b.first; | ||
} | ||
|
||
// 1. 1|1,需要翻转一个数字,同时翻转运算符 | ||
// 2. 0|0,需要翻转一个数字 | ||
// 3. 0|1,需要翻转运算符 | ||
// 4. 1&1,需要翻转一个数字 | ||
// 5. 0&0,需要翻转一个数字,同时翻转运算符 | ||
// 6. 0&1,需要翻转运算符 | ||
int evalFlip(char op, pair<int,int>&a, pair<int,int>&b) | ||
{ | ||
if (op == '#') | ||
return b.second; | ||
else if (op=='&') | ||
{ | ||
if (a.first + b.first == 2) | ||
return min(a.second, b.second); | ||
else if (a.first + b.first == 1) | ||
return 1; | ||
else | ||
return min(a.second, b.second)+1; | ||
|
||
} | ||
else | ||
{ | ||
if (a.first + b.first == 2) | ||
return min(a.second, b.second)+1; | ||
else if (a.first + b.first == 1) | ||
return 1; | ||
else | ||
return min(a.second, b.second); | ||
} | ||
} | ||
|
||
int minOperationsToFlip(string expression) | ||
{ | ||
stack<pair<int,int>>s1; // {val, # of operations to flip this value} | ||
stack<char>s2; | ||
pair<int,int> cur = {-1, -1}; | ||
char op = '#'; | ||
|
||
int ret = 0; | ||
expression = "(" + expression + ")"; | ||
|
||
for (auto ch:expression) | ||
{ | ||
if (ch=='&'||ch=='|') | ||
op = ch; | ||
else if (ch=='0' || ch=='1') | ||
{ | ||
pair<int,int> nxt = {ch-'0', 1}; | ||
int val = evalVal(op, cur, nxt); | ||
int flip = evalFlip(op, cur, nxt); | ||
cur = {val, flip}; | ||
ret = flip; | ||
} | ||
else if (ch=='(') | ||
{ | ||
s1.push(cur); | ||
s2.push(op); | ||
cur = {-1, -1}; | ||
op = '#'; | ||
} | ||
else | ||
{ | ||
auto last = s1.top(); | ||
s1.pop(); | ||
char op = s2.top(); | ||
s2.pop(); | ||
|
||
int val = evalVal(op, last, cur); | ||
int flip = evalFlip(op, last, cur); | ||
cur = {val, flip}; | ||
ret = flip; | ||
} | ||
} | ||
|
||
return ret; | ||
} | ||
}; | ||
|