Given a year year and a month month, return the number of days of that month.
Example 1:
Input: year = 1992, month = 7 Output: 31
Example 2:
Input: year = 2000, month = 2 Output: 29
Example 3:
Input: year = 1900, month = 2 Output: 28
Constraints:
1583 <= year <= 21001 <= month <= 12Problem Overview: You are given a year and a month. Return how many days exist in that month for the given year. The only tricky case is February because its length depends on whether the year is a leap year.
The problem sits in the math category and mainly tests whether you remember the leap year rules and can map months to their correct number of days.
Approach 1: Month Table + Leap Year Check (O(1) time, O(1) space)
Store the number of days for each month in a fixed array such as [31,28,31,30,31,30,31,31,30,31,30,31]. For most months, simply return the value from the table using month - 1 as the index. February requires special handling. If the month is 2, determine whether the given year is a leap year and return 29 instead of 28.
The leap year rule follows the Gregorian calendar: a year is a leap year if it is divisible by 4, except years divisible by 100 are not leap years unless they are also divisible by 400. In code, this usually becomes:
(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
After computing this condition, adjust February’s value accordingly. All other months return the precomputed value directly from the array. The algorithm performs only a few arithmetic checks and one array lookup, giving constant time and space complexity.
This approach is essentially a small implementation task backed by a simple math rule. No iteration, recursion, or extra data structures are required.
Approach 2: Conditional Month Groups (O(1) time, O(1) space)
Another way is to group months by the number of days instead of using an array. Months with 31 days are {1,3,5,7,8,10,12}. Months with 30 days are {4,6,9,11}. February is handled separately using the same leap year rule.
The logic becomes a sequence of conditional checks: if the month belongs to the 31-day set, return 31; if it belongs to the 30-day set, return 30; otherwise handle February with the leap year formula. This version avoids arrays but expresses the same idea through conditionals.
Recommended for interviews: The month-table approach is usually the cleanest and most readable. Interviewers expect you to know the leap year rule and apply it correctly. Writing the simple array plus the leap-year check demonstrates solid attention to edge cases while keeping the solution O(1) in both time and space.
We can first determine whether the given year is a leap year. If the year can be divided by 4 but not by 100, or can be divided by 400, then this year is a leap year.
February has 29 days in a leap year and 28 days in a common year.
We can use an array days to store the number of days in each month of the current year, where days[0]=0, days[i] represents the number of days in the ith month of the current year. Then the answer is days[month].
The time complexity is O(1), and the space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Month Table + Leap Year Check | O(1) | O(1) | Best general solution. Simple array lookup with a leap year adjustment for February. |
| Conditional Month Groups | O(1) | O(1) | Useful if you prefer explicit conditional logic instead of maintaining a lookup table. |
Leetcode 1118. Number of Days in a Month Python Solution • TechZoo • 199 views views
Watch 2 more video solutions →Practice Number of Days in a Month with our built-in code editor and test cases.
Practice on FleetCode