LeetCode 2678 - Number of Senior Citizens
This problem asks us to count the number of passengers in a list who are strictly older than 60. Each passenger's information is compressed into a fixed-length string of 15 characters.
Difficulty: 🟢 Easy
Topics: Array, String
Solution
Problem Understanding
This problem asks us to count the number of passengers in a list who are strictly older than 60. Each passenger's information is compressed into a fixed-length string of 15 characters. The string is structured as follows: the first ten characters represent the phone number, the eleventh character represents gender, the twelfth and thirteenth characters represent the age, and the last two characters represent the seat number.
The input is an array of strings, each of exactly 15 characters. We are guaranteed that all phone numbers and seat numbers are unique, and that the age portion is always two digits. Our task is to parse the age from each string, check if it is greater than 60, and return a count of how many satisfy that condition.
Edge cases to consider include the minimum and maximum possible age values (e.g., "00" or "99") and ensuring that the string slicing correctly extracts the age digits without off-by-one errors. The input size is constrained to at most 100 elements, so performance is not a major concern.
Approaches
The brute-force approach is straightforward. For each passenger, we extract the substring corresponding to the age, convert it to an integer, and check if it is greater than 60. If it is, we increment a counter. Since we process each passenger independently, this approach is correct and simple. Given the problem constraints, it is already efficient enough.
No further optimization is necessary because the input size is small and the algorithm is inherently linear in complexity. The key observation is that we do not need any auxiliary data structures, and a single pass over the array suffices.
| Approach | Time Complexity | Space Complexity | Notes |
|---|---|---|---|
| Brute Force | O(n) | O(1) | Iterate through each passenger, extract age, and count if > 60 |
| Optimal | O(n) | O(1) | Same as brute force, already optimal due to constraints |
Algorithm Walkthrough
- Initialize a counter variable
senior_countto 0. - Iterate through each string
detailin thedetailsarray. - Extract the substring corresponding to the age, which is at indices 11 and 12 (0-indexed). This can be done with
detail[11:13]. - Convert the age substring to an integer.
- Check if the integer value is greater than 60. If it is, increment
senior_count. - After processing all passengers, return
senior_count.
Why it works: The algorithm correctly identifies the age portion of each string and counts only those above 60. Since every string is guaranteed to be of length 15 and the age is always represented in two digits, there are no parsing ambiguities, ensuring correctness.
Python Solution
from typing import List
class Solution:
def countSeniors(self, details: List[str]) -> int:
senior_count = 0
for detail in details:
age = int(detail[11:13])
if age > 60:
senior_count += 1
return senior_count
In this implementation, we first initialize a counter. For each passenger string, we slice out the age portion (detail[11:13]), convert it to an integer, and check if it exceeds 60. Each time it does, we increment the counter. Finally, we return the total count. This directly follows the algorithm walkthrough and handles all edge cases guaranteed by the problem.
Go Solution
import "strconv"
func countSeniors(details []string) int {
seniorCount := 0
for _, detail := range details {
ageStr := detail[11:13]
age, _ := strconv.Atoi(ageStr)
if age > 60 {
seniorCount++
}
}
return seniorCount
}
In Go, we iterate over each string in the slice, extract the age substring, convert it to an integer using strconv.Atoi, and increment the counter if the age is greater than 60. Go requires explicit error handling for Atoi, but in this problem, the input is guaranteed to be numeric in the age positions, so ignoring the error is safe. Slice indices work similarly to Python, so detail[11:13] extracts the correct two-character substring.
Worked Examples
Example 1: details = ["7868190130M7522","5303914400F9211","9273338290F4010"]
| Index | detail | age substring | age | >60? | senior_count |
|---|---|---|---|---|---|
| 0 | 7868190130M7522 | "75" | 75 | Yes | 1 |
| 1 | 5303914400F9211 | "92" | 92 | Yes | 2 |
| 2 | 9273338290F4010 | "40" | 40 | No | 2 |
Output: 2
Example 2: details = ["1313579440F2036","2921522980M5644"]
| Index | detail | age substring | age | >60? | senior_count |
|---|---|---|---|---|---|
| 0 | 1313579440F2036 | "20" | 20 | No | 0 |
| 1 | 2921522980M5644 | "56" | 56 | No | 0 |
Output: 0
Complexity Analysis
| Measure | Complexity | Explanation |
|---|---|---|
| Time | O(n) | Each passenger is processed once and age parsing is O(1) |
| Space | O(1) | Only a single counter variable is used; no additional storage |
The algorithm scales linearly with the number of passengers and uses minimal memory since no extra data structures are required.
Test Cases
# Provided examples
assert Solution().countSeniors(["7868190130M7522","5303914400F9211","9273338290F4010"]) == 2 # two over 60
assert Solution().countSeniors(["1313579440F2036","2921522980M5644"]) == 0 # none over 60
# Edge cases
assert Solution().countSeniors(["1234567890M0066"]) == 1 # exactly 66
assert Solution().countSeniors(["1234567890F0060"]) == 0 # exactly 60, should not count
assert Solution().countSeniors(["1234567890O9999","0987654321M6101"]) == 2 # ages 99 and 61
assert Solution().countSeniors(["1234567890M0000"]) == 0 # minimum age 0
assert Solution().countSeniors(["1234567890F9999"]) == 1 # maximum age 99
| Test | Why |
|---|---|
| ["7868190130M7522","5303914400F9211","9273338290F4010"] | Standard case with multiple passengers over and under 60 |
| ["1313579440F2036","2921522980M5644"] | Standard case with no seniors |
| ["1234567890M0066"] | Age just above 60 |
| ["1234567890F0060"] | Age exactly 60, should not count |
| ["1234567890O9999","0987654321M6101"] | Multiple seniors including high age |
| ["1234567890M0000"] | Minimum possible age |
| ["1234567890F9999"] | Maximum possible age |
Edge Cases
One important edge case is when the age is exactly 60. The problem specifies "strictly more than 60," so 60 should not be counted. The slicing and integer comparison handle this correctly.
Another edge case is when the age substring starts with a zero, such as "06" or "09". Parsing the string to an integer correctly interprets these as 6 and 9, avoiding off-by-one or string comparison errors.
A third edge case is the maximum allowed input length. With 100 passengers, the algorithm must handle a full array efficiently. Since it operates in O(n) time and O(1) space, it easily meets this requirement. Additionally, the fixed-length string guarantee prevents index errors when slicing the age portion.