|
| 1 | +Below is the structured README.md snippet for **LeetCode 619: Biggest Single Number**, including the problem statement, example, solution approaches (SQL and Pandas), file structure, and useful links. |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +# **619. Biggest Single Number** |
| 6 | + |
| 7 | +## **Problem Statement** |
| 8 | +You are given a table `MyNumbers` that contains integers, which may include duplicates. |
| 9 | + |
| 10 | +### **MyNumbers Table** |
| 11 | +``` |
| 12 | ++-------------+------+ |
| 13 | +| Column Name | Type | |
| 14 | ++-------------+------+ |
| 15 | +| num | int | |
| 16 | ++-------------+------+ |
| 17 | +``` |
| 18 | +- There is **no primary key** for this table. |
| 19 | +- Each row contains an integer. |
| 20 | + |
| 21 | +### **Task:** |
| 22 | +A **single number** is a number that appears **only once** in the `MyNumbers` table. |
| 23 | +Find the **largest single number**. If there is no single number, report `null`. |
| 24 | + |
| 25 | +--- |
| 26 | + |
| 27 | +## **Example 1:** |
| 28 | + |
| 29 | +### **Input:** |
| 30 | +``` |
| 31 | +MyNumbers table: |
| 32 | ++-----+ |
| 33 | +| num | |
| 34 | ++-----+ |
| 35 | +| 8 | |
| 36 | +| 8 | |
| 37 | +| 3 | |
| 38 | +| 3 | |
| 39 | +| 1 | |
| 40 | +| 4 | |
| 41 | +| 5 | |
| 42 | +| 6 | |
| 43 | ++-----+ |
| 44 | +``` |
| 45 | + |
| 46 | +### **Output:** |
| 47 | +``` |
| 48 | ++-----+ |
| 49 | +| num | |
| 50 | ++-----+ |
| 51 | +| 6 | |
| 52 | ++-----+ |
| 53 | +``` |
| 54 | + |
| 55 | +### **Explanation:** |
| 56 | +- The single numbers (appear exactly once) are: **1, 4, 5, 6**. |
| 57 | +- The largest among these is **6**. |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +## **Example 2:** |
| 62 | + |
| 63 | +### **Input:** |
| 64 | +``` |
| 65 | +MyNumbers table: |
| 66 | ++-----+ |
| 67 | +| num | |
| 68 | ++-----+ |
| 69 | +| 8 | |
| 70 | +| 8 | |
| 71 | +| 7 | |
| 72 | +| 7 | |
| 73 | +| 3 | |
| 74 | +| 3 | |
| 75 | +| 3 | |
| 76 | ++-----+ |
| 77 | +``` |
| 78 | + |
| 79 | +### **Output:** |
| 80 | +``` |
| 81 | ++------+ |
| 82 | +| num | |
| 83 | ++------+ |
| 84 | +| null | |
| 85 | ++------+ |
| 86 | +``` |
| 87 | + |
| 88 | +### **Explanation:** |
| 89 | +- There are no single numbers (all numbers appear more than once), so the result is `null`. |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +## **Solution Approaches** |
| 94 | + |
| 95 | +### **SQL Solution** |
| 96 | +```sql |
| 97 | +SELECT MAX(num) AS num |
| 98 | +FROM ( |
| 99 | + SELECT num |
| 100 | + FROM MyNumbers |
| 101 | + GROUP BY num |
| 102 | + HAVING COUNT(num) = 1 |
| 103 | +) AS unique_numbers; |
| 104 | +``` |
| 105 | +**Explanation:** |
| 106 | +- The subquery groups by `num` and filters to include only those numbers that appear exactly once (`HAVING COUNT(num) = 1`). |
| 107 | +- The outer query returns the maximum value from these unique numbers. |
| 108 | +- If no unique number exists, `MAX(num)` returns `null`. |
| 109 | + |
| 110 | +--- |
| 111 | + |
| 112 | +### **Pandas Solution** |
| 113 | +```python |
| 114 | +import pandas as pd |
| 115 | + |
| 116 | +def biggest_single_number(my_numbers: pd.DataFrame) -> pd.DataFrame: |
| 117 | + # Group by 'num' and filter those numbers that appear exactly once |
| 118 | + unique_numbers = my_numbers.groupby('num').filter(lambda group: len(group) == 1) |
| 119 | + |
| 120 | + # Determine the largest single number, if any |
| 121 | + if unique_numbers.empty: |
| 122 | + result = None |
| 123 | + else: |
| 124 | + result = unique_numbers['num'].max() |
| 125 | + |
| 126 | + return pd.DataFrame({'num': [result]}) |
| 127 | + |
| 128 | +# Example usage: |
| 129 | +# df = pd.DataFrame({'num': [8, 8, 3, 3, 1, 4, 5, 6]}) |
| 130 | +# print(biggest_single_number(df)) |
| 131 | +``` |
| 132 | +**Explanation:** |
| 133 | +- The solution groups the DataFrame by `num` and filters groups where the number appears exactly once. |
| 134 | +- It then calculates the maximum from the filtered DataFrame. |
| 135 | +- If there are no unique numbers, it returns `None`. |
| 136 | + |
| 137 | +--- |
| 138 | + |
| 139 | +## **File Structure** |
| 140 | +``` |
| 141 | +LeetCode619/ |
| 142 | +├── problem_statement.md # Contains the problem description and constraints. |
| 143 | +├── sql_solution.sql # Contains the SQL solution. |
| 144 | +├── pandas_solution.py # Contains the Pandas solution for Python users. |
| 145 | +├── README.md # Overview of the problem and available solutions. |
| 146 | +``` |
| 147 | + |
| 148 | +--- |
| 149 | + |
| 150 | +## **Useful Links** |
| 151 | +- [LeetCode Problem 619](https://leetcode.com/problems/biggest-single-number/) |
| 152 | +- [SQL GROUP BY and HAVING Clause](https://www.w3schools.com/sql/sql_groupby.asp) |
| 153 | +- [Pandas GroupBy Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.groupby.html) |
| 154 | +- [Pandas filter() Documentation](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.filter.html) |
| 155 | + |
0 commit comments