返回介绍

solution / 2600-2699 / 2678.Number of Senior Citizens / README_EN

发布于 2024-06-17 01:03:01 字数 4686 浏览 0 评论 0 收藏 0

2678. Number of Senior Citizens

中文文档

Description

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:

  • The first ten characters consist of the phone number of passengers.
  • The next character denotes the gender of the person.
  • The following two characters are used to indicate the age of the person.
  • The last two characters determine the seat allotted to that person.

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 <= 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.

Solutions

Solution 1: Traversal and Counting

We can traverse each string $x$ in details and convert the $12$th and $13$th characters (indexed at $11$ and $12$) of $x$ to integers, and check if they are greater than $60$. If so, we add one to the answer.

After the traversal, we return the answer.

The time complexity is $O(n)$, where $n$ is the length of details. The space complexity is $O(1)`.

class Solution:
  def countSeniors(self, details: List[str]) -> int:
    return sum(int(x[11:13]) > 60 for x in details)
class Solution {
  public int countSeniors(String[] details) {
    int ans = 0;
    for (var x : details) {
      int age = Integer.parseInt(x.substring(11, 13));
      if (age > 60) {
        ++ans;
      }
    }
    return ans;
  }
}
class Solution {
public:
  int countSeniors(vector<string>& details) {
    int ans = 0;
    for (auto& x : details) {
      int age = stoi(x.substr(11, 2));
      ans += age > 60;
    }
    return ans;
  }
};
func countSeniors(details []string) (ans int) {
  for _, x := range details {
    age, _ := strconv.Atoi(x[11:13])
    if age > 60 {
      ans++
    }
  }
  return
}
function countSeniors(details: string[]): number {
  let ans = 0;
  for (const x of details) {
    const age = parseInt(x.slice(11, 13));
    if (age > 60) {
      ++ans;
    }
  }
  return ans;
}
impl Solution {
  pub fn count_seniors(details: Vec<String>) -> i32 {
    let mut ans = 0;

    for s in details.iter() {
      if let Ok(age) = s[11..13].parse::<i32>() {
        if age > 60 {
          ans += 1;
        }
      }
    }

    ans
  }
}

Solution 2

function countSeniors(details: string[]): number {
  return details.filter(v => parseInt(v.slice(11, 13)) > 60).length;
}
impl Solution {
  pub fn count_seniors(details: Vec<String>) -> i32 {
    details
      .iter()
      .filter_map(|s| s[11..13].parse::<i32>().ok())
      .filter(|&age| age > 60)
      .count() as i32
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文