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.length
n == mat.length[i]
1 <= m, n <= 40
1 <= k <= min(200, n ^ m)
1 <= mat[i][j] <= 5000
mat[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 ...
随机推荐
- Windows开发常用快捷键
毕业后一直在从事Windows开发工作,掌握些常用的Windows快捷键可以大大的提升工作效率,同时还能秀一波操作.本文记录在工作中常用的Windows快捷键,以及VS常用快捷键.掌握了这些键盘操作, ...
- vue关于导航守卫的几种应用场景
beforeEach 该钩子函数主要用来做权限的管理认证 router.beforeEach((to, from, next) => { if (to.matched.some(record = ...
- Docker 一次性进程与对话进程
目录 一次性进程 对话进程 退出的方法 参考 Docker在运行程序的时候,需要区分运行的程序是一次性进程还是对话进程,不同的进程操作方式有差异. 一次性进程 一些简单进程是不需要交互的,比如hell ...
- c++指针数组与二维数组的最大区别
下面随笔是关于指针数组说明及与二维数组的最大区别. 指针数组 数组的元素是指针型 例 利用指针数组存放矩阵 1 #include 2 using namespace std; 3 int main() ...
- 146. LRU 缓存机制 + 哈希表 + 自定义双向链表
146. LRU 缓存机制 LeetCode-146 题目描述 题解分析 java代码 package com.walegarrett.interview; /** * @Author WaleGar ...
- 使用python模块plotdigitizer抠取论文图片中的数据
技术背景 对于各行各业的研究人员来说,经常会面临这样的一个问题:有一篇不错的文章里面有很好的数据,但是这个数据在文章中仅以图片的形式出现.而假如我们希望可以从该图片中提取出数据,这样就可以用我们自己的 ...
- golang调用shell命令(实时输出, 终止等)
背景 是这样的,最近在研究一个定时任务系统的改造,可能有点像jenkins做到的那种吧. 可以输入shell命令,也可以执行py脚本等等,相比之前来说,也要能够及时停止! 但是遇到了这么个问题,gol ...
- python面试题总结
Python语言特性 1. Python的函数参数传递 看两个如下例子,分析运行结果 #代码1 a = 1 def fun(a): a = 2 fun(a) print(a) #1 #代码2 a ...
- 如何自己配置pip源
方式一:临时使用国内pypi镜像安装 pip install -i http://pypi.douban.com/simple/ numpy pip install -i http://pypi.do ...
- 顺序表及基本操作(C语言)
#include <stdio.h> #include <stdlib.h> //基本操作函数用到的状态码 #define TRUE 1; #define FALSE 0; # ...