LeetCode in Kotlin

149. Max Points on a Line

Hard

Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane, return the maximum number of points that lie on the same straight line.

Example 1:

Input: points = [[1,1],[2,2],[3,3]]

Output: 3

Example 2:

Input: points = [[1,1],[3,2],[5,3],[4,1],[2,3],[1,4]]

Output: 4

Constraints:

Solution

class Solution {
    fun maxPoints(points: Array<IntArray>): Int {
        if (points.size < 2) {
            return points.size
        }
        var max = 0
        for (i in points.indices) {
            for (j in i + 1 until points.size) {
                val x = points[j][0] - points[i][0]
                val y = points[j][1] - points[i][1]
                val c = x * points[j][1] - y * points[j][0]
                var count = 2
                for (k in j + 1 until points.size) {
                    if (c == x * points[k][1] - y * points[k][0]) {
                        count++
                    }
                }
                max = Math.max(count, max)
            }
        }
        return max
    }
}