LeetCode 2678: Number of Senior Citizens

ArrayString

Problem Description

Explanation:

  1. Iterate through the array of details.
  2. Extract the age information from each element.
  3. Check if the age is strictly greater than 60.
  4. Increment a counter for each passenger who is over 60.
  5. 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)

Solutions

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;
    }
}

Loading editor...