Leetcode Note: Go - Find Nearest Point That Has the Same X or Y Coordinate

Find Nearest Point That Has the Same X or Y Coordinate - LeetCode
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/

  • Go 言語で取り組んだメモ

回答

Go one pass easy hehe - Find Nearest Point That Has the Same X or Y Coordinate - LeetCode
https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/solutions/2207539/go-one-pass-easy-hehe/

func nearestValidPoint(x int, y int, points [][]int) int {
    var res int = -1
    
    smallestDistance := 1 << 63 - 1
    
    for i := 0; i < len(points); i++ {
        if points[i][0] == x || points[i][1] == y {
            if distance([]int{x, y}, points[i]) < smallestDistance {
                smallestDistance = distance([]int{x, y}, points[i])
                res = i
            }
        }
    }
    
    return res
}

func distance(a []int, b[]int) int {
    return (a[0] - b[0])*(a[0] - b[0]) + (a[1] - b[1])*(a[1] - b[1])
}