Created
December 15, 2012 21:14
-
-
Save quux00/4299281 to your computer and use it in GitHub Desktop.
Implementation of http://programmingpraxis.com/2012/11/27/amazon-interview-question/2/ using a Leftist Priority Queue from here: https://gist.github.com/4299232.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns thornydev.top100.heap | |
(:require [thornydev.lib.leftist-heap | |
:refer [pq-first pq-rest pq-insert pq-empty]])) | |
(defn distance [[x y]] | |
(+ (java.lang.Math/abs x) (java.lang.Math/abs y))) | |
(defn dist-lt? [pt1 pt2] | |
(< (distance pt2) (distance pt1))) | |
(defn heap-sift [pq pt] | |
(if (dist-lt? pt (pq-first pq)) | |
pq | |
(pq-insert dist-lt? (pq-rest dist-lt? pq) pt))) | |
(defn top-heap [points max-size] | |
(let [[add-all xs] (split-at max-size points) | |
init-q (reduce #(pq-insert dist-lt? % %2) pq-empty add-all)] | |
(reduce heap-sift init-q xs))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment