Split a problem, solve the halves and combine the answers.
A divide-and-conquer call splits its problem into smaller problems of the same kind, solves each, and combines the answers, including anything that crosses the split.
Use it when a problem on n items splits into smaller problems of the same kind, and the answers combine cheaply. Signal words: split in half, solve each half, the answer may cross the middle.
Split: choose a middle, or a cut chosen by the data. Make each part smaller.
Solve: call the same function on each part. Solve a part once and reuse the answer.
Combine: join the answers, and cover what crosses the split.
Cost: draw the tree, and add up the levels.
def fast_power(base, exp, mod):
# [1] Base case: any base to the power 0 is 1, and a call on 0 makes no further call.
if exp == 0:
return 1 % mod
# [2] Split: solve the half of the exponent once, and keep the answer.
half = fast_power(base, exp // 2, mod)
# [3] Combine: two equal halves make the even part. Reduce by mod so the product fits in 64 bits.
result = half * half % mod
# [4] An odd exponent has one more factor of the base than twice the half.
if exp % 2 == 1:
result = result * base % mod
return resultpublic long fastPower(long base, long exp, long mod) {
// [1] Base case: any base to the power 0 is 1, and a call on 0 makes no further call.
if (exp == 0) {
return 1 % mod;
}
// [2] Split: solve the half of the exponent once, and keep the answer.
long half = fastPower(base, exp / 2, mod);
// [3] Combine: two equal halves make the even part. Reduce by mod so the product fits in 64 bits.
long result = half * half % mod;
// [4] An odd exponent has one more factor of the base than twice the half.
if (exp % 2 == 1) {
result = result * base % mod;
}
return result;
}long long fastPower(long long base, long long exp, long long mod) {
// [1] Base case: any base to the power 0 is 1, and a call on 0 makes no further call.
if (exp == 0) {
return 1 % mod;
}
// [2] Split: solve the half of the exponent once, and keep the answer.
long long half = fastPower(base, exp / 2, mod);
// [3] Combine: two equal halves make the even part. Reduce by mod so the product fits in 64 bits.
long long result = half * half % mod;
// [4] An odd exponent has one more factor of the base than twice the half.
if (exp % 2 == 1) {
result = result * base % mod;
}
return result;
}// Numbers are exact up to 2^53, so use BigInt when mod is above 94,000,000.
function fastPower(base, exp, mod) {
// [1] Base case: any base to the power 0 is 1, and a call on 0 makes no further call.
if (exp === 0) {
return 1 % mod;
}
// [2] Split: solve the half of the exponent once, and keep the answer.
const half = fastPower(base, Math.floor(exp / 2), mod);
// [3] Combine: two equal halves make the even part. Reduce by mod so the product fits in 64 bits.
let result = half * half % mod;
// [4] An odd exponent has one more factor of the base than twice the half.
if (exp % 2 === 1) {
result = result * base % mod;
}
return result;
}def best_run(values):
return summarise(values, 0, len(values))[3]
def summarise(values, lo, hi):
# [1] Base case: one day is a run by itself, so its total, prefix, suffix and best run all equal it.
if hi - lo == 1:
return [values[lo], values[lo], values[lo], values[lo]]
# [2] Split: solve the left half and the right half.
mid = (lo + hi) // 2
left = summarise(values, lo, mid)
right = summarise(values, mid, hi)
# [3] Combine: a run across the middle is the best suffix of the left plus the best prefix of the right.
total = left[0] + right[0]
prefix = max(left[1], left[0] + right[1])
suffix = max(right[2], right[0] + left[2])
crossing = left[2] + right[1]
# [4] The best run of the range is the best of the left, the right and the crossing run.
return [total, prefix, suffix, max(left[3], right[3], crossing)]public long bestRun(int[] values) {
return summarise(values, 0, values.length)[3];
}
private long[] summarise(int[] values, int lo, int hi) {
// [1] Base case: one day is a run by itself, so its total, prefix, suffix and best run all equal it.
if (hi - lo == 1) {
return new long[] {values[lo], values[lo], values[lo], values[lo]};
}
// [2] Split: solve the left half and the right half.
int mid = (lo + hi) / 2;
long[] left = summarise(values, lo, mid);
long[] right = summarise(values, mid, hi);
// [3] Combine: a run across the middle is the best suffix of the left plus the best prefix of the right.
long total = left[0] + right[0];
long prefix = Math.max(left[1], left[0] + right[1]);
long suffix = Math.max(right[2], right[0] + left[2]);
long crossing = left[2] + right[1];
// [4] The best run of the range is the best of the left, the right and the crossing run.
return new long[] {total, prefix, suffix, Math.max(Math.max(left[3], right[3]), crossing)};
}vector<long long> summarise(const vector<int>& values, int lo, int hi) {
// [1] Base case: one day is a run by itself, so its total, prefix, suffix and best run all equal it.
if (hi - lo == 1) {
return {values[lo], values[lo], values[lo], values[lo]};
}
// [2] Split: solve the left half and the right half.
int mid = (lo + hi) / 2;
vector<long long> left = summarise(values, lo, mid);
vector<long long> right = summarise(values, mid, hi);
// [3] Combine: a run across the middle is the best suffix of the left plus the best prefix of the right.
long long total = left[0] + right[0];
long long prefix = max(left[1], left[0] + right[1]);
long long suffix = max(right[2], right[0] + left[2]);
long long crossing = left[2] + right[1];
// [4] The best run of the range is the best of the left, the right and the crossing run.
return {total, prefix, suffix, max(max(left[3], right[3]), crossing)};
}
long long bestRun(const vector<int>& values) {
return summarise(values, 0, (int)values.size())[3];
}function summarise(values, lo, hi) {
// [1] Base case: one day is a run by itself, so its total, prefix, suffix and best run all equal it.
if (hi - lo === 1) {
return [values[lo], values[lo], values[lo], values[lo]];
}
// [2] Split: solve the left half and the right half.
const mid = Math.floor((lo + hi) / 2);
const left = summarise(values, lo, mid);
const right = summarise(values, mid, hi);
// [3] Combine: a run across the middle is the best suffix of the left plus the best prefix of the right.
const total = left[0] + right[0];
const prefix = Math.max(left[1], left[0] + right[1]);
const suffix = Math.max(right[2], right[0] + left[2]);
const crossing = left[2] + right[1];
// [4] The best run of the range is the best of the left, the right and the crossing run.
return [total, prefix, suffix, Math.max(left[3], right[3], crossing)];
}
function bestRun(values) {
return summarise(values, 0, values.length)[3];
}This version writes fast_power(half) * fast_power(half). The answer is correct, but each level makes two calls where one was enough.
This version returns the best of the left half and the best of the right half.
This version returns 0 for a single day, as if an empty run were allowed.
Summing 1,000 numbers by halves works, but it gains nothing. The recursion makes 1,999 calls and 999 additions. A loop makes 999 additions and no calls.
systemdesignmastery.dev/dsa/cheatsheets/divide-and-conquer