
Problem Statement
In the given problem, we receive an array of strings, details
, each string being exactly 15 characters long. The string encodes various pieces of information about a passenger's travel details:
- The first ten characters represent the phone number of the passenger.
- The eleventh character stands for the gender of the passenger, denoted by 'M' for male, 'F' for female, and 'O' for other.
- The twelfth and thirteenth characters represent the age of the passenger as a two-digit number.
- The last two characters represent the seat number assigned to the passenger.
The task is to compute the number of passengers from this list whose age is strictly greater than 60 years.
Examples
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 <= 100
details[i].length == 15
details[i] consists of digits from '0' to '9'.
details[i][10] is either 'M' or 'F' or 'O'.
- The phone numbers and seat numbers of the passengers are distinct.
Approach and Intuition
To solve this problem, we can use a simple approach:
- Initialize a counter at 0 to keep track of passengers older than 60.
- Iterate through each string in the
details
array. - For each string:
- Extract the substring corresponding to the age, which are the characters at index 11 and 12.
- Convert these two characters to an integer to get the age.
- Check if this integer is greater than 60.
- If true, increase the counter by one.
- After finishing the iteration, the counter will have the total count of passengers who are older than 60.
This method is efficient and straightforward, given the specific format and constraints of the problem. The length of each string is constant at 15, and the array's length is at most 100, making this solution feasible within these limits.
Solutions
- C++
- Java
- Python
class Solution {
public:
int countElderly(vector<string>& data) {
int elders = 0;
for (string& personInfo : data) {
int decades = personInfo[11] - '0';
int years = personInfo[12] - '0';
int fullAge = decades * 10 + years;
if (fullAge > 60) {
elders++;
}
}
return elders;
}
};
This C++ solution tackles the challenge of determining the number of senior citizens in a given dataset. Define senior citizens as individuals older than 60 years. The function countElderly
processes a vector of strings, where each string represents a person's information.
- Parse the age directly from each string, assuming the string stores age at specific positions:
- Extract the digit representing the tens place of the age from position
11
. - Extract the digit representing the units place from position
12
. - Compute the full age using these two extracted values.
- Extract the digit representing the tens place of the age from position
- Increment the
elders
count each time a person's age exceeds 60.
This method efficiently counts senior citizens using direct access via string indices coupled with straightforward arithmetic. Ensure data strings are uniformly formatted for accurate age parsing. The solution uses effective iteration and condition checks while maintaining readability and simplicity.
class Solution {
public int calculateSeniorCitizens(String[] records) {
int countOfSeniors = 0;
for (String record : records) {
int tensDigit = record.charAt(11) - '0';
int onesDigit = record.charAt(12) - '0';
int fullAge = tensDigit * 10 + onesDigit;
if (fullAge > 60) {
countOfSeniors++;
}
}
return countOfSeniors;
}
}
This Java solution addresses the task of counting the number of senior citizens from a list of records, where each record includes age information. The method calculateSeniorCitizens
takes an array of strings as input, each representing a person's record. It initializes a counter countOfSeniors
to zero, then iterates through each record to extract the age of each individual.
Extract the age using two characters from the record string:
- Determine the tens place of the age by converting the character at the 11th position of the string.
- Determine the ones place by converting the character at the 12th position.
These two digits form the complete age, which is then checked:
- If the age exceeds 60, the method increments the senior citizens count.
Finally, the method returns the total count of seniors, providing a straightforward way to find out how many individuals over the age of 60 are present in the records.
class Solution:
def countOlderPassengers(self, passenger_details: List[str]) -> int:
count_of_seniors = 0
for info in passenger_details:
tens_digit = ord(info[11]) - ord('0')
ones_digit = ord(info[12]) - ord('0')
full_age = tens_digit * 10 + ones_digit
if full_age > 60:
count_of_seniors += 1
return count_of_seniors
The given Python code defines a Solution
class with a method countOlderPassengers
, which calculates the number of senior citizens among a list of passengers, assuming each passenger's age is contained within a specific format in the input list passenger_details
.
Here's a breakdown of the procedure:
- Initialize
count_of_seniors
to zero to keep track of the number of passengers aged over 60. - Iterate through each string in
passenger_details
:- Extract the tens and ones place values from specific positions in the string to construct the age of the passenger.
- Check if the computed age is greater than 60.
- If true, increment
count_of_seniors
.
- Finally, the method returns the count of senior citizens.
This solution efficiently parses and evaluates each passenger's age based on their details provided in the list, using standard string and arithmetic operations for age calculation and comparison.
No comments yet.