Repository navigation
Expand file tree
/
Copy pathSolution.java
More file actions
392 lines (336 loc) · 13.8 KB
/
Copy pathSolution.java
File metadata and controls
392 lines (336 loc) · 13.8 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import java.util.*;
public class Solution {
static class Result {
public static List<Integer> getUnfulfilledCustomers(List<List<Integer>> requests, int totalInventory) {
// bid -> list of {customerId, quantity, timestamp}
TreeMap<Integer, List<int[]>> bidGroups = new TreeMap<>(Collections.reverseOrder());
Map<Integer, Long> allocated = new LinkedHashMap<>();
for (List<Integer> r : requests) {
int customerId = r.get(0);
int quantity = r.get(1);
int bid = r.get(2);
int ts = r.get(3);
bidGroups.computeIfAbsent(bid, k -> new ArrayList<>())
.add(new int[]{customerId, quantity, ts});
allocated.put(customerId, 0L);
}
long inventory = totalInventory;
for (Map.Entry<Integer, List<int[]>> entry : bidGroups.entrySet()) {
if (inventory <= 0) break;
List<int[]> group = entry.getValue();
group.sort((a, b) -> Integer.compare(a[2], b[2])); // sort by timestamp
int m = group.size();
long totalDemand = 0;
for (int[] c : group) totalDemand += c[1];
if (totalDemand <= inventory) {
// everyone in this bid group gets fully served
for (int[] c : group) {
allocated.put(c[0], allocated.get(c[0]) + c[1]);
}
inventory -= totalDemand;
continue;
}
// Not enough for everyone: binary search max full rounds k
long lo = 0, hi = 0;
for (int[] c : group) hi = Math.max(hi, c[1]);
while (lo < hi) {
long mid = lo + (hi - lo + 1) / 2;
long sum = 0;
for (int[] c : group) sum += Math.min((long) c[1], mid);
if (sum <= inventory) {
lo = mid; // feasible, try more rounds
} else {
hi = mid - 1;
}
}
long k = lo;
long usedInFullRounds = 0;
for (int[] c : group) {
long give = Math.min((long) c[1], k);
allocated.put(c[0], allocated.get(c[0]) + give);
usedInFullRounds += give;
}
long rem = inventory - usedInFullRounds;
// distribute leftover single items, in timestamp order,
// to whoever still needs more than k
for (int[] c : group) {
if (rem == 0) break;
if (c[1] > k) {
allocated.put(c[0], allocated.get(c[0]) + 1);
rem--;
}
}
inventory = 0; // inventory fully exhausted in this group
}
List<Integer> result = new ArrayList<>();
for (Map.Entry<Integer, Long> e : allocated.entrySet()) {
if (e.getValue() == 0) result.add(e.getKey());
}
Collections.sort(result);
return result;
}
}
public static void main(String[] args) {
// Quick sanity checks against the examples given
List<List<Integer>> req1 = Arrays.asList(
Arrays.asList(1, 5, 5, 0),
Arrays.asList(2, 7, 8, 1),
Arrays.asList(3, 7, 5, 1),
Arrays.asList(4, 10, 3, 3)
);
System.out.println(Result.getUnfulfilledCustomers(req1, 18)); // expect [4]
List<List<Integer>> req2 = Arrays.asList(
Arrays.asList(1, 2, 5, 0),
Arrays.asList(2, 1, 4, 2),
Arrays.asList(3, 5, 4, 6)
);
System.out.println(Result.getUnfulfilledCustomers(req2, 3)); // expect [3]
List<List<Integer>> req3 = Arrays.asList(
Arrays.asList(101, 3, 10, 15),
Arrays.asList(102, 2, 8, 20),
Arrays.asList(103, 5, 8, 25),
Arrays.asList(104, 4, 5, 30)
);
System.out.println(Result.getUnfulfilledCustomers(req3, 8)); // expect [104]
}
}
// Not enough for everyone: need to do round-robin allocation
// First, find the maximum number of complete rounds we can do
long lo = 0;
long hi = 0;
for (int[] c : group) hi = Math.max(hi, c[1]);
// Binary search for the maximum k that fits in inventory
while (lo < hi) {
long mid = lo + (hi - lo + 1) / 2;
long sum = 0;
for (int[] c : group) {
sum += Math.min((long) c[1], mid);
if (sum > inventory) break; // ✅ Early exit to avoid overflow
}
if (sum <= inventory) {
lo = mid;
} else {
hi = mid - 1;
}
}
long k = lo;
// Allocate k full rounds to everyone
long usedInFullRounds = 0;
long[] remainingNeeds = new long[m]; // ✅ Track remaining needs
for (int i = 0; i < m; i++) {
int[] c = group[i];
long give = Math.min((long) c[1], k);
allocated.put(c[0], allocated.get(c[0]) + give);
usedInFullRounds += give;
remainingNeeds[i] = c[1] - give; // ✅ Store remaining needs
}
long rem = inventory - usedInFullRounds;
// ✅ CORRECT: Distribute remaining items one by one in round-robin fashion
int idx = 0;
while (rem > 0 && idx < m) {
for (int i = idx; i < m && rem > 0; i++) {
if (remainingNeeds[i] > 0) {
int[] c = group[i];
allocated.put(c[0], allocated.get(c[0]) + 1);
remainingNeeds[i]--;
rem--;
}
}
// Continue cycling through all customers until inventory exhausted
}
import java.util.*;
public class Solution {
static class Result {
public static List<Integer> getUnfulfilledCustomers(List<List<Integer>> requests, int totalInventory) {
// Group requests by bid amount (higher bid gets priority)
TreeMap<Integer, List<int[]>> bidGroups = new TreeMap<>(Collections.reverseOrder());
Map<Integer, Long> allocated = new LinkedHashMap<>();
// Process all requests and group by bid
for (List<Integer> r : requests) {
int customerId = r.get(0);
int quantity = r.get(1);
int bid = r.get(2);
int timestamp = r.get(3);
bidGroups.computeIfAbsent(bid, k -> new ArrayList<>())
.add(new int[]{customerId, quantity, timestamp});
allocated.put(customerId, 0L);
}
long inventory = totalInventory;
// Process each bid group from highest to lowest
for (Map.Entry<Integer, List<int[]>> entry : bidGroups.entrySet()) {
if (inventory <= 0) break;
List<int[]> group = entry.getValue();
// Sort by timestamp for round-robin among same bid
group.sort((a, b) -> Integer.compare(a[2], b[2]));
int m = group.size();
long totalDemand = 0;
for (int[] c : group) {
totalDemand += c[1];
}
// If inventory can fulfill all requests in this bid group
if (totalDemand <= inventory) {
for (int[] c : group) {
allocated.put(c[0], allocated.get(c[0]) + c[1]);
}
inventory -= totalDemand;
continue;
}
// Not enough inventory for everyone in this group
// Need round-robin allocation
// Binary search to find maximum full rounds possible
long lo = 0;
long hi = 0;
for (int[] c : group) {
hi = Math.max(hi, c[1]);
}
while (lo < hi) {
long mid = lo + (hi - lo + 1) / 2;
long sum = 0;
for (int[] c : group) {
sum += Math.min((long) c[1], mid);
if (sum > inventory) break; // Early exit optimization
}
if (sum <= inventory) {
lo = mid;
} else {
hi = mid - 1;
}
}
long fullRounds = lo;
// Allocate full rounds to everyone
long usedInFullRounds = 0;
long[] remainingNeeds = new long[m];
for (int i = 0; i < m; i++) {
int[] c = group[i];
long give = Math.min((long) c[1], fullRounds);
allocated.put(c[0], allocated.get(c[0]) + give);
usedInFullRounds += give;
remainingNeeds[i] = c[1] - give;
}
long remainingInventory = inventory - usedInFullRounds;
// Distribute remaining items one by one in round-robin fashion
int idx = 0;
while (remainingInventory > 0 && idx < m) {
for (int i = idx; i < m && remainingInventory > 0; i++) {
if (remainingNeeds[i] > 0) {
int[] c = group[i];
allocated.put(c[0], allocated.get(c[0]) + 1);
remainingNeeds[i]--;
remainingInventory--;
}
}
// Continue cycling through all customers
}
inventory = 0; // All inventory used in this group
}
// Collect customers who received no items
List<Integer> result = new ArrayList<>();
for (Map.Entry<Integer, Long> e : allocated.entrySet()) {
if (e.getValue() == 0) {
result.add(e.getKey());
}
}
Collections.sort(result);
return result;
}
}
public static void main(String[] args) {
// Test Case 1: Sample from problem
List<List<Integer>> req1 = Arrays.asList(
Arrays.asList(1, 5, 5, 0),
Arrays.asList(2, 7, 8, 1),
Arrays.asList(3, 7, 5, 1),
Arrays.asList(4, 10, 3, 3)
);
System.out.println("Test 1: " + Result.getUnfulfilledCustomers(req1, 18));
// Expected: [4]
// Test Case 2: Sample Input 0
List<List<Integer>> req2 = Arrays.asList(
Arrays.asList(1, 2, 5, 0),
Arrays.asList(2, 1, 4, 2),
Arrays.asList(3, 5, 4, 6)
);
System.out.println("Test 2: " + Result.getUnfulfilledCustomers(req2, 3));
// Expected: [3]
// Test Case 3: Sample Input 1
List<List<Integer>> req3 = Arrays.asList(
Arrays.asList(101, 3, 10, 15),
Arrays.asList(102, 2, 8, 20),
Arrays.asList(103, 5, 8, 25),
Arrays.asList(104, 4, 5, 30)
);
System.out.println("Test 3: " + Result.getUnfulfilledCustomers(req3, 8));
// Expected: [104]
// Test Case 4: All same bid with round-robin
List<List<Integer>> req4 = Arrays.asList(
Arrays.asList(1, 5, 5, 1),
Arrays.asList(2, 3, 5, 2),
Arrays.asList(3, 2, 5, 3)
);
System.out.println("Test 4: " + Result.getUnfulfilledCustomers(req4, 8));
// Expected: [] (all get items)
// Allocation: A gets 3, B gets 3, C gets 2
// Test Case 5: Not enough inventory for anyone
List<List<Integer>> req5 = Arrays.asList(
Arrays.asList(1, 5, 5, 1),
Arrays.asList(2, 3, 5, 2)
);
System.out.println("Test 5: " + Result.getUnfulfilledCustomers(req5, 0));
// Expected: [1, 2] (no inventory, all unfulfilled)
// Test Case 6: Edge case with single request
List<List<Integer>> req6 = Arrays.asList(
Arrays.asList(1, 10, 5, 1)
);
System.out.println("Test 6: " + Result.getUnfulfilledCustomers(req6, 7));
// Expected: [] (gets 7 items, but not fully fulfilled - still gets some)
// Wait, the requirement says "customers who received no items"
// So even if partially fulfilled, they received items
// Test Case 7: Partial fulfillment with same bid group
List<List<Integer>> req7 = Arrays.asList(
Arrays.asList(1, 5, 5, 1),
Arrays.asList(2, 5, 5, 2),
Arrays.asList(3, 5, 5, 3)
);
System.out.println("Test 7: " + Result.getUnfulfilledCustomers(req7, 7));
// Expected: [] (all get at least 1 item)
// Allocation: 3,2,2 in round-robin
}
}
AND (
:textQuery IS NULL OR
(
:type = 'title' AND
LOWER(m.title) LIKE LOWER(CONCAT('%', :textQuery, '%'))
) OR
(
:type = 'director' AND
LOWER(m.director) LIKE LOWER(CONCAT('%', :textQuery, '%'))
) OR
(
:type = 'writer' AND EXISTS (
SELECT 1 FROM m.writers w
WHERE LOWER(w) LIKE LOWER(CONCAT('%', :textQuery, '%'))
)
) OR
(
:type = 'star' AND EXISTS (
SELECT 1 FROM m.stars s
WHERE LOWER(s) LIKE LOWER(CONCAT('%', :textQuery, '%'))
)
) OR
(
:type = 'all' AND (
LOWER(m.title) LIKE LOWER(CONCAT('%', :textQuery, '%')) OR
LOWER(m.director) LIKE LOWER(CONCAT('%', :textQuery, '%')) OR
LOWER(m.description) LIKE LOWER(CONCAT('%', :textQuery, '%')) OR
EXISTS (
SELECT 1 FROM m.writers w
WHERE LOWER(w) LIKE LOWER(CONCAT('%', :textQuery, '%'))
) OR
EXISTS (
SELECT 1 FROM m.stars s
WHERE LOWER(s) LIKE LOWER(CONCAT('%', :textQuery, '%'))
)
)
)
)