Top 150-InterviewEinfach

Min. Stapel

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

Problemstellung

Einfach

Entwerfen Sie einen Stapel, der Push, Pop, Top und das Abrufen des minimalen Elements in konstanter Zeit unterstützt.

Implementieren Sie die Klasse MinStack:

- MinStack() initialisiert das Stapelobjekt.

- push(val: int) schiebt das Element val auf den Stapel.

- pop() entfernt das Element oben im Stapel.

- top() -> int ruft das oberste Element des Stapels ab.

- getMin() -> int ruft das minimale Element im Stapel ab.

Sie müssen für jede Funktion eine Lösung mit O(1)-Zeitkomplexität implementieren.

Einschränkungen
  • -2^31 <= val <= 2^31 - 1
  • Methods pop, top, and getMin are always called on non-empty stacks
  • At most 3 * 10^4 calls will be made to push, pop, top, and getMin

Beispiele

Example 1
Input
["MinStack", "push", "push", "push", "getMin", "pop", "top", "getMin"]
[[], [-2], [0], [-3], [], [], [], []]
Output
[None, None, None, None, -3, None, 0, -2]
Explanation

MinStack created. Push -2, 0, -3. getMin() returns -3. Pop removes -3. top() returns 0. getMin() returns -2.

Need a Hint?
Erwägen Sie die Verwendung stapelspezifischer Datenstrukturen 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.