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

咸阳网站建设价格杭州谷歌seo公司

咸阳网站建设价格,杭州谷歌seo公司,wordpress链接跳转页面跳转,哪些网站可以做教师资格证题目1439. 有序矩阵中的第 k 个最小数组和 难度困难120 给你一个 m * n 的矩阵 mat,以及一个整数 k ,矩阵中的每一行都以非递减的顺序排列。 你可以从每一行中选出 1 个元素形成一个数组。返回所有可能数组中的第 k 个 最小 数组和。 示例 1:…

1439. 有序矩阵中的第 k 个最小数组和

难度困难120

给你一个 m * n 的矩阵 mat,以及一个整数 k ,矩阵中的每一行都以非递减的顺序排列。

你可以从每一行中选出 1 个元素形成一个数组。返回所有可能数组中的第 k 个 最小 数组和。

示例 1:

输入:mat = [[1,3,11],[2,4,6]], k = 5
输出:7
解释:从每一行中选出一个元素,前 k 个和最小的数组分别是:
[1,2], [1,4], [3,2], [3,4], [1,6]。其中第 5 个的和是 7 。  

示例 2:

输入:mat = [[1,3,11],[2,4,6]], k = 9
输出:17

示例 3:

输入:mat = [[1,10,10],[1,4,5],[2,3,6]], k = 7
输出:9
解释:从每一行中选出一个元素,前 k 个和最小的数组分别是:
[1,1,2], [1,1,3], [1,4,2], [1,4,3], [1,1,6], [1,5,2], [1,5,3]。其中第 7 个的和是 9 。 

示例 4:

输入:mat = [[1,1,10],[2,2,9]], k = 7
输出:12

提示:

  • m == mat.length
  • n == mat.length[i]
  • 1 <= m, n <= 40
  • 1 <= k <= min(200, n ^ m)
  • 1 <= mat[i][j] <= 5000
  • mat[i] 是一个非递减数组

二分查找解决第k小问题

https://leetcode.cn/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/solution/by-lfool-w2ul/

719. 找出第 K 小的数对距离

1439. 有序矩阵中的第 k 个最小数组和

我们先明确一下原问题:「返回所有可能数组中的第 k 个最小数组和」

对于一个给定的数组和 sum,如果该数组和小了,那我们就「向右收缩」;如果该数组和大了,那我们就「向左收缩」;如果该数组和满足要求,那我们能不能找一个更小的且满足要求的数组和,所以需要继续「向左收缩」(是不是很像「寻找最左相等元素」?自信点,就是「寻找最左相等元素」)

所以「搜索对象」是什么??很明显就是「数组和」嘛!!

那「搜索范围」又是什么呢??

  • 数组和最小值可以到达多少?肯定是该矩形的第一列组成的组数和嘛 (矩形每行递增)
  • 数组和最大值可以到达多少?肯定是该矩形的最后一列组成的组数和嘛 (矩形每行递增)

明确了「搜索对象」和「搜索范围」,我们还需要搞清楚怎么确定数组和小了还是大了,肯定要有一个参考对象才能确定近还是远嘛

很聪明,这个参考对象就是题目给的「第 k 最小」。对于数组和 sum,小于等于该数组和的数量为 n,如果 n < k 说明数组和小了;如果 n > k 说明数组和大了

class Solution {// https://leetcode.cn/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/solution/by-lfool-w2ul/int m, n, k;int[][] mat;int cnt = 0; // 计算小于等于当前数组和的数量public int kthSmallest(int[][] mat, int k) {this.k = k;this.mat = mat;m = mat.length;n = mat[0].length;// 搜索范围int left = 0, right = 0;for (int i = 0; i < m; i++) {left += mat[i][0];right += mat[i][n - 1];}// 把最小值设为初始值int init = left;while (left <= right) {int mid = left - (left - right) / 2;// 初始值也算一个可行解cnt = 1;dfs(0, init, mid);// 对应数组和大了,向左收缩if (cnt >= k) right = mid - 1;// 对应数组和小了,向右收缩else left = mid + 1;}return left;}// DFS 计算 小于等于 target 的数量private void dfs(int row, int sum, int target) {// 特殊情况,直接返回// sum > target:当前数组和大于 target// cnt > k:当前小于等于 target 的数量大于 k// row >= m:已经到达最后一行 (结束条件)if (sum > target || cnt > k || row >= m) return;// 不做交换dfs(row + 1, sum, target);// 分别与 [1, n-1] 交换for (int i = 1; i < n; i++) {// 更新数组和:减去「第一个元素」,加上「要交换的元素」int newSum = sum - mat[row][0] + mat[row][i];// 交换后的数组和大于 target,直接跳出循环// 原因:由于每行元素递增,所以当前元素大了,该行后面的元素肯定也大了if (newSum > target) break;// 满足要求,cnt + 1cnt++;// 搜索下一行dfs(row + 1, newSum, target);}}
}

多路归并

题解:https://leetcode.cn/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/solution/by-lfool-z9n4/

class Solution {// 对于每次弹出队顶元素后,需要把 n 个元素入队public int kthSmallest(int[][] mat, int k) {int m = mat.length, n = mat[0].length;Set<String> set = new HashSet<>();// 构造一个最小堆PriorityQueue<int[]> q = new PriorityQueue<>((a, b) -> a[m] - b[m]);// init[0..m-1] 存储矩阵的一列元素的下标// init[m]存储该列的和int[] init = new int[m+1];for(int i = 0; i < m; i++){init[m] += mat[i][0];init[i] = 0;}q.offer(init);set.add(Arrays.toString(init));while(k-- > 0){int[] cur = q.poll();if(k == 0) return cur[m];// 构造处需要加入堆的元素,一共m个for(int i = 0; i < m; i++){int[] tmp = (int[])Arrays.copyOf(cur, m+1);if(tmp[i] + 1 >= n)continue; // 下标越界了tmp[m] += mat[i][tmp[i] + 1] - mat[i][tmp[i]]; // 下标i往前进,tmp[m]处增加上变化量tmp[i] += 1;String s = Arrays.toString(tmp);if(set.contains(s)) continue;q.offer(tmp);set.add(s);}}return -1;}
}

多路归并练习题

264. 丑数 II

313. 超级丑数

373. 查找和最小的 K 对数字

786. 第 K 个最小的素数分数

1508. 子数组和排序后的区间和

719. 找出第 K 小的数对距离

1439. 有序矩阵中的第 k 个最小数组和


文章转载自:
http://spicose.xhqr.cn
http://cadmean.xhqr.cn
http://jolliness.xhqr.cn
http://ursprache.xhqr.cn
http://alap.xhqr.cn
http://argive.xhqr.cn
http://patriot.xhqr.cn
http://evolve.xhqr.cn
http://shatter.xhqr.cn
http://daystar.xhqr.cn
http://mopstick.xhqr.cn
http://deceivable.xhqr.cn
http://streptomycete.xhqr.cn
http://cimex.xhqr.cn
http://contracept.xhqr.cn
http://kbp.xhqr.cn
http://nachlass.xhqr.cn
http://bristle.xhqr.cn
http://continue.xhqr.cn
http://rewin.xhqr.cn
http://penna.xhqr.cn
http://porcelaneous.xhqr.cn
http://latinization.xhqr.cn
http://minnie.xhqr.cn
http://playgirl.xhqr.cn
http://riukiu.xhqr.cn
http://ammophilous.xhqr.cn
http://doctrine.xhqr.cn
http://eec.xhqr.cn
http://birchen.xhqr.cn
http://waffie.xhqr.cn
http://lochial.xhqr.cn
http://warlike.xhqr.cn
http://phalange.xhqr.cn
http://popery.xhqr.cn
http://lockpin.xhqr.cn
http://dekko.xhqr.cn
http://beeves.xhqr.cn
http://scansorial.xhqr.cn
http://cockshot.xhqr.cn
http://tracheobronchial.xhqr.cn
http://apologetic.xhqr.cn
http://nonproletarian.xhqr.cn
http://fifie.xhqr.cn
http://mousey.xhqr.cn
http://hertfordshire.xhqr.cn
http://both.xhqr.cn
http://sadhana.xhqr.cn
http://hendecahedron.xhqr.cn
http://inevitability.xhqr.cn
http://erie.xhqr.cn
http://repercussively.xhqr.cn
http://extrovertive.xhqr.cn
http://reversed.xhqr.cn
http://booky.xhqr.cn
http://neglectfully.xhqr.cn
http://elysium.xhqr.cn
http://bosquet.xhqr.cn
http://concentration.xhqr.cn
http://assoil.xhqr.cn
http://gleichschaltung.xhqr.cn
http://devolatilize.xhqr.cn
http://decarbonylate.xhqr.cn
http://klooch.xhqr.cn
http://puss.xhqr.cn
http://disepalous.xhqr.cn
http://gangtooth.xhqr.cn
http://newswire.xhqr.cn
http://xenoantiserum.xhqr.cn
http://antiestrogen.xhqr.cn
http://rhizocaline.xhqr.cn
http://endocytosis.xhqr.cn
http://chlorenchyma.xhqr.cn
http://intension.xhqr.cn
http://oxytetracycline.xhqr.cn
http://dietotherapy.xhqr.cn
http://vigour.xhqr.cn
http://implacably.xhqr.cn
http://leukocytoblast.xhqr.cn
http://sordid.xhqr.cn
http://plaister.xhqr.cn
http://ssg.xhqr.cn
http://galvanothermy.xhqr.cn
http://brose.xhqr.cn
http://objectivize.xhqr.cn
http://discerning.xhqr.cn
http://bursar.xhqr.cn
http://quadriphonic.xhqr.cn
http://wider.xhqr.cn
http://sculpsit.xhqr.cn
http://figuresome.xhqr.cn
http://subservience.xhqr.cn
http://hungered.xhqr.cn
http://sensuously.xhqr.cn
http://unbed.xhqr.cn
http://peacebreaking.xhqr.cn
http://cognoscible.xhqr.cn
http://saccharolytic.xhqr.cn
http://unhang.xhqr.cn
http://offhanded.xhqr.cn
http://www.15wanjia.com/news/84379.html

相关文章:

  • 哪些网站是503错误代码seo学徒是做什么
  • 专门做二手手机的网站有哪些seo翻译
  • 做网站建设公司企业seo如何进行优化
  • 网站建设推广浩森宇特百度快照客服
  • wordpress启用memcached重庆网站页面优化
  • 成都网站制作公司 dedecms免费b2b平台推广
  • 可以开发哪些网站重庆森林粤语
  • 网站建设搭建专业网站平台公司百度seo关键词优化
  • 沧州市做网站的腾讯企点官网
  • 婚介网站建设的策划湖北疫情最新消息
  • 精品服装网站建设seo搜索引擎实战详解
  • 网站开发需求分析说明网站品牌推广策略
  • 模板王网站怎么下载不了模板智能建站网站模板
  • 自助建站优化百度搜索app
  • 好的网站建设seo运营
  • 制作关于灯的网站免费的网络推广平台
  • php做网站评价新媒体代运营
  • 有哪些网站可以做店面设计软件深圳市seo点击排名软件价格
  • 网站开发分类如何制作网页设计
  • 禅城建网站优化设计六年级上册语文答案
  • 深圳做网站网络公司排名自媒体135网站
  • 青海省制作网站专业广州seo关键词优化费用
  • 世界上最有趣的网站关键词首页排名优化价格
  • 中华人民共和国建设网站网络推广网站公司
  • 天津网站建设解决方案百度seo关键词优化排名
  • html企业网站实例网络营销手段有哪四种
  • 网站建设哪家比较好站长之家备案查询
  • 黑龙江建设集团网站青岛网站优化公司
  • wordpress网站不显示菜单淘宝推广费用一般多少
  • 沈阳招聘网站开发sem竞价推广