Turn a statement into inputs, outputs and edge rules, and read the time budget from the constraints with the 10^8 rule.
The size of n in the constraints names the slowest class that fits the time limit, and the range of the values names the integer width you need.
Use it when you meet any new statement. Signal words: constraints, input size, time limit, edge cases.
Start: read the input and output formats, then the limits.
Step: turn n into a class with Template 1 and the values into a width with Template 2.
Stop: list the edge cases the ranges allow: the smallest n, the extreme values, equal values and the empty answer.
It does not pick the method. It tells you which methods are too slow or too small.
Cost: two small checks and a short list. A minute of reading saves an hour of rewriting.
def slowest_fitting_class(n):
budget = 100000000
# [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] Class 5, 2 to the n: only n up to 26 can fit, so a larger n is skipped.
if n <= 26 and 2 ** n <= budget:
return 5
# [3] Class 4, n cubed: guard n first so the product cannot overflow.
if n <= 1000 and n * n * n <= budget:
return 4
# [4] Class 3, n squared: guard n first so the product cannot overflow.
if n <= 100000 and n * n <= budget:
return 3
# [5] Class 2, n log n.
if n * halvings <= budget:
return 2
# [6] Class 1, n.
if n <= budget:
return 1
# [7] Class 0, log n, fits every n you can store.
return 0public int slowestFittingClass(int n) {
long budget = 100000000L;
// [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] Class 5, 2 to the n: only n up to 26 can fit, so a larger n is skipped.
if (n <= 26 && (1L << n) <= budget) {
return 5;
}
// [3] Class 4, n cubed: guard n first so the product cannot overflow.
if (n <= 1000 && m * m * m <= budget) {
return 4;
}
// [4] Class 3, n squared: guard n first so the product cannot overflow.
if (n <= 100000 && m * m <= budget) {
return 3;
}
// [5] Class 2, n log n.
if (m * halvings <= budget) {
return 2;
}
// [6] Class 1, n.
if (m <= budget) {
return 1;
}
// [7] Class 0, log n, fits every n you can store.
return 0;
}int slowestFittingClass(int n) {
long long budget = 100000000LL;
// [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] Class 5, 2 to the n: only n up to 26 can fit, so a larger n is skipped.
if (n <= 26 && (1LL << n) <= budget) {
return 5;
}
// [3] Class 4, n cubed: guard n first so the product cannot overflow.
if (n <= 1000 && m * m * m <= budget) {
return 4;
}
// [4] Class 3, n squared: guard n first so the product cannot overflow.
if (n <= 100000 && m * m <= budget) {
return 3;
}
// [5] Class 2, n log n.
if (m * halvings <= budget) {
return 2;
}
// [6] Class 1, n.
if (m <= budget) {
return 1;
}
// [7] Class 0, log n, fits every n you can store.
return 0;
}function slowestFittingClass(n) {
const budget = 100000000;
// [1] The number of halvings: floor of log2 n, and 0 when n is 0.
const halvings = n > 0 ? 31 - Math.clz32(n) : 0;
// [2] Class 5, 2 to the n: only n up to 26 can fit, so a larger n is skipped.
if (n <= 26 && 2 ** n <= budget) {
return 5;
}
// [3] Class 4, n cubed: guard n first so the product cannot overflow.
if (n <= 1000 && n * n * n <= budget) {
return 4;
}
// [4] Class 3, n squared: guard n first so the product cannot overflow.
if (n <= 100000 && n * n <= budget) {
return 3;
}
// [5] Class 2, n log n.
if (n * halvings <= budget) {
return 2;
}
// [6] Class 1, n.
if (n <= budget) {
return 1;
}
// [7] Class 0, log n, fits every n you can store.
return 0;
}def needs_wide_total(count, biggest):
# [1] The largest total the data can make, held in a wide value where the language needs one.
largest_total = count * biggest
# [2] A 32-bit integer stops at 2,147,483,647; above that the total needs 64 bits.
return 1 if largest_total > 2147483647 else 0public int needsWideTotal(int count, int biggest) {
// [1] The largest total the data can make, held in a wide value where the language needs one.
long largestTotal = (long) count * biggest;
// [2] A 32-bit integer stops at 2,147,483,647; above that the total needs 64 bits.
return largestTotal > 2147483647L ? 1 : 0;
}int needsWideTotal(int count, int biggest) {
// [1] The largest total the data can make, held in a wide value where the language needs one.
long long largestTotal = (long long)count * biggest;
// [2] A 32-bit integer stops at 2,147,483,647; above that the total needs 64 bits.
return largestTotal > 2147483647LL ? 1 : 0;
}function needsWideTotal(count, biggest) {
// [1] The largest total the data can make, held in a wide value where the language needs one.
const largestTotal = count * biggest;
// [2] A 32-bit integer stops at 2,147,483,647; above that the total needs 64 bits.
return largestTotal > 2147483647 ? 1 : 0;
}This version reads the five builds in the example, sees that comparing every pair is instant, and ships it.
This version adds durations in a 32-bit integer.
This version returns max(durations) for the slowest build and never checks for an empty list.
This version multiplies the number of values by the largest value in a 32-bit int.
A task allows at most 20 items and asks for the best choice among all subsets. Template 1 at n = 20 returns class 5: 2 to the 20 is 1,048,576 steps, which fits easily.
systemdesignmastery.dev/dsa/cheatsheets/reading-problems-and-constraints