PU Triangle Judgement

Jan 01, 1970

A pupil Tim gets homework to identify whether three line segments could possibly form a triangle.

However, this assignment is very heavy because there are hundreds of records to calculate.
Could you help Tim by writing a query to judge whether these three sides can form a triangle, assuming table triangle holds the length of the three sides x, y and z.

| x  | y  | z  |
|----|----|----|
| 13 | 15 | 30 |
| 10 | 20 | 15 |

For the sample data above, your query should return the follow result:

| x  | y  | z  | triangle |
|----|----|----|----------|
| 13 | 15 | 30 | No       |
| 10 | 20 | 15 | Yes      |

Solution 1:

# Write your MySQL query statement below
SELECT x, y, z, 
    IF((x <= z and y <= z and x + y > z) or (x <= y and z <= y and x + z > y) or (y <= x and z <= x and y + z > x), 'Yes', 'No')
    AS triangle
FROM triangle

Solution 2:

# Write your MySQL query statement below
SELECT x, y, z, 
    IF(x + y > z and x + z > y and y + z > x, 'Yes', 'No')
    AS triangle
FROM triangle

Solution 3:

# Write your MySQL query statement below
SELECT x, y, z, 
    IF(x + y <= z or x + z <= y or y + z <= x, 'No', 'Yes')
    AS triangle
FROM triangle

Summary:

  • nothing to say

LeetCode: 610. Triangle Judgement