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)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...
随机推荐
- 由于MTU设置不当导致的访问超时
现象 工作中遇到一件怪事:搭建好服务器后(VPN服务器,创建了虚拟网卡),服务器和客户端之间响应正常且很稳定,客户端也能正常通过服务器访问外网.但是访问个别网站时可以打开文字,但是部分图片打不开(也不 ...
- Burp破解安装(1.7和2.0)
依赖 由于Brup是使用java语言开发的,因此我们需要本地有jdk8的环境,教程自己百度或者<a href="https://www.runoob.com/java/java-env ...
- 2、screen工具
1.背景 系统管理员经常需要SSH 或者telent 远程登录到Linux 服务器,经常运行一些需要很长时间才能完成的任务,比如系统备份.ftp 传输等等.通常情况下我们都是为每一个这样的任务开一个远 ...
- 7、Nginx基础Http原理
1Http协议概述 HTTP全称HyperText Transfer Protocol中文名为超文本传输协议 1.1.什么是超文本? 包含有超链接(Link)和各种多媒体元素标记的文本.这些超文本文件 ...
- XDCTF2014 Writeup
Web50 猜谜语类题目?FLAG在图片中有一些字符的 ASCii值,拼起来就是FLAG. Web100 隐写术.使用工具 StegSolve,把任一颜色的bit0拼起来图片的最开始部分即为 fla ...
- 菜鸟宝典之Windows Server 2012 R2上PHP、MySQL环境搭建
原文来自:https://www.jb51.net/article/59280.htm 上车准备一.准备工具服务器操作系统:Windows Server 2012PHP版本:5.6.9(根据自己需要) ...
- PHP提取富文本字符串中的纯文本,并进行进行截取
this is my first markdown article,i hope you like it /** * 提取富文本字符串的纯文本,并进行截取; * @param $string 需要进行 ...
- python对ip地址排序、对列表进行去重
一:使用python对ip地址排序所用代码示例一: import socket iplist = ['10.5.11.1','192.168.1.33','10.5.2.4','10.5.1.3',' ...
- Selenium(3)
练习1:Ecshop 录制登录后退出业务 打开系统 存储页面的标题 a.点击"登录"按钮 b.输入用户名:testing 存储输入的用户名 c.输入密码:123456 d.点击&q ...
- Layui 监听 复选框 提交表单
表单数据这一块 layui 做的是真的不好,无论是在渲染还是在交互方面,每次都要自己来重新实现代码 #贴上代码 <!DOCTYPE html> <head> <meta ...