【LeetCode】861. Score After Flipping Matrix 解题报告(Python & C++)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/score-after-flipping-matrix/description/
题目描述
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 <= 20
1 <= A[0].length <= 20
A[i][j] is 0 or 1.
题目大意
题目中给了一个数组A,这个数组中只包含0,1.现在需要整行或者整列的进行toggle操作。目标是进行一波toggle操作之后,把A中的每行数字转化成二进制数,是最终得到的二进制数的和最大。
解题方法
题目很烧脑,使用什么样的操作规则才能使得得到的最终数组二进制和最大。
首先,第一列肯定要全部变成1,很显然位数恒定时,1开头的二进制数要比任何0开头的都要大。
其次,我们采取贪心算法,让每一列中1的个数尽可能多。怎么理解这句话呢?
这和我们最终的目标有关,因为我们要求每行数字都转成二进制数之后的和
,所以,在同一列中,1出现在哪一行对结果一样的。
如果还不明白,看我的分析:
初始状态:
[0,0,1,1]
[1,0,1,0]
[1,1,0,0]
首列置为 1:
[1,1,0,0]
[1,0,1,0]
[1,1,0,0]
每一列 1 的个数大于 0 的个数:
[1,1,1,1]
[1,0,0,1]
[1,1,1,1]
计算结果:15 + 9 + 15 = 39.
其实,最后的计算结果完全可以这么算:
= 2^3 * 3(第一列有3个1) + 2^2 * 2(第二列有2个1)
+ 2^1 * 2(第三列有2个1) + 2^0 * 3(第四列有3个1)
= 8*3 + 4*2 + 2*2 + 1*3
= 24 + 8 + 4 + 3
= 39
即,我们只关心这一列出现的1的个数,不用关心1出现的位置。
代码如下:
class Solution(object):
def matrixScore(self, A):
"""
:type A: List[List[int]]
:rtype: int
"""
rows, cols = len(A), len(A[0])
for row in range(rows):
if A[row][0] == 0:
A[row] = self.toggle_row(A[row])
for col in range(1, cols):
col_array = [A[row][col] for row in range(rows)]
sum_col_array = sum(col_array)
if sum_col_array <= rows / 2:
col_array = self.toggle_col(col_array)
for row in range(rows):
A[row][col] = col_array[row]
bin_row = []
for row in range(rows):
bin_row.append(int("".join(map(str, A[row])), 2))
return sum(bin_row)
def toggle_row(self, row_array):
return [0 if x == 1 else 1 for x in row_array]
def toggle_col(self, col_array):
return [0 if x == 1 else 1 for x in col_array]
二刷,第一步判断每行的第一个位置是不是0,如果是0那么就把这行全部翻转。第二步,统计每一列有多少个1,计算2的max(count1, count0)次幂。
class Solution(object):
def matrixScore(self, A):
"""
:type A: List[List[int]]
:rtype: int
"""
M, N = len(A), len(A[0])
for i in range(M):
if A[i][0] == 0:
for j in range(N):
A[i][j] = 1 - A[i][j]
res = 0
for j in range(N):
count1 = 0
for i in range(M):
if A[i][j]:
count1 += 1
res += (1 << N - 1- j) * max(count1, M - count1)
return res
C++代码如下:
class Solution {
public:
int matrixScore(vector<vector<int>> &A) {
int M = A.size(), N = A[0].size();
for (int i = 0; i < M; i++)
if (A[i][0])
for (int j = 0; j < N; j++)
A[i][j] = 1 - A[i][j];
int res = 0;
for (int j = 0; j < N; j++) {
int count1 = 0;
for (int i = 0; i < M; i++) {
if (A[i][j]) {
count1++;
}
}
res += (1 << (N - 1 - j)) * max(count1, M - count1);
}
return res;
}
};
日期
2018 年 7 月 19 日 —— 今天主要在忙实验室项目,刷题有点晚了
2018 年 12 月 2 日 —— 又到了周日
【LeetCode】861. Score After Flipping Matrix 解题报告(Python & C++)的更多相关文章
- 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 ...
- 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
题目如下: 解题思路:本题需要知道一个数字规律,即pow(2,n) > sum(pow(2,0)+pow(2,1)+...+pow(2,n-1)).所以,为了获得最大值,要保证所有行的最高位是1 ...
- 【LeetCode】566. Reshape the Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 变长数组 求余法 维护行列 相似题目 参考资料 日期 ...
- 【LeetCode】519. Random Flip Matrix 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/random-fl ...
- 【LeetCode】822. Card Flipping Game 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/card-flip ...
- 【LeetCode】756. Pyramid Transition Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
随机推荐
- WINDOWS中使用svn
官网:https://tortoisesvn.net/index.zh.html (SVN安装包) 然后下载对应的64位安装包(语言包) 安装完后运行 可以存到D盘,新建一个文件夹存放 右键桌面会多 ...
- day11 函数
day11 函数 一.函数基础 """ 1 什么是函数 函数是盛放代码的容器:把实现某一功能的代码放到一个函数内就制造一个工具 2 为何要用函数 没有用函数之前程序的问题 ...
- JS模块化,Javascript 模块化管理的历史
模块管理这个概念其实在前几年前端度过了刀耕火种年代之后就一直被提起. 直接回想起来的就是 cmd amd commonJS 这三大模块管理的印象.接下来,我们来详细聊聊. 一.什么是模块化开发 为了让 ...
- [PE结构]导入表与IAT表
导入表的结构导入表的结构 typedef struct _IMAGE_IMPORT_DESCRIPTOR { union { DWORD Characteristics; // 0 for termi ...
- Oracle之DBMS_LOCK包用法详解
概述与背景 某些并发程序,在高并发的情况下,必须控制好并发请求的运行时间和次序,来保证处理数据的正确性和完整性.对于并发请求的并发控制,EBS系统可以通过Concurrent Program定义界面的 ...
- proguard 混淆工具的用法 (适用于初学者参考)
一. ProGuard简介 附:proGuard官网 因为Java代码是非常容易反编码的,况且Android开发的应用程序是用Java代码写的,为了很好的保护Java源代码,我们需要对编译好后的cla ...
- Linux学习 - 目录表
目录名 作用 权限 说明 /bin/ 存放系统命令的目录 所有用户 存放在/bin/下的命令单用户模式下也可以执行 /sbin/ 保存和系统环境设置相关的命令 root ...
- SQL优化原理
SQL优化过程: 1,捕获高负荷的SQL语句-->2得到SQL语句的执行计划和统计信息--->3分析SQL语句的执行计划和统计信息--->4采取措施,对SQL语句进行调整.1找出高负 ...
- 【编程思想】【设计模式】【结构模式Structural】代理模式Proxy
Python版 https://github.com/faif/python-patterns/blob/master/structural/proxy.py #!/usr/bin/env pytho ...
- 【Linux】【Services】【Cache】使用Sentinel搭建高可用Redis
1. 简介 1.1. 一些基础概念请参考 http://www.cnblogs.com/demonzk/p/7453494.html 1.2. 几种常用的集群方式. -- Redis Sentinel ...