Binary search over possible answers with a feasibility check.
Every candidate before lo fails the check, and every candidate from hi on passes it, so lo ends as the smallest candidate that passes.
Use it when the answer is a number, and you can test one candidate easily. A bigger candidate must never hurt, or a smaller one must never hurt. Signal words: the smallest speed that still works, minimise the largest, maximise the smallest.
Start: lo is the smallest candidate that could be the answer. hi is a candidate that is certain to pass the check. The window [lo, hi) holds the candidates still in question.
Step: while lo < hi, set mid = lo + (hi - lo) / 2 and run the check on mid. If it passes, set hi = mid. Otherwise set lo = mid + 1.
Stop: when lo == hi. The answer is lo. For a largest answer, search for the first candidate that fails, and return lo - 1.
It does not work when the check can pass, then fail, then pass again.
Cost: at most floor(log2 of the number of candidates) + 1 checks, and each check costs one pass over the input: O(n log range). Extra space is O(1).
lo, hi and mid, and the check keeps a running total, so the extra space is O(1).def slowest_rate(sizes, hours):
# [1] The candidates are the whole rates 1 to the largest file. At that rate every file takes one hour, so it works when there are enough hours: hi starts there.
lo = 1
hi = max(sizes)
# [2] While the window holds a rate, look at the rate in its middle.
while lo < hi:
mid = lo + (hi - lo) // 2
# [3] The check for this one rate: add up the whole hours each file needs, rounding up.
needed = 0
for s in sizes:
needed += (s + mid - 1) // mid
if needed <= hours:
# [4] The rate is fast enough, so every faster rate is too. It may be the slowest, so keep it: hi moves to it.
hi = mid
else:
# [5] The rate is too slow, so every slower rate is too. lo moves past it.
lo = mid + 1
# [6] The window is empty: lo is the slowest rate that finishes in time.
return lopublic int slowestRate(int[] sizes, long hours) {
// [1] The candidates are the whole rates 1 to the largest file. At that rate every file takes one hour, so it works when there are enough hours: hi starts there.
int lo = 1;
int hi = 0;
for (int s : sizes) {
hi = Math.max(hi, s);
}
// [2] While the window holds a rate, look at the rate in its middle.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
// [3] The check for this one rate: add up the whole hours each file needs, rounding up.
long needed = 0;
for (int s : sizes) {
needed += ((long) s + mid - 1) / mid;
}
if (needed <= hours) {
// [4] The rate is fast enough, so every faster rate is too. It may be the slowest, so keep it: hi moves to it.
hi = mid;
} else {
// [5] The rate is too slow, so every slower rate is too. lo moves past it.
lo = mid + 1;
}
}
// [6] The window is empty: lo is the slowest rate that finishes in time.
return lo;
}int slowestRate(vector<int>& sizes, long long hours) {
// [1] The candidates are the whole rates 1 to the largest file. At that rate every file takes one hour, so it works when there are enough hours: hi starts there.
int lo = 1;
int hi = 0;
for (int s : sizes) {
hi = max(hi, s);
}
// [2] While the window holds a rate, look at the rate in its middle.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
// [3] The check for this one rate: add up the whole hours each file needs, rounding up.
long long needed = 0;
for (int s : sizes) {
needed += ((long long)s + mid - 1) / mid;
}
if (needed <= hours) {
// [4] The rate is fast enough, so every faster rate is too. It may be the slowest, so keep it: hi moves to it.
hi = mid;
} else {
// [5] The rate is too slow, so every slower rate is too. lo moves past it.
lo = mid + 1;
}
}
// [6] The window is empty: lo is the slowest rate that finishes in time.
return lo;
}function slowestRate(sizes, hours) {
// [1] The candidates are the whole rates 1 to the largest file. At that rate every file takes one hour, so it works when there are enough hours: hi starts there.
let lo = 1;
let hi = 0;
for (const s of sizes) {
hi = Math.max(hi, s);
}
// [2] While the window holds a rate, look at the rate in its middle.
while (lo < hi) {
const mid = lo + Math.floor((hi - lo) / 2);
// [3] The check for this one rate: add up the whole hours each file needs, rounding up.
let needed = 0;
for (const s of sizes) {
needed += Math.ceil(s / mid);
}
if (needed <= hours) {
// [4] The rate is fast enough, so every faster rate is too. It may be the slowest, so keep it: hi moves to it.
hi = mid;
} else {
// [5] The rate is too slow, so every slower rate is too. lo moves past it.
lo = mid + 1;
}
}
// [6] The window is empty: lo is the slowest rate that finishes in time.
return lo;
}def longest_piece(ribbons, k):
# [1] The candidates are the whole lengths 1 to the longest ribbon. One more than that gives no piece at all, so it fails when k is at least 1: hi starts there.
lo = 1
hi = max(ribbons) + 1
# [2] While the window holds a length, look at the length in its middle.
while lo < hi:
mid = lo + (hi - lo) // 2
# [3] The check for this one length: count the whole pieces that every ribbon gives.
pieces = 0
for r in ribbons:
pieces += r // mid
if pieces >= k:
# [4] Enough pieces, so every shorter length gives enough too. lo moves past mid.
lo = mid + 1
else:
# [5] Too few pieces, so every longer length gives too few. mid may be the first length that fails, so keep it: hi moves to it.
hi = mid
# [6] The window is empty: lo is the first length that fails, so lo - 1 is the longest length that works. It is 0 when none works.
return lo - 1public int longestPiece(int[] ribbons, long k) {
// [1] The candidates are the whole lengths 1 to the longest ribbon. One more than that gives no piece at all, so it fails when k is at least 1: hi starts there.
int lo = 1;
int hi = 0;
for (int r : ribbons) {
hi = Math.max(hi, r);
}
hi = hi + 1;
// [2] While the window holds a length, look at the length in its middle.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
// [3] The check for this one length: count the whole pieces that every ribbon gives.
long pieces = 0;
for (int r : ribbons) {
pieces += r / mid;
}
if (pieces >= k) {
// [4] Enough pieces, so every shorter length gives enough too. lo moves past mid.
lo = mid + 1;
} else {
// [5] Too few pieces, so every longer length gives too few. mid may be the first length that fails, so keep it: hi moves to it.
hi = mid;
}
}
// [6] The window is empty: lo is the first length that fails, so lo - 1 is the longest length that works. It is 0 when none works.
return lo - 1;
}int longestPiece(vector<int>& ribbons, long long k) {
// [1] The candidates are the whole lengths 1 to the longest ribbon. One more than that gives no piece at all, so it fails when k is at least 1: hi starts there.
int lo = 1;
int hi = 0;
for (int r : ribbons) {
hi = max(hi, r);
}
hi = hi + 1;
// [2] While the window holds a length, look at the length in its middle.
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
// [3] The check for this one length: count the whole pieces that every ribbon gives.
long long pieces = 0;
for (int r : ribbons) {
pieces += r / mid;
}
if (pieces >= k) {
// [4] Enough pieces, so every shorter length gives enough too. lo moves past mid.
lo = mid + 1;
} else {
// [5] Too few pieces, so every longer length gives too few. mid may be the first length that fails, so keep it: hi moves to it.
hi = mid;
}
}
// [6] The window is empty: lo is the first length that fails, so lo - 1 is the longest length that works. It is 0 when none works.
return lo - 1;
}function longestPiece(ribbons, k) {
// [1] The candidates are the whole lengths 1 to the longest ribbon. One more than that gives no piece at all, so it fails when k is at least 1: hi starts there.
let lo = 1;
let hi = 0;
for (const r of ribbons) {
hi = Math.max(hi, r);
}
hi = hi + 1;
// [2] While the window holds a length, look at the length in its middle.
while (lo < hi) {
const mid = lo + Math.floor((hi - lo) / 2);
// [3] The check for this one length: count the whole pieces that every ribbon gives.
let pieces = 0;
for (const r of ribbons) {
pieces += Math.floor(r / mid);
}
if (pieces >= k) {
// [4] Enough pieces, so every shorter length gives enough too. lo moves past mid.
lo = mid + 1;
} else {
// [5] Too few pieces, so every longer length gives too few. mid may be the first length that fails, so keep it: hi moves to it.
hi = mid;
}
}
// [6] The window is empty: lo is the first length that fails, so lo - 1 is the longest length that works. It is 0 when none works.
return lo - 1;
}This version computes the hours of a file as s // r and forgets to round up. It thinks that a file of 14 blocks takes 0 hours at rate 15.
This version splits loads into runs, and starts the search at lo = 1. A load heavier than L gets a run of its own, as if it fitted, so impossible values of L pass the check.
This version, in Java, writes int hi = sum of the loads for the split search. The sum of two loads of 1,500,000,000 is 3,000,000,000, which is above 2,147,483,647, so it wraps to -1,294,967,296.
This version searches for the largest length with the first-true template, and tests "at least k pieces" as the condition. That condition is true for small lengths and false for large ones, so it is true and then false.
Your team lead asks for the slowest rate at which the files take exactly 9 hours, and not at most 9. The files are [10, 10, 10]. The hours are 30, 15, 12, 9, 6, 6, 6, 6, 6 and 3 for the rates 1 to 10. Only the rate 4 takes exactly 9 hours.
systemdesignmastery.dev/dsa/cheatsheets/search-on-the-answer