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: 39
Explanation:
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.

Runtime: 4 ms, faster than 58.17% of C++ online submissions for Score After Flipping Matrix.

注意一下,首列如果某行从0换成1那么在之后计算这一行时要反向。

class Solution {
public:
int matrixScore(vector<vector<int>>& A) {
int r = A.size();
int c = A[].size();
vector<int> rsign(r,);
int base = << (A[].size()-), ret = ;
for(int j=; j<c; j++){
int cntzero = , cntone = ;
for(int i=; i<r; i++){
if(j == ){
if(A[i][j] != ) rsign[i]++;
}else{
if((A[i][j] == && rsign[i] == ) || (A[i][j] == && rsign[i] == )) cntzero++;
else cntone++;
}
}
if(j == ){
ret += base * r;
} else if(cntzero > cntone) {
ret += base * cntzero;
}else ret += base * cntone;
base >>= ;
} return ret;
}
};

LC 861. Score After Flipping Matrix的更多相关文章

  1. 861. Score After Flipping Matrix

    We have a two dimensional matrix A where each value is 0 or 1. A move consists of choosing any row o ...

  2. 【LeetCode】861. Score After Flipping Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  3. 【leetcode】861. Score After Flipping Matrix

    题目如下: 解题思路:本题需要知道一个数字规律,即pow(2,n) > sum(pow(2,0)+pow(2,1)+...+pow(2,n-1)).所以,为了获得最大值,要保证所有行的最高位是1 ...

  4. [LeetCode] Score After Flipping Matrix 翻转矩阵后的分数

    We have a two dimensional matrix A where each value is 0 or 1. A move consists of choosing any row o ...

  5. [Swift]LeetCode861. 翻转矩阵后的得分 | Score After Flipping Matrix

    We have a two dimensional matrix A where each value is 0 or 1. A move consists of choosing any row o ...

  6. LC 856. Score of Parentheses

    Given a balanced parentheses string S, compute the score of the string based on the following rule: ...

  7. [LC] 240. Search a 2D Matrix II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  8. [LC] 74. Search a 2D Matrix

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  9. 算法与数据结构基础 - 贪心(Greedy)

    贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...

随机推荐

  1. ath6kl 架构

    转:http://blog.csdn.net/robertsong2004/article/details/38899415 AR600x软件被划分为主机端和目标端软件.主机端软件或驱动程序的代码被提 ...

  2. 抓住“新代码”的影子 —— 基于GoAhead系列网络摄像头多个漏洞分析

    PDF 版本下载:抓住“新代码”的影子 —— 基于GoAhead系列网络摄像头多个漏洞分析 Author:知道创宇404实验室 Date:2017/03/19 一.漏洞背景 GoAhead作为世界上最 ...

  3. BLE 5协议栈-通用访问规范层(GAP)

    文章转载自:http://www.sunyouqun.com/2017/04/ 通用访问规范GAP(Generic Access Profile)是BLE设备内部功能对外的接口层,它规定了三个方面:G ...

  4. Winfrom TextBox 添加水印文字 + 字体颜色

    using System.Drawing; using System.Windows.Forms; namespace KK.WaterMark.Control { public partial cl ...

  5. FLUSH TABLES WITH READ LOCK 获取锁的速度

    最近有一台MySQL的从库老是报延迟,观察到:FLUSH TABLES WITH READ LOCK,阻塞了4个多小时,还有另外一条SQL语句select *,从现象上来看是select * 阻塞了f ...

  6. golang gin框架 集成swagger 自动生成文档

    goswagger github仓库 https://github.com/swaggo/swag 安装 swag cli 1.因为网络原因,先安装gopm 管理工具 go get -v -u git ...

  7. Linux下DB2指令总结

    1.显示当前实例 >> get instance The current database manager instance is: db2axing 2.列出当前实例中激活的数据库 &g ...

  8. JAVA学习第三周

    判断某个字符串是否为回文 时间2019年9月23日下午 这个题有很多种写法,其一是用String来存这个字符串,然后调用charAt函数进行字符串的遍历,从两头开始遍历是否相等 其二是用toCharA ...

  9. PHP swoole TCP服务端和客户端

    服务端 <?php $server = ,SWOOLE_PROCESS,SWOOLE_SOCK_TCP); $server->set(array( , )); $server->on ...

  10. Acwing-201-可见的点(数学, 欧拉函数)

    链接: https://www.acwing.com/problem/content/description/203/ 题意: 在一个平面直角坐标系的第一象限内,如果一个点(x,y)与原点(0,0)的 ...