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]
        }
    }
  • 相关阅读:
    网页的摘要信息
    自我介绍
    DIV和SPAN的区别
    软件工程简介
    设置层的漂移
    构建之法现代软件工程
    手机验证码的相关知识
    Python2和Python3的区别
    我的第一个练习
    结对编程后传之做汉堡
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13407296.html
Copyright © 2011-2022 走看看