LeetCode 2678: Number of Senior Citizens Solution
Master LeetCode problem 2678 (Number of Senior Citizens), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
Problem Explanation
Explanation:
- Iterate through the array of details.
- Extract the age information from each element.
- Check if the age is strictly greater than 60.
- Increment a counter for each passenger who is over 60.
- Return the total count of senior citizens.
Time Complexity: O(n) where n is the number of elements in the details array.
Space Complexity: O(1)
Solution Code
class Solution {
public int numSeniorCitizens(String[] details) {
int count = 0;
for (String detail : details) {
int age = Integer.parseInt(detail.substring(11, 13));
if (age > 60) {
count++;
}
}
return count;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 2678 (Number of Senior Citizens)?
This page provides optimized solutions for LeetCode problem 2678 (Number of Senior Citizens) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 2678 (Number of Senior Citizens)?
The time complexity for LeetCode 2678 (Number of Senior Citizens) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 2678 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2678 in Java, C++, or Python.