Tutti Nuovi

user:thomas utente che ha creato il gist

title:mygist titolo del gist

filename:myfile.txt nome di file nel gist

extension:yml estensione del file nel gist

language:go linguaggio del file nel gist

topic:homelab gists with given topic

Registrati

Entra


Tutti Nuovi Registrati Entra

linkjobai

Unito 1794541555

Più recente creazione
Meno recente creazione
Più recente aggiornamento
Meno recente aggiornamento
Tutti i gists 1

linkjobai / Monotonic Stack: The Pattern Behind "Next Greater Element" Questions

0 mi piace
0 forks
1 files
Ultima volta attivo 1794541558
Interview prep notes
interview

Monotonic Stack: The Pattern Behind "Next Greater Element" Questions

If a question asks for the next greater, previous smaller, or "how many days until a warmer temperature", a monotonic stack usually turns an O(n²) scan into O(n).

Core idea

Keep a stack of indices whose values are in sorted order (decreasing for "next greater"). When a new value breaks the order, it is the answer for every index you pop.

def daily_temperatures(temps):          # LeetCode 739
Più nuovi Più vecchi

Creato da Opengist ⋅ Load: 33ms⋅

Italiano
Čeština Deutsch English Español Français Magyar Italiano 日本語 Polski Português Русский Türkçe Українська 中文 繁體中文