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 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 <= A.length <= 201 <= A[0].length <= 20A[i][j]is0or1.
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的更多相关文章
- 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 ...
- 【LeetCode】861. Score After Flipping Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】861. Score After Flipping Matrix
题目如下: 解题思路:本题需要知道一个数字规律,即pow(2,n) > sum(pow(2,0)+pow(2,1)+...+pow(2,n-1)).所以,为了获得最大值,要保证所有行的最高位是1 ...
- [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 ...
- [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 ...
- LC 856. Score of Parentheses
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
- [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 ...
- [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 ...
- 算法与数据结构基础 - 贪心(Greedy)
贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...
随机推荐
- JavaScript事件的基本学习
- 第八章· MySQL日志管理
一.MySQL日志简介  二.错误日志 1.作用: 记录mysql数据库的一般状态信息及报错信息,是我们对于数据库常规报错处理的常用日志. 2.默认位置: $MYSQL_HOME/data/ 3.开 ...
- 用qpython3写一个发送短信的程序
用qpython3写一个最简单的发送短信的程序 用qpython3写一个最简单的发送短信的程序到目前为止并没有多少手机应用是用python开发的,不过qpython可以作为一个不错的玩具推荐给大家来玩 ...
- mtd介绍
转:http://blog.csdn.net/lwj103862095/article/details/21545791 MTD,Memory Technology Device即内存技术设备 字符设 ...
- mysql数据库:分表、多表关联、外键约束、级联操作
一.分表.外键.级联.多对一 二.多对多 三.一对一 一.分表.外键.级联.多对一 将部门数据与员工数据放到同一张表中会造成 数据重复 结构混乱 扩展维护性差 需要分表 create table de ...
- 【input】标签去除默认样式
input{-webkit-appearance: none; -moz-appearance: none; -o-appearance: none; appearance: none;}
- sql 183. 从不订购的客户
SQL架构 某网站包含两个表,Customers 表和 Orders 表.编写一个 SQL 查询,找出所有从不订购任何东西的客户. Customers 表: +----+-------+ | Id | ...
- python和ruby:一些需要注意的小区别。
python和ruby的一些区别 基础区别 运算符号/和// ruby只有/符号.它根据操作的数字类型返回对应的结果.如果数字的类型是int,则返回整除结构,如果是float,则返回float类型的计 ...
- 最简单之在线安装mysql
1,下载Repo wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm 2,安装repo .n ...
- 返回的json数据中有属性为null的情况,报错 "message" : "Could not write JSON: Object is null