Skip to content

Instantly share code, notes, and snippets.

@Ahmah2009
Created April 4, 2019 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ahmah2009/e4e60e596968dfaafafba04ea284f10f to your computer and use it in GitHub Desktop.
Save Ahmah2009/e4e60e596968dfaafafba04ea284f10f to your computer and use it in GitHub Desktop.
973. K Closest Points to Origin leetcode
import "sort"
func kClosest(points [][]int, K int) [][]int {
sort.SliceStable(points,func(i,j int)bool{
point1 := points[i]
point2 := points[j]
return distance(point1[0],point1[1]) < distance(point2[0],point2[1])
})
return points[:K]
}
func distance(x, y int) int {
return x*x + y*y
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment