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.
There is an m x n grid, where (0, 0) is the top-left cell and (m - 1, n - 1) is the bottom-right cell. You are given an integer array startPos where startPos = [startrow, startcol] indicates that initially, a robot is at the cell (startrow, startcol). You are also given an integer array homePos where homePos = [homerow, homecol] indicates that its home is at the cell (homerow, homecol).
The robot needs to go to its home. It can move one cell in four directions: left, right, up, or down, and it can not move outside the boundary. Every move incurs some cost. You are further given two 0-indexed integer arrays: rowCosts of length m and colCosts of length n.
r, then this move costs rowCosts[r].c, then this move costs colCosts[c].Return the minimum total cost for this robot to return home.
Example 1:
Input: startPos = [1, 0], homePos = [2, 3], rowCosts = [5, 4, 3], colCosts = [8, 2, 6, 7] Output: 18 Explanation: One optimal path is that: Starting from (1, 0) -> It goes down to (2, 0). This move costs rowCosts[2] = 3. -> It goes right to (2, 1). This move costs colCosts[1] = 2. -> It goes right to (2, 2). This move costs colCosts[2] = 6. -> It goes right to (2, 3). This move costs colCosts[3] = 7. The total cost is 3 + 2 + 6 + 7 = 18
Example 2:
Input: startPos = [0, 0], homePos = [0, 0], rowCosts = [5], colCosts = [26] Output: 0 Explanation: The robot is already at its home. Since no moves occur, the total cost is 0.
Constraints:
m == rowCosts.lengthn == colCosts.length1 <= m, n <= 1050 <= rowCosts[r], colCosts[c] <= 104startPos.length == 2homePos.length == 20 <= startrow, homerow < m0 <= startcol, homecol < nProblem summary: There is an m x n grid, where (0, 0) is the top-left cell and (m - 1, n - 1) is the bottom-right cell. You are given an integer array startPos where startPos = [startrow, startcol] indicates that initially, a robot is at the cell (startrow, startcol). You are also given an integer array homePos where homePos = [homerow, homecol] indicates that its home is at the cell (homerow, homecol). The robot needs to go to its home. It can move one cell in four directions: left, right, up, or down, and it can not move outside the boundary. Every move incurs some cost. You are further given two 0-indexed integer arrays: rowCosts of length m and colCosts of length n. If the robot moves up or down into a cell whose row is r, then this move costs rowCosts[r]. If the robot moves left or right into a cell whose column is c, then this move costs colCosts[c]. Return the minimum total cost for this robot to
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Greedy
[1,0] [2,3] [5,4,3] [8,2,6,7]
[0,0] [0,0] [5] [26]
unique-paths)minimum-path-sum)bomb-enemy)count-square-submatrices-with-all-ones)paths-in-matrix-whose-sum-is-divisible-by-k)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2087: Minimum Cost Homecoming of a Robot in a Grid
class Solution {
public int minCost(int[] startPos, int[] homePos, int[] rowCosts, int[] colCosts) {
int i = startPos[0], j = startPos[1];
int x = homePos[0], y = homePos[1];
int ans = 0;
if (i < x) {
for (int k = i + 1; k <= x; ++k) {
ans += rowCosts[k];
}
} else {
for (int k = x; k < i; ++k) {
ans += rowCosts[k];
}
}
if (j < y) {
for (int k = j + 1; k <= y; ++k) {
ans += colCosts[k];
}
} else {
for (int k = y; k < j; ++k) {
ans += colCosts[k];
}
}
return ans;
}
}
// Accepted solution for LeetCode #2087: Minimum Cost Homecoming of a Robot in a Grid
func minCost(startPos []int, homePos []int, rowCosts []int, colCosts []int) (ans int) {
i, j := startPos[0], startPos[1]
x, y := homePos[0], homePos[1]
if i < x {
ans += sum(rowCosts, i+1, x+1)
} else {
ans += sum(rowCosts, x, i)
}
if j < y {
ans += sum(colCosts, j+1, y+1)
} else {
ans += sum(colCosts, y, j)
}
return
}
func sum(nums []int, i, j int) (s int) {
for k := i; k < j; k++ {
s += nums[k]
}
return
}
# Accepted solution for LeetCode #2087: Minimum Cost Homecoming of a Robot in a Grid
class Solution:
def minCost(
self,
startPos: List[int],
homePos: List[int],
rowCosts: List[int],
colCosts: List[int],
) -> int:
i, j = startPos
x, y = homePos
ans = 0
if i < x:
ans += sum(rowCosts[i + 1 : x + 1])
else:
ans += sum(rowCosts[x:i])
if j < y:
ans += sum(colCosts[j + 1 : y + 1])
else:
ans += sum(colCosts[y:j])
return ans
// Accepted solution for LeetCode #2087: Minimum Cost Homecoming of a Robot in a Grid
/**
* [2087] Minimum Cost Homecoming of a Robot in a Grid
*
* There is an m x n grid, where (0, 0) is the top-left cell and (m - 1, n - 1) is the bottom-right cell. You are given an integer array startPos where startPos = [startrow, startcol] indicates that initially, a robot is at the cell (startrow, startcol). You are also given an integer array homePos where homePos = [homerow, homecol] indicates that its home is at the cell (homerow, homecol).
* The robot needs to go to its home. It can move one cell in four directions: left, right, up, or down, and it can not move outside the boundary. Every move incurs some cost. You are further given two 0-indexed integer arrays: rowCosts of length m and colCosts of length n.
*
* If the robot moves up or down into a cell whose row is r, then this move costs rowCosts[r].
* If the robot moves left or right into a cell whose column is c, then this move costs colCosts[c].
*
* Return the minimum total cost for this robot to return home.
*
* Example 1:
* <img alt="" src="https://assets.leetcode.com/uploads/2021/10/11/eg-1.png" style="width: 282px; height: 217px;" />
* Input: startPos = [1, 0], homePos = [2, 3], rowCosts = [5, 4, 3], colCosts = [8, 2, 6, 7]
* Output: 18
* Explanation: One optimal path is that:
* Starting from (1, 0)
* -> It goes down to (<u>2</u>, 0). This move costs rowCosts[2] = 3.
* -> It goes right to (2, <u>1</u>). This move costs colCosts[1] = 2.
* -> It goes right to (2, <u>2</u>). This move costs colCosts[2] = 6.
* -> It goes right to (2, <u>3</u>). This move costs colCosts[3] = 7.
* The total cost is 3 + 2 + 6 + 7 = 18
* Example 2:
*
* Input: startPos = [0, 0], homePos = [0, 0], rowCosts = [5], colCosts = [26]
* Output: 0
* Explanation: The robot is already at its home. Since no moves occur, the total cost is 0.
*
*
* Constraints:
*
* m == rowCosts.length
* n == colCosts.length
* 1 <= m, n <= 10^5
* 0 <= rowCosts[r], colCosts[c] <= 10^4
* startPos.length == 2
* homePos.length == 2
* 0 <= startrow, homerow < m
* 0 <= startcol, homecol < n
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/minimum-cost-homecoming-of-a-robot-in-a-grid/
// discuss: https://leetcode.com/problems/minimum-cost-homecoming-of-a-robot-in-a-grid/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
pub fn min_cost(
start_pos: Vec<i32>,
home_pos: Vec<i32>,
row_costs: Vec<i32>,
col_costs: Vec<i32>,
) -> i32 {
0
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[ignore]
fn test_2087_example_1() {
let start_pos = vec![0, 0];
let home_pos = vec![2, 3];
let row_costs = vec![5, 4, 3];
let col_costs = vec![8, 2, 6, 7];
let result = 2;
assert_eq!(
Solution::min_cost(start_pos, home_pos, row_costs, col_costs),
result
);
}
#[test]
#[ignore]
fn test_2087_example_2() {
let start_pos = vec![1, 0];
let home_pos = vec![0, 0];
let row_costs = vec![5];
let col_costs = vec![26];
let result = 18;
assert_eq!(
Solution::min_cost(start_pos, home_pos, row_costs, col_costs),
result
);
}
}
// Accepted solution for LeetCode #2087: Minimum Cost Homecoming of a Robot in a Grid
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #2087: Minimum Cost Homecoming of a Robot in a Grid
// class Solution {
// public int minCost(int[] startPos, int[] homePos, int[] rowCosts, int[] colCosts) {
// int i = startPos[0], j = startPos[1];
// int x = homePos[0], y = homePos[1];
// int ans = 0;
// if (i < x) {
// for (int k = i + 1; k <= x; ++k) {
// ans += rowCosts[k];
// }
// } else {
// for (int k = x; k < i; ++k) {
// ans += rowCosts[k];
// }
// }
// if (j < y) {
// for (int k = j + 1; k <= y; ++k) {
// ans += colCosts[k];
// }
// } else {
// for (int k = y; k < j; ++k) {
// ans += colCosts[k];
// }
// }
// return ans;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
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: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.