Search over real numbers to a precision, and over sorted matrices.
Every rate below lo fails the check and every rate above hi passes it, so the lowest rate that passes always lies between lo and hi.
Use it when the answer is a real number, and you can test one candidate. A larger candidate must never hurt, or a smaller one must never hurt. Signal words: the lowest rate, the length to six places, the root.
Start: lo is a value that cannot be the answer, or the smallest allowed value. hi is a value that surely passes the check.
Step: repeat 100 times. Set mid = (lo + hi) / 2 and run the check on mid. If it passes, set hi = mid. Otherwise set lo = mid.
Stop: after the rounds. Scale the answer to the precision that the problem asks for, and round in the direction that the problem needs.
It does not work when the check passes, fails and passes again. And it cannot give an exact answer, only one inside the precision that doubles hold.
Cost: rounds × the cost of one check. For 100 rounds and a check of O(months), that is O(100 × months).
m + n - 1 steps. For a grid of 1,000 × 1,000 that is at most 1,999 steps, against 1,000,000 entries.3r + 5c. Its 250,000th smallest entry needs 13 checks of at most 1,999 steps, found by searching the answer values.import math
def needed_rate(deposit, months, target):
# [1] The rate lies between 0 and 0.05 a month. The plans offer at most 5 percent a month, and the target is within reach at that rate, so hi works.
lo = 0.0
hi = 0.05
# [2] Halve the interval a fixed 100 times. That is far more than doubles can use, and it needs no tolerance.
for _ in range(100):
mid = (lo + hi) / 2
# [3] The check for this one rate: grow a balance month by month, interest on the balance first and then the deposit.
balance = 0.0
for _ in range(months):
balance = balance * (1 + mid) + deposit
if balance >= target:
# [4] The rate works, so every higher rate works too. It may be the lowest, so keep it: hi moves to it.
hi = mid
else:
# [5] The rate is too low, so every lower rate is too. lo moves to it: rates are not whole numbers, so there is no + 1.
lo = mid
# [6] lo is just below the lowest working rate. Round it up to a whole millionth, the first rate that surely works.
return math.ceil(lo * 1000000)public long neededRate(int deposit, int months, long target) {
// [1] The rate lies between 0 and 0.05 a month. The plans offer at most 5 percent a month, and the target is within reach at that rate, so hi works.
double lo = 0.0;
double hi = 0.05;
// [2] Halve the interval a fixed 100 times. That is far more than doubles can use, and it needs no tolerance.
for (int round = 0; round < 100; round++) {
double mid = (lo + hi) / 2;
// [3] The check for this one rate: grow a balance month by month, interest on the balance first and then the deposit.
double balance = 0.0;
for (int month = 0; month < months; month++) {
balance = balance * (1 + mid) + deposit;
}
if (balance >= target) {
// [4] The rate works, so every higher rate works too. It may be the lowest, so keep it: hi moves to it.
hi = mid;
} else {
// [5] The rate is too low, so every lower rate is too. lo moves to it: rates are not whole numbers, so there is no + 1.
lo = mid;
}
}
// [6] lo is just below the lowest working rate. Round it up to a whole millionth, the first rate that surely works.
return (long) Math.ceil(lo * 1000000);
}long long neededRate(int deposit, int months, long long target) {
// [1] The rate lies between 0 and 0.05 a month. The plans offer at most 5 percent a month, and the target is within reach at that rate, so hi works.
double lo = 0.0;
double hi = 0.05;
// [2] Halve the interval a fixed 100 times. That is far more than doubles can use, and it needs no tolerance.
for (int round = 0; round < 100; round++) {
double mid = (lo + hi) / 2;
// [3] The check for this one rate: grow a balance month by month, interest on the balance first and then the deposit.
double balance = 0.0;
for (int month = 0; month < months; month++) {
balance = balance * (1 + mid) + deposit;
}
if (balance >= (double)target) {
// [4] The rate works, so every higher rate works too. It may be the lowest, so keep it: hi moves to it.
hi = mid;
} else {
// [5] The rate is too low, so every lower rate is too. lo moves to it: rates are not whole numbers, so there is no + 1.
lo = mid;
}
}
// [6] lo is just below the lowest working rate. Round it up to a whole millionth, the first rate that surely works.
return (long long)ceil(lo * 1000000);
}function neededRate(deposit, months, target) {
// [1] The rate lies between 0 and 0.05 a month. The plans offer at most 5 percent a month, and the target is within reach at that rate, so hi works.
let lo = 0.0;
let hi = 0.05;
// [2] Halve the interval a fixed 100 times. That is far more than doubles can use, and it needs no tolerance.
for (let round = 0; round < 100; round++) {
const mid = (lo + hi) / 2;
// [3] The check for this one rate: grow a balance month by month, interest on the balance first and then the deposit.
let balance = 0.0;
for (let month = 0; month < months; month++) {
balance = balance * (1 + mid) + deposit;
}
if (balance >= target) {
// [4] The rate works, so every higher rate works too. It may be the lowest, so keep it: hi moves to it.
hi = mid;
} else {
// [5] The rate is too low, so every lower rate is too. lo moves to it: rates are not whole numbers, so there is no + 1.
lo = mid;
}
}
// [6] lo is just below the lowest working rate. Round it up to a whole millionth, the first rate that surely works.
return Math.ceil(lo * 1000000);
}def count_at_most(grid, limit):
m = len(grid)
n = len(grid[0])
# [1] Start in the top-right corner. There the row grows to the left and the column grows downwards.
row = 0
col = n - 1
count = 0
# [2] While the corner is still inside the grid, look at its entry.
while row < m and col >= 0:
if grid[row][col] <= limit:
# [3] The entry fits, so everything to its left in this row fits too. Add them all, and go down a row.
count += col + 1
row += 1
else:
# [4] The entry is too big, so everything below it in this column is too big. Go left a column.
col -= 1
# [5] The corner has left the grid: every entry at most the limit has been counted.
return countpublic long countAtMost(int[][] grid, int limit) {
int m = grid.length;
int n = grid[0].length;
// [1] Start in the top-right corner. There the row grows to the left and the column grows downwards.
int row = 0;
int col = n - 1;
long count = 0;
// [2] While the corner is still inside the grid, look at its entry.
while (row < m && col >= 0) {
if (grid[row][col] <= limit) {
// [3] The entry fits, so everything to its left in this row fits too. Add them all, and go down a row.
count += col + 1;
row++;
} else {
// [4] The entry is too big, so everything below it in this column is too big. Go left a column.
col--;
}
}
// [5] The corner has left the grid: every entry at most the limit has been counted.
return count;
}long long countAtMost(vector<vector<int>>& grid, int limit) {
int m = (int)grid.size();
int n = (int)grid[0].size();
// [1] Start in the top-right corner. There the row grows to the left and the column grows downwards.
int row = 0;
int col = n - 1;
long long count = 0;
// [2] While the corner is still inside the grid, look at its entry.
while (row < m && col >= 0) {
if (grid[row][col] <= limit) {
// [3] The entry fits, so everything to its left in this row fits too. Add them all, and go down a row.
count += col + 1;
row++;
} else {
// [4] The entry is too big, so everything below it in this column is too big. Go left a column.
col--;
}
}
// [5] The corner has left the grid: every entry at most the limit has been counted.
return count;
}function countAtMost(grid, limit) {
const m = grid.length;
const n = grid[0].length;
// [1] Start in the top-right corner. There the row grows to the left and the column grows downwards.
let row = 0;
let col = n - 1;
let count = 0;
// [2] While the corner is still inside the grid, look at its entry.
while (row < m && col >= 0) {
if (grid[row][col] <= limit) {
// [3] The entry fits, so everything to its left in this row fits too. Add them all, and go down a row.
count += col + 1;
row++;
} else {
// [4] The entry is too big, so everything below it in this column is too big. Go left a column.
col--;
}
}
// [5] The corner has left the grid: every entry at most the limit has been counted.
return count;
}This version loops while hi - lo > 0.000001, because the answer is wanted to a millionth. It returns after 16 rounds, when lo is 12361.9 millionths.
This version loops while hi - lo > 1e-18. Near the rate 0.0124, neighbouring doubles are 1.7 × 10^-18 apart, so hi - lo can never get below that gap.
This version copies the integer template and sets lo = mid + 1 when the check fails. For a real rate, adding 1 jumps past the whole range.
This version rounds up hi and not lo. The value hi never equals 0, because it is always a middle point above lo.
This version reads the grid in reading order and runs one binary search. That is valid only when each row starts above the end of the row before it.
Your team lead asks for a rate whose balance is within 1 unit of 4,500, and not at least 4,500. The balance is in that band only for the rates 12351 to 12374 millionths. Everywhere else the check is false.
systemdesignmastery.dev/dsa/cheatsheets/real-values-and-2d