O04
Tag Repair Cost
Problem Statement & Objective
Compute the minimum cost to repair and match mismatched warehouse barcode tags using stack-based cancellation.
Function Signature (Python)
min_parentheses_repair_cost(s: str) -> intInput & Output Contract
Output Rule:Negatif olmayan tam sayı. Zaten dengeli olan metin için 0.
System Constraints
0 <= metin.length <= 10^4
metin yalnızca '(' ve ')' içerirExample Execution Walkthrough
metin = "(()))("
soldan sağa tara, `open` sayacını tut:
'(' open=1 | '(' open=2 | ')' open=1 | ')' open=0
')' open=0 -> eşleşecek açılış yok -> bu kapanış SİLİNMELİ, maliyet +1
'(' open=1 -> metin bitti, 1 açılış eşleşmedi -> SİLİNMELİ, maliyet +2
toplam maliyet = 1 + 2 = 3
cevap: 3Example Test Scenarios (3)
Example 1
Input:
["(()))("]Expected Output:
3Example 2: Zaten Dengeli
Input:
["()()"]Expected Output:
0Example 3: Tek Kapanış Parantezi
Input:
[")"]Expected Output:
1