Skip to content

Commit d89ca93

Browse files
committed
feat: 0011.Container-With-Most-Water
2 parents ae7d0a5 + 0a41ea9 commit d89ca93

File tree

4 files changed

+205
-2
lines changed

4 files changed

+205
-2
lines changed

Diff for: Leetcode/0074.Search-a-2D-Matrix/README.md

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
---
2+
title: 74. Search a 2D Matrix
3+
subtitle: "https://leetcode.com/problems/search-a-2d-matrix/description/"
4+
date: 2024-03-24T22:10:00+08:00
5+
lastmod: 2024-03-24T22:10:00+08:00
6+
draft: false
7+
author: "Kimi.Tsai"
8+
authorLink: "https://kimi0230.github.io/"
9+
description: "0074.Search-a-2D-Matrix"
10+
license: ""
11+
images: []
12+
13+
tags: [LeetCode, Go, Medium, Search a 2D Matrix, Array, Binary Search, Matrix, Facebook, Amazon, Microsoft, Bloomberg]
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+
# [74. Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/description/)
62+
63+
## 題目
64+
65+
## 題目大意
66+
編寫一個高效的演算法來判斷 m x n 矩陣中,是否存在一個目標值。 該矩陣具有如下特性:
67+
* 每行中的整數從左到右按升序排列。
68+
* 每行的第一个整数大于前一行的最后一个整数。
69+
70+
## 解題思路
71+
可以將二維轉成一維矩陣
72+
73+
## Big O
74+
75+
* 時間複雜 : `O(log⁡mn)` 其中 m 和 n 分別是矩陣的行數和列數
76+
* 空間複雜 : `O(1)`
77+
78+
## 來源
79+
* https://leetcode.com/problems/search-a-2d-matrix/description/
80+
* https://leetcode.cn/problems/
81+
82+
## 解答
83+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0074.Search-a-2D-Matrix/main.go
84+
85+
```go
86+
package searcha2dmatrix
87+
88+
// 時間複雜 O(logmn), 空間複雜 O(1)
89+
func searchMatrix(matrix [][]int, target int) bool {
90+
m := len(matrix)
91+
if m <= 0 {
92+
return false
93+
}
94+
n := len(matrix[0])
95+
96+
left, right := 0, m*n-1
97+
for left <= right {
98+
mid := int(uint(right+left) >> 1) //left + (right-left)/2
99+
val := getMatrix(matrix, mid)
100+
if val == target {
101+
return true
102+
} else if val < target {
103+
left = mid + 1
104+
} else {
105+
right = mid - 1
106+
}
107+
}
108+
return false
109+
}
110+
111+
// 將 mxn的二維陣列 映射成一維陣列
112+
func getMatrix(matrix [][]int, index int) int {
113+
n := len(matrix[0])
114+
i, j := index/n, index%n
115+
return matrix[i][j]
116+
}
117+
118+
```
119+
120+
## Benchmark
121+
122+
```sh
123+
124+
```

Diff for: Leetcode/0074.Search-a-2D-Matrix/main.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package searcha2dmatrix
2+
3+
// 時間複雜 O(logmn), 空間複雜 O(1)
4+
func searchMatrix(matrix [][]int, target int) bool {
5+
m := len(matrix)
6+
if m <= 0 {
7+
return false
8+
}
9+
n := len(matrix[0])
10+
11+
left, right := 0, m*n-1
12+
for left <= right {
13+
mid := int(uint(right+left) >> 1) //left + (right-left)/2
14+
val := getMatrix(matrix, mid)
15+
if val == target {
16+
return true
17+
} else if val < target {
18+
left = mid + 1
19+
} else {
20+
right = mid - 1
21+
}
22+
}
23+
return false
24+
}
25+
26+
// 將 mxn的二維陣列 映射成一維陣列
27+
func getMatrix(matrix [][]int, index int) int {
28+
n := len(matrix[0])
29+
i, j := index/n, index%n
30+
return matrix[i][j]
31+
}

Diff for: Leetcode/0074.Search-a-2D-Matrix/main_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package searcha2dmatrix
2+
3+
import "testing"
4+
5+
var tests = []struct {
6+
matrix [][]int
7+
target int
8+
want bool
9+
}{
10+
{
11+
[][]int{
12+
{1, 3, 5, 7},
13+
{10, 11, 16, 20},
14+
{23, 30, 34, 60},
15+
},
16+
3,
17+
true,
18+
},
19+
{
20+
[][]int{
21+
{1, 3, 5, 7},
22+
{10, 11, 16, 20},
23+
{23, 30, 34, 60},
24+
},
25+
13,
26+
false,
27+
},
28+
{
29+
[][]int{},
30+
5,
31+
false,
32+
},
33+
}
34+
35+
func TestSearchMatrix(t *testing.T) {
36+
for _, tt := range tests {
37+
if got := searchMatrix(tt.matrix, tt.target); got != tt.want {
38+
t.Errorf("searchMatrix(%v, %v) = %v, want %v", tt.matrix, tt.target, got, tt.want)
39+
}
40+
}
41+
}
42+
43+
func BenchmarkSearchMatrix(b *testing.B) {
44+
for i := 0; i < b.N; i++ {
45+
searchMatrix(tests[0].matrix, tests[0].target)
46+
}
47+
}

Diff for: README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ https://kimi0230.github.io/LeetcodeGolang/
3333
- [Sweep Line](#sweep-line)
3434
- [Rolling Sum](#rolling-sum)
3535
- [Two Pointers](#two-pointers)
36-
- [Bit Manipulation](#bit-manipulation)
36+
- [Bit Manipulation](#bit-manipulation)
3737
- [Union Find](#union-find)
3838
- [Breadth First Search](#breadth-first-search)
3939
- [Binary Search](#binary-search)
@@ -309,10 +309,11 @@ https://labuladong.gitee.io/algo/2/21/57/
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 |
311311
| [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 |
312+
| [0074](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0074.Search-a-2D-Matrix/) | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0074.Search-a-2D-Matrix) | Medium | | | Binary Search, Two Pointers |
312313

313314
---
314315

315-
#### Bit Manipulation
316+
#### Bit Manipulation
316317

317318
| No. | Title | Solution | Difficulty | Time | Space | Topic |
318319
|------------------------------------------------------------------------------------------------------|:---------------------------------------------------------------------------------------------------------:|:--------------------------------------------------------------------------------------------------------------:|------------|------------|-------------|------------------|

0 commit comments

Comments
 (0)