Score After Flipping Matrix Solutions in C++
Number 861
Difficulty Medium
Acceptance 72.8%
Link LeetCode
Other languages —
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/score-after-flipping-matrix/description//// Author : liuyubobobo/// Time : 2018-06-30#include <iostream>#include <vector>using namespace std;/// Greedy/// Time Complexity: O(n^2)/// Space Complexity: O(n^2)class Solution {public:int matrixScore(vector<vector<int>>& A) {for(int i = 0 ; i < A.size() ; i ++)if(A[i][0] == 0)flipRow(A, i);for(int j = 1 ; j < A[0].size() ; j ++){int zeros = zeroNumInCol(A, j);if(zeros > A.size() / 2)flipCol(A, j);}// for(int i = 0 ; i < A.size() ; i ++)// for(int j = 0 ; j < A[i].size() ; j ++)// cout << A[i][j] << (j == A[i].size() - 1 ? '\n' : ' ');return score(A);}private:int score(const vector<vector<int>>& A){int res = 0;for(int i = 0 ; i < A.size() ; i ++)for(int j = 0 ; j < A[i].size() ; j ++)if(A[i][j])res += (1 << (A[i].size() - 1 - j));return res;}void flipRow(vector<vector<int>>& A, int row){for(int j = 0 ; j < A[row].size() ; j ++)A[row][j] = 1 - A[row][j];}void flipCol(vector<vector<int>>& A, int col){for(int i = 0 ; i < A.size() ; i ++)A[i][col] = 1 - A[i][col];}int zeroNumInCol(const vector<vector<int>>& A, int col){int zeros = 0;for(int i = 0 ; i < A.size() ; i ++)zeros += (A[i][col] == 0 ? 1 : 0);return zeros;}};int main() {vector<vector<int>> A1 = {{0, 0, 1, 1},{1, 0, 1, 0},{1, 1, 0, 0}};cout << Solution().matrixScore(A1) << endl;return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/score-after-flipping-matrix/description//// Author : liuyubobobo/// Time : 2018-06-30#include <iostream>#include <vector>using namespace std;/// Greedy////// An unbelievable concise implementation to the same concept of this problem////// We make the left most digit of every row into 0 by xor A[r][0],/// After that, we can of course toggling the entire column to get all the left most digit to 1,////// Since we make every left most digit is 0, it means we need to toggle every row if the original left most digit is 1/// We make every element to xor the original leftmost digit in the same row,/// If the leftmost digit is 1, we need to toggle it, A[r][c] ^ 1 means toggle it! Since 0^1 = 1 and 1^1 = 0;/// If the leftmost digit is 0, we don't need to toggle it, A[r][c] ^ 0 means stick to the original digit./// The col variable record the one number if we don't toggle the column, just toggle the needed rows if we want to keep all the leftmost digits zero;/// The R - col variable record the one number if we toggle the column:)////// Time Complexity: O(n^2)/// Space Complexity: O(n^2)class Solution {public:int matrixScore(vector<vector<int>>& A) {int R = A.size(), C = A[0].size();int ans = 0;for (int c = 0; c < C; c ++) {int col = 0;for (int r = 0; r < R; r ++)col += A[r][c] ^ A[r][0];ans += max(col, R - col) * (1 << (C - 1 - c));}return ans;}};int main() {vector<vector<int>> A1 = {{0, 0, 1, 1},{1, 0, 1, 0},{1, 1, 0, 0}};cout << Solution().matrixScore(A1) << endl;return 0;}