zoukankan      html  css  js  c++  java
  • 973. K Closest Points to Origin

    package LeetCode_973
    
    import java.util.*
    
    /**
     * 973. K Closest Points to Origin
     * https://leetcode.com/problems/k-closest-points-to-origin/description/
     *
     * We have a list of points on the plane.  Find the K closest points to the origin (0, 0).
    (Here, the distance between two points on a plane is the Euclidean distance.)
    You may return the answer in any order.  The answer is guaranteed to be unique (except for the order that it is in.)
    
    Example 1:
    Input: points = [[1,3],[-2,2]], K = 1
    Output: [[-2,2]]
    Explanation:
    The distance between (1, 3) and the origin is sqrt(10).
    The distance between (-2, 2) and the origin is sqrt(8).
    Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin.
    We only want the closest K = 1 points from the origin, so the answer is just [[-2,2]].
     * */
    class Solution {
        /*
        * solution: PriorityQueue, Time complexity:O(nlogk), Space complexity:O(1)
        * because heapify: O(logk), k is size of PriorityQueue
        * */
        fun kClosest(points: Array<IntArray>, K: Int): Array<IntArray>? {
            //sort by distance ascending
            val minHeap = PriorityQueue<IntArray> { a, b -> distance(b) - distance(a) }
            for (point in points) {
                minHeap.add(point)
                if (minHeap.size > K) {
                    minHeap.poll()
                }
            }
            val result = Array(K, { IntArray(2) })
            for (i in minHeap.indices) {
                result[i] = minHeap.poll()
            }
            return result
        }
    
        private fun distance(array: IntArray): Int {
            return array[0] * array[0] + array[1] * array[1]
        }
    }
  • 相关阅读:
    Are You Safer With Firefox?(zz)
    IIS+PHP下调用WebService初试
    垃圾链接和网络欺骗
    微软即将发布64位XP和Win2003 SP1(zz)
    今日个人大事记:)
    GT4 Web Service编译和发布初探
    纪念一下QQ等级和在线时长
    今天安装GT3.9.5碰到的问题
    判断32位整数二进制中1的个数
    Windows 2003 SP1新体验
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13407296.html
Copyright © 2011-2022 走看看