Wettbewerbsfähige ProgrammierungEinfach

Min. Kostenpfad

Detaillierte Anleitung und Python-Implementierung für das Problem „Min Cost Path“.

Problemstellung

Einfach

Schreiben Sie eine Funktion min_cost_path(cost, m, n), die die Mindestkosten ermittelt, um Zelle (m, n) von Zelle (0, 0) in einer 2D-Kostenmatrix cost zu erreichen. Sie können sich von einer Zelle aus nur nach unten, rechts und diagonal nach unten-rechts bewegen.

Einschränkungen
  • 1 <= len(cost), len(cost[0]) <= 100
  • 0 <= cost[i][j] <= 1000
  • 0 <= m < len(cost)
  • 0 <= n < len(cost[0])

Beispiele

Example 1
Input
min_cost_path([[1, 2, 3], [4, 8, 2], [1, 5, 3]], 2, 2)
Output
8
Explanation

The path with minimum cost is (0,0) -> (0,1) -> (1,2) -> (2,2) with total cost 1 + 2 + 2 + 3 = 8.

Example 2
Input
min_cost_path([[1, 2, 3], [4, 8, 2], [1, 5, 3]], 1, 1)
Output
9
Explanation

The path with minimum cost is (0,0) -> (1,1) with total cost 1 + 8 = 9.

Need a Hint?
Erwägen Sie die Verwendung spezieller Datenstrukturen für dynamische Programmierung wie Mengen oder Heaps.
Edge Cases to Watch
  • Leere Eingabestrukturen
  • Einzelelementeingaben
  • Große numerische Grenzen

Bereit zur Lösung?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Im Editor öffnen
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Empfohlene Python-Ressourcen

Erweitern Sie Ihr Wissen mit zugehörigen interaktiven Tutorials, Spickzetteln und Codevergleichen.