Re: [閒聊] 每日leetcode
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
推
10/08 09:39,
1年前
, 1F
10/08 09:39, 1F
推
10/08 10:04,
1年前
, 2F
10/08 10:04, 2F
討論串 (同標題文章)
以下文章回應了本文 (最舊先):
完整討論串 (本文為第 961 之 1548 篇):