-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKth_Smallest_Number_In_Sorted_Matrix.cpp
More file actions
83 lines (72 loc) · 1.7 KB
/
Kth_Smallest_Number_In_Sorted_Matrix.cpp
File metadata and controls
83 lines (72 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
Given an N * NN∗N matrix where each row and column is sorted in ascending order, find the Kth smallest element in the matrix.
Example 1:
Input: Matrix=[
[2, 6, 8],
[3, 7, 10],
[5, 8, 11]
],
K=5
Output: 7
Explanation: The 5th smallest number in the matrix is 7.
*/
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class Find
{
public:
static int findKthSmallest(const vector<vector<int>> &mat,const int k)
{
int n=mat.size();
int start=mat[0][0], end=mat[n-1][n-1];
while(start<end)
{
int mid=start+(end-start)/2;
pair<int,int> smalllargepair={mat[0][0],mat[n-1][n-1]};
int count=countlessequal(mat,mid,smalllargepair);
if(count==k)
{
return smalllargepair.first;
}
if(count<k)
{
start=smalllargepair.second;
}
else
{
end=smalllargepair.first;
}
}
return start;
}
private:
static int countlessequal(vector<vector<int>> mat, int mid, pair<int,int> &smallargepair)
{
int count=0;
int n=mat.size();
int row=n-1;
int col=0;
while(row>=0 &&col<n)
{
if(mid<mat[row][col])
{
smallargepair.second=min(smallargepair.second,mat[row][col]);
row--;
}
else
{
smallargepair.first=max(smallargepair.first, mat[row][col]);
count+=row+1;
col++;
}
}
return count;
}
};
int main()
{
vector<vector<int>> matrix2={vector<int> {2,6,8}, vector<int> {3,7,10}, vector<int> {5,8,11}};
int result=Find::findKthSmallest(matrix2,5);
cout<<result;
}