|
| 1 | +--- |
| 2 | +title: 0011.Container With Most Water |
| 3 | +subtitle: "https://leetcode.com/problems/container-with-most-water/description/" |
| 4 | +date: 2024-03-24T16:31:00+08:00 |
| 5 | +lastmod: 2024-03-24T16:31:00+08:00 |
| 6 | +draft: false |
| 7 | +author: "Kimi.Tsai" |
| 8 | +authorLink: "https://kimi0230.github.io/" |
| 9 | +description: "0011.Container-With-Most-Water" |
| 10 | +license: "" |
| 11 | +images: [] |
| 12 | + |
| 13 | +tags: [LeetCode, Go, Medium, Container With Most Water, Amazon, Microsoft, Adobe, Facebook, Google, Array, Two Pointers, Greedy] |
| 14 | +categories: [LeetCode] |
| 15 | + |
| 16 | +featuredImage: "" |
| 17 | +featuredImagePreview: "" |
| 18 | + |
| 19 | +hiddenFromHomePage: false |
| 20 | +hiddenFromSearch: false |
| 21 | +twemoji: false |
| 22 | +lightgallery: true |
| 23 | +ruby: true |
| 24 | +fraction: true |
| 25 | +fontawesome: true |
| 26 | +linkToMarkdown: false |
| 27 | +rssFullText: false |
| 28 | + |
| 29 | +toc: |
| 30 | + enable: true |
| 31 | + auto: true |
| 32 | +code: |
| 33 | + copy: true |
| 34 | + maxShownLines: 200 |
| 35 | +math: |
| 36 | + enable: false |
| 37 | + # ... |
| 38 | +mapbox: |
| 39 | + # ... |
| 40 | +share: |
| 41 | + enable: true |
| 42 | + # ... |
| 43 | +comment: |
| 44 | + enable: true |
| 45 | + # ... |
| 46 | +library: |
| 47 | + css: |
| 48 | + # someCSS = "some.css" |
| 49 | + # located in "assets/" |
| 50 | + # Or |
| 51 | + # someCSS = "https://cdn.example.com/some.css" |
| 52 | + js: |
| 53 | + # someJS = "some.js" |
| 54 | + # located in "assets/" |
| 55 | + # Or |
| 56 | + # someJS = "https://cdn.example.com/some.js" |
| 57 | +seo: |
| 58 | + images: [] |
| 59 | + # ... |
| 60 | +--- |
| 61 | +# [0011.Container With Most Water](https://leetcode.com/problems/container-with-most-water/description/) |
| 62 | + |
| 63 | +## 題目 |
| 64 | + |
| 65 | +## 題目大意 |
| 66 | + |
| 67 | + |
| 68 | +## 解題思路 |
| 69 | +利用雙指針, 找出最小的高並乘上兩指針距離, 得出面積 |
| 70 | +當左指針高度比右指針高度高時, 將右指針往左移, 反之將左指針往右移. |
| 71 | + |
| 72 | +## Big O |
| 73 | + |
| 74 | +* 時間複雜 : `O(n)` |
| 75 | +* 空間複雜 : `O(1)` |
| 76 | + |
| 77 | +## 來源 |
| 78 | +* https://leetcode.com/problems/container-with-most-water/description/ |
| 79 | +* https://leetcode.cn/problems/container-with-most-water/description/ |
| 80 | + |
| 81 | +## 解答 |
| 82 | +https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0011.Container-With-Most-Water/main.go |
| 83 | + |
| 84 | +```go |
| 85 | +package containerwithmostwater |
| 86 | + |
| 87 | +// 時間複雜 O(n), 空間複雜 O(1) |
| 88 | +func maxArea(height []int) int { |
| 89 | + left, right := 0, len(height)-1 |
| 90 | + result := 0 |
| 91 | + for left < right { |
| 92 | + tmp := (right - left) * min(height[left], height[right]) |
| 93 | + result = max(result, tmp) |
| 94 | + if height[left] > height[right] { |
| 95 | + right-- |
| 96 | + } else { |
| 97 | + left++ |
| 98 | + } |
| 99 | + } |
| 100 | + return result |
| 101 | +} |
| 102 | + |
| 103 | +func min(a, b int) int { |
| 104 | + if a < b { |
| 105 | + return a |
| 106 | + } |
| 107 | + return b |
| 108 | +} |
| 109 | + |
| 110 | +func max(a, b int) int { |
| 111 | + if a > b { |
| 112 | + return a |
| 113 | + } |
| 114 | + return b |
| 115 | +} |
| 116 | + |
| 117 | +``` |
| 118 | + |
| 119 | +## Benchmark |
| 120 | + |
| 121 | +```sh |
| 122 | + |
| 123 | +``` |
0 commit comments