当前位置:网站首页>力扣练习——27 翻转矩阵后的得分
力扣练习——27 翻转矩阵后的得分
2022-07-22 07:48:00 【qq_43403657】
27 翻转矩阵后的得分
1.问题描述
有一个二维矩阵 A ,其中每个元素的值为 0 或 1 。
翻转是指选择任一行或列,并转换该行或列中的每一个值:将所有 0 都更改为 1,将所有 1 都更改为 0。
在做出任意次数的翻转后,将该矩阵的每一行都按照二进制数来解释,矩阵的得分就是这些数字的总和。
返回尽可能高的分数。
示例:
输入:[[0,0,1,1],[1,0,1,0],[1,1,0,0]]
输出:39
解释:
转换为 [[1,1,1,1],[1,0,0,1],[1,1,1,1]]
0b1111 + 0b1001 + 0b1111 = 15 + 9 + 15 = 39
2.输入说明
首先输入矩阵的行数m、列数n,
然后输入m行,每行n个数字,每个数字都是0或1。
1 <= m <= 20
1 <= n <= 20
3.输出说明
输出一个整数
4.范例
输入
3 4
0 0 1 1
1 0 1 0
1 1 0 0
输出
39
5.代码
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<unordered_map>
using namespace std;
int traverse_score(vector<vector<int>>&nums)
{
//思想:给定一个翻转方案,则它们之间任意交换顺序后,得到的结果保持不变。因此,我们总可以先考虑所有的行翻转,再考虑所有的列翻转。
//行翻转:为了得到最高的分数,矩阵的每一行的最左边的数都必须为 1,因此可以将每一行的最左边的数为0的都翻转为1
//列翻转:当将每一行的最左边的数都变为 1 之后,就只能进行列翻转了 ;扫描第一列后的所有列,要让每个列中 1的数目尽可能多
//扫描除了最左边的列以外的每一列,如果该列 0 的数目多于 1 的数目,就翻转该列,其他的列则保持不变。
//计算每一列对元素的贡献值 , 共m行 n列 ,第一列全为1时 ,贡献值为m*2^(n-1)
//第j列 ,元素1的个数为k , 贡献值为 k*2^(n-j-1)
int m = nums.size();//行数
int n = nums[0].size();//列数
//行翻转得到的贡献值
//int res = m * (1<<(n - 1)); //int res=m*(1<<(n-1)); 1<<代表二进制数1左移一位变成10(2)
int res = m * pow(2, n - 1);
//int res=m*(2^(n-1)); 这个输出来是错误的 ,写成 int res=m*pow(2,n-1);
for (int j = 1; j < n; j++)//对第一列后的其他列进行遍历
{
int s = 0;//计算第一列后的每列的贡献值 ,如果该列 0 的数目多于 1 的数目,就翻转该列,其他的列则保持不变。
for (int i = 0; i < m; i++)//先确保每一行的第一列元素都为1
{
if (nums[i][0] == 1)//第i行第一列元素为1
{
s += nums[i][j];//计算该行的贡献值 【1的个数】
}
else
s += (1 - nums[i][j]);//该行进行反转,所以结果为1-nums[i][j]
}
int k = max(s, m - s);//用s统计1的个数,m-s统计0的个数 ,理解下这里为什么是取max? 当0的个数比1的个数时,翻转 ;否则不变,因此取max
res += k * (1<<(n - j - 1));
}
return res;
}
int main()
{
int n,m,tmp;
cin >> n>>m;//n代表行数, m代表列数
vector<vector<int>>nums;
vector<int> temp;
for (int i = 0; i < n; i++)
{
temp.clear();
for (int j = 0; j < m; j++)
{
cin >> tmp;
temp.push_back(tmp);
}
nums.push_back(temp);
}
int res = traverse_score(nums);
cout << res<<endl;
return 0;
}
边栏推荐
- [MCU simulation project] external interrupt 0 controls the light-emitting diode on and off
- Instruction arrangement problem
- 2022-07-21:给定一个字符串str,和一个正数k, 你可以随意的划分str成多个子串, 目的是找到在某一种划分方案中,有尽可能多的回文子串,长度>=k,并且没有重合。 返回有几个回文子串。 来
- MySQL 增删改查(進階)
- Excel导入导出Controller
- Oracle怎么设置创建时不去检查编译错误?
- impdp content=data_only 当存在记录时可否选择跳过还是覆盖选项?
- [cloud native] docker deployment database persistence
- (11) 51 Single Chip Microcomputer -- realize the storage of stopwatch data with AT24C02 (attached with achievement display)
- 【单片机仿真项目】 外部中断0控制发光二极管亮灭
猜你喜欢
深度学习(二)一文带你了解神经网络,激活函数
数据高效治理如何开展,指标管理与数据溯源来帮忙!
[database] addition, deletion, modification and query of MySQL table (Advanced)
MySQL join and index
Abnormal understanding and learning
Take CRM system as an example to explain data analysis (importance introduction and analysis method)
MySQL 增删改查(進階)
MySQL 增删改查(进阶)
[harmony OS] [ark UI] [demo] loading animation
[Digital IC] understand Axi protocol in simple terms
随机推荐
MySQL series 3: Function & Index & View & error code number meaning
-bash-4.2$
web3分享
[micro Service ~ remote call] integrate resttemplate, webclient, feign
【数字IC】深入浅出理解AXI协议
Using dichotomy to find the elements of an array
【案例分享】配置IS-IS的路由渗透功能
C language branch structure and loop structure (1)
网络之数据链路层(PPP协议)
(11) 51 Single Chip Microcomputer -- realize the storage of stopwatch data with AT24C02 (attached with achievement display)
Bigder:38/100 一个误操作的问题解决了
QWidget添加右键菜单
Bigder:35/100 开发同事说,我自己测了。可是上线后出问题了。
Evolution of multi activity in different places
Stack / heap / queue question brushing (Part 1)
C#静态类和静态类成员
异常的理解学习
Fibonacci series of Niuke network
2022-07-21: given a string STR and a positive number k, you can divide STR into multiple substrings at will. The purpose is to find that in a certain division scheme, there are as many palindrome subs
[HMS core] [FAQ] [account kit] typical problem set 2