#1527
Patients With a Condition
EasyDatabaseHash MapArray
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n²) | O(n) |
| Space | O(1) | O(n) |
💡
Intuition
Time O(n)Space O(n)
We can optimize the search by using the SQL 'LIKE' operator directly to filter patients who have conditions starting with 'DIAB1'. This avoids unnecessary checks and is efficient.
⚙️
Algorithm
3 steps- 1Step 1: Use a SQL query to select patient_id, patient_name, and conditions from the Patients table.
- 2Step 2: Apply a WHERE clause to filter conditions that start with 'DIAB1'.
- 3Step 3: Return the results.
solution.py1 lines
1SELECT patient_id, patient_name, conditions FROM Patients WHERE conditions LIKE 'DIAB1%';ℹ
Complexity note: The time complexity is O(n) because we are scanning through the list of patients once. The space complexity is O(n) for storing the results.
- 1Using SQL's LIKE operator allows for efficient string matching.
- 2Filtering directly in the query reduces unnecessary computations.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.