You are given a 0-indexed array of strings details. Each element of details provides information about a given passenger compressed into a string of length 15. The system is such that:
Return the number of passengers who are strictly more than 60 years old.
Example 1:
Input: details = ["7868190130M7522","5303914400F9211","9273338290F4010"] Output: 2 Explanation: The passengers at indices 0, 1, and 2 have ages 75, 92, and 40. Thus, there are 2 people who are over 60 years old.
Example 2:
Input: details = ["1313579440F2036","2921522980M5644"] Output: 0 Explanation: None of the passengers are older than 60.
Constraints:
1 <= details.length <= 100details[i].length == 15details[i] consists of digits from '0' to '9'.details[i][10] is either 'M' or 'F' or 'O'.Approach: Extract the age from each detail string by selecting the appropriate substring. Convert this substring to an integer and count how many such integers are greater than 60.
The given details string has a fixed format. We know from the problem description that the age of the person is stored between the 11th and 12th character indices. By iterating over each string, extracting these characters, converting them to a number, and then checking if the number is greater than 60, we can count the number of senior citizens.
This function iterates through each string in the list, extracts the age substring (at indices 11 to 12), converts it to an integer, and checks if it is greater than 60. If so, it increments the counter senior_count.
JavaScript
C
C++
Java
C#
The time complexity is O(n) where n is the number of entries in details.
The space complexity is O(1) as we are using a fixed amount of additional space.
Approach: Instead of using string slicing or substrings, calculate the age by examining the individual characters and converting them into the age. This approach does not explicitly create a substring but directly works with character indices and mathematical operations.
This avoids the creation of additional strings and might be beneficial in languages where string manipulation is costly.
Here, we manually form the age by converting both age characters at indices 11 and 12 to integers and then calculating the age. We increment senior_count if the calculated age is greater than 60.
JavaScript
C
C++
Java
C#
The time complexity is O(n) due to single traversal through details. The space complexity remains O(1).
| Approach | Complexity |
|---|---|
| String Substring Extraction | The time complexity is |
| Character-Based Calculation | The time complexity is |
Number of Senior Citizens - Leetcode 2678 - Python • NeetCodeIO • 5,626 views views
Watch 9 more video solutions →Practice Number of Senior Citizens with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor