美文网首页
LeetCode之Container With Most Wat

LeetCode之Container With Most Wat

作者: 糕冷羊 | 来源:发表于2021-09-22 13:26 被阅读0次

问题:



方法:
水槽的底最长的情况为height.lastIndex - 0,所以如果存在比底最长情况更大水槽的情况是当height[index]更大的情况,所以只有height[index]更大时才可能存在候选,其他情况只需要移动index。

package com.eric.leetcode

class ContainerWithMostWater {
    fun maxArea(height: IntArray): Int {
        var i = 0
        var j = height.lastIndex
        var max = minOf(height[i], height[j]) * (j - i)
        while (i < j) {
            val cur = minOf(height[i], height[j]) * (j - i)
            if (cur > max) {
                max = cur
            }
            if (height[i] < height[j]) {
                i++
            } else {
                j--
            }
        }
        return max
    }
}

有问题随时沟通

具体代码实现可以参考Github

相关文章

网友评论

      本文标题:LeetCode之Container With Most Wat

      本文链接:https://www.haomeiwen.com/subject/tqawgltx.html