精華區beta Marginalman 關於我們 聯絡資訊
1963. Minimum Number of Swaps to Make the String Balanced ## 思路 計算不match的pair數量, swap次數=(res+1) // 2 ][ -- 1 ([ ]) ]][[ -- 1 ([][]) ]]][[[ -- 2 ([ [][] ]) ]]]][[[[ -- 2 ([][][][]) ]]]]][[[[[ -- 3 ([ [][][][] ]) ## Code ```python class Solution: def minSwaps(self, s: str) -> int: res = left = 0 for ch in s: if ch == '[': left += 1 elif left == 0: res += 1 else: left -= 1 return (res + 1) // 2 ``` -- https://i.imgur.com/kyBhy6o.jpeg -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 185.213.82.144 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1728350949.A.013.html
sustainer123: 大師 10/08 09:39
DJYOSHITAKA: 放過我 10/08 10:04