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 0
s to 1
s, and all 1
s to 0
s.
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 <= 20
1 <= A[0].length <= 20
A[i][j]
is0
or1
.
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的更多相关文章
- 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 ...
- 【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 ...
- 算法与数据结构基础 - 贪心(Greedy)
贪心基础 贪心(Greedy)常用于解决最优问题,以期通过某种策略获得一系列局部最优解.从而求得整体最优解. 贪心从局部最优角度考虑,只适用于具备无后效性的问题,即某个状态以前的过程不影响以后的状态. ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- 【Leetcode周赛】从contest-91开始。(一般是10个contest写一篇文章)
Contest 91 (2018年10月24日,周三) 链接:https://leetcode.com/contest/weekly-contest-91/ 模拟比赛情况记录:第一题柠檬摊的那题6分钟 ...
随机推荐
- 使用springboot写一个简单的测试用例
使用springboot写一个简单的测试用例 目录结构 pom <?xml version="1.0" encoding="UTF-8"?> < ...
- 【DevExpress】1、SearchLookUpEdit详解
一.属性的基本介绍: 绑定数据源: lookUpEdit.Properties.ValueMember = 实际要用的字段; //相当于Editvalue lookUpEdit.Propertie ...
- krpano之语音介绍
语音介绍:在每进入一个场景时,播放一段该场景的语音介绍. 制作步骤: 1.定义全局事件.在关闭场景时执行stopsounds(),在打开新场景时执行automusic(). <events on ...
- linux中stdout,stdin,stderr意义
stdout, stdin, stderr的中文名字分别是标准输出,标准输入和标准错误. 在Linux下,当一个用户进程被创建的时候,系统会自动为该进程创建三个数据流,也就是题目中所提到的这三个.那么 ...
- Timer定时函数的用法
- UIThread
功能如下:点击Create则新创建一个窗口 一 . 资源中添加对话框,右键添加类MyDlg 双击初始对话框中的按钮,实现按钮功能:点击则创建一个对话框 CMyDialog* pDialog = new ...
- thinkphp对mysql的CURD操作
利用thinkphp(3.2.3)来操作数据库,首先要连接数据库.我们需要对某数据库写一个配置文件,thinkphp会根据该配置文件自动连接上数据库.而model文件就不用自定义,内置的即可解决问题. ...
- Linux3一些文件操作命令more,less,pr,head,tail,wc
查看文件内容命令: more和less 用cat命令可以查看文件.有时候文件太大,可以用管道符号|配合more或者less一同使用. cat <文本文件名称>|more cat < ...
- Shiro的 rememberMe 功能使用指导(为什么rememberMe设置了没作用?)
UsernamePasswordToken token = new UsernamePasswordToken(loginForm.getUsername(),loginForm.getPasswor ...
- css四可见,部分可见和重叠半透明
<html> <head> <title>javascript</title> <style type="text/css"& ...