作者: 负雪明烛
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,很显然位数恒定时,1开头的二进制数要比任何0开头的都要大。

  2. 其次,我们采取贪心算法,让每一列中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++)的更多相关文章

  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. 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 ...

  3. 【leetcode】861. Score After Flipping Matrix

    题目如下: 解题思路:本题需要知道一个数字规律,即pow(2,n) > sum(pow(2,0)+pow(2,1)+...+pow(2,n-1)).所以,为了获得最大值,要保证所有行的最高位是1 ...

  4. 【LeetCode】566. Reshape the Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 变长数组 求余法 维护行列 相似题目 参考资料 日期 ...

  5. 【LeetCode】519. Random Flip Matrix 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/random-fl ...

  6. 【LeetCode】822. Card Flipping Game 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/card-flip ...

  7. 【LeetCode】756. Pyramid Transition Matrix 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  8. 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ...

  9. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

随机推荐

  1. 听老外吐槽框架设计,Why I Hate Frameworks?

    原创:微信公众号 码农参上,欢迎分享,转载请保留出处. Hello,小伙伴们,今天不聊技术,分享点有意思的东西.前段时间,表弟给我发过来一篇老外写的文章,以略带讽刺的对话方式调侃了自己对框架的看法,我 ...

  2. 从 ClickHouse 到 ByteHouse:实时数据分析场景下的优化实践

    本文来自火山引擎公众号,原文发布于2021-09-06. 近日,字节跳动旗下的企业级技术服务平台火山引擎正式对外发布「ByteHouse」,作为 ClickHouse 企业版,解决开源技术上手难 &a ...

  3. vim文本编辑器的基本使用

    vim文本编辑器的基本使用 1. vi和vim的区别和联系 可以说vim是vi的增强版,在使用vim编辑文本时,可以根据字体颜色来判断编写程序的正确性. 2. vim文本编辑器的常用命令 1. 编辑指 ...

  4. 内存中 1k 代表什么

    1K也就是 1KB   == 1000 bytes == 1000 *8 位 通常一个地址里面有8位,就是说一个房间里面能存8个0或者1

  5. Function overloading and return type

    In C++ and Java, functions can not be overloaded if they differ only in the return type. For example ...

  6. shell脚本实现openss自建CA和证书申请

    #!/bin/bash # #******************************************************************** #Author: Ma Xue ...

  7. 小程序的事件 bindtap bindinput

    一.bindtap事件 在wxml文件里绑定: <view class='wel-list' bindtap='TZdown'> <image src="/images/w ...

  8. mybatis分页插件PageHelper源码浅析

    PageHelper 是Mybaties中的一个分页插件.其maven坐标 <!-- https://mvnrepository.com/artifact/com.github.pagehelp ...

  9. ssm动态查询向前台传json

    1.数据协议层 public User selectById(Integer id);//通过id值查询用户 2.数据层 <select id="selectById" re ...

  10. 访问Github速度很慢以及解决方法(系统通用)

    原因分析1,CDN,Content Distribute Network,可以直译成内容分发网络,CDN解决的是如何将数据快速可靠从源站传递到用户的问题.用户获取数据时,不需要直接从源站获取,通过CD ...