Domain 1EasyCORE

Number of Days in a Month

The problem asks us to determine the number of days in a given month of a specific year. The inputs are: - `Y`: an integer representing the year (1583 <= Y <= 2100) - `M`: an integer representing the month (1 <= M <= 12)

Domain: Easy•Updated: 2026-08-31
#Algorithms#LeetCode#qwen2.5-coder:14b#**FAANG asked:** 1

1118. Number of Days in a Month

Model: qwen2.5-coder:14b
Topics:
FAANG asked: 1
Acceptance rate: 57.4%


Problem Breakdown

The problem asks us to determine the number of days in a given month of a specific year. The inputs are:

  • Y: an integer representing the year (1583 <= Y <= 2100)
  • M: an integer representing the month (1 <= M <= 12)

The output is an integer representing the number of days in that month.

Implicit constraints and edge cases include:

  • Handling leap years correctly, especially for February.
  • Months with different numbers of days: January, March, May, July, August, October, December have 31 days; April, June, September, November have 30 days.
  • Special handling for February in leap years (29 days) versus non-leap years (28 days).

Intuition

A brute-force approach would involve checking the month and then determining if it’s a leap year to decide the number of days. This is insufficient because it requires conditional checks for each month, leading to a complex solution.

The key insight is to use an array or list to store the number of days in each month, with special handling for February. This reduces the complexity by allowing direct access to the number of days based on the month index.

Approach

  1. Create an array days_in_month where the index represents the month (0-based) and the value at that index is the number of days in that month.
  2. For February, store 28 as a default since it’s the most common case.
  3. Write a helper function is_leap_year(Y) to determine if a year is a leap year:
    • A year is a leap year if it is divisible by 4.
    • However, if the year is divisible by 100, it is not a leap year unless it is also divisible by 400.
  4. If the month is February and the year is a leap year, return 29 days.
  5. Otherwise, return the number of days from the days_in_month array.

Complexity Analysis

  • Time complexity: O(1) - The solution involves constant-time operations regardless of input size.
  • Space complexity: O(1) - The space used is constant as it does not depend on the input size.

Solution

def numberOfDays(Y: int, M: int) -> int:
    # Array to store the number of days in each month (0-based index)
    days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    
    # Helper function to determine if a year is a leap year
    def is_leap_year(Y: int) -> bool:
        return Y % 4 == 0 and (Y % 100 != 0 or Y % 400 == 0)
    
    # If it's February and a leap year, return 29 days
    if M == 2 and is_leap_year(Y):
        return 29
    
    # Otherwise, return the number of days in the month
    return days_in_month[M - 1]

Edge Cases Handled

  • Leap Year Check: The function correctly identifies leap years (e.g., 2000) and non-leap years (e.g., 1900).
  • February Days: Handles February with 28 days in a non-leap year and 29 days in a leap year.
  • Month Range: Ensures that the month is between 1 and 12, as specified in the problem constraints.