-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAggresive Cows
More file actions
209 lines (100 loc) · 3.83 KB
/
Aggresive Cows
File metadata and controls
209 lines (100 loc) · 3.83 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
199
200
201
202
https://www.geeksforgeeks.org/problems/aggressive-cows/1
You are given:
n stalls positioned at different locations along a line (given as an array of integers).
k cows that you need to place in these stalls.
You want to place the cows in such a way that the minimum distance between any two cows is as large as possible.
"minimum largest" terminology, consider this:
If you put cows at 1,2,8. Distance btw stalls is 1, 6 resp.
but if you keep at 1,4,8 distance is 3, 4.
In the first case, the minimum distance is 1 while in the 2nd case it's 3.
You can never put cows in a way that gives a minimum that is larger than 3 i.e. distance cannot be 4,5 or 6,7.
Input:
stalls = [1, 2, 4, 8, 9]
k = 3
Output:
3
1,2,4,8,9
i have 3 stalls
so i need to place them such that
1 2 4 8 9
One stall i can place like this :
I can place at 2-4 -> 2 - 3 - 4
then the distance will be
1-2 =1
2-3=1
3-4=1
4-8=4
8-9=1
minimze the maximum means now in the distqnces b/w stalls
nowout of all the maximum distance is 4
now I will place one more stall here:
1 2 3 4 6 7 8 9
I can place at ->
then the distance will be
all distnces are 1
so mimimizing the max is 1.
so what are all the points i have where i can place the cows stalls
so i will put the cows in stall and check all possibilities and backtrck and continue.
Not optimised.
BS+Greedy:
SORT the stall if not already.
the range :
min distance - low will be starting stall
max - max(stall) - min(stall)
so the max distance would be the starting stall - ending stall
min ? is 1 or? there is only one stall.
they are placed side by side
______________________________________
How to decide if we move left or right.
-> if i can place the cos with lets a distance.
I will check if i can check if i can place the more distance.
class Solution {
public:
bool canPlace(vector<int> &stalls, int k, int minDistance){
//so start from the starting, and check
//and place the cow, after the min distance
int cowsPlaced=1 , lastPos=stalls[0];
for(int i=1;i<stalls.size();i++){
if(stalls[i]-lastPos>=minDistance){
cowsPlaced+=1;
lastPos=stalls[i];
}
}
return cowsPlaced>=k;
}
int aggressiveCows(vector<int> &stalls, int k) {
//sort
sort(stalls.begin(),stalls.end());
int n=stalls.size();
//min distance =1, placing cow side by side
//max distance - max distance b/w the start and stall
int l=1,dis=0;
int h=stalls[n-1]-stalls[0];
while(l<=h){
int mid = l+(h-l)/2;
//if i can place this with the distance b/w stalls
//should i move left or right.
//
if(canPlace(stalls,k,mid)){
dis = mid;
l=mid+1;
}
else h=mid-1;
}
return dis;
}
};
// The actual greedy idea
// Place the first cow at stalls[0].
// Keep track of the last position where you placed a cow (lastPos).
// For every next stall:
// If stalls[i] - lastPos >= minDistance, place another cow at stalls[i].
// Update lastPos = stalls[i].
// Keep a counter cowsPlaced.
// If cowsPlaced >= k → return true.
// Otherwise → return false.
We want to maximize the min distance.
So each time placement with distance mid succeeds, we ask:
Can I push the cows even farther apart?
That’s why we go right.
If it fails, it means we asked for too large a min distance, so we must reduce → go left.