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. 获取iframe子页面内容高度给iframe动态设置高度

    <!DOCTYPE html><html> <head> <meta charset="UTF-8" /> <meta nam ...

  2. django_celery_results安装的坑

    前言  在Celery4.0之前的版本中,有一个专门供Django使用的Celery版本django-celery.但现在Celery已经统一为一个版本,所以直接安装原生的Celery即可.这里就暂时 ...

  3. Spring-AOP-学习笔记(2)-AspectJ

    1.启用@AspectJ,需要下载aspectjweaver.jar <!-- 默认启用动态代理 --><aop:aspectj-autoproxy/> <!-- 注解启 ...

  4. netty-4.客户端与服务端心跳

    (原) 第四篇,客户端与服务端心跳 心跳事件有三种,读空闲,写空闲,读写空闲,定义在了IdleState枚举类中,分别为READER_IDLE,WRITER_IDLE,ALL_IDLE 服务端: ma ...

  5. Dom修改元素样式

    提纲:我们可以通过js来修改元素的大小,颜色,位置等样式 1.element.style                         行内样式的操作 2.element.className    ...

  6. 【技巧 二进制分组】bzoj4398: 福慧双修&&2407: 探险

    二进制分组也可以说是一种比较优美的拆贡献方式吧? Description 菩萨为行,福慧双修,智人得果,不忘其本.——唐朠立<大慈恩寺三藏法师传>有才而知进退,福慧双修,这才难得.——乌雅 ...

  7. UnexpectedRollbackException解决方案

    前言 最近在项目中发现了一则报错:“org.springframework.transaction.UnexpectedRollbackException: Transaction rolled ba ...

  8. Mybatis Generator-自动化生成代码步骤

    链接:https://blog.csdn.net/guo_xl/article/details/86004068 文档:http://mybatis.org/generator/configrefer ...

  9. js中window.event对象

    event代表事件的状态,例如触发event对象的元素.鼠标的位置及状态.按下的键等等. event对象只在事件发生的过程中才有效. event的某些属性只对特定的事件有意义.比如,fromEleme ...

  10. MessagePack Jackson 数据大小

    我们在使用 MessagePack 对 List 对象数据进行序列化的时候,发现序列化以后的二进制数组数据偏大的情况. 请注意,不是所有的 List 对象都会出现这种情况,这个根据你 List 对象中 ...