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.

Approach #1: C++.

class Solution {
public:
int matrixScore(vector<vector<int>>& 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.

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

  1. LC 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. 算法与数据结构基础 - 贪心(Greedy)

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

  7. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  8. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  9. 【Leetcode周赛】从contest-91开始。(一般是10个contest写一篇文章)

    Contest 91 (2018年10月24日,周三) 链接:https://leetcode.com/contest/weekly-contest-91/ 模拟比赛情况记录:第一题柠檬摊的那题6分钟 ...

随机推荐

  1. 给安卓端调用的apk、图片下载接口

    package com.js.ai.modules.pointwall.action; import java.io.File; import java.io.FileInputStream; imp ...

  2. Py修行路 python基础 (十七)property 静态方法 类方法

    一.property 属性 特性 (装饰器是可调用对象,被装饰对象也是可调用对象)   1.在类内函数属性上添加一个@property,就会对函数属性进行伪装. import math class C ...

  3. STM32与PC机串口通讯

    有时要将板子的信息输出到电脑上来调试之类的,或者把传感器收集到的数据显示到电脑. 当然了,这只是最基本的串口通信,简单的说,是有一根USB线连着的. mbed上并没有能显示printf的功能.需要自己 ...

  4. MongoDB在Windows下的环境配置和使用

    总是觉得配置环境是一个超级麻烦的事情啊,而且网上说的又比较乱,配置完后又没有说怎么开始运行,在哪输入增删改查语句,像突然断层一样.所以就在这里详细说说. 一:下载安装 1.去官网的下载页面 2.下载完 ...

  5. Reading RxJava Marble Diagrams

    ------>表示一个Observable(承时间推移,由左入右,左边item先发射) ------>上面的图形,表示这个Observable发射的item ------>上的的|( ...

  6. 把List<string>转为DataTable

    //把List<string>转为DataTable List<string> myList = new List<string>(); DataTable dt2 ...

  7. Shiro的 rememberMe 功能使用指导(为什么rememberMe设置了没作用?)

    UsernamePasswordToken token = new UsernamePasswordToken(loginForm.getUsername(),loginForm.getPasswor ...

  8. 目录、目录项、文件名、inode、软硬链接的关系

    对于Unix系列的操作系统,大多都有v节点.但是对于linux来说,只有通用的i节点,却没有v节点. 下面来探讨一下,linux下的i节点. linux中,文件查找不是通过文件名称来查找的.实际上是通 ...

  9. 为什么rand和srand总是同时出现?

    如果没有srand,那么rand在我电脑上运行每次返回的随机数是一样的.如果如果先调用srand,而且srand的参数不一样,那么最后产生的随机数就会不一样?那怎么然srand的参数是不一样的呢? 是 ...

  10. Linux扩展根目录

    一.简介 使用linux系统的过程中,有时发现系统根目录(/)的空间不足,导致系统运行很慢,针对该现象,本文详细介绍根目录(/)的空间扩展方法.   二.操作步骤 1)查看根目录大小 df 2)查找系 ...