Function to find Number of customers who could not get a computer
Problem Statement
A cyber cafe has n computers and m customers arrive sequentially. Each customer has a use time duration[i]. A customer gets a computer only if one is available when they arrive. Find the number of customers who could not get a computer.
Example:
- Input: n = 2, arrivals = [1, 2, 3, 4, 5], durations = [3, 3, 3, 3, 3]
- Output: 3
- Explanation:
- Customer 1 arrives at t=1, uses computer until t=4
- Customer 2 arrives at t=2, uses computer until t=5
- Customer 3 arrives at t=3, no computer free → rejected
- Customer 4 arrives at t=4, computer from customer 1 is free
- Customer 5 arrives at t=5, computer from customer 2 is free
Approach 1: Brute Force (Simulate with Array)
Intuition
Simulate the process using an array to track when each computer becomes free. For each customer, check if any computer is free at their arrival time.
Algorithm
- Initialize array of size n to track computer free times (all 0)
- For each customer:
- Find a computer that's free (free_time <= arrival_time)
- If found, update its free time to arrival + duration
- If not found, increment rejected count
- Return rejected count
public class Solution {
public int countRejectedCustomers(int n, int[] arrivals, int[] durations) {
int m = arrivals.length;
int[] computerFreeTime = new int[n];
int rejected = 0;
for (int i = 0; i < m; i++) {
int arrival = arrivals[i];
int duration = durations[i];
boolean found = false;
for (int j = 0; j < n; j++) {
if (computerFreeTime[j] <= arrival) {
computerFreeTime[j] = arrival + duration;
found = true;
break;
}
}
if (!found) {
rejected++;
}
}
return rejected;
}
}Complexity Analysis
- Time Complexity: O(n * m) - For each of m customers, check n computers
- Space Complexity: O(n) - Array to track computer free times
Approach 2: Using Min-Heap (Optimal)
Intuition
Use a min-heap to track computer free times. The minimum free time tells us if any computer is available.
Algorithm
- Initialize min-heap with n zeros (all computers free at t=0)
- For each customer:
- Pop minimum free time
- If min_time <= arrival, computer is available
- Push new free time (arrival + duration)
- If min_time > arrival, no computer available
- Push back the min_time (computer still busy)
- Increment rejected count
- Return rejected count
import java.util.*;
public class Solution {
public int countRejectedCustomers(int n, int[] arrivals, int[] durations) {
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
for (int i = 0; i < n; i++) {
minHeap.offer(0);
}
int rejected = 0;
for (int i = 0; i < arrivals.length; i++) {
int arrival = arrivals[i];
int duration = durations[i];
int earliestFree = minHeap.poll();
if (earliestFree <= arrival) {
minHeap.offer(arrival + duration);
} else {
minHeap.offer(earliestFree);
rejected++;
}
}
return rejected;
}
}Complexity Analysis
- Time Complexity: O(m log n) - For each customer, heap operations are O(log n)
- Space Complexity: O(n) - Heap of size n
Approach 3: Two Pointer / Sliding Window (When Arrivals are Sequential)
Intuition
If customers arrive sequentially (arrival[i] = i+1), we can use a deque to track which computers become free.
This approach works well when arrivals are in order and we need O(1) check for free computers, but maintaining sorted order in deque makes it O(n) per operation in worst case. The heap approach remains optimal.
String Version of the Problem
Sometimes this problem is given with a string where each character represents a customer:
Dry Run Example
n = 2, arrivals = [1, 2, 3, 4, 5], durations = [3, 3, 3, 3, 3]
Initial heap: [0, 0]
Customer 1: arrival=1, duration=3
pop 0, 0 <= 1, push 4
heap: [0, 4]
Customer 2: arrival=2, duration=3
pop 0, 0 <= 2, push 5
heap: [4, 5]
Customer 3: arrival=3, duration=3
pop 4, 4 > 3, push 4 back
rejected = 1
heap: [4, 5]
Customer 4: arrival=4, duration=3
pop 4, 4 <= 4, push 7
heap: [5, 7]
Customer 5: arrival=5, duration=3
pop 5, 5 <= 5, push 8
heap: [7, 8]
Total rejected: 1
Wait, let me recalculate...
n = 2, arrivals = [1, 2, 3, 4, 5], durations = [3, 3, 3, 3, 3]
Customer 1 at t=1: gets comp, free at t=4
Customer 2 at t=2: gets comp, free at t=5
Customer 3 at t=3: both comps busy (free at 4,5), REJECTED
Customer 4 at t=4: comp 1 free, gets it, free at t=7
Customer 5 at t=5: comp 2 free, gets it, free at t=8
Rejected: 1 (not 3 as I said earlier - my initial example was wrong)
Key Takeaways
- Min-heap provides efficient O(log n) operations for finding/updating free computers
- Simulation approach is intuitive but O(n * m)
- The problem tests understanding of scheduling and resource allocation
- Similar to meeting rooms problem (can n rooms accommodate m meetings?)
- Edge case: if n >= m, no one is rejected
- Can be extended to track which customer used which computer