博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
861. Score After Flipping Matrix
阅读量:4986 次
发布时间:2019-06-12

本文共 1481 字,大约阅读时间需要 4 分钟。

We have a two dimensional matrix A where each value is 0 or 1.

A move consists of choosing any row or column, and toggling each value in that row or column: changing all 0s to 1s, and all 1s to 0s.

After making any number of moves, every row of this matrix is interpreted as a binary number, and the score of the matrix is the sum of these numbers.

Return the highest possible score.

 

Example 1:

Input: [[0,0,1,1],[1,0,1,0],[1,1,0,0]]Output: 39Explanation:Toggled to [[1,1,1,1],[1,0,0,1],[1,1,1,1]].0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39

 

Note:

  1. 1 <= A.length <= 20
  2. 1 <= A[0].length <= 20
  3. A[i][j] is 0 or 1.

 

Approach #1: C++.

class Solution {public:    int matrixScore(vector
>& A) { int r = A.size(), c = A[0].size(); int ans = 0; for (int i = 0; i < c; ++i) { int col = 0; for (int j = 0; j < r; ++j) col += A[j][i] ^ A[j][0]; //相同为0, 不同为1。 ans += max(col, r - col) * (1 << (c - 1 - i)); } return ans; }};

  

Analysis:

Because every row of the matrix repersent a binary number, we want to the number become bigger, so the left-most column we can make it become 1. The others columns we can make it become 1 as more as possible by flipping the column. we count the number of how many number in the column is same as the left-most column. and using max(col, r - col) to get the number of 1 we can get.

转载于:https://www.cnblogs.com/ruruozhenhao/p/10305862.html

你可能感兴趣的文章
百度之星B题(组合数)
查看>>
利用zabbix api添加、删除、禁用主机
查看>>
从头到尾彻底理解KMP
查看>>
字符等价关系
查看>>
Java内存泄露监控工具:JVM监控工具介绍【转】
查看>>
FIREDAC字段类型映射
查看>>
Android 中文字体的设置方法和使用技巧
查看>>
反射的一个小实例
查看>>
windows下搭建nginx+php+laravel开发环境(转)
查看>>
PHP+MySql实现后台数据的读取
查看>>
一致性hash算法 - consistent hashing
查看>>
单向链表的反转
查看>>
Python Redis string
查看>>
mipi 调试经验(转)
查看>>
按位与、或、异或等运算方法
查看>>
Hash::make与Hash::check
查看>>
初步理解前端模块化开发
查看>>
UDF-java获取名字中的姓
查看>>
201421123042 《Java程序设计》第11周学习总结
查看>>
PHP 中文工具类,支持汉字转拼音、拼音分词、简繁互转
查看>>