Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Runtime: 0 ms, faster than 100.00% of Java online submissions for Score After Flipping Matrix.
- class Solution {
- int[][] a;
- int m;
- int n;
- public int matrixScore(int[][] A) {
- a = A;
- m = a.length;
- n = a[0].length;
- for (int r = 0; r < m; r++)
- if (a[r][0] == 0) flipRow(r);
- for (int c = 1; c < n; c++) {
- int ones = 0;
- for (int r = 0; r < m; r++)
- if (a[r][c] == 1) ones++;
- if (ones <= m/2) flipColumn(c);
- }
- return score();
- }
- private int score() {
- int sum = 0;
- for (int r = 0; r < m; r++) {
- int num = 0;
- int p = 1;
- for (int c = n-1; c >= 0; c--){
- if (a[r][c] == 1) num += p;
- p <<= 1;
- }
- sum += num;
- }
- return sum;
- }
- private void flipColumn(int c) {
- for (int r = 0; r < m; r++) a[r][c] = 1 - a[r][c];
- }
- private void flipRow(int r) {
- for (int c = 0; c < n; c++) a[r][c] = 1 - a[r][c];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement