Skip to content

Commit

Permalink
Solve the "Search in a 2D sorted array" problem
Browse files Browse the repository at this point in the history
  • Loading branch information
mrekucci committed Nov 4, 2015
1 parent 3ad3772 commit ea769bd
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ Searching

| Problem | Test | Implemented |
|--------------------------------------------------------------------------|:------------:|:-----------:|
| [Search in a 2D sorted array][200] | [tests][201] | |
| [Search in a 2D sorted array][200] | [tests][201] | |
| [Find the min and max simultaneously][202] | [tests][203] | |
| [Find the *k*th largest element][204] | [tests][205] | |
| [Compute the optimum mailbox placement][206] | [tests][207] | |
Expand Down Expand Up @@ -603,8 +603,8 @@ Honors Class
[197]: in_progress.md
[198]: in_progress.md
[199]: in_progress.md
[200]: in_progress.md
[201]: in_progress.md
[200]: search/matrix.go
[201]: search/matrix_test.go
[202]: in_progress.md
[203]: in_progress.md
[204]: in_progress.md
Expand Down
23 changes: 23 additions & 0 deletions search/matrix.go
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
}
48 changes: 48 additions & 0 deletions search/matrix_test.go
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) }

0 comments on commit ea769bd

Please sign in to comment.