summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2022-08-29 14:27:40 -0600
committermo khan <mo@mokhan.ca>2022-08-29 14:27:40 -0600
commit44e8488d7b22262e94cfcd739a459ade1ffc064f (patch)
tree47e60e6061c6acb72feb3555a87cc80c3fe734bf
parent5b225d4b1027770ee4b08fef8b84acd1cc3920aa (diff)
reconstruct queue by height
-rw-r--r--misc/queue-reconstruct-by-height/main.rb20
1 files changed, 20 insertions, 0 deletions
diff --git a/misc/queue-reconstruct-by-height/main.rb b/misc/queue-reconstruct-by-height/main.rb
new file mode 100644
index 0000000..474d18a
--- /dev/null
+++ b/misc/queue-reconstruct-by-height/main.rb
@@ -0,0 +1,20 @@
+#/usr/bin/env ruby
+
+def assert_equal(x, y)
+ raise "expected: #{x.inspect}, actual: #{y.inspect}" unless x == y
+end
+
+def reconstruct(input)
+ sorted = input.sort_by do |item|
+ [-item[0], item[1]]
+ end
+ result = []
+ sorted.each do |item|
+ result.insert(item[1], item)
+ end
+ result
+end
+
+input = [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]]
+output = [[5, 0], [7, 0], [5, 2], [6, 1], [4, 4], [7, 1]]
+assert_equal(output, reconstruct(input))