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]
        }
    }
  • 相关阅读:
    jQuery
    基于Js实现的UrlEncode和UrlDecode函数代码
    利用缓存、Timer间隔时间发送微信的实例,很有用的例子
    VisualStudio 自动排版等 快捷键
    正则表达式判断手机号码属于哪个运营商
    .Net常用方法汇总
    .NET中的三种Timer的区别和用法
    C# List和String互相转换
    Tempdb--TempDB Basic
    Tempdb--查看tempdb使用的脚本
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13407296.html
Copyright © 2011-2022 走看看