-
Notifications
You must be signed in to change notification settings - Fork 1
/
findPeakElement2d.go
49 lines (44 loc) · 1.28 KB
/
findPeakElement2d.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package binarySearch
import "math"
/*
Find a peak element in a 2D array
*/
/*Solution:
Consider mid column and find maximum element in it.
Let index of mid column be ‘mid’, value of maximum element in mid column be ‘max’ and maximum element be at ‘mat[max_index][mid]’.
If max >= A[index][mid-1] & max >= A[index][pick+1], max is a peak, return max.
If max < mat[max_index][mid-1], recur for left half of matrix.
If max < mat[max_index][mid+1], recur for right half of matrix.
*/
func FindPeakElement2d(nums [][]int) int {
return findPeakElementRecur(nums, len(nums) >> 1)
}
func findPeakElementRecur(nums [][]int, m int) int {
i, max := findMax(nums, m)
// If we are on the first or last column,
// max is a peak
if m == 0 || m == len(nums) - 1 {
return max
}
// If max is less than its left
if max < nums[i][m-1] {
return findPeakElementRecur(nums, m - int(math.Ceil(float64(m >> 1))))
}
// if max is less than its right
if max < nums[i][m+1] {
return findPeakElementRecur(nums, m + int(math.Ceil(float64(m >> 1))))
}
// If mid column maximum is also peak
return max
}
func findMax(nums [][]int, m int) (int, int) {
var curMax int
var index int
for i, _ := range nums {
if nums[i][m] > curMax {
curMax = nums[i][m]
index = i
}
}
return index, curMax
}