Programmazione competitivaFacile

Bilanciamento delle fasce di Minimum Swap

Guida dettagliata e implementazione Python per il problema del "bilanciamento delle parentesi degli scambi minimi".

Dichiarazione del problema

Facile

Scrivi una funzione minimum_swaps_balancing(s) che accetta una stringa s di parentesi quadre [ e ] contenente un numero uguale di parentesi di apertura e chiusura e restituisce il numero minimo di scambi di caratteri adiacenti necessari per bilanciare la stringa.

Vincoli
  • 2 <= len(s) <= 10^5
  • s contains only '[' and ']'
  • Count of '[' equals count of ']'

Esempi

Example 1
Input
minimum_swaps_balancing('[]][[]')
Output
1
Explanation

Swap index 2 and 3 -> '[][][]', which is balanced. (1 swap)

Example 2
Input
minimum_swaps_balancing(']]][[[')
Output
6
Explanation

We can swap adjacent brackets to make it balanced, requiring 6 swaps in total.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche di Greedy come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

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

Apri nell'editor
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

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.