문제
Return the number of days in month of year in the Gregorian calendar. February has 29 days in a leap year: years divisible by 4, except those divisible by 100 unless also divisible by 400. A helper function is_leap(year) keeps the code tidy.
Implement days_in_month(year, month).
Input
year: The year. month: The month, 1 (January) to 12 (December).
Output
Return the number of days.
제약 조건
1 ≤ year ≤ 9,999; 1 ≤ month ≤ 12
예시
입력:
year = 2024
month = 2
예상 출력:
expected = 29
입력:
long long year = 2024;
long long month = 2;
예상 출력:
long long expected = 29;
입력:
long year = 2024;
long month = 2;
예상 출력:
long expected = 29;
설명
2024 is divisible by 4 and not by 100, so it is a leap year: 29 days.
입력:
year = 1900
month = 2
예상 출력:
expected = 28
입력:
long long year = 1900;
long long month = 2;
예상 출력:
long long expected = 28;
입력:
long year = 1900;
long month = 2;
예상 출력:
long expected = 28;
설명
1900 is divisible by 100 but not 400, so it is not a leap year: 28 days.
접근 방법
먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.
힌트 1
대부분의 달은 일수가 고정이고, 2월은 윤년인지에 따라 달라집니다.
힌트 2
월별 일수는 작은 lookup으로 처리하고, 2월은 윤년 규칙으로 보정하세요.
힌트 3
100으로 나누어떨어진 해는 윤년이 아니고, 400으로도 나누어떨어져야 합니다.
예상 복잡도
시간 O(1), 추가 공간 O(1)
참고 자료
이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.