MediumNo. 2777LeetCode Premium
Date Range Generator — LeetCode 2777 Python Solution
- Topics
- JavaScript
- Reading time
- 2 min
- Source
- leetcode.com
The problem
Given a start date start, an end date end, and a positive integer step, return a generator object that yields dates in the range from start to end inclusive. The value of step indicates the number of days between consecutive yielded values.
Example
- Input
- start = "2023-04-01", end = "2023-04-04", step = 1
- Output
- ["2023-04-01","2023-04-02","2023-04-03","2023-04-04"]
- Explanation
- const g = dateRangeGenerator(start, end, step);
Python solution
from datetime import date, timedelta
def dateRangeGenerator(start: str, end: str, step: int):
cur = date.fromisoformat(start)
last = date.fromisoformat(end)
delta = timedelta(days=step)
while cur <= last:
yield cur.isoformat()
cur += deltaComplexity
- Time
- O(k)where k is the number of yielded dates
- Space · auxiliary
- O(1)
Related problems
| No. | Problem | Difficulty |
|---|---|---|
| 2778 | Sum of Squares of Special Elements | Easy |
| 0057 | Insert Interval | Medium |
| 0192 | Word Frequency | Medium |
| 0193 | Valid Phone Numbers | Easy |
| 0194 | Transpose File | Medium |
| 0195 | Tenth Line | Easy |
Frequently asked questions
- How hard is LeetCode 2777. Date Range Generator?
LeetCode 2777. Date Range Generator is rated Medium on LeetCode.
- What is the time complexity of LeetCode 2777. Date Range Generator?
The Python solution on this page runs in O(k), where k is the number of yielded dates.
- What is the space complexity of LeetCode 2777. Date Range Generator?
The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 2777. Date Range Generator cover?
LeetCode 2777. Date Range Generator is tagged JavaScript on LeetCode.
- Is LeetCode 2777. Date Range Generator a premium problem?
Yes. LeetCode 2777. Date Range Generator is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.