Recognise O(1), O(log n), O(n), O(n log n), O(n^2) and O(2^n) and what each means at real sizes.
Divide the time limit's step budget by the steps per input item for each class, and the class tells you the largest n it can handle.
Use it when you must decide whether an approach fits a time limit, or how large an input a method can take. Signal words: will it fit, how large can n be, about 10^8 steps a second.
Start: the step budget: steps per second times seconds. Use 100,000,000 a second as a planning estimate.
Step: count the steps of the class at the real n, with a 64-bit counter and a cap for huge counts.
Stop: compare the count with the budget. Divide by the rate for a time.
It does not give a promise. The rate depends on the machine and the language, and constants inside the steps matter, so keep a margin.
Cost: the check is one formula. It replaces a run that could take hours.
def steps_for(kind, n):
cap = 4000000000000000000
# [1] The number of halvings: floor of log2 n, and 0 when n is 0.
halvings = n.bit_length() - 1 if n > 0 else 0
# [2] O(log n) and O(n): read them straight off.
if kind == 0:
return halvings
if kind == 1:
return n
# [3] O(n log n): n steps for each halving.
if kind == 2:
return n * halvings
# [4] O(n squared) and O(n cubed): a product, capped before it can overflow.
if kind == 3:
return n * n
if kind == 4:
return cap if n > 1000000 else n * n * n
# [5] O(2 to the n): capped above 61, where it would pass 64 bits.
return cap if n > 61 else 2 ** npublic long stepsFor(int kind, int n) {
long cap = 4000000000000000000L;
// [1] The number of halvings: floor of log2 n, and 0 when n is 0.
long halvings = n > 0 ? 31 - Integer.numberOfLeadingZeros(n) : 0;
long m = n;
// [2] O(log n) and O(n): read them straight off.
if (kind == 0) {
return halvings;
}
if (kind == 1) {
return m;
}
// [3] O(n log n): n steps for each halving.
if (kind == 2) {
return m * halvings;
}
// [4] O(n squared) and O(n cubed): a product, capped before it can overflow.
if (kind == 3) {
return m * m;
}
if (kind == 4) {
return n > 1000000 ? cap : m * m * m;
}
// [5] O(2 to the n): capped above 61, where it would pass 64 bits.
return n > 61 ? cap : 1L << n;
}long long stepsFor(int kind, int n) {
long long cap = 4000000000000000000LL;
// [1] The number of halvings: floor of log2 n, and 0 when n is 0.
long long halvings = n > 0 ? 31 - __builtin_clz(n) : 0;
long long m = n;
// [2] O(log n) and O(n): read them straight off.
if (kind == 0) {
return halvings;
}
if (kind == 1) {
return m;
}
// [3] O(n log n): n steps for each halving.
if (kind == 2) {
return m * halvings;
}
// [4] O(n squared) and O(n cubed): a product, capped before it can overflow.
if (kind == 3) {
return m * m;
}
if (kind == 4) {
return n > 1000000 ? cap : m * m * m;
}
// [5] O(2 to the n): capped above 61, where it would pass 64 bits.
return n > 61 ? cap : 1LL << n;
}function stepsFor(kind, n) {
const cap = 4000000000000000000n;
// [1] The number of halvings: floor of log2 n, and 0 when n is 0.
const halvings = n > 0 ? 31 - Math.clz32(n) : 0;
const m = BigInt(n);
// [2] O(log n) and O(n): read them straight off.
if (kind === 0) {
return Number(BigInt(halvings));
}
if (kind === 1) {
return Number(m);
}
// [3] O(n log n): n steps for each halving.
if (kind === 2) {
return Number(m * BigInt(halvings));
}
// [4] O(n squared) and O(n cubed): a product, capped before it can overflow.
if (kind === 3) {
return Number(m * m);
}
if (kind === 4) {
return Number(n > 1000000 ? cap : m * m * m);
}
// [5] O(2 to the n): capped above 61, where it would pass 64 bits.
return Number(n > 61 ? cap : 1n << m);
}def milliseconds_needed(steps, steps_per_ms):
# [1] Round up: any part of a millisecond counts as a whole one.
whole = (steps + steps_per_ms - 1) // steps_per_ms
# [2] Return the time in milliseconds.
return wholepublic long millisecondsNeeded(long steps, int stepsPerMs) {
// [1] Round up: any part of a millisecond counts as a whole one.
long whole = (steps + stepsPerMs - 1) / stepsPerMs;
// [2] Return the time in milliseconds.
return whole;
}long long millisecondsNeeded(long long steps, int stepsPerMs) {
// [1] Round up: any part of a millisecond counts as a whole one.
long long whole = (steps + stepsPerMs - 1) / stepsPerMs;
// [2] Return the time in milliseconds.
return whole;
}function millisecondsNeeded(steps, stepsPerMs) {
// [1] Round up: any part of a millisecond counts as a whole one.
const whole = (BigInt(steps) + BigInt(stepsPerMs) - 1n) / BigInt(stepsPerMs);
// [2] Return the time in milliseconds.
return Number(whole);
}This version uses the Python rate measured on one laptop, 50,000,000 steps a second, to say n squared fits only up to 7,000 items in any language.
This version says any code with at most 100,000,000 steps always finishes in a second.
This version says n log n and n squared are both fast, because both are polynomial-looking.
This version holds the step count in a 32-bit integer.
The budget rule counts simple steps. A job that makes 1,000 network calls is not decided by a step count, because one call takes milliseconds and not nanoseconds.
systemdesignmastery.dev/dsa/cheatsheets/common-classes-felt