C++20 sections · 1024 units
Open in Course

George - Implementation

Working solution


plaintext
function countSuitableRooms(n)
count := 0
for i from 1 to n
read occupancy, capacity
available := capacity - occupancy
if available >= 2 then
 count := count + 1
 return count

Space is O(1)O(1) since you never store rooms, just process and discard. This read-process-discard pattern appears in streaming problems where you filter without loading everything into memory. Sorting brings identical elements adjacent. Then one pass counts transitions between different values. Each transition means a new unique value. Alternatively, a set gives the count directly.