Skip to content

Commit ae7d0a5

Browse files
committed
feat: 0011.Container-With-Most-Water
1 parent 337adb0 commit ae7d0a5

File tree

5 files changed

+234
-0
lines changed

5 files changed

+234
-0
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package containerwithmostwater
2+
3+
// 時間複雜 O(n), 空間複雜 O(1)
4+
func maxArea(height []int) int {
5+
left, right := 0, len(height)-1
6+
result := 0
7+
for left < right {
8+
tmp := (right - left) * min(height[left], height[right])
9+
result = max(result, tmp)
10+
if height[left] > height[right] {
11+
right--
12+
} else {
13+
left++
14+
}
15+
}
16+
return result
17+
}
18+
19+
func min(a, b int) int {
20+
if a < b {
21+
return a
22+
}
23+
return b
24+
}
25+
26+
func max(a, b int) int {
27+
if a > b {
28+
return a
29+
}
30+
return b
31+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package containerwithmostwater
2+
3+
import "testing"
4+
5+
var tests = []struct {
6+
height []int
7+
want int
8+
}{
9+
{
10+
[]int{1, 8, 6, 2, 5, 4, 8, 3, 7},
11+
49,
12+
},
13+
{
14+
[]int{4, 3, 2, 1, 4},
15+
16,
16+
},
17+
{
18+
[]int{1, 2, 1},
19+
2,
20+
},
21+
{
22+
[]int{1, 1},
23+
1,
24+
},
25+
}
26+
27+
func TestMaxArea(t *testing.T) {
28+
for _, tt := range tests {
29+
if got := maxArea(tt.height); got != tt.want {
30+
t.Errorf("maxArea(%v) = %v, want %v", tt.height, got, tt.want)
31+
}
32+
}
33+
}
34+
35+
func BenchmarkMaxArea(b *testing.B) {
36+
for i := 0; i < b.N; i++ {
37+
maxArea(tests[0].height)
38+
}
39+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// 時間複雜度: O(n)
2+
// 空間複雜度: O(1)
3+
/*
4+
* @lc app=leetcode.cn id=11 lang=golang
5+
*
6+
* [11] 盛最多水的容器
7+
*/
8+
9+
// @lc code=start
10+
func maxArea(height []int) int {
11+
left, right := 0, len(height)-1
12+
result := 0
13+
for left < right {
14+
tmp := (right - left) * min(height[left], height[right])
15+
result = max(result, tmp)
16+
if height[left] > height[right] {
17+
right--
18+
} else {
19+
left++
20+
}
21+
}
22+
return result
23+
}
24+
25+
func min(a, b int) int {
26+
if a < b {
27+
return a
28+
}
29+
return b
30+
}
31+
32+
func max(a, b int) int {
33+
if a > b {
34+
return a
35+
}
36+
return b
37+
}
38+
39+
// @lc code=end
40+

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ https://labuladong.gitee.io/algo/2/21/57/
308308
| [0142](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0142.Linked-List-CycleII/) | [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0142.Linked-List-CycleII) | Medium | O(n) | O(1) | Linked List, Two Pointers |
309309
| [0344](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0344.Reverse-String/) | [Reverse String](https://leetcode.com/problems/reverse-string/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0344.Reverse-String) | Easy | O(n) | O(1) | Two Pointers |
310310
| [0876](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0876.Middle-of-the-Linked-List/) | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0876.Middle-of-the-Linked-List) | Easy | | | Linked List, Two Pointers |
311+
| [0011](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0011.Container-With-Most-Water/) | [Container With Most Water](0011.Container-With-Most-Water) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0011.Container-With-Most-Water) | Medium | | |Two Pointers |
311312

312313
---
313314

0 commit comments

Comments
 (0)