The Minimum Cost Homecoming of a Robot in a Grid problem places a robot at startPos and a home at homePos in a 2D grid. Every time the robot moves into a new row or column, there is a cost associated with that specific row or column. You are given two arrays, rowCosts and colCosts. You need to find the minimum total cost for the robot to reach home.
This is a "trick" Minimum Cost Homecoming interview question seen at Goldman Sachs and Oracle. It looks like a shortest-path problem (like Dijkstra or BFS), but it's actually a Greedy problem. It tests if a candidate can realize that the total cost is independent of the order of moves, as long as you don't move in the wrong direction.
The Greedy interview pattern is the intended solution.
r1 to r2, you must enter every row in between exactly once (if you move optimally).rowCosts for all rows between startPos[0] and homePos[0] (excluding the start row) plus the sum of colCosts for all columns between startPos[1] and homePos[1].Start: (1, 0), Home: (2, 3).
Row costs: [10, 20, 30]. Col costs: [1, 2, 3, 4].
30 + 2 + 3 + 4 = 39.
Any zigzag path will still visit these same rows and columns.homePos is "above" or "to the left" of startPos.Don't assume every grid problem requires BFS or DP. Read the cost structure carefully. If the cost depends only on the destination row/column and not the transition, it's likely a Greedy or Mathematical coding problem.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Gas Station | Medium | Solve | |
| Decrease Elements To Make Array Zigzag | Medium | Solve | |
| Increasing Triplet Subsequence | Medium | Solve | |
| Largest Element in an Array after Merge Operations | Medium | Solve | |
| Maximize Score After Pair Deletions | Medium | Solve |