【golang】算法 -- 最大容积 - Go语言中文社区

【golang】算法 -- 最大容积


参考 https://leetcode-cn.com/problems/container-with-most-water/
在这里插入图片描述

package main

import "fmt"

var (
	max int
	l   int
	r   int
)

func main() {
	max = 0
	l = 0
	height := [9]int{1, 8, 6, 2, 5, 4, 8, 3, 7}
	r = len(height) - 1

	// 从两端开始
	for l < r {

		temp := 0
		if height[l] > height[r] {
			temp = height[r] * (r - l)
		} else {
			temp = height[l] * (r - l)
		}
		if temp > max {
			max = temp
		}

		// 容积,有短板决定,所以不断查找更长的板
		if height[l] > height[r] {
			r--
		} else {
			l++
		}
	}

	fmt.Println("max area : ", max)
}

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/G_LiQing/article/details/104675927
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-06-27 23:17:18
  • 阅读 ( 1505 )
  • 分类:Go

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢