5403. Find the Kth Smallest Sum of a Matrix With Sorted Rows
You are given an m * n matrix, mat, and an integer k, which has its rows sorted in non-decreasing order.
You are allowed to choose exactly 1 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.
Example 4:
Input: mat = [[1,1,10],[2,2,9]], k = 7
Output: 12
Constraints:
m == mat.lengthn == mat.length[i]1 <= m, n <= 401 <= k <= min(200, n ^ m)1 <= mat[i][j] <= 5000mat[i]is a non decreasing array.
题意:
给出一个二维数组,数组的每一行按照非减的顺序进行排列,在每一行中选出一个数字,求出所选数字的和,将这些数字排序,并求出第k小的那个。
思路:
本来想的是用DFS进行求解,如果这样做的话,最欢情况下的时间复杂度会是O(40^40),用DFS遍历求解的话,有可能会出现后面出现的数字比前面出现数字要小的情况,好像没办法剪枝,这种方法行不通。换种思路,我们可以用相邻两行进行相加,求得的和进行排序,截取前面的k个数字。然后再将所求的和与下一行元素相加。
Code:
1 class Solution {
2 public:
3 int kthSmallest(vector<vector<int>>& mat, int k) {
4 vector<int> ans = {0}, temp;
5 for (int i = 0; i < mat.size(); ++i) {
6 for (int j = 0; j < mat[i].size(); ++j) {
7 for (int v = 0; v < ans.size(); ++v) {
8 temp.push_back(mat[i][j] + ans[v]);
9 }
10 }
11 sort(temp.begin(), temp.end());
12 ans.clear();
13 int len = min(k, (int)temp.size());
14 for (int j = 0; j < len; ++j) {
15 ans.push_back(temp[j]);
16 }
17 temp.clear();
18 }
19 return ans[k-1];
20 }
21 };
参考:
5403. Find the Kth Smallest Sum of a Matrix With Sorted Rows的更多相关文章
- [LeetCode] Kth Smallest Element in a Sorted Matrix 有序矩阵中第K小的元素
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- Leetcode:378. Kth Smallest Element in a Sorted Matrix
题目: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the ...
- [LintCode] Kth Smallest Number in Sorted Matrix 有序矩阵中第K小的数字
Find the kth smallest number in at row and column sorted matrix. Have you met this question in a rea ...
- 378. Kth Smallest Element in a Sorted Matrix
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- Leetcode: Kth Smallest Element in a Sorted Matrix
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- Lintcode: Kth Smallest Number in Sorted Matrix
Find the kth smallest number in at row and column sorted matrix. Example Given k = 4 and a matrix: [ ...
- [Swift]LeetCode378. 有序矩阵中第K小的元素 | Kth Smallest Element in a Sorted Matrix
Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the kth ...
- 378. Kth Smallest Element in a Sorted Matrix(java,优先队列)
题目: Given a n x n matrix where each of the rows and columns are sorted in ascending order, find the ...
- 【Leetcode】378. Kth Smallest Element in a Sorted Matrix
Question: Given a n x n matrix where each of the rows and columns are sorted in ascending order, fin ...
随机推荐
- Redis缓存中的常见问题
缓存穿透:是指查询一个Redis和数据库中都不存在的数据. 问题:查询一个Redis和数据库中都不存在的数据,大量请求去访问数据库,导致数据库宕机. 解决办法: 1.根据id查询,如果id是自增的,将 ...
- 00.从0实现一个JVM语言系列
00.一个JVM语言的诞生 由于方才才获悉博客园文章默认不放在首页的, 原创文章主要通过随笔显示, 所以将文章迁移到随笔; 这篇帖子将后续更新, 欢迎关注! 这段时间要忙着春招实习, 所以项目更新会慢 ...
- “Mac应用”已损坏,打不开解决办法
问题说明: 通常在非 Mac App Store下载的软件都会提示"xxx已损坏,打不开.您应将它移到废纸篓"或者"打不开 xxx,因为它来自身份不明的开发者" ...
- Redis之面试连环炮
目录 1.简单介绍一下Redis 2.分布式缓存常见的技术选型方案有哪些? 3.Redis和Memcached的区别和共同点 4. 缓存数据的处理流程是怎样的? 5. 为什么要用 Redis/为什么要 ...
- PAT-1154(Vertex Coloring )+map使用+vector建图+set的使用
Vertex Coloring PAT-1154 #include<iostream> #include<cstring> #include<string> #in ...
- Google单元测试框架gtest之官方sample笔记4--事件监控之内存泄漏测试
sample 10 使用event listener监控Water类的创建和销毁.在Water类中,有一个静态变量allocated,创建一次值加一,销毁一次值减一.为了实现这个功能,重载了new和d ...
- Java 集合框架 01
集合框架· ArrayList 和 Vector 对象数组的概述和使用 * A:案例演示 * 需求:我有5个学生,请把这5个学生的信息存储到数组中,并遍历数组,获取到每个学生的信息 Student[] ...
- MySQL 40题练习题和答案
2.查询"生物"课程比"物理"课程成绩高的所有学生的学号: 思路: 获取所有有生物课程的人(学号,成绩) - 临时表 获取所有有物理课程的人(学号, ...
- [UWP] 模仿哔哩哔哩的一键三连
1. 一键三连 什么是一键三连? 哔哩哔哩弹幕网中用户可以通过长按点赞键同时完成点赞.投币.收藏对UP主表示支持,后UP主多用"一键三连"向视频浏览者请求对其作品同时进行点赞.投币 ...
- Ubuntu更换python默认版本
设置python版本: 1 sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100 2 sudo ...