Code RoomMinimum cost grid path
HardPrep Room Coding #1490

Minimum cost grid path

CodingAlgorithms & data structuresSenior–Staff~35 min

Each cell of an m x n grid contains an arrow (1=right, 2=left, 3=down, 4=up) giving the default outgoing direction. Starting at the top-left cell you follow arrows for free, but you may change the sign of any single cell once for a cost of 1 (then it keeps that new sign). Return the minimum total cost of modifications needed so that there exists a valid path from the top-left cell to the bottom-right cell.

Implement
min_cost_grid_path(grid: list[list[int]]) → int
Examples
in[[[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]]out3
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 35 min
InputExpectedGot
[[[1,1,1,1],[2,2,2,2],[1,1,1,1],[2,2,2,2]]]3not run yetsample