Golang Leetcode 849. Maximize Distance to Closest Person.go

2019-05-05 11:52:51 浏览数 (1)

版权声明:原创勿转 https://cloud.tencent.com/developer/article/1420468

思路

如果两头是0,开头和末尾就是距离

如果中间是0,距离是0的个数/2

取最大值

code

代码语言:javascript复制
func maxDistToClosest(seats []int) int {
	l := len(seats)
	ret := []int{}
	for k, v := range seats {
		if v == 1 {
			ret = append(ret, k)
		}
	}
	m := len(ret)
	pre := ret[0]
	max := mymax(pre, l-1-ret[m-1])
	for _, v := range ret {
		max = mymax(max, (v-pre)/2)
		pre = v
	}
	return max
}

func mymax(x, y int) int {
	if x > y {
		return x
	}
	return y
}

0 人点赞