Want Help Cracking FAANG?

(Then click this)

×
Back to Question Bank

1118. Number of Days in a Month - Leetcode Solution

Code Implementation

def numberOfDays(year: int, month: int) -> int:
    # February: check for leap year
    if month == 2:
        if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
            return 29
        else:
            return 28
    # Months with 31 days
    if month in [1, 3, 5, 7, 8, 10, 12]:
        return 31
    # Months with 30 days
    return 30
      
class Solution {
public:
    int numberOfDays(int year, int month) {
        if (month == 2) {
            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
                return 29;
            else
                return 28;
        }
        if (month == 1 || month == 3 || month == 5 || month == 7 ||
            month == 8 || month == 10 || month == 12)
            return 31;
        return 30;
    }
};
      
class Solution {
    public int numberOfDays(int year, int month) {
        if (month == 2) {
            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
                return 29;
            else
                return 28;
        }
        if (month == 1 || month == 3 || month == 5 || month == 7 ||
            month == 8 || month == 10 || month == 12)
            return 31;
        return 30;
    }
}
      
function numberOfDays(year, month) {
    if (month === 2) {
        if ((year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0)) {
            return 29;
        } else {
            return 28;
        }
    }
    if ([1, 3, 5, 7, 8, 10, 12].includes(month)) {
        return 31;
    }
    return 30;
}
      

Problem Description

Given two integers, year and month, return the number of days in that month for the specified year. For example, February has either 28 or 29 days depending on whether the year is a leap year. The input values are guaranteed to represent a valid year and month (1 ≤ month ≤ 12, year ≥ 1).

Key constraints:

  • Only one correct answer for each input.
  • No need to check for invalid months or years.
  • Leap year rules apply for February.

Thought Process

The problem asks us to determine how many days are in a given month, considering leap years for February. At first glance, you might think of using a big set of if-else statements for each month, but that would be repetitive and hard to maintain. Instead, we can use the known rules of the Gregorian calendar:

  • Most months have a fixed number of days (either 30 or 31).
  • February is the only exception, with 28 or 29 days depending on whether the year is a leap year.
  • Leap years occur every 4 years, except for years that are multiples of 100 but not 400.

By focusing on these rules, we can quickly map each month to its day count, with a special case for February.

Solution Approach

  • Step 1: Check if the month is February (i.e., month == 2).
  • Step 2: If it is February, determine if the year is a leap year:
    • A year is a leap year if it is divisible by 4, but not by 100, unless it is also divisible by 400.
  • Step 3: If it is a leap year, return 29 for February; otherwise, return 28.
  • Step 4: For other months, check if the month is one of those with 31 days (January, March, May, July, August, October, December). If so, return 31.
  • Step 5: For the remaining months (April, June, September, November), return 30.

This approach is efficient because it uses simple conditionals and requires no loops or complex data structures. The leap year check is performed only if necessary.

Example Walkthrough

Let's take an example: year = 2024, month = 2.

  1. First, we check if month == 2. It is, so we proceed to the leap year check.
  2. 2024 is divisible by 4 (2024 % 4 == 0), and not by 100 (2024 % 100 != 0), so it is a leap year.
  3. Since it's a leap year, we return 29.

Another example: year = 2023, month = 11.

  1. Check if month == 2. It's not, so we check if 11 is in the set of months with 31 days. It's not.
  2. Therefore, November has 30 days, so we return 30.

Time and Space Complexity

  • Brute-force Approach: If you were to use a brute-force approach (like a big switch statement for every year and month), it would still be O(1) since the input space is tiny and bounded.
  • Optimized Approach: The current solution is O(1) time and O(1) space, since it only uses a few conditional checks and no extra data structures or recursion.

The efficiency comes from mapping months directly to their day counts and only checking leap years when necessary.

Summary

To solve the "Number of Days in a Month" problem, we leveraged the rules of the Gregorian calendar and handled February as a special case with a leap year check. All other months have a fixed number of days. The solution is concise, fast, and easy to maintain, making it both elegant and practical for real-world use.