跳至主要內容
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?

4563博客

全新的繁體中文 WordPress 網站
  • 首頁
  • 微软面试题: K 个最近的点
未分類
24 10 月 2020

微软面试题: K 个最近的点

微软面试题: K 个最近的点

資深大佬 : zzzrf 3

给定一些 points 和一个 origin,从 points 中找到 k 个离 origin 最近的点。按照距离由小到大返回。如果两个点有相同距离,则按照 x 值来排序;若 x 值也相同,就再按照 y 值排序。

在线做题地址

例 1:

输入: points = [[4,6],[4,7],[4,4],[2,5],[1,1]], origin = [0, 0], k = 3  输出: [[1,1],[2,5],[4,4]] 

例 2:

输入: points = [[0,0],[0,9]], origin = [3, 1], k = 1 输出: [[0,0]] 

题解

使用 Heapify 的方法 heapify 时间复杂度 O(n) 然后 pop k 个点出来,时间复杂度 klogn 总共的时间复杂度 O(n+klogn) 如果 n >> k 的话,这种方法的时间复杂度是很优秀的。

public class Solution {     private Point origin;     private int size;          /**      * @param points a list of points      * @param origin a point      * @param k an integer      * @return the k closest points      */     public Point[] kClosest(Point[] points, Point origin, int k) {         if (points == null || points.length == 0) {             return points;         }                  this.origin = origin;         heapify(points); // O(n)                  Point[] results = new Point[k];         // O(klogn)         for (int i = 0; i < k; i++) {             results[i] = pop(points);         }                  return results;     }          private void heapify(Point[] points) {         size = points.length;         for (int i = size / 2; i >= 0; i--) {             siftDown(points, i);         }      }          private void siftDown(Point[] points, int index) {         while (index < size) {             int left = index * 2 + 1;             int right = index * 2 + 2;             int min = index;             if (left < size && compare(points[min], points[left]) > 0) {                 min = left;             }             if (right < size && compare(points[min], points[right]) > 0) {                 min = right;             }             if (index != min) {                 Point temp = points[index];                 points[index] = points[min];                 points[min] = temp;                 index = min;             } else {                 break;             }         }     }          private Point pop(Point[] points) {         Point top = points[0];         points[0] = points[size - 1];         this.size--;                  siftDown(points, 0);         return top;     }          private int compare(Point a, Point b) {         int square = distanceSquare(a, origin) - distanceSquare(b, origin);         if (square != 0) {             return square;         }         if (a.x != b.x) {             return a.x - b.x;         }         return a.y - b.y;     }          private int distanceSquare(Point a, Point b) {         return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);     } } 

大佬有話說 (3)

  • 資深大佬 : hws8033856

    实际应用:GIS 系统中的周边查询

  • 資深大佬 : princelai

    工作中遇到过和你这个类似的,我是给定一万个二维坐标点( x,y ),要分别求出每个坐标点的 TopK 近邻,我最后用的 BallTree 和 cKDTree,sklearn 和 scipy 都封装好了,速度惊人,同样也适合高维数据

  • 資深大佬 : WhoMercy

    小根堆取 top k

文章導覽

上一篇文章
下一篇文章

AD

其他操作

  • 登入
  • 訂閱網站內容的資訊提供
  • 訂閱留言的資訊提供
  • WordPress.org 台灣繁體中文

51la

4563博客

全新的繁體中文 WordPress 網站
返回頂端
本站採用 WordPress 建置 | 佈景主題採用 GretaThemes 所設計的 Memory
4563博客
  • Hostloc 空間訪問刷分
  • 售賣場
  • 廣告位
  • 賣站?
在這裡新增小工具