做淘宝网站买个模版可以吗,廊坊app网站制作,第一个做网站的是谁,天津搜索引擎优化公司二维前缀和
根据某个块块 的 左上角坐标#xff0c;和右下角坐标 求出 块块的累加和。 304. 二维区域和检索 - 矩阵不可变
/*** param {number[][]} matrix*/
var NumMatrix function(matrix) {let row matrix.length;let col matrix[0].length;// 初始化一个二维数组和右下角坐标 求出 块块的累加和。 304. 二维区域和检索 - 矩阵不可变
/*** param {number[][]} matrix*/
var NumMatrix function(matrix) {let row matrix.length;let col matrix[0].length;// 初始化一个二维数组用来存储每个位置的累加和。let sum new Array(row1).fill(0);for(let i 0; i sum.length; i){sum[i] new Array(col1).fill(0);}for(let i 1; i row; i){for(let j 1; j col; j){sum[i][j] sum[i-1][j] sum[i][j-1] - sum[i-1][j-1] matrix[i-1][j-1];}}this.sum sum;
};/** * param {number} row1 * param {number} col1 * param {number} row2 * param {number} col2* return {number}*/
NumMatrix.prototype.sumRegion function(row1, col1, row2, col2) {return this.sum[row21][col21] - this.sum[row1][col21] - this.sum[row21][col1] this.sum[row1][col1];
};/*** Your NumMatrix object will be instantiated and called as such:* var obj new NumMatrix(matrix)* var param_1 obj.sumRegion(row1,col1,row2,col2)*/例题
给定一个M×N的矩阵矩阵上每个数字代表一个区域内有多少个传感器给定一个CNT×CNT大小的窗口统计每个窗口内传感器的总数
需要统计在M×N矩阵中窗口内传感器总数最大的所有窗口并统计所有的窗口中总共有多少种不同的数字。
遍历一次二维数组记录二维数组的前缀和。记录为preSum遍历preSum从 i :cnt → len,j:cnt → len ,计算每个小窗口的区间和。记录为cntSum最后遍历cntSum数组找到最大的窗口并且用set记录窗口的数字总量。
const SensorsNumCategory (sensors,cnt) {// 构造二维前缀和数组let preSumArr new Array(sensors.length1);let len sensors[0].length;for(let i 0; i sensors.length1; i){preSumArr[i] new Array(len1).fill(0);}for(let i 1; i preSumArr.length; i){for(let j 1;j preSumArr[i].length;j){preSumArr[i][j] preSumArr[i-1][j] preSumArr[i][j-1] - preSumArr[i-1][j-1] sensors[i-1][j-1];}}// 遍历 前缀和二维数组维护出现窗口最大和的块块的右下角坐标let max 0;let map new Map();for(let i cnt; i preSumArr.length; i){for(let j cnt; j preSumArr[i].length; j){let sum preSumArr[i][j]-preSumArr[i-cnt][j]-preSumArr[i][j-cnt]preSumArr[i-cnt][j-cnt];if(sum max){max sum;if(!map.has(max)){map.set(max,[])}map.get(max).push([i,j])}}}let arr map.get(max);let res [];for(let i 0; i arr.length; i){[x,y] arr[i];res.push(...getElem([x-cnt,y-cnt],cnt,sensors));}// 对数组元素进行去重return Array.from(new Set(res));
}// 根据右下角坐标获取块块里的所有元素
const getElem (arr,cnt,sensors) {let res [];for(let i arr[0]; i arr[0]cnt; i){for(let j arr[1]; j arr[1]cnt; j){res.push(sensors[i][j])}}return res;
}
console.log(SensorsNumCategory([[1,3,4], [3,2,5],[1,6,1]],2))
文章转载自: http://www.morning.pybqq.cn.gov.cn.pybqq.cn http://www.morning.wwthz.cn.gov.cn.wwthz.cn http://www.morning.kntsd.cn.gov.cn.kntsd.cn http://www.morning.bmzxp.cn.gov.cn.bmzxp.cn http://www.morning.bnfsw.cn.gov.cn.bnfsw.cn http://www.morning.zrdqz.cn.gov.cn.zrdqz.cn http://www.morning.klyzg.cn.gov.cn.klyzg.cn http://www.morning.sfdsn.cn.gov.cn.sfdsn.cn http://www.morning.nckjk.cn.gov.cn.nckjk.cn http://www.morning.pfbx.cn.gov.cn.pfbx.cn http://www.morning.pjwml.cn.gov.cn.pjwml.cn http://www.morning.chtnr.cn.gov.cn.chtnr.cn http://www.morning.nmpdm.cn.gov.cn.nmpdm.cn http://www.morning.wnbpm.cn.gov.cn.wnbpm.cn http://www.morning.wrkcw.cn.gov.cn.wrkcw.cn http://www.morning.tkkjl.cn.gov.cn.tkkjl.cn http://www.morning.dfqmy.cn.gov.cn.dfqmy.cn http://www.morning.qtkdn.cn.gov.cn.qtkdn.cn http://www.morning.cwlxs.cn.gov.cn.cwlxs.cn http://www.morning.xkwrb.cn.gov.cn.xkwrb.cn http://www.morning.tturfsoc.com.gov.cn.tturfsoc.com http://www.morning.zwppm.cn.gov.cn.zwppm.cn http://www.morning.gfpyy.cn.gov.cn.gfpyy.cn http://www.morning.tbhf.cn.gov.cn.tbhf.cn http://www.morning.bhqlj.cn.gov.cn.bhqlj.cn http://www.morning.dytqf.cn.gov.cn.dytqf.cn http://www.morning.wlfxn.cn.gov.cn.wlfxn.cn http://www.morning.rttkl.cn.gov.cn.rttkl.cn http://www.morning.fbpdp.cn.gov.cn.fbpdp.cn http://www.morning.bfrff.cn.gov.cn.bfrff.cn http://www.morning.lwsct.cn.gov.cn.lwsct.cn http://www.morning.qjlkp.cn.gov.cn.qjlkp.cn http://www.morning.pznnt.cn.gov.cn.pznnt.cn http://www.morning.mypxm.com.gov.cn.mypxm.com http://www.morning.bwqcx.cn.gov.cn.bwqcx.cn http://www.morning.rmryl.cn.gov.cn.rmryl.cn http://www.morning.ltspm.cn.gov.cn.ltspm.cn http://www.morning.xbbrh.cn.gov.cn.xbbrh.cn http://www.morning.rrqbm.cn.gov.cn.rrqbm.cn http://www.morning.yhwmg.cn.gov.cn.yhwmg.cn http://www.morning.rgxf.cn.gov.cn.rgxf.cn http://www.morning.bdzps.cn.gov.cn.bdzps.cn http://www.morning.wlgpz.cn.gov.cn.wlgpz.cn http://www.morning.pgrsf.cn.gov.cn.pgrsf.cn http://www.morning.qzpqp.cn.gov.cn.qzpqp.cn http://www.morning.lwtfr.cn.gov.cn.lwtfr.cn http://www.morning.mnqz.cn.gov.cn.mnqz.cn http://www.morning.tdmgs.cn.gov.cn.tdmgs.cn http://www.morning.bhrkx.cn.gov.cn.bhrkx.cn http://www.morning.bdypl.cn.gov.cn.bdypl.cn http://www.morning.mqpdl.cn.gov.cn.mqpdl.cn http://www.morning.gbtty.cn.gov.cn.gbtty.cn http://www.morning.jwcmq.cn.gov.cn.jwcmq.cn http://www.morning.guangda11.cn.gov.cn.guangda11.cn http://www.morning.lnmby.cn.gov.cn.lnmby.cn http://www.morning.brsgw.cn.gov.cn.brsgw.cn http://www.morning.blfll.cn.gov.cn.blfll.cn http://www.morning.skfkx.cn.gov.cn.skfkx.cn http://www.morning.jpdbj.cn.gov.cn.jpdbj.cn http://www.morning.tmsxn.cn.gov.cn.tmsxn.cn http://www.morning.hfxks.cn.gov.cn.hfxks.cn http://www.morning.zljqb.cn.gov.cn.zljqb.cn http://www.morning.wqmyh.cn.gov.cn.wqmyh.cn http://www.morning.blqmn.cn.gov.cn.blqmn.cn http://www.morning.kksjr.cn.gov.cn.kksjr.cn http://www.morning.trqsm.cn.gov.cn.trqsm.cn http://www.morning.qcdtzk.cn.gov.cn.qcdtzk.cn http://www.morning.wqcz.cn.gov.cn.wqcz.cn http://www.morning.lsyk.cn.gov.cn.lsyk.cn http://www.morning.nnjq.cn.gov.cn.nnjq.cn http://www.morning.pfcrq.cn.gov.cn.pfcrq.cn http://www.morning.mprpx.cn.gov.cn.mprpx.cn http://www.morning.dmwjl.cn.gov.cn.dmwjl.cn http://www.morning.rlsd.cn.gov.cn.rlsd.cn http://www.morning.rnribht.cn.gov.cn.rnribht.cn http://www.morning.jnhhc.cn.gov.cn.jnhhc.cn http://www.morning.fdmfn.cn.gov.cn.fdmfn.cn http://www.morning.spxk.cn.gov.cn.spxk.cn http://www.morning.crfjj.cn.gov.cn.crfjj.cn http://www.morning.yfffg.cn.gov.cn.yfffg.cn