[Python] Greedy O(n), explained
The idea is to traverse our string and check the biggest possible disbalance. For example if we look at ]]][]]][[[[[, for traversals we have:\n-1, -2, -3, -2, -
The idea is to traverse our string and check the biggest possible disbalance. For example if we look at `]]][]]][[[[[`, for traversals we have:\n`-1, -2, -3, -2, -3, -4, -5, -4, -3, -2, -1, 0` and the biggest disbalance is equal to `5`. What we can do in one swap of brackets is to decreas it at most by `2`: for this we need to take the leftest `]` with negative balance and the rightest `[` with negative balance and change them. For example in our case we have:\n\n`[]][]]][[[[] : [1, 0, -1, 0, -1, -2, -3, -2, -1, 0, 1, 0]`\n\n`[][[]]][][[] : [1, 0,  1, 2, 1, 0, -1, 0, -1, 0, 1, 0]`\n\n`[][[]][[]][] : [1, 0,  1, 2, 1, 0, 1, 2, 1, 0, 1, 0]`\n\n#### Complexity\nTime complexity is `O(n)` to traverse our string, space is `O(1)`.\n\n#### Code\n```python\nclass Solution:\n    def minSwaps(self, s):\n        bal, ans = 0, 0\n        for symb in s:\n            if symb == "[": bal += 1\n            else: bal -= 1\n            ans = min(bal, ans)\n        return (-ans + 1)//2\n```\n\nIf you have any questions, feel free to ask. If you like solution and explanations, please **Upvote!**
['Greedy']