Skip to main content

Number of Days in a Month - Video Solutions

EasyMath

Leetcode 1118. Number of Days in a Month Python Solution

TechZoo
4:01199 views
3 video solutions available

Number of Days in a Month - Video Solution

Watch 3 video solutions for Number of Days in a Month, a easy level problem involving Math. This walkthrough by TechZoo has 199 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

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 <= 2100
  • 1 <= month <= 12
Read full problem with examples

Approach Overview

Problem 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.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Month Table + Leap Year CheckO(1)O(1)Best general solution. Simple array lookup with a leap year adjustment for February.
Conditional Month GroupsO(1)O(1)Useful if you prefer explicit conditional logic instead of maintaining a lookup table.