給定一個旅客的車票字串陣列,每個字串的最後第四個和第三個數字代表旅客的年齡。
例如: XXX...XXX2015 代表旅客年齡為20歲。
請計算總共有多少位乘客的年齡 > 60 歲?
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
輸入陣列長度介於1~100之間。
details[i].length == 15
每個旅客字串長度為15
details[i] consists of digits from '0' to '9'.
旅客字串裡面的數字一定在0~9的範圍內
details[i][10] is either 'M' or 'F' or 'O'.
第11碼代表性別
每位旅客的電話號碼和座號保證都相異
根據題意,擷取年齡所在的字串位置,轉成數字,統計有多少位旅客 > 60歲。
class Solution:
def countSeniors(self, details: List[str]) -> int:
return sum( ( 1 for d in details if int(d[-4:-2]) > 60 ) )
線性掃描每一筆旅客資料,所需時間為O(n)
所用到的都是固定尺寸的臨時變數,所需空間為O(1),常數級別。