forked from yijizhichang/LeetCodeInGo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4.go
More file actions
51 lines (40 loc) · 727 Bytes
/
4.go
File metadata and controls
51 lines (40 loc) · 727 Bytes
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
50
51
package problem
/*
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。
请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。
你可以假设 nums1 和 nums2 不会同时为空。
示例 1:
nums1 = [1, 3]
nums2 = [2]
则中位数是 2.0
示例 2:
nums1 = [1, 2]
nums2 = [3, 4]
则中位数是 (2 + 3)/2 = 2.5
*/
func hmax(x, y int) int {
if x > y {
return x
}
return y
}
func hmin(x, y int) int {
if x > y {
return y
}
return x
}
func maxArea(height []int) int {
l := 0
r := len(height) - 1
max := 0
for l < r {
max = hmax(max, hmin(height[r], height[l])*(r-l))
if height[l] < height[r] {
l++
} else {
r--
}
}
return max
}