넘치게 채우기

[LeetCode] 2678. Number of Senior Citizens 본문

PS/LeetCode

[LeetCode] 2678. Number of Senior Citizens

riveroverflow 2024. 8. 1. 09:23
728x90
반응형

https://leetcode.com/problems/number-of-senior-citizens/description/?envType=daily-question&envId=2024-08-01

leetcode - Number of Senior Citizens

문제 유형 : 문자열 처리

문제 난이도 : Easy

 

문제

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.

 

0-indexed의 문자열 배열 details가 주어진다. 승객들의 정보가 주어진다.

각 요소는 15자의 길이로 이루어져 있는데, 요소는 다음과 같다:

  • 첫 10글자는 전화번호이다.
  • 그 다음 문자는 성별을 나타낸다.
  • 그 이후 두 글자는 나이를 나타낸다.
  • 마지막 두 문잔느 할당된 좌석번호이다.

60세 초과인 승객의 수를 구하시오.

 

풀이

details의 요소의 11번째 인덱스에 10을 곱한 값과 12번째 인덱스를 더하면 나이가 나온다.

단, 문자이니 형변환에 유의해야 한다.

구한 나이가 60보다 크면, ans에 1을 누적하라.

 

코드

C++

#pragma GCC optimize("03", "unroll-loops");
static const int __ = [](){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    return 0;
}();

class Solution {
public:
    int countSeniors(vector<string>& details) {
        int ans = 0;
        for(const auto &detail : details) {
            if((detail[11] - '0') * 10 + (detail[12] - '0') > 60) ans++;
        }

        return ans;
    }
};
728x90
반응형