Date Range Generator — LeetCode 2777 Python Solution
MediumLeetCode PremiumJavaScript
- Problem
- #2777
- 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
Python
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
| Measure | Complexity |
|---|---|
| Time | O(k), where k is the number of yielded dates |
| Space | O(1) auxiliary |
Related problems
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.