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 an integer array nums and three integers k, op1, and op2.
You can perform the following operations on nums:
i and divide nums[i] by 2, rounding up to the nearest whole number. You can perform this operation at most op1 times, and not more than once per index.i and subtract k from nums[i], but only if nums[i] is greater than or equal to k. You can perform this operation at most op2 times, and not more than once per index.Note: Both operations can be applied to the same index, but at most once each.
Return the minimum possible sum of all elements in nums after performing any number of operations.
Example 1:
Input: nums = [2,8,3,19,3], k = 3, op1 = 1, op2 = 1
Output: 23
Explanation:
nums[1] = 8, making nums[1] = 5.nums[3] = 19, making nums[3] = 10.[2, 5, 3, 10, 3], which has the minimum possible sum of 23 after applying the operations.Example 2:
Input: nums = [2,4,3], k = 3, op1 = 2, op2 = 1
Output: 3
Explanation:
nums[0] = 2, making nums[0] = 1.nums[1] = 4, making nums[1] = 2.nums[2] = 3, making nums[2] = 0.[1, 2, 0], which has the minimum possible sum of 3 after applying the operations.Constraints:
1 <= nums.length <= 1000 <= nums[i] <= 1050 <= k <= 1050 <= op1, op2 <= nums.lengthProblem summary: You are given an integer array nums and three integers k, op1, and op2. You can perform the following operations on nums: Operation 1: Choose an index i and divide nums[i] by 2, rounding up to the nearest whole number. You can perform this operation at most op1 times, and not more than once per index. Operation 2: Choose an index i and subtract k from nums[i], but only if nums[i] is greater than or equal to k. You can perform this operation at most op2 times, and not more than once per index. Note: Both operations can be applied to the same index, but at most once each. Return the minimum possible sum of all elements in nums after performing any number of operations.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[2,8,3,19,3] 3 1 1
[2,4,3] 3 2 1
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3366: Minimum Array Sum
class Solution {
public int minArraySum(int[] nums, int d, int op1, int op2) {
int n = nums.length;
int[][][] f = new int[n + 1][op1 + 1][op2 + 1];
final int inf = 1 << 29;
for (var g : f) {
for (var h : g) {
Arrays.fill(h, inf);
}
}
f[0][0][0] = 0;
for (int i = 1; i <= n; ++i) {
int x = nums[i - 1];
for (int j = 0; j <= op1; ++j) {
for (int k = 0; k <= op2; ++k) {
f[i][j][k] = f[i - 1][j][k] + x;
if (j > 0) {
f[i][j][k] = Math.min(f[i][j][k], f[i - 1][j - 1][k] + (x + 1) / 2);
}
if (k > 0 && x >= d) {
f[i][j][k] = Math.min(f[i][j][k], f[i - 1][j][k - 1] + (x - d));
}
if (j > 0 && k > 0) {
int y = (x + 1) / 2;
if (y >= d) {
f[i][j][k] = Math.min(f[i][j][k], f[i - 1][j - 1][k - 1] + (y - d));
}
if (x >= d) {
f[i][j][k]
= Math.min(f[i][j][k], f[i - 1][j - 1][k - 1] + (x - d + 1) / 2);
}
}
}
}
}
int ans = inf;
for (int j = 0; j <= op1; ++j) {
for (int k = 0; k <= op2; ++k) {
ans = Math.min(ans, f[n][j][k]);
}
}
return ans;
}
}
// Accepted solution for LeetCode #3366: Minimum Array Sum
func minArraySum(nums []int, d int, op1 int, op2 int) int {
n := len(nums)
const inf = int(1e9)
f := make([][][]int, n+1)
for i := range f {
f[i] = make([][]int, op1+1)
for j := range f[i] {
f[i][j] = make([]int, op2+1)
for k := range f[i][j] {
f[i][j][k] = inf
}
}
}
f[0][0][0] = 0
for i := 1; i <= n; i++ {
x := nums[i-1]
for j := 0; j <= op1; j++ {
for k := 0; k <= op2; k++ {
f[i][j][k] = f[i-1][j][k] + x
if j > 0 {
f[i][j][k] = min(f[i][j][k], f[i-1][j-1][k]+(x+1)/2)
}
if k > 0 && x >= d {
f[i][j][k] = min(f[i][j][k], f[i-1][j][k-1]+(x-d))
}
if j > 0 && k > 0 {
y := (x + 1) / 2
if y >= d {
f[i][j][k] = min(f[i][j][k], f[i-1][j-1][k-1]+(y-d))
}
if x >= d {
f[i][j][k] = min(f[i][j][k], f[i-1][j-1][k-1]+(x-d+1)/2)
}
}
}
}
}
ans := inf
for j := 0; j <= op1; j++ {
for k := 0; k <= op2; k++ {
ans = min(ans, f[n][j][k])
}
}
return ans
}
# Accepted solution for LeetCode #3366: Minimum Array Sum
class Solution:
def minArraySum(self, nums: List[int], d: int, op1: int, op2: int) -> int:
n = len(nums)
f = [[[inf] * (op2 + 1) for _ in range(op1 + 1)] for _ in range(n + 1)]
f[0][0][0] = 0
for i, x in enumerate(nums, 1):
for j in range(op1 + 1):
for k in range(op2 + 1):
f[i][j][k] = f[i - 1][j][k] + x
if j > 0:
f[i][j][k] = min(f[i][j][k], f[i - 1][j - 1][k] + (x + 1) // 2)
if k > 0 and x >= d:
f[i][j][k] = min(f[i][j][k], f[i - 1][j][k - 1] + (x - d))
if j > 0 and k > 0:
y = (x + 1) // 2
if y >= d:
f[i][j][k] = min(f[i][j][k], f[i - 1][j - 1][k - 1] + y - d)
if x >= d:
f[i][j][k] = min(
f[i][j][k], f[i - 1][j - 1][k - 1] + (x - d + 1) // 2
)
ans = inf
for j in range(op1 + 1):
for k in range(op2 + 1):
ans = min(ans, f[n][j][k])
return ans
// Accepted solution for LeetCode #3366: Minimum Array Sum
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3366: Minimum Array Sum
// class Solution {
// public int minArraySum(int[] nums, int d, int op1, int op2) {
// int n = nums.length;
// int[][][] f = new int[n + 1][op1 + 1][op2 + 1];
// final int inf = 1 << 29;
// for (var g : f) {
// for (var h : g) {
// Arrays.fill(h, inf);
// }
// }
// f[0][0][0] = 0;
// for (int i = 1; i <= n; ++i) {
// int x = nums[i - 1];
// for (int j = 0; j <= op1; ++j) {
// for (int k = 0; k <= op2; ++k) {
// f[i][j][k] = f[i - 1][j][k] + x;
// if (j > 0) {
// f[i][j][k] = Math.min(f[i][j][k], f[i - 1][j - 1][k] + (x + 1) / 2);
// }
// if (k > 0 && x >= d) {
// f[i][j][k] = Math.min(f[i][j][k], f[i - 1][j][k - 1] + (x - d));
// }
// if (j > 0 && k > 0) {
// int y = (x + 1) / 2;
// if (y >= d) {
// f[i][j][k] = Math.min(f[i][j][k], f[i - 1][j - 1][k - 1] + (y - d));
// }
// if (x >= d) {
// f[i][j][k]
// = Math.min(f[i][j][k], f[i - 1][j - 1][k - 1] + (x - d + 1) / 2);
// }
// }
// }
// }
// }
// int ans = inf;
// for (int j = 0; j <= op1; ++j) {
// for (int k = 0; k <= op2; ++k) {
// ans = Math.min(ans, f[n][j][k]);
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3366: Minimum Array Sum
function minArraySum(nums: number[], d: number, op1: number, op2: number): number {
const n = nums.length;
const inf = Number.MAX_SAFE_INTEGER;
const f: number[][][] = Array.from({ length: n + 1 }, () =>
Array.from({ length: op1 + 1 }, () => Array(op2 + 1).fill(inf)),
);
f[0][0][0] = 0;
for (let i = 1; i <= n; i++) {
const x = nums[i - 1];
for (let j = 0; j <= op1; j++) {
for (let k = 0; k <= op2; k++) {
f[i][j][k] = Math.min(f[i][j][k], f[i - 1][j][k] + x);
if (j > 0) {
f[i][j][k] = Math.min(f[i][j][k], f[i - 1][j - 1][k] + Math.floor((x + 1) / 2));
}
if (k > 0 && x >= d) {
f[i][j][k] = Math.min(f[i][j][k], f[i - 1][j][k - 1] + (x - d));
}
if (j > 0 && k > 0) {
const y = Math.floor((x + 1) / 2);
if (y >= d) {
f[i][j][k] = Math.min(f[i][j][k], f[i - 1][j - 1][k - 1] + (y - d));
}
if (x >= d) {
f[i][j][k] = Math.min(
f[i][j][k],
f[i - 1][j - 1][k - 1] + Math.floor((x - d + 1) / 2),
);
}
}
}
}
}
let ans = inf;
for (let j = 0; j <= op1; j++) {
for (let l = 0; l <= op2; l++) {
ans = Math.min(ans, f[n][j][l]);
}
}
return ans;
}
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.