-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Solve the "Search in a 2D sorted array" problem
- Loading branch information
Showing
3 changed files
with
74 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) 2015, Peter Mrekaj. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE.txt file. | ||
|
||
package search | ||
|
||
// Matrix returns true if m contains x, otherwise returns false. | ||
// The time complexity is O(m+n) where m is number of row and n is | ||
// number of columns in matrix. The O(1) additional space is needed. | ||
func Matrix(m [][]int, x int) bool { | ||
r, c := 0, len(m)-1 | ||
for r < len(m) && c >= 0 { | ||
switch e := m[r][c]; { | ||
case e == x: | ||
return true | ||
case e > x: | ||
c-- | ||
case e < x: | ||
r++ | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) 2015, Peter Mrekaj. All rights reserved. | ||
// Use of this source code is governed by a MIT-style | ||
// license that can be found in the LICENSE.txt file. | ||
|
||
package search | ||
|
||
import ( | ||
"math/rand" | ||
"sort" | ||
"testing" | ||
) | ||
|
||
func TestMatrix(t *testing.T) { | ||
for _, test := range []struct { | ||
m [][]int | ||
x int | ||
want bool | ||
}{ | ||
{[][]int{[]int{0, 1}, []int{1, 2}}, 2, true}, | ||
{[][]int{[]int{0, 1}, []int{1, 2}}, -1, false}, | ||
{[][]int{[]int{0, 1, 2}, []int{1, 2, 3}, []int{2, 3, 4}}, 2, true}, | ||
{[][]int{[]int{0, 1, 2}, []int{1, 2, 3}, []int{2, 3, 4}}, -1, false}, | ||
} { | ||
if got := Matrix(test.m, test.x); got != test.want { | ||
t.Errorf("Matrix(%v, %d) = %t; want %t", test.m, test.x, got, test.want) | ||
} | ||
} | ||
} | ||
|
||
func benchMatrix(b *testing.B, size int) { | ||
b.StopTimer() | ||
n := size * size | ||
data := rand.Perm(n) | ||
sort.Ints(data) | ||
x := data[n/2] | ||
var m [][]int | ||
for i := 1; i <= size; i++ { | ||
m = append(m, data[(i-1)*size:i*size]) | ||
} | ||
b.StartTimer() | ||
for i := 0; i < b.N; i++ { | ||
Matrix(m, x) | ||
} | ||
} | ||
|
||
func BenchmarkMatrix1e1(b *testing.B) { benchMatrix(b, 1e1) } | ||
func BenchmarkMatrix1e2(b *testing.B) { benchMatrix(b, 1e2) } | ||
func BenchmarkMatrix1e3(b *testing.B) { benchMatrix(b, 1e3) } |