Minimum Flips to Make a OR b Equal to c Solutions in C++
Number 1318
Difficulty Medium
Acceptance 62.8%
Link LeetCode
Other languages —
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/minimum-flips-to-make-a-or-b-equal-to-c//// Author : liuyubobobo/// Time : 2020-01-11#include <iostream>#include <vector>using namespace std;/// Convert every number into binary representation/// Using array to store/// Time Complexity: O(log(max(a, b, c)))/// Space Complexity: O(log(max(a, b, c)))class Solution {public:int minFlips(int a, int b, int c) {vector<int> av = convert(a), bv = convert(b), cv = convert(c);int n = max(av.size(), max(bv.size(), cv.size()));while(av.size() < n) av.push_back(0);while(bv.size() < n) bv.push_back(0);while(cv.size() < n) cv.push_back(0);int res = 0;for(int i = 0; i < n; i ++)if(cv[i] == 0) res += av[i] + bv[i];else res += (av[i] || bv[i]) ? 0 : 1;return res;}private:vector<int> convert(int x){vector<int> res;while(x) res.push_back(x % 2), x /= 2;return res;}};int main() {cout << Solution().minFlips(2, 6, 5) << endl;// 3cout << Solution().minFlips(4, 2, 7) << endl;// 1cout << Solution().minFlips(1, 2, 3) << endl;// 0return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/minimum-flips-to-make-a-or-b-equal-to-c//// Author : liuyubobobo/// Time : 2020-01-11#include <iostream>#include <vector>using namespace std;/// Convert every number into binary representation in the run/// Time Complexity: O(log(max(a, b, c)))/// Space Complexity: O(1)class Solution {public:int minFlips(int a, int b, int c) {int res = 0;while(a || b || c){int abit = a & 1, bbit = b & 1, cbit = c & 1;a >>= 1, b >>= 1, c >>= 1;if(cbit == 0) res += abit + bbit;else res += (abit | bbit) ? 0 : 1;}return res;}};int main() {cout << Solution().minFlips(2, 6, 5) << endl;// 3cout << Solution().minFlips(4, 2, 7) << endl;// 1cout << Solution().minFlips(1, 2, 3) << endl;// 0return 0;}