diff --git a/leetcode/3401-3500/3443.Maximum-Manhattan-Distance-After-K-Changes/README.md b/leetcode/3401-3500/3443.Maximum-Manhattan-Distance-After-K-Changes/README.md index ba4b87990..b877a87f9 100755 --- a/leetcode/3401-3500/3443.Maximum-Manhattan-Distance-After-K-Changes/README.md +++ b/leetcode/3401-3500/3443.Maximum-Manhattan-Distance-After-K-Changes/README.md @@ -1,28 +1,52 @@ # [3443.Maximum Manhattan Distance After K Changes][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 string `s` consisting of the characters `'N'`, `'S'`, `'E'`, and `'W'`, where `s[i]` indicates movements in an infinite grid: + +- `'N'` : Move north by 1 unit. +- `'S'` : Move south by 1 unit. +- `'E'` : Move east by 1 unit. +- `'W'` : Move west by 1 unit. + +Initially, you are at the origin `(0, 0)`. You can change **at most** `k` characters to any of the four directions. + +Find the **maximum Manhattan distance** from the origin that can be achieved **at any time** while performing the movements **in order**. + +The **Manhattan Distance** between two cells `(xi, yi)` and `(xj, yj)` is `|xi - xj| + |yi - yj|`. + **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" -``` +Input: s = "NWSE", k = 1 + +Output: 3 -## 题意 -> ... +Explanation: -## 题解 +Change s[2] from 'S' to 'N'. The string s becomes "NWNE". -### 思路1 -> ... -Maximum Manhattan Distance After K Changes -```go +Movement Position (x, y) Manhattan Distance Maximum +s[0] == 'N' (0, 1) 0 + 1 = 1 1 +s[1] == 'W' (-1, 1) 1 + 1 = 2 2 +s[2] == 'N' (-1, 2) 1 + 2 = 3 3 +s[3] == 'E' (0, 2) 0 + 2 = 2 3 +The maximum Manhattan distance from the origin that can be achieved is 3. Hence, 3 is the output. ``` +**Example 2:** + +``` +Input: s = "NSWWEW", k = 3 + +Output: 6 + +Explanation: + +Change s[1] from 'S' to 'N', and s[4] from 'E' to 'W'. The string s becomes "NNWWWW". + +The maximum Manhattan distance from the origin that can be achieved is 6. Hence, 6 is the output. +``` ## 结语 diff --git a/leetcode/3401-3500/3443.Maximum-Manhattan-Distance-After-K-Changes/Solution.go b/leetcode/3401-3500/3443.Maximum-Manhattan-Distance-After-K-Changes/Solution.go index d115ccf5e..5d79efbc6 100644 --- a/leetcode/3401-3500/3443.Maximum-Manhattan-Distance-After-K-Changes/Solution.go +++ b/leetcode/3401-3500/3443.Maximum-Manhattan-Distance-After-K-Changes/Solution.go @@ -1,5 +1,32 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(s string, k int) int { + n1, s1, e1, w1 := 0, 0, 0, 0 + var ans int + for _, b := range s { + if b == 'N' { + n1++ + } + if b == 'S' { + s1++ + } + if b == 'W' { + w1++ + } + if b == 'E' { + e1++ + } + // 我们只需要将少的移动的多的方向,另外就是,南北,东西变化可以增加,但是南东这种变化没用 + minY := min(n1, s1) + maxY := max(n1, s1) + a := min(minY, k) + + minX := min(e1, w1) + maxX := max(e1, w1) + b := min(minX, k-a) + // n1 - (s1-a) + a + ans = max(ans, maxY-minY+a*2+maxX-minX+b*2) + + } + return ans } diff --git a/leetcode/3401-3500/3443.Maximum-Manhattan-Distance-After-K-Changes/Solution_test.go b/leetcode/3401-3500/3443.Maximum-Manhattan-Distance-After-K-Changes/Solution_test.go index 14ff50eb4..0b9fcf309 100644 --- a/leetcode/3401-3500/3443.Maximum-Manhattan-Distance-After-K-Changes/Solution_test.go +++ b/leetcode/3401-3500/3443.Maximum-Manhattan-Distance-After-K-Changes/Solution_test.go @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + s string + k int + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "NWSE", 1, 3}, + {"TestCase2", "NSWWEW", 3, 6}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.s, c.k) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.s, c.k) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }