Difficulty: Easy | Acceptance: 50.00% | Paid: No Topics: Math, String
Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the day number of the year.
- Examples
- Constraints
- Iterative Summation
- Pre-computed Cumulative Days
- Built-in Date Functions
Examples
Example 1
Input:
date = "2019-01-09"
Output:
9
Explanation: Given date is the 9th day of the year in 2019.
Example 2
Input:
date = "2019-02-10"
Output:
41
Constraints
date.length == 10
date[4] == date[7] == '-'
All other characters are digits
date represents a valid calendar date between Jan 1st, 1900 and Dec 31st, 2019.
Iterative Summation
Intuition Parse the date into year, month, and day components. Sum the days of all preceding months and add the current day, accounting for leap years.
Steps
- Parse the year, month, and day from the input string
- Check if the year is a leap year (divisible by 4, not by 100 unless also by 400)
- Create an array of days in each month, adjusting February for leap years
- Sum the days for all months before the current month
- Add the day of the current month to get the result
python
class Solution:
def dayOfYear(self, date: str) -> int:
year, month, day = map(int, date.split('-'))
def is_leap(y):
return y % 400 == 0 or (y % 4 == 0 and y % 100 != 0)
days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if is_leap(year):
days_in_month[1] = 29
result = 0
for i in range(month - 1):
result += days_in_month[i]
result += day
return resultComplexity
- Time: O(1) - We iterate through at most 12 months, which is a constant
- Space: O(1) - We use a fixed-size array of 12 elements
- Notes: Simple and intuitive approach with minimal overhead
Pre-computed Cumulative Days
Intuition Pre-compute the cumulative days before each month for a non-leap year. Add the current day and adjust for leap years if the month is after February.
Steps
- Parse the year, month, and day from the input string
- Use a pre-computed array of cumulative days before each month
- Add the cumulative days for the current month to the day
- If the month is after February and it’s a leap year, add 1
python
class Solution:
def dayOfYear(self, date: str) -> int:
year, month, day = map(int, date.split('-'))
def is_leap(y):
return y % 400 == 0 or (y % 4 == 0 and y % 100 != 0)
cumulative_days = [0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334]
result = cumulative_days[month] + day
if month > 2 and is_leap(year):
result += 1
return resultComplexity
- Time: O(1) - Direct array lookup and arithmetic operations
- Space: O(1) - We use a fixed-size array of 13 elements
- Notes: More efficient than iterative approach with no loop overhead
Built-in Date Functions
Intuition Leverage the language’s built-in date/time libraries to parse the date and directly get the day of the year.
Steps
- Parse the input string into a date object using the language’s date library
- Use the library’s built-in method to get the day of the year
- Return the result
python
from datetime import datetime
class Solution:
def dayOfYear(self, date: str) -> int:
dt = datetime.strptime(date, '%Y-%m-%d')
return dt.timetuple().tm_ydayComplexity
- Time: O(1) - Depends on the language’s date library implementation
- Space: O(1) - Depends on the language’s date library implementation
- Notes: Most concise solution but relies on library availability and behavior