Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given two integer arrays energyDrinkA and energyDrinkB of the same length n by a futuristic sports scientist. These arrays represent the energy boosts per hour provided by two different energy drinks, A and B, respectively.
You want to maximize your total energy boost by drinking one energy drink per hour. However, if you want to switch from consuming one energy drink to the other, you need to wait for one hour to cleanse your system (meaning you won't get any energy boost in that hour).
Return the maximum total energy boost you can gain in the next n hours.
Note that you can start consuming either of the two energy drinks.
Example 1:
Input: energyDrinkA = [1,3,1], energyDrinkB = [3,1,1]
Output: 5
Explanation:
To gain an energy boost of 5, drink only the energy drink A (or only B).
Example 2:
Input: energyDrinkA = [4,1,1], energyDrinkB = [1,1,3]
Output: 7
Explanation:
To gain an energy boost of 7:
Constraints:
n == energyDrinkA.length == energyDrinkB.length3 <= n <= 1051 <= energyDrinkA[i], energyDrinkB[i] <= 105Problem summary: You are given two integer arrays energyDrinkA and energyDrinkB of the same length n by a futuristic sports scientist. These arrays represent the energy boosts per hour provided by two different energy drinks, A and B, respectively. You want to maximize your total energy boost by drinking one energy drink per hour. However, if you want to switch from consuming one energy drink to the other, you need to wait for one hour to cleanse your system (meaning you won't get any energy boost in that hour). Return the maximum total energy boost you can gain in the next n hours. Note that you can start consuming either of the two energy drinks.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[1,3,1] [3,1,1]
[4,1,1] [1,1,3]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3259: Maximum Energy Boost From Two Drinks
class Solution {
public long maxEnergyBoost(int[] energyDrinkA, int[] energyDrinkB) {
int n = energyDrinkA.length;
long[][] f = new long[n][2];
f[0][0] = energyDrinkA[0];
f[0][1] = energyDrinkB[0];
for (int i = 1; i < n; ++i) {
f[i][0] = Math.max(f[i - 1][0] + energyDrinkA[i], f[i - 1][1]);
f[i][1] = Math.max(f[i - 1][1] + energyDrinkB[i], f[i - 1][0]);
}
return Math.max(f[n - 1][0], f[n - 1][1]);
}
}
// Accepted solution for LeetCode #3259: Maximum Energy Boost From Two Drinks
func maxEnergyBoost(energyDrinkA []int, energyDrinkB []int) int64 {
n := len(energyDrinkA)
f := make([][2]int64, n)
f[0][0] = int64(energyDrinkA[0])
f[0][1] = int64(energyDrinkB[0])
for i := 1; i < n; i++ {
f[i][0] = max(f[i-1][0]+int64(energyDrinkA[i]), f[i-1][1])
f[i][1] = max(f[i-1][1]+int64(energyDrinkB[i]), f[i-1][0])
}
return max(f[n-1][0], f[n-1][1])
}
# Accepted solution for LeetCode #3259: Maximum Energy Boost From Two Drinks
class Solution:
def maxEnergyBoost(self, energyDrinkA: List[int], energyDrinkB: List[int]) -> int:
n = len(energyDrinkA)
f = [[0] * 2 for _ in range(n)]
f[0][0] = energyDrinkA[0]
f[0][1] = energyDrinkB[0]
for i in range(1, n):
f[i][0] = max(f[i - 1][0] + energyDrinkA[i], f[i - 1][1])
f[i][1] = max(f[i - 1][1] + energyDrinkB[i], f[i - 1][0])
return max(f[n - 1])
// Accepted solution for LeetCode #3259: Maximum Energy Boost From Two Drinks
/**
* [3259] Maximum Energy Boost From Two Drinks
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn max_energy_boost(energy_drink_a: Vec<i32>, energy_drink_b: Vec<i32>) -> i64 {
let n = energy_drink_a.len();
let mut dp_drink_a = vec![0; n];
let mut dp_drink_b = vec![0; n];
for i in 0..n {
if i >= 2 {
dp_drink_a[i] = dp_drink_a[i - 1].max(dp_drink_b[i - 2]) + energy_drink_a[i] as i64;
dp_drink_b[i] = dp_drink_b[i - 1].max(dp_drink_a[i - 2]) + energy_drink_b[i] as i64;
} else if i >= 1 {
dp_drink_a[i] = dp_drink_a[i - 1] + energy_drink_a[i] as i64;
dp_drink_b[i] = dp_drink_b[i - 1] + energy_drink_b[i] as i64;
} else {
dp_drink_a[i] = energy_drink_a[i] as i64;
dp_drink_b[i] = energy_drink_b[i] as i64;
}
}
dp_drink_a[n - 1].max(dp_drink_b[n - 1])
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_3259() {
assert_eq!(5, Solution::max_energy_boost(vec![1, 3, 1], vec![3, 1, 1]));
assert_eq!(7, Solution::max_energy_boost(vec![4, 1, 1], vec![1, 1, 3]));
}
}
// Accepted solution for LeetCode #3259: Maximum Energy Boost From Two Drinks
function maxEnergyBoost(energyDrinkA: number[], energyDrinkB: number[]): number {
const n = energyDrinkA.length;
const f: number[][] = Array.from({ length: n }, () => [0, 0]);
f[0][0] = energyDrinkA[0];
f[0][1] = energyDrinkB[0];
for (let i = 1; i < n; i++) {
f[i][0] = Math.max(f[i - 1][0] + energyDrinkA[i], f[i - 1][1]);
f[i][1] = Math.max(f[i - 1][1] + energyDrinkB[i], f[i - 1][0]);
}
return Math.max(...f[n - 1]!);
}
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.