Advertisement
ogv

Untitled

ogv
Nov 13th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. class Solution {
  2. int[][] a;
  3. int m;
  4. int n;
  5. public int matrixScore(int[][] A) {
  6. a = A;
  7. m = a.length;
  8. n = a[0].length;
  9.  
  10. int max = opt(1);
  11. flipColumn(0);
  12.  
  13. max = Math.max(max, opt(1));
  14. return max;
  15. }
  16.  
  17. private void opt(int c){
  18. if (c == n) return score();
  19.  
  20. int max = opt(c+1);
  21. flipColumn(c);
  22. max = Math.max(max, opt(c+1));
  23. flipColumn(c);
  24. return max;
  25. }
  26.  
  27. private int score() {
  28. int sum = 0;
  29.  
  30. for (int r = 0; r < m; r++) {
  31. int num = 0;
  32. int p = 1;
  33. for (int c = n-1; c >= 0; c--){
  34. if (a[r][c] == 1) num += p;
  35. p <<= 1;
  36. }
  37. }
  38.  
  39. return sum;
  40. }
  41.  
  42. private void flipColumn(int c) {
  43. for (int r = 0; r < m; r++) a[r][c] = 1 - a[r][c];
  44. }
  45.  
  46. private void flipRow(int r) {
  47. for (int c = 0; c < n; c++)
  48. a[r][c] = 1 - a[r][c];
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement