Design Data Structures · Pattern 10 of 10
Simulation designs: model the state directly
When the limits are small, the simplest correct design is to store the state exactly as described and scan it. A memory of n cells is an array of owner ids, and each call walks it once.
Watch the lesson
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
When to use it
- Small limits (around 1000) on both size and number of calls
- The rules describe the state step by step
- First fit, leftmost free block, or "free everything with this id"
The template
class Allocator {
int[] mem; // mem[i] = id that owns cell i, 0 if free
Allocator(int n) { mem = new int[n]; }
int allocate(int size, int mID) {
int run = 0; // free cells in a row, ending at i
for (int i = 0; i < mem.length; i++) {
run = mem[i] == 0 ? run + 1 : 0;
if (run == size) { // leftmost block that fits
for (int j = i - size + 1; j <= i; j++) mem[j] = mID;
return i - size + 1;
}
}
return -1;
}
int freeMemory(int mID) {
int count = 0;
for (int i = 0; i < mem.length; i++)
if (mem[i] == mID) { mem[i] = 0; count++; }
return count;
}
} class Allocator:
def __init__(self, n):
self.mem = [0] * n # mem[i] = id that owns cell i, 0 if free
def allocate(self, size, mID):
run = 0 # free cells in a row, ending at i
for i, owner in enumerate(self.mem):
run = run + 1 if owner == 0 else 0
if run == size: # leftmost block that fits
start = i - size + 1
self.mem[start:i + 1] = [mID] * size
return start
return -1
def freeMemory(self, mID):
count = self.mem.count(mID)
self.mem = [0 if owner == mID else owner for owner in self.mem]
return count class Allocator {
vector<int> mem; // mem[i] = id that owns cell i, 0 if free
public:
Allocator(int n) : mem(n) {}
int allocate(int size, int mID) {
int run = 0; // free cells in a row, ending at i
for (int i = 0; i < (int)mem.size(); i++) {
run = mem[i] == 0 ? run + 1 : 0;
if (run == size) { // leftmost block that fits
fill(mem.begin() + i - size + 1, mem.begin() + i + 1, mID);
return i - size + 1;
}
}
return -1;
}
int freeMemory(int mID) {
int count = 0;
for (int& owner : mem)
if (owner == mID) { owner = 0; count++; }
return count;
}
}; class Allocator {
constructor(n) { this.mem = new Array(n).fill(0); } // mem[i] = id that owns cell i, 0 if free
allocate(size, mID) {
let run = 0; // free cells in a row, ending at i
for (let i = 0; i < this.mem.length; i++) {
run = this.mem[i] === 0 ? run + 1 : 0;
if (run === size) { // leftmost block that fits
this.mem.fill(mID, i - size + 1, i + 1);
return i - size + 1;
}
}
return -1;
}
freeMemory(mID) {
let count = 0;
for (let i = 0; i < this.mem.length; i++)
if (this.mem[i] === mID) { this.mem[i] = 0; count++; }
return count;
}
} The variations
-
1Store the state as described, then scan
One array cell per memory unit, holding its owner. Allocation finds the first run of
sizefree cells in one pass; freeing clears every cell with that id. With bigger limits, a sorted map of free blocks makes each call O(log n).
Mistakes to avoid
- Not resetting the free-run counter when you hit a used cell, so two separate gaps are glued together.
- Assuming one id owns one block: the same id can allocate several times, and
freeMemorymust free all of them. - Reaching for an interval tree when n and the calls are only 1000: O(n) per call is plenty.
Lesson done
Back to Design Data Structures →Mark it done once you can write the template from memory and you have solved the worked examples yourself.