当前位置:网站首页>Force deduction exercise - the sum of the kth smallest array in the 21 ordered matrix
Force deduction exercise - the sum of the kth smallest array in the 21 ordered matrix
2022-07-24 12:12:00 【qq_ forty-three million four hundred and three thousand six hun】
21 In an ordered matrix k The smallest array and
1. Problem description
To give you one m * n Matrix mat, And an integer k , Every row in the matrix is arranged in a non decreasing order .
You can choose from each line 1 Elements form an array . Returns the second of all possible arrays k individual Minimum Array and .
Example 1:
Input :mat = [[1,3,11],[2,4,6]], k = 5
Output :7
explain : Select an element from each row , front k And the smallest array is :
[1,2], [1,4], [3,2], [3,4], [1,6]. Among them the first 5 The sum of two 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
explain : Select an element from each row , front k And the smallest array is :
[1,1,2], [1,1,3], [1,4,2], [1,4,3], [1,1,6], [1,5,2], [1,5,3]. Among them the first 7 The sum of two is 9 .
Example 4:
Input :mat = [[1,1,10],[2,2,9]], k = 7
Output :12
explain :
m ,n For matrix mat The number of rows and columns of
1 <= m, n <= 40
1 <= k <= min(200, n ^ m)
1 <= mat[i][j] <= 5000
mat[i] It is a group of non decreasing numbers
Refer to the following main function :
int main()
{
int m, n,data,k;
vector<vector<int> > mat;
cin>>m>>n;
for(int i=0; i<m; i++)
{
vector<int> row;
for(int j=0; j<n; j++)
{
cin>>data;
row.push_back(data);
}
mat.push_back(row);
}
cin>>k;
int res=Solution().kthSmallest(mat,k);
cout<<res<<endl;
return 0;
}
2. Enter description
First enter the matrix mat The number of rows and columns of m and n
Then input m That's ok , Each row n Nonnegative integers , Express mat The element value of , Space off .
Last input k.
3. The output shows that
Output an integer , Show the result .
4. Example
Input
2 3
1 3 11
2 4 6
5
Output
7
5. Code
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<set>
using namespace std;
int kthSmallest(vector<vector<int> >mat, int k)
{
// Violence solution
// Refer to the website https://leetcode.cn/problems/find-the-kth-smallest-sum-of-a-matrix-with-sorted-rows/solution/find-the-kth-smallest-sum-of-a-matrix-by-ikaruga/
//1. Calculate the number of rows and columns
int m = mat.size();// Row number
int n = mat[0].size();// Number of columns
vector<int>ans(mat[0]);//ans Save the numbers in the first line
for (int i = 1; i < m; i++)
{
multiset<int>st;//multiset It can always ensure that the numbers in the sequence are ordered , And there can be repeated numbers in the sequence .
for (int x : ans)// stay i=1 when , Take the number in the first row ;i=2 when , Take the first row and the second row together to get the first K Number of combinations
{
for (int y : mat[i]) //i=1 when , Fetching in the second line etc
{
st.insert(x + y);//i=1, Add the number combinations of the first and second rows
}
}
ans.assign(st.begin(), st.end());// Assign the added number to ans, Implement update operation
ans.resize(min(k, (int)ans.size()));// Only take the first in the sequence table K It's worth
}
return ans.back();// The final value is the desired number K The smallest array and
}
int main()
{
int m, n, data, k;
vector<vector<int> > mat;
cin >> m >> n;
for (int i = 0; i < m; i++)
{
vector<int> row;
for (int j = 0; j < n; j++)
{
cin >> data;
row.push_back(data);
}
mat.push_back(row);
}
cin >> k;
int res = kthSmallest(mat, k);
cout << res << endl;
return 0;
}
边栏推荐
- Skillfully using command line parameters in Delphi to realize the trigger function of dragging files onto program icons
- JS image to Base64
- [C and pointer Chapter 14] preprocessor
- Oceanbase Database Setup Test
- 【C和指针第11章】动态内存分配
- Install MariaDB columnstore (version 10.3)
- 如何在IM系统中实现抢红包功能?
- Import the data in MariaDB into columnstore
- 6k+ star,面向小白的深度学习代码库!一行代码实现所有Attention机制!
- Wechat official account development: Material Management (temporary and permanent)
猜你喜欢

L1-059 ring stupid bell

Source code analysis sentry user behavior record implementation process

NFT digital collection system construction - app development

Remember to optimize my personal blog once
![[mathematical basis of Cyberspace Security Chapter 9] finite field](/img/2b/27ba1f3c6ec2ecff4538f9a63a79e8.jpg)
[mathematical basis of Cyberspace Security Chapter 9] finite field

Easy to use example
![[rust] Why do I suggest you learn rust | a preliminary study of rust](/img/33/a5e7d22e87502fa8582920cb34de9f.png)
[rust] Why do I suggest you learn rust | a preliminary study of rust

如何将Typora中图片上传到csdn

6k+ star, a deep learning code base for Xiaobai! One line of code implements all attention mechanisms!

Aruba learning notes 04 Web UI -- Introduction to configuration panel
随机推荐
A*与JPS
Leetcode:51. queen n
How to find out the function calling process of complex code running as soon as possible
Common shortcuts to VIM editor
Day3: branch structure
QT notes - custom data types
Understand what goals the MES system can achieve
Common formulas and application scenarios of discrete distribution
3、 Implementation principle of MFC message mapping mechanism
在kuborad图形化界面中,操作Kubernetes 集群,实现mysql中的主从复制
PM's alarm: "NPM warn config global --global, --local are deprecated
The biggest crisis for testers in the workplace is not at the age of 30, but being laid off in middle age
L1-043 reading room
In kuborad graphical interface, operate kubernetes cluster to realize master-slave replication in MySQL
Install JMeter
Aruba learning notes 04 Web UI -- Introduction to configuration panel
TypeNameExtractor could not be found
MySQL creates partition tables and automatically partitions them by day
Repeated calls, messages, idempotent schemes, full collation
Day4: circular structure