最接近原點的 k 個點_第K個最接近原點的位置

最接近原點的 k 個點

In this article, I will be explaining to you one of the problems that you may find when tackling questions in data structures and algorithm. You will need some basic knowledge of data structures in order to understand the optimized solution to the problem. The code in this article will be based on Python (note that Python is zero-indexed)!

在這篇文章中,我會向你解釋的問題之一是解決在數據結構和算法的問題時,你可能會發現。 您將需要一些數據結構的基礎知識,以了解針對該問題的優化解決方案。 本文中的代碼將基于Python(請注意,Python的索引為零)!

Difficulty: ???????💛Ingredient: Priority Queue (or Heap)

難度 :???????💛 成分 :優先隊列(或堆)

At one point of your life, you may have come across an algorithm and data structures question that goes like this:

在人生的某個時刻,您可能遇到過這樣的算法和數據結構問題:

Given a non-ordered (unsorted) array of coordinates and the value of k, find the kth nearest point to the origin. The coordinates given can be be in 1D, 2D or 3D space.

給定一個無序(未排序)的坐標數組和k的值,找到離原點最近的第k個點。 給定的坐標可以在1D,2D或3D空間中。

For instance, if you have an array of 2D coordinates,

例如,如果您有2D坐標數組,

[ (1,2), (1,0), (9,8), (6,8), (3,3) ]

and also given the value of k,

并給定k的值

k = 3

You are supposed to find the 3rd set of coordinates closest to the origin (0, 0). Let’s approach this step by step.

您應該找到最接近原點(0,0)的第三組坐標 。 讓我們逐步解決這個問題。

蠻力 (Brute Force)

One of the possible questions you may ask yourself, instead of kth element, how do I get the 1st element closest to the origin (where k = 1)? To simplify the problem, what if I am given 1D coordinates instead of 2D or 3D?

您可能會問自己一個可能的問題,而不是第k個元素,我如何使第一個元素最接近原點(其中k = 1)? 為了簡化問題,如果給我1D坐標而不是2D或3D怎么辦?

For instance, given the following array

例如,給定以下數組

[ 2, 3, 1, 5, 7, 6]

How do I get the closest value to origin 0 (in layman terms, smallest value) for 1D case? There are 2 distinct way of doing so,

對于一維情況,如何獲得最接近原點0的值(以外行術語而言,最小值)? 有兩種不同的方法,

  1. Sort the array from smallest to largest value, and take the first value, or

    從最小值到最大值對數組進行排序,然后取第一個值, 或者

  2. Go through every single element in the array, and record the smallest you have seen. This is as good as remembering k number of elements closest to the origin, and replace if necessary.

    遍歷數組中的每個元素,并記錄最小的元素。 這與記住k個最接近原點的元素以及在必要時進行替換一樣好。

Both solutions actually works! But there are notable difference in the runtime complexity versus space complexity (see Big O Notation).

兩種解決方案均有效! 但是,運行時復雜度與空間復雜度之間存在顯著差異(請參閱Big O Notation )。

蠻力—方法1:排序 (Brute Force — Method 1: Sorting)

In the first method, it is very straightforward. You sort the array,

在第一種方法中,它非常簡單。 您對數組進行排序,

[ 1, 2, 3, 5, 6, 7]

And to get the smallest element (k = 1), just get the index 0 element. What about second (k = 2) element? It will be the element at index 1.

而要獲得最小的元素(k = 1),只需獲取索引為0的元素即可。 第二個(k = 2)元素呢? 它將是索引1處的元素。

The code (written as a function) will look something like this:

該代碼(作為函數編寫)將如下所示:

def kthClosestPoint(k: int, array: list):
if k < 1:
raise Exception('Invalid k') return sorted(array)[k-1]

Depending on the sorting algorithm, you will have a typical runtime complexity of O(n log n). Unlike the above code that obtains a new sorted array behind the hood which will give you a space complexity of O(n), if you sort in-place, you will have a space complexity of O(1) instead.

根據排序算法,運行時復雜度通常為O(n log n) 。 與上面的代碼在幕后獲得一個新的排序數組不同,這將為您提供O(n)的空間復雜度,如果就地排序,則將具有O(1)的空間復雜度。

But is there any possibility of further improving this method in terms of runtime complexity? Probably not.

但是是否有可能在運行時復雜性方面進一步改進此方法? 可能不是。

蠻力—方法2:記住k個元素 (Brute Force — Method 2: Remember k number of elements)

Now, instead of doing a sort, what if you just keep track of k number of elements closest to the origin?

現在,不進行排序,而只是跟蹤最接近原點的k個元素怎么辦?

Back to the same 1D example and given k = 1,

回到相同的一維示例,給定k = 1,

[ 2, 3, 1, 5, 7, 6]

You will pick up every element in the array one by one, and remember the smallest you have seen so far! Similarly for k = 2, you will remember only the 2 smallest you have seen.

您將一個接一個地拾取數組中的每個元素,并記住到目前為止所看到的最小元素! 同樣,對于k = 2,您將只記得所見過的最小的2。

Now, if you are familiar with priority queue or heap queue (I will be using heapq for Python), then you will realize that you can actually make use of this data structure to obtain k smallest elements.

現在,如果您熟悉優先級隊列或堆隊列(我將在Python中使用heapq ),那么您將意識到,您實際上可以利用此數據結構來獲取k個最小的元素。

import heapqdef kthClosestPoint(k: int, array: list):
if k < 1:
raise Exception('Invalid k') # Convert array into heap
heapq.heapify(array) return heapq.nsmallest(k, array)

If your array length (a.k.a. heap queue) is n, using this method, you will end up with a worse case runtime complexity of O(n log n), since pushing and popping an element to a heap takes O(log n). The space complexity is O(n) if you duplicate the array or in this example code, O(1) since I am doing it in place.

如果使用此方法,如果數組長度(也就是堆隊列)為n ,則最終將導致運行時復雜度為O(n log n) ,因為將元素推入和彈出到堆中需要O(log n) 。 如果您復制數組,則空間復雜度為O(n) ,或者在此示例代碼中為O(1),因為我在原地執行了此操作。

優化 (Optimization)

You can actually further improve the runtime complexity of this method by limiting the heap queue to k instead of the whole array length n:

您實際上可以通過將堆隊列限制為k而不是整個數組長度n來進一步提高此方法的運行時復雜度

import heapqdef kthClosestPoint(k: int, array: list):
if k < 1:
raise Exception('Invalid k') k_elements = [] for num in array:
heappush(k_elements, -num) if len(k_elements) > k:
heappop(k_elements) return [-num for num in k_elements]

Note that since heappop only removes the smallest element, one possibility is to invert the polarity of the elements i.e. positive integers will be negative and negative integers will be positive. This will force all large integers to appear small, hence only large integers will be removed from the heap queue.

請注意,由于heappop僅刪除最小的元素,因此一種可能性是反轉元素的極性,即正整數將為負,負整數將為正。 這將強制所有大整數看起來很小,因此只有大整數將從堆隊列中刪除。

The typical runtime complexity will be O(n log k), since you will be heappush-ing and heappop-ing every single element of the array, while the heap queue length is at most k. This is as bad as having the worse case scenario!

典型的運行時復雜度為O(n log k) ,因為您將對數組的每個單個元素進行強制和堆彈出,而堆隊列長度最多為k。 這和更糟的情況一樣糟糕!

進一步優化 (Further Optimization)

Can we further improve this for typical case? Instead of placing every element into the heap queue and removing them, can we check before we do it? Yes we can!

對于典型案例,我們可以進一步改進嗎? 除了將每個元素放入堆隊列并刪除它們之外,我們還能在執行之前檢查一下嗎? 我們可以!

If we already have a heap queue of size k, we should peek at the “largest” element in the heap queue and see if our current element is larger or smaller than that, before we push an element in. If the heap queue is still smaller than length k, we can continue to push elements into it!

如果我們已經有大小為k的堆隊列,我們應該在堆隊列中的“最大”元素偷看 ,看看我們目前的元素比更大或更小,我們在推的元素之前,如果堆隊列仍小于長度k ,我們可以繼續將元素推入其中!

import heapqdef kthClosestPoint(k: int, array: list):
if k < 1:
raise Exception('Invalid k') k_elements = [] for num in array: if len(k_elements) < k or k_elements[0] < -num:
heappush(k_elements, -num) if len(k_elements) > k:
heappop(k_elements) return [-num for num in k_elements]

Similarly, if you are dealing with 2D or even 3D data, you can modify this code to accommodate them, using the exact same method.

同樣,如果要處理2D甚至3D數據,則可以使用完全相同的方法修改此代碼以容納它們。

求解2D數據 (Solving for 2D Data)

Assuming you have data points in an array looking like this:

假設數組中的數據點如下所示:

[ (1, 2), (3, 5), (6, 7)]

The distance for each point to origin (0, 0) is simply expressed using Pythagoras theorem in its reduced form:

每個點到原點的距離(0,0)可以使用畢達哥拉斯定理以其簡化形式簡單表示:

distance = x**2 + y**2

Nothing beats looking code so by modifying the previous 1D code:

修改前面的一維代碼,沒有什么比看代碼更好的了:

import heapqdef kthClosestPoint(k: int, array: list):
if k < 1:
raise Exception('Invalid k') k_elements = [] for x, y in array: dist = x**2, y**2 if len(k_elements) < k or k_elements[0][0] < -dist:
heappush(k_elements, (-dist, x, y)) if len(k_elements) > k:
heappop(k_elements) return [[x, y] for dist, x, y in k_elements]

If you have any feedback or anything that you wish to share, feel free to drop a comment 👇!

如果您有任何反饋或希望分享的任何內容,請隨時發表評論👇!

翻譯自: https://medium.com/@kevingxyz/kth-closest-points-to-origin-899c762885e0

最接近原點的 k 個點

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/389161.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389161.shtml
英文地址,請注明出處:http://en.pswp.cn/news/389161.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

網絡瀏覽器如何工作

Behind the scenes of modern Web Browsers現代Web瀏覽器的幕后花絮 The Web Browser is inarguably the most common portal for users to access the web. The advancement of the web browsers (through the series of history) has led many traditional “thick clients”…

讓自己的頭腦極度開放

為什么80%的碼農都做不了架構師&#xff1f;>>> 一. 頭腦封閉和頭腦開放 頭腦封閉 你是否經常有這樣的經歷&#xff0c;在一次會議或者在一次小組討論時&#xff0c;當你提出一個觀點而被別人否定時&#xff0c;你非常急迫地去反駁別人&#xff0c;從而捍衛自己的尊…

簡介DOTNET 編譯原理 簡介DOTNET 編譯原理 簡介DOTNET 編譯原理

簡介DOTNET 編譯原理 相信大家都使用過 Dotnet &#xff0c;可能還有不少高手。不過我還要講講Dotnet的基礎知識&#xff0c;Dotnet的編譯原理。 Dotnet是一種建立在虛擬機上執行的語言&#xff0c;它直接生成 MSIL 的中間語言&#xff0c;再由DotNet編譯器 JIT 解釋映象為本機…

RecyclerView詳細了解

關于RecyclerView大家都不陌生了&#xff0c;它的使用也越來越受歡迎&#xff0c;現在總體了解一下RecyclerView的作用&#xff0c;為什么會有RecyclerView呢&#xff0c;我用ListView也能干所有的事情啊&#xff0c;尺有所短&#xff0c;寸有所長&#xff0c;先來看看Recycler…

案例與案例之間的非常規排版

In 1929 the Cond Nast publishing group brought Russian-born Mehemed Fehmy Agha—who had been working for the German edition of Vogue magazine—to America as art director for House & Garden, Vanity Fair, and the senior edition of Vogue.1929年&#xff0c…

熊貓分發_熊貓新手:第二部分

熊貓分發This article is a continuation of a previous article which kick-started the journey to learning Python for data analysis. You can check out the previous article here: Pandas for Newbies: An Introduction Part I.本文是上一篇文章的延續&#xff0c;該文…

淺析微信支付:申請退款、退款回調接口、查詢退款

本文是【淺析微信支付】系列文章的第八篇&#xff0c;主要講解商戶如何處理微信申請退款、退款回調、查詢退款接口&#xff0c;其中有一些坑的地方&#xff0c;會著重強調。 淺析微信支付系列已經更新七篇了喲&#xff5e;&#xff0c;沒有看過的朋友們可以看一下哦。 淺析微信…

view工作原理-計算視圖大小的過程(onMeasure)

view的視圖有兩種情況&#xff1a; 內容型視圖&#xff1a;由視圖的內容決定其大小。圖形型視圖&#xff1a;父視圖為view動態調整大小。 ### measure的本質 把視圖布局使用的“相對值”轉化成具體值的過程&#xff0c;即把WRAP_CONTENT,MATCH_PARENT轉化為具體的值。 measur…

[轉載]使用.net 2003中的ngen.exe編譯.net程序

ngen.exe程序為.net 2003自帶&#xff0c; 在&#xff1a;/windows/microsoft.net/framework/v1.1.4322目錄下ngen.exe ngen能把.net框架的東西編譯成機器碼.... 網友&#xff1a;Visual Studio .NET 2003程序的運行速度怎么樣&#xff0c;我有一個感覺&#xff0c;V…

基于Redis實現分布式鎖實戰

背景在很多互聯網產品應用中&#xff0c;有些場景需要加鎖處理&#xff0c;比如&#xff1a;秒殺&#xff0c;全局遞增ID&#xff0c;樓層生成等等。大部分的解決方案是基于DB實現的&#xff0c;Redis為單進程單線程模式&#xff0c;采用隊列模式將并發訪問變成串行訪問&#x…

數據分析 績效_如何在績效改善中使用數據分析

數據分析 績效Imagine you need to do a bank transaction, but the website is so slow. The page takes so much time to load, all you can see is a blue circle.想象您需要進行銀行交易&#xff0c;但是網站是如此緩慢。 該頁面需要花費很多時間來加載&#xff0c;您只能看…

隱私策略_隱私圖標

隱私策略During its 2020 Worldwide Developers Conference, Apple spent time on one of today’s hottest topics — privacy. During the past couple of years, Apple has been rolling out various public campaigns aiming to position itself as a company that respect…

Java中的數組

一、數組的定義type[] arrayName;type arrayName[]; 推薦第一種 二、數組的初始化 含義&#xff1a;所謂的初始化&#xff0c;就是為數組的數組元素分配內存空間&#xff0c;并為每個數組元素賦初始值 &#xff08;1&#xff09;靜態初始化&#xff1a;arrayName new type[…

您一直在尋找5+個簡單的一線工具來提升Python可視化效果

Insightful and aesthetic visualizations don’t have to be a pain to create. This article will prevent 5 simple one-liners you can add to your code to increase its style and informational value.富有洞察力和美學的可視化不必費心創建。 本文將防止您添加到代碼中…

用C#編寫的代碼經C#編譯器后,并非生成本地代碼而是生成托管代碼

用C#編寫的代碼經C#編譯器后&#xff0c;并非生成本地代碼而是生成托管代碼。也就是說&#xff0c;程序集在打包時是連同CLR一起打包的。在客戶端的機器上&#xff0c;CLR一行行的讀取IL&#xff0c;在讀取每行IL時&#xff0c;CLR利用JIT編譯器將IL編譯成本地的CPU指令。若要節…

figma 安裝插件_彩色濾光片Figma插件,用于色盲

figma 安裝插件So as a UX Designer, it is important to design with disabilities in mind. One of these is color blindness. It is important to make sure important information on your product is legible to everyone. This is why I like using this tool:因此&…

服務器運維

1.服務器和網站漏洞檢測&#xff0c;對Web漏洞、弱口令、潛在的惡意行為、違法信息等進行定期掃描&#xff1b;代碼的定期檢查&#xff0c;漏洞檢查及服務器安全加固 2.服務器數據備份&#xff0c;包括網站程序文件備份&#xff0c;數據庫文件備份、配置文件備份&#xff0c;如…

產品觀念:更好的捕鼠器_故事很重要:為什么您需要成為更好的講故事的人

產品觀念&#xff1a;更好的捕鼠器重點 (Top highlight)Telling a compelling story helps you get your point across effectively else you get lost in translation.講一個引人入勝的故事可以幫助您有效地傳達觀點&#xff0c;否則您會迷失在翻譯中。 Great stories happen…

7月15號day7總結

今天復習了springMVC的框架搭建。 思維導圖&#xff1a; 轉載于:https://www.cnblogs.com/kangy123/p/9315919.html

關于注意力的問題

問題&#xff1a;一旦持續的注意力分散和精力無法集中成為習慣性動作&#xff0c;這將成為一個嚴重的問題。 實質&#xff1a;加強有意識的集中程度和持續時間&#xff0c;盡量避免無意識注意對大腦的干擾。 不要浪費注意力。大腦以天為周期&#xff0c;每天注意力是有限的。T…