Memory beats 98.61% [EXPLAINED]
Intuition\nTo make a string of brackets balanced, we need to ensure that every closing bracket ] has a corresponding opening bracket [. The challenge is to iden
# Intuition\nTo make a string of brackets balanced, we need to ensure that every closing bracket ] has a corresponding opening bracket [. The challenge is to identify how many swaps are needed to rearrange the brackets correctly.\n\n# Approach\nCount how many more ] brackets there are than [ brackets as you scan through the string, track the maximum imbalance, and calculate the required swaps as half of that maximum imbalance rounded up.\n\n# Complexity\n- Time complexity:\nO(n), where n is the length of the string; we only go through the string once.\n\n- Space complexity:\nO(1); we only use a few variables, regardless of the input size.\n\n# Code\n```python3 []\nclass Solution:\n    def minSwaps(self, s: str) -> int:\n        # Initialize balance and swap counter\n        balance = 0\n        max_imbalance = 0\n        \n        # Iterate through the string to calculate balance\n        for char in s:\n            if char == \'[\':\n                balance += 1\n            else:  # char == \']\'\n                balance -= 1\n            \n            # Track the maximum imbalance (how many more closing brackets we have)\n            max_imbalance = max(max_imbalance, -balance)\n        \n        # Each swap can fix two units of imbalance\n        return (max_imbalance + 1) // 2\n\n```
['Python3']