-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKth_Smallest_Number_In_M_Smallest_Numbers.cpp
182 lines (139 loc) · 4.06 KB
/
Kth_Smallest_Number_In_M_Smallest_Numbers.cpp
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Given ‘M’ sorted arrays, find the K’th smallest number among all the arrays.
Example 1:
Input: L1=[2, 6, 8], L2=[3, 6, 7], L3=[1, 3, 4], K=5
Output: 4
Explanation: The 5th smallest number among all the arrays is 4, this can be verified from the merged
list of all the arrays: [1, 2, 3, 3, 4, 6, 6, 7, 8]
Example 2:
Input: L1=[5, 8, 9], L2=[1, 7], K=3
Output: 7
Explanation: The 3rd smallest number among all the arrays is 7.
*/
/* This solution is for linked list input
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class ListNode
{
public:
int value=0;
ListNode *next;
ListNode(int value)
{
this->value=value;
this->next=nullptr;
}
};
class Find_Kth_smallest_Number
{
public:
struct Compare
{
bool operator()(const ListNode *x, const ListNode *y)
{
return x->value > y->value;
}
};
static ListNode *find(const vector<ListNode *> &lists, int k)
{
priority_queue<ListNode*, vector<ListNode *>, Compare> minheap;
for(auto root: lists)
{
if(root!=nullptr)
{
minheap.push(root);
}
}
ListNode *node;
while(k>0)
{
node=minheap.top();
minheap.pop();
k--;
if(node->next!=nullptr)
{
minheap.push(node->next);
}
}
return node;
}
};
int main()
{
ListNode *l1=new ListNode(2);
l1->next=new ListNode(6);
l1->next->next=new ListNode(8);
ListNode *l2=new ListNode(3);
l2->next=new ListNode(6);
l2->next->next=new ListNode(7);
ListNode *l3= new ListNode(2);
l3->next=new ListNode(3);
l3->next->next=new ListNode(4);
ListNode *result= Find_Kth_smallest_Number::find(vector<ListNode*> {l1,l2,l3},5);
cout<<result->value;
}
Time Complexity: O(K * Log M)
where M is the total number of elements in all K arrays
Space Complexity: O(K)
*/
/*
Given arrays as input, the approach is same as Merge K sorted list ,
but inorder to keep track of next elemnt in the current array we need to maintain element indices
*/
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class find_kth_smallest_num
{
public:
struct Compare
{
bool operator()(const pair<int,pair<int,int>> &x, pair<int,pair<int,int>> &y)
{
return x.first>y.first;
}
};
static int find(const vector<vector<int>> &lists, int k)
{
priority_queue<pair<int,pair<int,int>>, vector<pair<int,pair<int,int>>>, Compare> minheap;
// put the first element of all input arrays into minheap
for(int i=0;i<lists.size();i++)
{
if(!lists[i].empty())
{
minheap.push(make_pair(lists[i][0],make_pair(i,0)));
}
}
int numbercount=0,result=0;
while(!minheap.empty())
{
auto node=minheap.top();
minheap.pop();
result=node.first;
if(++numbercount==k)
{
break;
}
node.second.second++;
if(lists[node.second.first].size()>node.second.second)
{
node.first=lists[node.second.first][node.second.second];
minheap.push(node);
}
}
return result;
}
};
int main()
{
vector<vector<int>> lists={{2,6,8},{3,6,7},{1,3,4}};
int result=find_kth_smallest_num::find(lists,5);
cout<<result;
}
/*
Since we’ll be going through at most ‘K’ elements among all the arrays,
and we will remove/add one element in the heap in each step, the time
complexity of the above algorithm will be O(K*logM)O(K∗logM) where ‘M’ is the total number of input arrays.
Space complexity #
The space complexity will be O(M) because, at any time, our min-heap will be storing one number from all the ‘M’ input arrays.*/