Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active August 4, 2020 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbvf50mobile/53b34cdf18b64cb1fa46727c8374994a to your computer and use it in GitHub Desktop.
Save lbvf50mobile/53b34cdf18b64cb1fa46727c8374994a to your computer and use it in GitHub Desktop.
Leetcode: 1037. Valid Boomerang.
# Leetcode: 1037. Valid Boomerang.
# https://leetcode.com/problems/valid-boomerang/
# @param {Integer[][]} points
# @return {Boolean}
def is_boomerang(points)
p0,p1,p2 = points
x0,y0 = p0
x1,y1 = p1
x2,y2 = p2
a = (y1-y0)/(x1-x0)
b = y0 - a*x0
a*x2 + b != y2
end
# Leetcode: 1037. Valid Boomerang.
# https://leetcode.com/problems/valid-boomerang/
# Runtime: 40 ms, faster than 50.00% of Ruby online submissions for Valid Boomerang.
# Memory Usage: 9.8 MB, less than 100.00% of Ruby online submissions for Valid Boomerang.
# @param {Integer[][]} points
# @return {Boolean}
def is_boomerang(points)
return false if one_point(points)
return false if two_points(points)
return false if all_on_vertical(points)
return false if all_on_horisontal(points)
return true if two_on_vertical(points)
return true if two_on_horisontal(points)
p0,p1,p2 = points
x0,y0 = p0
x1,y1 = p1
x2,y2 = p2
a = (y1-y0)/(x1-x0)
b = y0 - a*x0
a*x2 + b != y2
end
def one_point(x)
x.uniq.size == 1
end
def two_points(x)
2 == x.uniq.size
end
def all_on_vertical(x)
x.map(&:first).uniq.size == 1
end
def all_on_horisontal(x)
x.map(&:last).uniq.size == 1
end
def two_on_vertical(x)
x.map(&:first).uniq.size == 2
end
def two_on_horisontal(x)
x.map(&:last).uniq.size == 2
end
@lbvf50mobile
Copy link
Author

[[1,1],[2,3],[3,2]]
[[1,1],[2,2],[3,3]]
[[0,0],[0,2],[2,1]]

@lbvf50mobile
Copy link
Author

[[1,1],[2,3],[3,2]]
[[1,1],[2,2],[3,3]]
[[0,0],[0,2],[2,1]]
[[0,2],[2,1],[1,1]]

@lbvf50mobile
Copy link
Author

# Leetcode: 1037. Valid Boomerang.
# https://leetcode.com/problems/valid-boomerang/
# @param {Integer[][]} points
# @return {Boolean}
def is_boomerang(points)
    # https://stackoverflow.com/questions/13242738/how-can-i-find-the-general-form-equation-of-a-line-from-two-points#:~:text=If%20you%20start%20from%20the,(y2%2Dy1)*x1%20.
    p0,p1,p2 = points
    x0,y0 = p0
    x1,y1 = p1
    x2,y2 = p2
    a = y1-y2
    b = x2-x1
    c = (x1-x2)*y1 + (y2-y1)+x1
    first = [a,b,c]
    a = y0-y2
    b = x2-x0
    c = (x0-x2)*y0 + (y2-y0)+x0
    second = [a,b,c]
    first != second
    
    
   
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment