Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Shortest Path in Cost Maze
00:00
5 left

Shortest Path in Cost Maze

MediumPython

Problem

Given a 2D grid maze of non-negative integers, find the minimum total cost to move from the top-left cell (0, 0) to the bottom-right cell (m-1, n-1). You may move up, down, left, or right. Entering a cell adds that cell’s cost to the path total, including the starting and ending cells. Return -1 if the destination cannot be reached.

Constraints

  • 1 <= m, n <= 200
  • maze[i][j] is either -1 or an integer in [0, 10^6]
  • Movement is allowed only in the 4 cardinal directions
  • Return -1 if the start or destination is blocked

Function Signature

def min_cost_maze_path(maze):
Interviewer

Your question is Shortest Path in Cost Maze. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.