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 };

参考:

  https://leetcode.com/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/discuss/609707/c%2B%2B-solution-with-explanation

5403. Find the Kth Smallest Sum of a Matrix With Sorted Rows的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [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 ...

  4. 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 ...

  5. 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 ...

  6. 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: [ ...

  7. [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 ...

  8. 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 ...

  9. 【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 ...

随机推荐

  1. Pandas初体验

    目录 Pandas 一.简介 1.安装 2.引用方法 二.series 1.创建方法 2.缺失数据处理 2.1 什么是缺失值 2.2 NaN特性 2.3 填充NaN 2.4 删除NaN 2.5 其他方 ...

  2. 使用Prometheus搞定微服务监控

    最近对服务进行监控,而当前监控最流行的数据库就是 Prometheus,同时 go-zero 默认接入也是这款数据库.今天就对 go-zero 是如何接入 Prometheus ,以及开发者如何自己定 ...

  3. 八. SpringCloud消息总线

    1. 消息总线概述 1.1 分布式配置的动态刷新问题 Linux运维修改Github上的配置文件内容做调整 刷新3344,发现ConfigServer配置中心立刻响应 刷新3355,发现ConfigC ...

  4. Python3.x 基础练习题100例(81-90)

    练习81: 题目: 809??=800??+9?? 其中??代表的两位数, 809??为四位数,8??的结果为两位数,9??的结果为3位数.求??代表的两位数,及809*??后的结果. 程序: a = ...

  5. groovy-map.each{}

    ConfigDetail postEdiUrl(TtxSession sess, String code) { return cdSvc.getByRecordTypeAndIdentifier(se ...

  6. Web全段重点整理

    1.  HTML+CSS 1.1. HTML+CssDay01 1.1.1. 常用普通标签 常用标签如下 div span a p ul+li h1-h6 img 代码示例: <img src= ...

  7. 推荐模型DeepCrossing: 原理介绍与TensorFlow2.0实现

    DeepCrossing是在AutoRec之后,微软完整的将深度学习应用在推荐系统的模型.其应用场景是搜索推荐广告中,解决了特征工程,稀疏向量稠密化,多层神经网路的优化拟合等问题.所使用的特征在论文中 ...

  8. key解析

    密钥在不同实体之间传递,因此密钥必须可以序列化. 所有密钥三个特性: 算法:密钥使用的算法,如DES和DSA等,通过getAlgorithm()获取算法名 编码形式:密钥的外部编码形式,如X.509, ...

  9. Python深入:setuptools进阶

    作者:gqtcgq 来源:CSDN 原文:https://blog.csdn.net/gqtcgq/article/details/49519685 Setuptools是Python Distuti ...

  10. python-实现双链表

    双链表和单链表进行比较的优点与不同 节点多了一个前驱指针域 在很多基本操作上,多了一种选择,因为双链表可以向前进行移动寻位 如果给每个节点添加一个对应的下标,那么在寻找节点时,我们可以使用二分发来进行 ...