From 4ba6a146e99e16d5e2b09145bdacd4ad35297872 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Tue, 8 Oct 2024 09:11:06 +0800 Subject: [PATCH] Add solution and test-cases for problem 1963 --- .../README.md | 43 +++++++++++++------ .../Solution.go | 25 ++++++++++- .../Solution_test.go | 14 +++--- 3 files changed, 60 insertions(+), 22 deletions(-) diff --git a/leetcode/1901-2000/1963.Minimum-Number-of-Swaps-to-Make-the-String-Balanced/README.md b/leetcode/1901-2000/1963.Minimum-Number-of-Swaps-to-Make-the-String-Balanced/README.md index 1e36461fe..d405d607f 100755 --- a/leetcode/1901-2000/1963.Minimum-Number-of-Swaps-to-Make-the-String-Balanced/README.md +++ b/leetcode/1901-2000/1963.Minimum-Number-of-Swaps-to-Make-the-String-Balanced/README.md @@ -1,28 +1,45 @@ # [1963.Minimum Number of Swaps to Make the String Balanced][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +You are given a **0-indexed** string `s` of **even** length `n`. The string consists of **exactly** `n / 2` opening brackets `'['` and `n / 2` closing brackets `']'`. + +A string is called **balanced** if and only if: + +- It is the empty string, or +- It can be written as `AB`, where both `A` and `B` are **balanced** strings, or +- It can be written as `[C]`, where `C` is a **balanced** string. + +You may swap the brackets at **any** two indices **any** number of times. + +Return the **minimum** number of swaps to make `s` **balanced**. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: s = "][][" +Output: 1 +Explanation: You can make the string balanced by swapping index 0 with index 3. +The resulting string is "[[]]". ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Minimum Number of Swaps to Make the String Balanced -```go ``` +Input: s = "]]][[[" +Output: 2 +Explanation: You can do the following to make the string balanced: +- Swap index 0 with index 4. s = "[]][][". +- Swap index 1 with index 5. s = "[[][]]". +The resulting string is "[[][]]". +``` + +**Example 3:** +``` +Input: s = "[]" +Output: 0 +Explanation: The string is already balanced. +``` ## 结语 diff --git a/leetcode/1901-2000/1963.Minimum-Number-of-Swaps-to-Make-the-String-Balanced/Solution.go b/leetcode/1901-2000/1963.Minimum-Number-of-Swaps-to-Make-the-String-Balanced/Solution.go index d115ccf5e..125815910 100644 --- a/leetcode/1901-2000/1963.Minimum-Number-of-Swaps-to-Make-the-String-Balanced/Solution.go +++ b/leetcode/1901-2000/1963.Minimum-Number-of-Swaps-to-Make-the-String-Balanced/Solution.go @@ -1,5 +1,26 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(s string) int { + bs := []byte(s) + stackIndex := -1 + for index := 0; index < len(s); index++ { + if s[index] == ']' { + if stackIndex != -1 && bs[stackIndex] == '[' { + stackIndex-- + continue + } + } + stackIndex++ + bs[stackIndex] = s[index] + } + if stackIndex == -1 { + return 0 + } + // 最终就是]]]]]][[[[ 这情况 + count := (stackIndex + 1) / 2 + need := count / 2 + if count&1 == 1 { + need++ + } + return need } diff --git a/leetcode/1901-2000/1963.Minimum-Number-of-Swaps-to-Make-the-String-Balanced/Solution_test.go b/leetcode/1901-2000/1963.Minimum-Number-of-Swaps-to-Make-the-String-Balanced/Solution_test.go index 14ff50eb4..9b26953cc 100644 --- a/leetcode/1901-2000/1963.Minimum-Number-of-Swaps-to-Make-the-String-Balanced/Solution_test.go +++ b/leetcode/1901-2000/1963.Minimum-Number-of-Swaps-to-Make-the-String-Balanced/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs string + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "][][", 1}, + {"TestCase2", "]]][[[", 2}, + {"TestCase3", "[]", 0}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }