LeetCode之Widest Vertical Area Between Two Points Containing No Points(Kotlin)

问题:



方法:
这题最大难点是理解题意。。需要找到横轴距离相距最远的两个点,所以先对points进行排序,然后再遍历得到最大宽度即可。

class WidestVerticalAreaBetweenTwoPointsContainingNoPoints {
    fun maxWidthOfVerticalArea(points: Array): Int {
        var width = 0
        points.sortWith(compareBy { it[0] })
        for (index in 0 until points.lastIndex) {
            if (points[index+1][0] - points[index][0] > width) {
                width = points[index+1][0] - points[index][0]
            }
        }
        return width
    }
}

fun main() {

}

有问题随时沟通

具体代码实现可以参考Github

你可能感兴趣的:(LeetCode之Widest Vertical Area Between Two Points Containing No Points(Kotlin))