【leetcode】861. Score After Flipping Matrix
题目如下:

解题思路:本题需要知道一个数字规律,即pow(2,n) > sum(pow(2,0)+pow(2,1)+...+pow(2,n-1))。所以,为了获得最大值,要保证所有行的最高位是1,即需要优先进行行变换,把最高位变成1。接下来就是列变换,把0多于1的列做变换变成1多于0的列即可。
代码如下:
class Solution(object):
def matrixScore(self, A):
"""
:type A: List[List[int]]
:rtype: int
"""
if len(A) == 0 or len(A[0]) == 0:
return 0
#set the highest bit to 1.Since pow(2,N) > sum(pow(2,0) + pow(2,1) + ...+pow(2,n-1)
for i in range(len(A)):
if A[i][0] == 1:
continue
else:
for j in range(len(A[i])):
if A[i][j] == 0:
A[i][j] = 1
else:
A[i][j] = 0
row = len(A)
column = len(A[0])
for i in range(column):
count1 = 0
count0 = 0
for j in range(row):
if A[j][i] == 0:
count0 += 1
else:
count1 += 1
if count0 > count1:
#convert
for j in range(row):
if A[j][i] == 0:
A[j][i] = 1
else:
A[j][i] = 0
res = 0
for i in A:
v = ''.join([str(j) for j in i])
res += int(v,2)
return res
【leetcode】861. Score After Flipping Matrix的更多相关文章
- 【LeetCode】861. Score After Flipping Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.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 ...
- 【LeetCode】74. Search a 2D Matrix
Difficulty:medium More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...
- 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】856. Score of Parentheses 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 栈 递归 计数 日期 题目地址:https://le ...
- 【LeetCode】240. Search a 2D Matrix II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】74. Search a 2D Matrix 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 左下或者右上开始查找 顺序查找 库函数 日期 题目地 ...
- 【LeetCode】240. Search a 2D Matrix II
Search a 2D Matrix II Write an efficient algorithm that searches for a value in an m x n matrix. Thi ...
- 【LEETCODE】48、867. Transpose Matrix
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
随机推荐
- node.js运行配置(vs code非控制台输出)
node.js运行配置(vs code非控制台输出) node 配置 简化 vs code 是非常强大的编译器,皆因它有有各种各样好用的插件. 在没有安装code runner插件之前,想要执行n ...
- qbzt day5 下午
农场主John新买了一块长方形的新牧场,这块牧场被划分成M行N列(1 ≤ M ≤ 12; 1 ≤ N ≤ 12),每一格都是一块正方形的土地.John打算在牧场上的某几格里种上美味的草,供他的奶牛们享 ...
- 二十三、python中的time和datetime模块
A.time模块 1. sleep():强制等待 import timeimport datetime print("start to sleep.....")time.sle ...
- something about motorcycle and automobile
cycle: 循环, 周期, 自行车. 摩托车: motorcycle, motor cycle 轮胎 continent(al): 大陆的, (七)大洲的; 德国的大陆轮胎, 马牌轮胎; 如吉普的c ...
- Powershell 邮件发送
目录 目录 前言 Send-MailMessage NETMail 使用OutLook发送邮件 前言 最近领导想在winServer2012上搞个自动发送邮件的计划任务,下面有几种发送邮件的方式. 如 ...
- Delphi XE2 之 FireMonkey 入门(14) - 滤镜: 概览
相关单元: FMX.Filter FMX.FilterCatBlur FMX.FilterCatGeometry FMX.FilterCatTransition FMX_FilterCatColor ...
- 测开之路一百零七:bootstrap排版
引入bootstrap和jquery 标题 对齐 正文强调 引言 <!DOCTYPE html><html lang="en"><head> & ...
- 类Calendar
/* * Calendar类概述及其方法 * * Calendar类概述 * Calendar类是一个抽象类,它为特定瞬间与一组诸如YEAR.MONTH.DAY_OF_MONTH.HOUR等 * 日历 ...
- python每日一练:0001题
第 0001 题:做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? import o ...
- airtestUI简单操作
touch 判断坐标位置 如touch((500, 600), duration=1) swipe 滑动位置 wait 等待画面出现 exists 判断画面中是否存在某个图片 test 调用输入法,输 ...