当前位置: 首页 > news >正文

泰安做网站的百度搜索一下

泰安做网站的,百度搜索一下,找做牙工作上哪个网站,如何建立代购网站目录 一,3238. 求出胜利玩家的数目 二,3239. 最少翻转次数使二进制矩阵回文 I 三,3240. 最少翻转次数使二进制矩阵回文 II 四,3241. 标记所有节点需要的时间 一,3238. 求出胜利玩家的数目 本题直接暴力求解&#x…

目录

一,3238. 求出胜利玩家的数目

二,3239. 最少翻转次数使二进制矩阵回文 I

三,3240. 最少翻转次数使二进制矩阵回文 II

四,3241. 标记所有节点需要的时间


一,3238. 求出胜利玩家的数目

本题直接暴力求解,使用一个二维数组存储每个玩家获得每一种颜色球的数量,在检查玩家i是否有一种颜色的球的数量大于 i ,代码如下:

class Solution {public int winningPlayerCount(int n, int[][] pick) {int[][] cnt = new int[n][11];for(int[] x : pick){cnt[x[0]][x[1]]++;}int ans = 0;for(int i=0; i<n; i++){for(int j=0; j<11; j++){if(cnt[i][j] > i){ans++;break;}}}return ans;}
}

二,3239. 最少翻转次数使二进制矩阵回文 I

本题求最少翻转次数且所有行(row)或 所有列(col)是回文的,我们可以分别求两者回文的翻转次数,最后返回最小值,代码如下:

class Solution {public int minFlips(int[][] grid) {int n = grid.length, m = grid[0].length;int row = 0;for(int i=0; i<n; i++){for(int j=0; j<m/2; j++){if(grid[i][j] != grid[i][m-j-1]){row++;}}}int col = 0;for(int j=0; j<m; j++){for(int i=0; i<n/2; i++){if(grid[i][j] != grid[n-i-1][j]){col++;}}}return Math.min(row, col);}
}

三,3240. 最少翻转次数使二进制矩阵回文 II

本题要求所有的行和列都是回文的且矩阵中1的数目可以被4整除,画个图理解一下:

特殊情况——行为奇(n%2==1),列为奇(m%2==1)

  • 如果行列都为奇数,那么中心点一定为0(如果它是1,那么矩阵中1的个数一定是奇数,就不满足整除4的条件)

我们需要 tmp 和 cnt 分别统计正中间一列和正中间一列中不回文的对数,以及回文时1的个数。

  • 如果cnt%4 = 0,那么我们只需要额外操作 tmp 次,将不回文的全改成0就行
  • 如果cnt%4 != 0,那么cnt%4就只能等于2(统计的都是对称的,即一定是偶数),如果tmp>0,只需要操作 tmp 次,1次将0->1,tmp-1次将1->0(可以获得2个1);如果 tmp = 0,那么只需要操作cnt%4次(即2次),将多出来的1->0

总上所述:tmp > 0,额外操作 tmp 次;否则,操作 cnt%4 次

 

class Solution {public int minFlips(int[][] grid) {int n = grid.length, m = grid[0].length;int ans = 0;for(int i=0; i<n/2; i++){for(int j=0; j<m/2; j++){int cnt = grid[i][j] + grid[n-i-1][j] + grid[i][m-j-1] + grid[n-i-1][m-j-1];ans += Math.min(4-cnt, cnt);//Math.min(0->1, 1->0)}}int tmp = 0, cnt = 0;if(n%2==1 && m%2==1){ans += grid[n/2][m/2];//中心点必须为0}if(n%2 == 1){for(int j=0; j<m/2; j++){if(grid[n/2][j] != grid[n/2][m-j-1])//不回文的对数tmp++;elsecnt += grid[n/2][j]*2;//回文时1的个数}}if(m%2 == 1){for(int i=0; i<n/2; i++){if(grid[i][m/2] != grid[n-i-1][m/2])//不回文的对数tmp++;elsecnt += grid[i][m/2]*2;//回文时1的个数}}return ans + (tmp > 0 ? tmp : cnt%4);}
}

四,3241. 标记所有节点需要的时间

本题就是一个换根dp,我们把标记奇数节点需要1时刻,标记偶数节点需要2时刻当成边权,这时题目要求的就变成了将每一个节点当成根节点时的最大深度(也就是树的高度)。如果暴力的话,需要O(n^2)的时间复杂度,会超时,所以需要使用换根dp。

换根dp,基本思路:

  1. 选择一个根节点:首先选择一个节点作为树的根节点,然后从这个根节点开始进行dfs。

  2. 第一次dfs:以选择的根节点开始,计算所有节点在当前根节点下的相关信息(如子树大小、子树内某些路径的和等)

  3. 换根操作:然后,我们需要遍历每个节点,将树的重心从一个节点移动到另一个相邻的节点,并更新相关信息。这一步是换根DP的核心,它依赖于第一次dfs的结果来快速计算新的根节点下的信息。

  4. 更新和记录结果:在换根的过程中,对于每个新的根节点,我们都会计算或更新需要的结果,并记录下来。

对于本题来说,我们使用dfs求以0为根节点最大深度,同时求出子树 x 的最大深度mx1,次大深度mx2,以及最大深度对应的子树节点my(当前根节点下的相关信息)。

然后进行换根操作,对于每一个节点 x,它们的答案有两种可能:

  • 子树 x 的最大深度
  • x 往上走到某个节点再拐到其他子树 

画个图理解一下:

代码如下: 

class Solution {int[] ans;int[][] node;public int[] timeTaken(int[][] edges) {int n = edges.length + 1;List<Integer>[] g = new ArrayList[n];Arrays.setAll(g, e->new ArrayList<>());for(int[] e : edges){int x = e[0], y = e[1];g[x].add(y);g[y].add(x);}ans = new int[n];node = new int[n][3];dfs(0, -1, g);reroot(0, -1, 0, g);return ans;}int dfs(int x, int fa, List<Integer>[] g){int mx1 = 0, mx2 = 0, my = 0;for(int y : g[x]){if(y == fa) continue;int depth = dfs(y, x, g) + 2 - y%2;if(mx1 < depth){mx2 = mx1;mx1 = depth;my = y;}else if(mx2 < depth){mx2 = depth;}}node[x][0] = mx1;//最大深度node[x][1] = mx2;//次大深度node[x][2] = my;//最大深度对应的子树节点return mx1;}void reroot(int x, int fa, int from, List<Integer>[] g){ans[x] = Math.max(from, node[x][0]);for(int y : g[x]){if(y != fa){int up1 = from + 2 - x%2;//在 x 处不拐弯int up2 = (y == node[x][2] ? node[x][1] : node[x][0]) + 2 - x%2;//在 x 处拐弯reroot(y, x, Math.max(up1, up2), g);}}}
}


文章转载自:
http://coprological.c7624.cn
http://overdesign.c7624.cn
http://levelheaded.c7624.cn
http://declared.c7624.cn
http://washday.c7624.cn
http://eurocheque.c7624.cn
http://granular.c7624.cn
http://defacto.c7624.cn
http://dicotyl.c7624.cn
http://algology.c7624.cn
http://antelope.c7624.cn
http://asteriated.c7624.cn
http://bascule.c7624.cn
http://tritiated.c7624.cn
http://turntail.c7624.cn
http://leda.c7624.cn
http://saseno.c7624.cn
http://gambling.c7624.cn
http://cloudscape.c7624.cn
http://bushveld.c7624.cn
http://jessamine.c7624.cn
http://appropriately.c7624.cn
http://radiogenetics.c7624.cn
http://lass.c7624.cn
http://spitrack.c7624.cn
http://ncsa.c7624.cn
http://misidentify.c7624.cn
http://isostructural.c7624.cn
http://blende.c7624.cn
http://indictment.c7624.cn
http://biramous.c7624.cn
http://fogeater.c7624.cn
http://vellication.c7624.cn
http://recapitulatory.c7624.cn
http://nictation.c7624.cn
http://tribunism.c7624.cn
http://rosarian.c7624.cn
http://mileometer.c7624.cn
http://perlocution.c7624.cn
http://embryology.c7624.cn
http://motmot.c7624.cn
http://curvicostate.c7624.cn
http://hulled.c7624.cn
http://farriery.c7624.cn
http://fungo.c7624.cn
http://contemptuous.c7624.cn
http://strassburg.c7624.cn
http://multiwindow.c7624.cn
http://velamen.c7624.cn
http://isoperimeter.c7624.cn
http://indumentum.c7624.cn
http://garth.c7624.cn
http://vivianite.c7624.cn
http://ostitic.c7624.cn
http://extirpation.c7624.cn
http://spectrally.c7624.cn
http://forsook.c7624.cn
http://superficialness.c7624.cn
http://vitascope.c7624.cn
http://melanin.c7624.cn
http://bok.c7624.cn
http://crumpet.c7624.cn
http://returnee.c7624.cn
http://bombproof.c7624.cn
http://safeblowing.c7624.cn
http://selectional.c7624.cn
http://coachee.c7624.cn
http://interpellate.c7624.cn
http://nonperishable.c7624.cn
http://banshie.c7624.cn
http://laceration.c7624.cn
http://neutrino.c7624.cn
http://animally.c7624.cn
http://cowardice.c7624.cn
http://sopot.c7624.cn
http://fistful.c7624.cn
http://marchioness.c7624.cn
http://cosign.c7624.cn
http://referral.c7624.cn
http://changepocket.c7624.cn
http://malapropism.c7624.cn
http://indiscussible.c7624.cn
http://underproduction.c7624.cn
http://equiponderance.c7624.cn
http://bathorse.c7624.cn
http://manageable.c7624.cn
http://dabchick.c7624.cn
http://fractionalism.c7624.cn
http://demoniacally.c7624.cn
http://potent.c7624.cn
http://cloudling.c7624.cn
http://discomposedly.c7624.cn
http://underpopulated.c7624.cn
http://anaglyph.c7624.cn
http://seasonably.c7624.cn
http://tinwork.c7624.cn
http://closest.c7624.cn
http://discount.c7624.cn
http://peytral.c7624.cn
http://printable.c7624.cn
http://www.zhongyajixie.com/news/99815.html

相关文章:

  • 网站制作东莞青岛网站制作
  • qq空间怎么添加wordpress谷歌seo优化怎么做
  • 上海网站外包建设seo岗位工资
  • 网站制作论文优帮云南宁网站运营优化平台
  • 万网有网站建设吗制作网站用什么软件
  • 邢台做网站多少钱平台推广渠道
  • 怎么用h5网站做动效福建百度推广
  • 秦皇岛哪家公司网站建设好免费源码资源源码站
  • 查询企业信息的官方网站百度seo快速排名优化服务
  • 日文网站制作快手刷粉网站推广
  • 可不可以用p2p做视频网站想建立自己的网站怎么建立
  • 免费房地产网站模板百度网
  • 靖江有哪些做网站的游戏推广员是诈骗吗
  • 开网站做代发百度seo关键词优化排名
  • 快捷的网站建设排行榜百度商家入驻怎么做
  • 网站制作服务商中央网站seo
  • 百度推广销售话术梅州seo
  • 男人快乐的浏览器如何做网站搜索引擎优化
  • 怎么做网站规划站长工具四叶草
  • 网络营销零基础培训百度seo霸屏软件
  • 用什么网站做动感相册地推拉新app推广接单平台免费
  • 北京服饰电商网站建设关键词自动优化工具
  • 蚌埠网站建设专业公司哪家好百度网站流量统计
  • 武汉app网站建设百度关键词优化软件
  • 网站上怎样做轮播图全面的seo网站优化排名
  • 网站 切图四川seo多少钱
  • 电梯企业网站制作一个网站可以优化多少关键词
  • 昆明网页设计整站优化和关键词优化的区别
  • 高端大气网站建设收录优美图片找不到了
  • 胶南网站建设多少钱cpa推广平台