|
| 1 | +Below is the structured README.md snippet for **LeetCode 610: Triangle Judgement**, including the problem statement, example, solution approaches (SQL and Pandas), file structure, and useful links. |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +# **610. Triangle Judgement** |
| 6 | + |
| 7 | +## **Problem Statement** |
| 8 | +You are given a table `Triangle` that contains three integer values representing the lengths of three line segments. |
| 9 | + |
| 10 | +### **Triangle Table** |
| 11 | +``` |
| 12 | ++-------------+------+ |
| 13 | +| Column Name | Type | |
| 14 | ++-------------+------+ |
| 15 | +| x | int | |
| 16 | +| y | int | |
| 17 | +| z | int | |
| 18 | ++-------------+------+ |
| 19 | +``` |
| 20 | +- **(x, y, z)** is the **primary key**. |
| 21 | +- Each row represents the lengths of three line segments. |
| 22 | + |
| 23 | +### **Task:** |
| 24 | +Report for each row whether the three line segments can form a triangle. A triangle can be formed if and only if the sum of any two sides is greater than the third side. |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +## **Example 1:** |
| 29 | + |
| 30 | +### **Input:** |
| 31 | +#### **Triangle Table** |
| 32 | +``` |
| 33 | ++----+----+----+ |
| 34 | +| x | y | z | |
| 35 | ++----+----+----+ |
| 36 | +| 13 | 15 | 30 | |
| 37 | +| 10 | 20 | 15 | |
| 38 | ++----+----+----+ |
| 39 | +``` |
| 40 | + |
| 41 | +### **Output:** |
| 42 | +``` |
| 43 | ++----+----+----+----------+ |
| 44 | +| x | y | z | triangle | |
| 45 | ++----+----+----+----------+ |
| 46 | +| 13 | 15 | 30 | No | |
| 47 | +| 10 | 20 | 15 | Yes | |
| 48 | ++----+----+----+----------+ |
| 49 | +``` |
| 50 | + |
| 51 | +### **Explanation:** |
| 52 | +- For the first row: `13 + 15` is not greater than `30`, so the segments cannot form a triangle. |
| 53 | +- For the second row: All conditions are met (`10+20 > 15`, `10+15 > 20`, `20+15 > 10`), so they form a triangle. |
| 54 | + |
| 55 | +--- |
| 56 | + |
| 57 | +## **Solution Approaches** |
| 58 | + |
| 59 | +### **SQL Solution** |
| 60 | +```sql |
| 61 | +SELECT |
| 62 | + x, |
| 63 | + y, |
| 64 | + z, |
| 65 | + IF(x + y > z AND x + z > y AND y + z > x, 'Yes', 'No') AS triangle |
| 66 | +FROM Triangle; |
| 67 | +``` |
| 68 | +**Explanation:** |
| 69 | +- The query checks if the sum of any two sides is greater than the third side. |
| 70 | +- If all conditions are true, it returns `'Yes'`; otherwise, it returns `'No'`. |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +### **Pandas Solution** |
| 75 | +```python |
| 76 | +import pandas as pd |
| 77 | + |
| 78 | +def triangle_judgement(triangle: pd.DataFrame) -> pd.DataFrame: |
| 79 | + # Create a new column 'triangle' based on the triangle inequality conditions |
| 80 | + triangle['triangle'] = triangle.apply( |
| 81 | + lambda row: 'Yes' if (row['x'] + row['y'] > row['z'] and |
| 82 | + row['x'] + row['z'] > row['y'] and |
| 83 | + row['y'] + row['z'] > row['x']) else 'No', |
| 84 | + axis=1 |
| 85 | + ) |
| 86 | + return triangle |
| 87 | + |
| 88 | +# Example usage: |
| 89 | +# df = pd.DataFrame({'x': [13, 10], 'y': [15, 20], 'z': [30, 15]}) |
| 90 | +# print(triangle_judgement(df)) |
| 91 | +``` |
| 92 | +**Explanation:** |
| 93 | +- The Pandas solution uses `apply()` with a lambda function to evaluate the triangle inequality for each row. |
| 94 | +- It then creates a new column `triangle` with the result `'Yes'` or `'No'`. |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +## **File Structure** |
| 99 | +``` |
| 100 | +LeetCode610/ |
| 101 | +├── problem_statement.md # Contains the problem description and constraints. |
| 102 | +├── sql_solution.sql # Contains the SQL solution. |
| 103 | +├── pandas_solution.py # Contains the Pandas solution. |
| 104 | +├── README.md # Overview of the problem and available solutions. |
| 105 | +``` |
| 106 | + |
| 107 | +--- |
| 108 | + |
| 109 | +## **Useful Links** |
| 110 | +- [LeetCode Problem 610](https://leetcode.com/problems/triangle-judgement/) |
| 111 | +- [SQL IF Function](https://www.w3schools.com/sql/func_mysql_if.asp) |
| 112 | +- [Pandas apply() Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.apply.html) |
| 113 | + |
| 114 | +--- |
| 115 | + |
| 116 | +This structured format provides a comprehensive overview of the problem along with multiple solution approaches. Happy coding! 🚀 |
0 commit comments