第六十一天:力扣861题,翻转矩阵后的得分
地址:leetcode-cn.com/problems/sc…
思路:贪心算法,先对每个数组的第一项进行转化,全变成1,在按列对每个数组的后面的列进行0和1数量的比较即可。
/**
* @param {number[][]} A
* @return {number}
*/
var matrixScore = function(A) {
let res = 0;
for(let i = 0; i < A.length; i++)
{
if(A[i][0] !== 1)
{
A[i].forEach((arr,index,a) => {
a[index] = a[index]^1;
})
}
res += Math.pow(2, A[0].length - 1);
}
for(let i = 1;i < A[0].length; i++)
{
let sum = 0;
for(let j = 0; j < A.length; j++)
{
if(A[j][i] === 1)
{
sum++;
}
}
sum = Math.max(sum, A.length - sum);
res += Math.pow(2, A[0].length - i - 1)*sum;
}
return res;
};
执行用时:92 ms, 在所有 JavaScript 提交中击败了41.18%的用户
内存消耗:37.8 MB, 在所有 JavaScript 提交中击败了70.59%的用户