首页 > 其他分享 >LeetCode 1439. Find the Kth Smallest Sum of a Matrix With Sorted Rows

LeetCode 1439. Find the Kth Smallest Sum of a Matrix With Sorted Rows

时间:2024-04-07 10:36:19浏览次数:28  
标签:Rows Matrix int Sum mat length new sum row

原题链接在这里:https://leetcode.com/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/description/

题目:

You are given an m x n matrix mat that has its rows sorted in non-decreasing order and an integer k.

You are allowed to choose exactly one element from each row to form an array.

Return the kth smallest array sum among all possible arrays.

Example 1:

Input: mat = [[1,3,11],[2,4,6]], k = 5
Output: 7
Explanation: Choosing one element from each row, the first k smallest sum are:
[1,2], [1,4], [3,2], [3,4], [1,6]. Where the 5th sum is 7.

Example 2:

Input: mat = [[1,3,11],[2,4,6]], k = 9
Output: 17

Example 3:

Input: mat = [[1,10,10],[1,4,5],[2,3,6]], k = 7
Output: 9
Explanation: Choosing one element from each row, the first k smallest sum are:
[1,1,2], [1,1,3], [1,4,2], [1,4,3], [1,1,6], [1,5,2], [1,5,3]. Where the 7th sum is 9.  

Constraints:

  • m == mat.length
  • n == mat.length[i]
  • 1 <= m, n <= 40
  • 1 <= mat[i][j] <= 5000
  • 1 <= k <= min(200, nm)
  • mat[i] is a non-decreasing array.

题解:

We know how to find the kth smallest sum for 2 arrays.

Now we have a matrix. We can start with 2, merge it into a new array and continue with rest rows of matrix.

Each time, we keep the k smallest sums into a new array and continue with the next row.

Time Complexity: O(m * (n + k) * logn)). m = mat.length. n = mat[0].length. 

Space: O(k + n).

AC Java:

 1 class Solution {
 2     public int kthSmallest(int[][] mat, int k) {
 3         int[] row = mat[0];
 4         for(int r = 1; r < mat.length; r++){
 5             row = kSmall(row, mat[r], k);
 6         }
 7 
 8         return row[k - 1];
 9     }
10 
11     private int[] kSmall(int[] nums1, int[] nums2, int k){
12         List<Integer> res = new ArrayList<>();
13         int m = nums1.length;
14         int n = nums2.length;
15         PriorityQueue<int[]> minHeap = new PriorityQueue<>((a, b) -> Integer.compare(a[2], b[2]));
16         for(int j = 0; j < n; j++){
17             minHeap.add(new int[]{0, j, nums1[0] + nums2[j]});
18         }
19 
20         while(k-- > 0 && !minHeap.isEmpty()){
21             int[] cur = minHeap.poll();
22             res.add(cur[2]);
23             if(cur[0] == m - 1){
24                 continue;
25             }
26 
27             minHeap.add(new int[]{cur[0] + 1, cur[1], nums1[cur[0] + 1] + nums2[cur[1]]});
28         }
29 
30         int[] resArr = new int[res.size()];
31         for(int i = 0; i < resArr.length; i++){
32             resArr[i] = res.get(i);
33         }
34 
35         return resArr;
36     }
37 }

类似Find K Pairs with Smallest Sums.

标签:Rows,Matrix,int,Sum,mat,length,new,sum,row
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18118527

相关文章

  • 2024-04-06:用go语言,给你两个非负整数数组 rowSum 和 colSum, 其中 rowSum[i] 是二维矩
    2024-04-06:用go语言,给你两个非负整数数组rowSum和colSum,其中rowSum[i]是二维矩阵中第i行元素的和,colSum[j]是第j列元素的和,换言之你不知道矩阵里的每个元素,但是你知道每一行和每一列的和。请找到大小为rowSum.lengthxcolSum.length的任意非负整数矩阵。且该......
  • #线段树,模拟费用流#CF280D k-Maximum Subsequence Sum
    题目给定一个大小为\(n\)的序列,要求支持单点修改和查询区间内至多\(k\)个不交子区间之和的最大值(可以不取)分析考虑源点向每个点、每个点向汇点流流量1费用0的边,每个点向右边的点流流量1费用\(a_i\)的边,流量最大为\(k\),这样构建出一个费用流的模型。很显然,退流相当于给区......
  • 树莓派无桌面系统(RaspberryPI Lite)启动自动打开Chromium-Browser的具体方法
    https://blog.csdn.net/sinat_36939362/article/details/95391676RaspberryPILite自动打开Chromium-Browser情景:需要用电视机通过网页显示一些数据需要到的工具:前期准备步骤:在RPILite安装相应的Package完善功能解决Chromium中文乱码的问题光标隐藏代码Lite需要满屏显示写批处......
  • Least Prefix Sum
    题目链接Hello2023C.LeastPrefixSum思路:仔细看式子,发现可以对它进行推理(mmm是定值,1≤......
  • ceisum 画矩形 画带高度的矩形 画竖起来的矩形
    一、画矩形,每个点不带高度,距离地表500米viewer.entities.add({polygon:{hierarchy:newCesium.PolygonHierarchy(Cesium.Cartesian3.fromDegreesArray([......
  • LeetCode 2461. Maximum Sum of Distinct Subarrays With Length K
    原题链接在这里:https://leetcode.com/problems/maximum-sum-of-distinct-subarrays-with-length-k/description/题目:Youaregivenanintegerarray nums andaninteger k.Findthemaximumsubarraysumofallthesubarraysof nums thatmeetthefollowingconditi......
  • 生信小白菜之关于summarize函数的一切(part 1)
    准备包和示例数据library(dplyr)library(nycflights13)library(ggplot2)summarize()的基本用法#获取摘要的函数#作用是将数据框折叠成一行#举例summarise(flights,delay=mean(dep_delay,na.rm=T))#第二个参数新的一列,也是根据数据框原有数据计算得来#返回结......
  • WPF如何使用 System.Windows.Forms.FolderBrowserDialog
    WPF如何使用System.Windows.Forms.FolderBrowserDialog在WPF中,如果你想使用System.Windows.Forms.FolderBrowserDialog来选择文件夹,你需要添加对WinForms的引用,因为FolderBrowserDialog是WindowsForms的一部分,不是WPF的一部分。下面是如何在WPF应用程序中使用FolderBro......
  • 从零开始 使用OMNET++结合VEINS,INET和SUMO的联合仿真
    背景知识当我们探索未来的交通系统和智能交通解决方案时,车辆到一切(Vehicle-to-Everything,V2X)通信技术显得尤为重要。V2X是指在车辆与车辆(V2V)、车辆与基础设施(V2I)、车辆与行人(V2P)以及车辆与网络(V2N)之间进行的通信。这种技术能够提高道路安全,优化交通流量,减少拥堵,提升驾驶体验......
  • SUM-ACM,3月24-3-31周报
    两场天梯赛和一场atcoder。主要错误知识点在于字符串的处理和并查集的掌握不够,不懂灵活运用。第一场pta天梯赛7-56翻了一道字符串的题,我只拿了14分。我不熟悉一个点,f(i,0,s.length())是有问题的,在replace后,s.length()会改变,这个时候replace不是一个好的选择。我们只需要输......