Problem Description
Given a year and a month, determine the number of days in that month. Note that February can have 28 or 29 days, depending on whether the given year is a leap year.
Key Insights
- Use the standard list of months with 31 and 30 days.
- Special handling is required for February:
- A leap year is divisible by 4, but not by 100 unless it is also divisible by 400.
- The solution uses constant time and space complexities, as it only involves conditional checks.
Space and Time Complexity
Time Complexity: O(1)
Space Complexity: O(1)
Solution
To solve the problem, we first check if the month is February. If it is, we determine if the year is a leap year using the given conditions. If it is a leap year, February has 29 days; otherwise, it has 28 days. For other months, we can use a mapping or conditions to output 31 days for months: January, March, May, July, August, October, and December; and 30 days for the remaining months: April, June, September, and November. This approach uses simple conditional checks without the need for additional data structures.