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. python中open与with open的区别

    读写文件是最常见的IO操作.Python内置了读写文件的函数,用法和C是兼容的.在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求操作系统打开 ...

  2. DevExpress Blazor组件全新来袭!增强Data Grid、TreeView API

    点击获取DevExpress v19.1.7最新完整版试用下载 DevExpress UI for Blazor即将在最新的v19.1.8中可用,此次更新发布包括DevExpress Blazor组件 ...

  3. Java 中 && 和 & 的区别

    在java中&和&&都属于逻辑运算符,都是判断两边条件为真时为真,否则则为假. 在程序中: int i = 1; if ((i++ > 1) & (i++ > ...

  4. pandas聚合和分组运算之groupby

    pandas提供了一个灵活高效的groupby功能,它使你能以一种自然的方式对数据集进行切片.切块.摘要等操作.根据一个或多个键(可以是函数.数组或DataFrame列名)拆分pandas对象.计算分 ...

  5. jenkins发送jemter邮件附件格式配置

    原文:https://www.cnblogs.com/chenchen-tester/p/6930200.html build.xml <?xml version="1.0" ...

  6. 给PS添加ICO格式文件

    为什么你的ps不能直接打开favicon.ico文件呢?因为你没有安装识别ico的格式插件. 安装步骤如下: 下载格式文件:https://pan.baidu.com/s/1lE0El1VtDqD5l ...

  7. Parallels Desktop 安装centos7

    配置网卡: vi /etc/sysconfig/network-scripts/ifcfg-eth0 修改BOOTPROTO=dhcp,ONBOOT=yes. 保存后重启网卡 service netw ...

  8. [spring cloud] [error] java.lang.IllegalStateException: Only one connection receive subscriber allowed.

    前言 最近在开发api-gateway的时候遇到了一个问题,网上能够找到的解决方案也很少,之后由公司的大佬解决了这个问题.写下这篇文章记录一下解决方案.希望可以帮助到更多的人. 环境 java版本:8 ...

  9. 微信小程序_(组件)swiper轮播图

    微信小程序swiper轮播图组件官方文档 传送门 Learn: swiper组件 一.swiper组件 indicator-dots:是否显示面板指示点[默认值false] autoplay:是否自动 ...

  10. POJ2942Knights of the round table

    这题第一次做的人一般是颓题解的. 首先我们转化一下问题,既然厌恶的人不能一起出席,是一种不传递关系,我们构建补图,这样补图的边表示两个骑士可以同时出席. 此时,由于只能有奇数个人参加,则我们要找出奇环 ...