-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLearnings_Mock_Interviews
More file actions
198 lines (132 loc) · 3.71 KB
/
Learnings_Mock_Interviews
File metadata and controls
198 lines (132 loc) · 3.71 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
1: ==> My code for the problem in which we I did not come up with cases <===========
Code for this one :
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
vector<int> getIndicesOfItemWeights( const vector<int>& arr, int limit)
{
arr.at(0)
if(arr.empty()) return {};
unordered_map<int,int> hash; // numToIndexCache
vector<int> res(2,-1);
for(int i=0;i<arr.size();i++){
auto it= hash.find(limit-arr[i]);
if(it !=hash.end()){
res[0]=i; res[1]=hash[limit-arr[i]];
hash[arr[i]]=i;
auto it2=hash.find(limit-arr[i]);
cout<<" it2->"<<it2->second<<endl;
}else{
hash.insert({arr[i],i});
}
}
if(res[0]==-1 || res[1]==-1) return {};
else return res;
}
int main() {
return 0;
}
#if 0
// int
// limt -> int
// -> constarints : sorted data in the array < dont't >
// space : ...//
x,y lim -> x=lim-y ;
[4-0, 6-1]
[8-4]
[4, 6] limit [8]
[4, 1]
[4 4 4 7] [8]
[2,1] [2,0] [1,0]
4,1
7,2
// lookup table -> all
// ->
//[4, 6, 10, 15, 16], lim = 21
// 4,0 ; 6,1, ; 10,2 ; 21-4 =>
==> time : On)
==>spac : O(n) ;
#endif
2 ===> How the other person did <===
Sorting Intervals
#include <iostream>
#include <vector>
using namespace std;
struct Interval{
int start;
int end;
};
Interval getCommonInterval(Interval intervalOne, Interval intervalTwo) {
if(intervalOne.end < intervalTwo.satrt || intervalTwo.end < intervalOne.start) {
return {};
}
return {max(intervalOne.start, intervalTwo.start), min(intervalOne.end, intervalTwo.end};
}
bool isValidRange(Interval interval, int dur) {
return interval.start + dur <= interval.end;
}
vector<int> meetingPlanner( const vector<vector<int>>& slotOne, const vector<vector<int>>& slotTwo, int dur)
{
if(dur <= 0 || slotOne.empty() || slotTwo.empty()) {
return {};
}
int pointerOne = 0, pointerTwo = 0;
while(pointerOne < slotOne.size() && pointerTwo < slotTwo.size()) {
Interval intervalOne = {slotOne.at(pointerOne).at(0), slotOne.at(pointerOne).at(1)};
Interval intervalTwo = {slotOne.at(pointerTwo).at(0), slotOne.at(pointerTwo).at(1)};
Interval commonInterval = getCommonInterval(intervalOne, intervalTwo);
if(commonInterval.empty()) {
if(intervalOne.end < intervalTwo.start) {
pointerOne++;
} else {
pointerTwo++;
}
} else {
if(isValidRange(commonInterval, dur)) {
return {commonInterval.start, commonInterval.start + dur};
}
}
}
return {};
}
int main() {
return 0;
}
#if 0
// test suites
// dur = 0
// [] [[10, 20]] ==> []
// [] [] => []
[1, 4] [4, 6] [4, 4]
// [10, 20] [40, 50] => []
// [10, 40] [30, 60] dur (12) => []
// [10, 40 ] [30, 60] dur(7) => [40, 47]
// [[10, 40] [50, 60]] [[30,40] , [50, 57]] dur(5) ==> [30, 35]
1. comparing intervals from sets
2. get intersection if possible
if yes(validate if dur is less than range) if yes (return ans)
if no (slide the pointer for min set);
3. end condition (if any list exhaust)
[[10, 50], [60, 120], [140, 210]]
[[0, 15], [60, 70]]
dur = 8
[10, 15], [60, 70]
[60, 68]
requirement
sorted on start
disjoint
(earliest slot)
one of list empty
+ve
return empty (no possibilities)
//ogic for intersection
[1, 4] [5, 7]
[1, 4] [3, 7]
[3, 7] ===> [3, 5]
Complexities
T(n) = O(m + n)
S(n) = O(1)
[1, 5] [3, 8]
[3, 5]
#endif