diff options
| author | mo khan <mo.khan@gmail.com> | 2020-11-30 11:43:45 -0700 |
|---|---|---|
| committer | mo khan <mo.khan@gmail.com> | 2020-11-30 11:43:45 -0700 |
| commit | ac4cbff6b2dc1c11b26eb39f599af4bbabb75f60 (patch) | |
| tree | 2602992f280e6f9a68df1e89c4484818c63971e5 /2020 | |
| parent | 0a57fa76953b4d1ef9f3553449a1365b6aaa5b7c (diff) | |
Solve buildings problem
Diffstat (limited to '2020')
| -rw-r--r-- | 2020/11/30/main.rb | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/2020/11/30/main.rb b/2020/11/30/main.rb new file mode 100644 index 0000000..2d03e38 --- /dev/null +++ b/2020/11/30/main.rb @@ -0,0 +1,27 @@ +def assert(x, y) + raise [x, y].inspect unless x == y +end + +class Solution + def self.run(buildings) + changes = [] + result = [] + buildings.each do |building| + left, right, height = building + left.upto(right) do |n| + changes[n] = height + end + end + current = nil + changes.each_with_index do |item, index| + next if item == current + + current = item + result << [index, item] + end + result << [changes.size, 0] + result + end +end + +assert Solution.run([[2,8,3], [4,6,5]]), [[2,3], [4,5], [7,3], [9,0]] |
