Minimum Cost to Move Within a Grid (Akuna Shang Hai π)
Learn this problemProblem statement
A player stands on a cell within a grid. The player can move to one of four adjacent cells, but the motion is constrained by lasers. To move from one position to another involves a cost: the cost to move from row i to row i Β± 1 is costRows[i] and the cost to move from column j to column j Β± 1 is costCols[j]. Find the minimum cost to move from a starting point to an ending point within the grid.
Function
minCost(rows: int, cols: int, initR: int, initC: int, finalR: int, finalC: int, costRows: int[], costCols: int[]) β int
Complete the function minCost in the editor below.
minCost has the following parameters:
int rows: the number of rows in the gridint cols: the number of columns in the gridint initR: the player's starting rowint initC: the player's starting columnint finalR: the goal's rowint finalC: the goal's columnint costRows[n]: eachcostRows[i]denotes the cost to move between rows i and i + 1.int costCols[m]: eachcostCols[j]denotes the cost to move between columns j and j + 1.
Returns
int: the minimum cost to move from the starting position to the goal
Examples
Example 1
rows = 3cols = 3initR = 0initC = 0finalR = 1finalC = 2costRows = [2, 5]costCols = [6, 1]return = 9Moving from row 0 to row 1 crosses boundary costRows[0] = 2. Moving from column 0 to column 2 crosses boundaries costing 6 and 1. The total is 2 + 6 + 1 = 9.
Example 2
rows = 4cols = 4initR = 1initC = 2finalR = 3finalC = 3costRows = [1, 2, 3]costCols = [7, 8, 9]return = 14Moving from row 1 to row 3 costs costRows[1] + costRows[2] = 2 + 3 = 5. Moving from column 2 to column 3 costs costCols[2] = 9. The total is 14.
Constraints
1 β€ rows, cols β€ 1050 β€ initR, finalR < rows0 β€ initC, finalC < cols0 β€ costRows[i] β€ 104 (0 β€ i < rows-1)0 β€ costCols[j] β€ 104 (0 β€ j < cols-1)
More Akuna Capital problems
- Binary CircuitSeen Jul 2026
- Minimize Malware Spread by Removing a NodeOA Β· Seen Jul 2026
- Sort Array by FrequencyOA Β· Seen Jul 2026
- Array Challenge (QR Intern)OA Β· Seen Jul 2026
- Communications HandlerOA Β· Seen Jul 2026
- K Smallest SubstringOA Β· Seen Jul 2026
- Maximum K-Star SumOA Β· Seen Jul 2026
- Delivery Management SystemOA Β· Seen Jul 2026