Number of Days in a Month - Problem
Welcome to your role as a calendar system developer! You need to build a function that accurately determines the number of days in any given month of any year.
Given two integers year and month, return the exact number of days in that month. This might seem simple, but there's a catch - you need to handle leap years correctly!
Remember: February has 28 days in regular years, but 29 days in leap years. A leap year occurs every 4 years, except for years divisible by 100 (unless they're also divisible by 400).
Examples: January 2023 has 31 days, February 2020 (leap year) has 29 days, February 2021 (not leap year) has 28 days.
Input & Output
example_1.py โ Regular month
$
Input:
year = 2023, month = 1
โบ
Output:
31
๐ก Note:
January always has 31 days regardless of the year
example_2.py โ Leap year February
$
Input:
year = 2020, month = 2
โบ
Output:
29
๐ก Note:
2020 is a leap year (divisible by 4), so February has 29 days
example_3.py โ Non-leap year February
$
Input:
year = 2021, month = 2
โบ
Output:
28
๐ก Note:
2021 is not a leap year (not divisible by 4), so February has 28 days
Constraints
- 1 โค month โค 12
- 1583 โค year โค 104
- Follow Gregorian calendar rules
Visualization
Tap to expand
Understanding the Visualization
1
Month Classification
Identify if the month is February (variable) or others (fixed)
2
Leap Year Detection
For February, apply the leap year formula
3
Direct Calculation
Return the appropriate number of days instantly
Key Takeaway
๐ฏ Key Insight: The optimal solution recognizes that only February has variable days - all other months are constants. This reduces the problem to a simple lookup with one special case handling.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code