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

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

一个由0和1构成的矩阵,每次可以选择任意一行或列,行或列的数字0变成1,1变成0.

每一行代表一个二进制数,求能够成的这些二进制数和的最大值

由二进制转十进制原理,1的位置越靠前越好,所以第一步,把每一行的第一个数变成1.

第二步,对除了第一列外的每一列的1的个数做统计,如果翻转后能增加1的个数,就翻转该列。

 1 class Solution {
 2 public:
 3     int matrixScore(vector<vector<int>>& A) {
 4         int row = A.size();
 5         int col = A[0].size();
 6         vector<int> cnt(col, 0);
 7         for (int i = 0; i < row; ++i) {
 8             if (A[i][0] == 0) {
 9                 for (int j = 0; j < col; ++j) {
10                     A[i][j] = 1 - A[i][j];
11                     if (A[i][j] == 1)   ++cnt[j];
12                 }
13             }
14             else {
15                 for (int j = 0; j < col; ++j) {
16                     if (A[i][j] == 1)  ++cnt[j];
17                 }
18             }
19         }
20         for (int j = 1; j < col; ++j) {
21             if (cnt[j] * 2 < row) {
22                 for (int i = 0; i < row; ++i)
23                     A[i][j] = 1 - A[i][j];
24             }
25         }
26         int sum = 0;
27         int v = 0;;
28         int res = 0;
29         for (int i = 0; i < row; ++i) {
30             sum = 0;
31             v = 1;
32             for (int j = col - 1; j >= 0; --j) {
33                 sum += A[i][j] * v;
34                 v = v * 2;
35             }
36             res += sum;
37         }
38         return res;
39     }
40 };

原文地址:https://www.cnblogs.com/Zzz-y/p/9275410.html

时间: 2024-10-08 06:13:59

861. Score After Flipping Matrix的相关文章

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

【LeetCode】贪心 greedy(共38题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: [55]Jump Game (2018年11月27日,算法群) 给了一个数组nums,nums[i] = k 代表站在第 i 个位置的情况下, 我最多能往前走 k 个单

【leetcode】Weekly Contest 91

leetcode周赛,早上起来发现没网,用热点意识模糊的a了三个水题. 1.Lemonade Change 简单模拟题,收到十元用五元钱找回,收到20元时优先用一张10一张5,如果10不够用3张5,如果没有就返回flase(贪心). 1 public boolean lemonadeChange(int[] bills) { 2 int five = 0; 3 int ten = 0; 4 int twenty = 0; 5 for (int i = 0; i < bills.length; i

我的改进版2048(2)

(如果有谁想要这个软件的话,在评论中留一个邮箱吧.) 上篇讲解了我的2048的基本设计过程.每次按方向键,16个方格就随之改变,每次叠加相同数字,分数就会增加:叠加的相同数字越大,分数越大:分数会实时刷新.最后还有一个问题,就是怎么判断是否game over.其实也是比较简单,稍微想想就知道.首先判断16个方格是否已满,如不满,则肯定不会game over:否则,判断任何相邻的两个数字是否都不一样,如是,则game over! 接下来说说怎么实现的存档.读档和后退一步功能. 因为要存储数据,所以

序列比对那点事儿

序列比对那点事儿 2012-04-17 ~ ADMIN 本来这应该是一本书,那样的话的确需要花一点心思,就写成一篇短文吧. 从字符比对开始说起吧.第一个问题最简单,如何判断两个字符串是相等的. int strcmp(const char *s1, const char *s2) { int ret = 0; while (!(ret = *(unsigned char *) s1 - *(unsigned char *) s2) && *s2) ++s1, ++s2;   if (ret

Faster RCNN 运行自己的数据,刚开始正常,后来就报错: Index exceeds matrix dimensions. Error in ori_demo (line 114) boxes_cell{i} = [boxes(:, (1+(i-1)*4):(i*4)), scores(:, i)];

function script_faster_rcnn_demo() close all; clc; clear mex; clear is_valid_handle; % to clear init_key run(fullfile(fileparts(fileparts(mfilename('fullpath'))), 'startup')); %% -------------------- CONFIG -------------------- opts.caffe_version = '

832. Flipping an Image

Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image. To flip an image horizontally means that each row of the image is reversed. For example, flipping [1, 1, 0] horizontally results in [0, 1

[LeetCode] 832. Flipping an Image_Easy

Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image. To flip an image horizontally means that each row of the image is reversed.  For example, flipping [1, 1, 0] horizontally results in [0,

语义定位:Semantic Localization Via the Matrix Permanent(二)

论文假设和单目标模型 这部分想讲一下Semantic Localization Via the Matrix Permanent这篇文章的一些假设. 待求解的问题可以描述为 假设从姿态\(x\)看到的物体(路标点)集合为\(Y(x)={y_1,...,y_n}\),观测为\(Z={z_1,...,z_m}\).求后验概率\(p(Z|Y,x)\). 这里引入数据关联\(\pi\)表示从物体到测量的一个对应关系,其中即包含正确的配对,也包含错误的配对和缺失的配对. 一些假设 作者对目标检测和数据关联