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 ...
随机推荐
- 大数据开发-Spark-Streaming处理数据到mysql
前面一篇讲到streamin读取kafka数据加工处理后写到kafka数据,大数据开发-Spark-开发Streaming处理数据 && 写入Kafka是针对比如推荐领域,实时标签等场 ...
- JavaScript疑难点
什么是闭包 我个人理解闭包就是函数中嵌套函数,但是嵌套的那个函数必须是返回值,才构成闭包: //标准的闭包 function fn(){ var i=1; return function fnn(){ ...
- Zeebe服务学习1-简单部署与实现demo
1.Zeebe是什么? Camunda公司研发的工作流引擎Zeebe,目标是对微服务的编排.具体详细介绍可以参考官网:https://zeebe.io/what-is-zeebe/ 2.背景 随着微服 ...
- Java 集合(List、Set、Map 等)相关问答归纳再整理
写在最前面 这个项目是从20年末就立好的 flag,经过几年的学习,回过头再去看很多知识点又有新的理解.所以趁着找实习的准备,结合以前的学习储备,创建一个主要针对应届生和初学者的 Java 开源知识项 ...
- 【odoo14】第十六章、odoo web库(OWL)
odoo14引入了名为OWL(Odoo Web Library)的JavaScript框架.OWL是以组件为基础的UI框架,通过QWeb模板作为架构.OWL与传统的组件系统相比更快,并引入了一些新的特 ...
- C语言入门-mingw64安装+配置
OK,大家好,结合上期所说,本期让我们来配置编译器吧! 首先先下载mingw64离线包,官网下载慢,可以去群里下载,*.7z格式(有些同学可能没有解压软件,为了照顾这部分同学,笔者提供*.exe格式的 ...
- Go语言学习 学习资料汇总
从进入实验室以来,一直听小溪师兄说Go语言,但是第一学期的课很多,一直没有时间学习,现在终于空出来时间学习,按照我的学习习惯,我一般分为三步走 学习一门语言首先要知道学会了能干什么, 然后再把网上的资 ...
- fianl关键词
一.final关键字概述 final关键字具有最终或不可改变的含义,可用于修饰类.变量.方法.因此被final修饰的类.变量.方法具有以下特征: --final修饰的类不能被继承: --final修饰 ...
- 吃透 MQ
本文主要讲解 MQ 的通用知识,让大家先弄明白:如果让你来设计一个 MQ,该如何下手?需要考虑哪些问题?又有哪些技术挑战? 有了这个基础后,我相信后面几篇文章再讲 Kafka 和 RocketMQ 这 ...
- IDA报错fatal error before kernel init
编写了一个IDA64插件,结果再打开IDA后报错fatal error before kernel init,然后闪退. 检查了一遍代码没发现有问题,后来发现是环境有一处配置错误, IDA64.exe ...