作者: 负雪明烛
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:

  1. Input: [[0,0,1,1],[1,0,1,0],[1,1,0,0]]
  2. Output: 39
  3. Explanation:
  4. Toggled to [[1,1,1,1],[1,0,0,1],[1,1,1,1]].
  5. 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出现在哪一行对结果一样的。

如果还不明白,看我的分析:

初始状态:

  1. [0,0,1,1]
  2. [1,0,1,0]
  3. [1,1,0,0]

首列置为 1:

  1. [1,1,0,0]
  2. [1,0,1,0]
  3. [1,1,0,0]

每一列 1 的个数大于 0 的个数:

  1. [1,1,1,1]
  2. [1,0,0,1]
  3. [1,1,1,1]

计算结果:15 + 9 + 15 = 39.

其实,最后的计算结果完全可以这么算:

  1. = 2^3 * 3(第一列有31 + 2^2 * 2(第二列有21
  2. + 2^1 * 2(第三列有21 + 2^0 * 3(第四列有31
  3. = 8*3 + 4*2 + 2*2 + 1*3
  4. = 24 + 8 + 4 + 3
  5. = 39

即,我们只关心这一列出现的1的个数,不用关心1出现的位置。

代码如下:

  1. class Solution(object):
  2. def matrixScore(self, A):
  3. """
  4. :type A: List[List[int]]
  5. :rtype: int
  6. """
  7. rows, cols = len(A), len(A[0])
  8. for row in range(rows):
  9. if A[row][0] == 0:
  10. A[row] = self.toggle_row(A[row])
  11. for col in range(1, cols):
  12. col_array = [A[row][col] for row in range(rows)]
  13. sum_col_array = sum(col_array)
  14. if sum_col_array <= rows / 2:
  15. col_array = self.toggle_col(col_array)
  16. for row in range(rows):
  17. A[row][col] = col_array[row]
  18. bin_row = []
  19. for row in range(rows):
  20. bin_row.append(int("".join(map(str, A[row])), 2))
  21. return sum(bin_row)
  22. def toggle_row(self, row_array):
  23. return [0 if x == 1 else 1 for x in row_array]
  24. def toggle_col(self, col_array):
  25. return [0 if x == 1 else 1 for x in col_array]

二刷,第一步判断每行的第一个位置是不是0,如果是0那么就把这行全部翻转。第二步,统计每一列有多少个1,计算2的max(count1, count0)次幂。

  1. class Solution(object):
  2. def matrixScore(self, A):
  3. """
  4. :type A: List[List[int]]
  5. :rtype: int
  6. """
  7. M, N = len(A), len(A[0])
  8. for i in range(M):
  9. if A[i][0] == 0:
  10. for j in range(N):
  11. A[i][j] = 1 - A[i][j]
  12. res = 0
  13. for j in range(N):
  14. count1 = 0
  15. for i in range(M):
  16. if A[i][j]:
  17. count1 += 1
  18. res += (1 << N - 1- j) * max(count1, M - count1)
  19. return res

C++代码如下:

  1. class Solution {
  2. public:
  3. int matrixScore(vector<vector<int>> &A) {
  4. int M = A.size(), N = A[0].size();
  5. for (int i = 0; i < M; i++)
  6. if (A[i][0])
  7. for (int j = 0; j < N; j++)
  8. A[i][j] = 1 - A[i][j];
  9. int res = 0;
  10. for (int j = 0; j < N; j++) {
  11. int count1 = 0;
  12. for (int i = 0; i < M; i++) {
  13. if (A[i][j]) {
  14. count1++;
  15. }
  16. }
  17. res += (1 << (N - 1 - j)) * max(count1, M - count1);
  18. }
  19. return res;
  20. }
  21. };

日期

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. WINDOWS中使用svn

    官网:https://tortoisesvn.net/index.zh.html  (SVN安装包) 然后下载对应的64位安装包(语言包) 安装完后运行 可以存到D盘,新建一个文件夹存放 右键桌面会多 ...

  2. day11 函数

    day11 函数 一.函数基础 """ 1 什么是函数 函数是盛放代码的容器:把实现某一功能的代码放到一个函数内就制造一个工具 2 为何要用函数 没有用函数之前程序的问题 ...

  3. JS模块化,Javascript 模块化管理的历史

    模块管理这个概念其实在前几年前端度过了刀耕火种年代之后就一直被提起. 直接回想起来的就是 cmd amd commonJS 这三大模块管理的印象.接下来,我们来详细聊聊. 一.什么是模块化开发 为了让 ...

  4. [PE结构]导入表与IAT表

    导入表的结构导入表的结构 typedef struct _IMAGE_IMPORT_DESCRIPTOR { union { DWORD Characteristics; // 0 for termi ...

  5. Oracle之DBMS_LOCK包用法详解

    概述与背景 某些并发程序,在高并发的情况下,必须控制好并发请求的运行时间和次序,来保证处理数据的正确性和完整性.对于并发请求的并发控制,EBS系统可以通过Concurrent Program定义界面的 ...

  6. proguard 混淆工具的用法 (适用于初学者参考)

    一. ProGuard简介 附:proGuard官网 因为Java代码是非常容易反编码的,况且Android开发的应用程序是用Java代码写的,为了很好的保护Java源代码,我们需要对编译好后的cla ...

  7. Linux学习 - 目录表

    目录名 作用 权限 说明 /bin/ 存放系统命令的目录 所有用户 存放在/bin/下的命令单用户模式下也可以执行 /sbin/ 保存和系统环境设置相关的命令 root                 ...

  8. SQL优化原理

    SQL优化过程: 1,捕获高负荷的SQL语句-->2得到SQL语句的执行计划和统计信息--->3分析SQL语句的执行计划和统计信息--->4采取措施,对SQL语句进行调整.1找出高负 ...

  9. 【编程思想】【设计模式】【结构模式Structural】代理模式Proxy

    Python版 https://github.com/faif/python-patterns/blob/master/structural/proxy.py #!/usr/bin/env pytho ...

  10. 【Linux】【Services】【Cache】使用Sentinel搭建高可用Redis

    1. 简介 1.1. 一些基础概念请参考 http://www.cnblogs.com/demonzk/p/7453494.html 1.2. 几种常用的集群方式. -- Redis Sentinel ...