O04

Tag Repair Cost

Mid / Senior Engineer
Target Time: 25 - 35 Minutes
Type: NUMERIC

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) -> int

Input & 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çerir

Example 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: 3

Example Test Scenarios (3)

Example 1
Input:["(()))("]
Expected Output:3
Example 2: Zaten Dengeli
Input:["()()"]
Expected Output:0
Example 3: Tek Kapanış Parantezi
Input:[")"]
Expected Output:1
Loading Python Editor...
Input Parameters
[
  "(()))("
]
Expected Output
3