Advertisement
brewersfan1976

segments_union.rb

Mar 26th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.38 KB | None | 0 0
  1. def segmentsUnion(left, right)
  2.    x = 1
  3.    sum = 0
  4.    left = left.sort
  5.    right = right.sort
  6.    sum = right[0] - left[0]
  7.    
  8.    while (x < left.length)
  9.          if left[x] < right[x - 1] then
  10.             sum = right[x] - left[0]
  11.          else
  12.             sum = sum + (right[x] - left[x])
  13.          end
  14.            
  15.          x = x + 1
  16.    end
  17.      
  18.    return sum
  19.    
  20. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement