Count the extra memory an algorithm needs, including the call stack.
Space is the peak of live memory at any moment: count the extra cells beyond the input, and count every call frame still waiting.
Use it when you must say how much memory code needs, whether it fits a limit, or whether a recursion is safe. Signal words: how much memory, in place, extra space, recursion depth.
Start: decide what you count: auxiliary or total, cells or bytes, and the input size n.
Step: follow the running total of live cells and frames as the code allocates, frees, calls and returns.
Stop: report the peak of that total, and say what it depends on.
It does not give bytes by itself. Bytes need the size of one cell, which depends on the language and the machine.
Cost: one pass over the events, with a few numbers kept.
def stack_frames(kind, n):
# [1] The first call is one frame.
frames = 1
# [2] One fewer item each call: n more frames are alive at the bottom.
if kind == 0:
return n + 1
# [3] Halving: count how often the size halves before it reaches 1.
size = n
while size > 1:
# [4] Both halves run one after the other, so only the larger half is alive: it rounds up.
size = size // 2 if kind == 1 else (size + 1) // 2
frames += 1
# [5] Return the frames alive at the deepest call.
return framespublic int stackFrames(int kind, int n) {
// [1] The first call is one frame.
int frames = 1;
// [2] One fewer item each call: n more frames are alive at the bottom.
if (kind == 0) {
return n + 1;
}
// [3] Halving: count how often the size halves before it reaches 1.
int size = n;
while (size > 1) {
// [4] Both halves run one after the other, so only the larger half is alive: it rounds up.
size = kind == 1 ? size / 2 : (size + 1) / 2;
frames++;
}
// [5] Return the frames alive at the deepest call.
return frames;
}int stackFrames(int kind, int n) {
// [1] The first call is one frame.
int frames = 1;
// [2] One fewer item each call: n more frames are alive at the bottom.
if (kind == 0) {
return n + 1;
}
// [3] Halving: count how often the size halves before it reaches 1.
int size = n;
while (size > 1) {
// [4] Both halves run one after the other, so only the larger half is alive: it rounds up.
size = kind == 1 ? size / 2 : (size + 1) / 2;
frames++;
}
// [5] Return the frames alive at the deepest call.
return frames;
}function stackFrames(kind, n) {
// [1] The first call is one frame.
let frames = 1;
// [2] One fewer item each call: n more frames are alive at the bottom.
if (kind === 0) {
return n + 1;
}
// [3] Halving: count how often the size halves before it reaches 1.
let size = n;
while (size > 1) {
// [4] Both halves run one after the other, so only the larger half is alive: it rounds up.
size = kind === 1 ? Math.floor(size / 2) : Math.floor((size + 1) / 2);
frames++;
}
// [5] Return the frames alive at the deepest call.
return frames;
}def peak_live_cells(events):
# [1] No cells are live at the start, and the peak so far is 0.
live = 0
peak = 0
for e in events:
# [2] A positive event allocates cells and a negative event frees them.
live += e
# [3] The peak is the largest number of live cells at any moment.
if live > peak:
peak = live
# [4] Return the peak.
return peakpublic long peakLiveCells(int[] events) {
// [1] No cells are live at the start, and the peak so far is 0.
long live = 0;
long peak = 0;
for (int e : events) {
// [2] A positive event allocates cells and a negative event frees them.
live += e;
// [3] The peak is the largest number of live cells at any moment.
if (live > peak) {
peak = live;
}
}
// [4] Return the peak.
return peak;
}long long peakLiveCells(vector<int>& events) {
// [1] No cells are live at the start, and the peak so far is 0.
long long live = 0;
long long peak = 0;
for (int e : events) {
// [2] A positive event allocates cells and a negative event frees them.
live += e;
// [3] The peak is the largest number of live cells at any moment.
if (live > peak) {
peak = live;
}
}
// [4] Return the peak.
return peak;
}function peakLiveCells(events) {
// [1] No cells are live at the start, and the peak so far is 0.
let live = 0;
let peak = 0;
for (const e of events) {
// [2] A positive event allocates cells and a negative event frees them.
live += e;
// [3] The peak is the largest number of live cells at any moment.
if (live > peak) {
peak = live;
}
}
// [4] Return the peak.
return peak;
}This version says sorting a list in place uses no extra memory.
This version says a recursive sum uses no memory beyond its input, because it declares no list.
This version adds up every allocation and calls the sum the space.
This version says reversing in place and reversing a copy both use O(n) space, because the input has n cells.
Counting cells assumes every cell is the same size. In Python, a list of 1,000,000 integers holds 8,000,056 bytes of pointers, and each integer from 257 to 2^30 - 1 is a separate object of 28 bytes.
systemdesignmastery.dev/dsa/cheatsheets/space-complexity