LeetCode 1225: Report Contiguous Dates Solution
Master LeetCode problem 1225 (Report Contiguous Dates), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
1225. Report Contiguous Dates
Problem Explanation
Explanation
Given a list of date ranges, we need to report the contiguous dates within those ranges.
To solve this problem, we can follow these steps:
- Sort the date ranges based on the start date.
- Iterate through the sorted date ranges and merge overlapping ranges.
- Report the contiguous dates.
Time complexity: O(n log n) where n is the number of date ranges. Space complexity: O(n) for the merged ranges.
Solution Code
import java.util.*;
class Solution {
public List<List<String>> reportContiguousDates(List<List<String>> dateRanges) {
List<List<String>> result = new ArrayList<>();
if (dateRanges == null || dateRanges.size() == 0) {
return result;
}
Collections.sort(dateRanges, (a, b) -> a.get(0).compareTo(b.get(0)));
List<String> currentRange = new ArrayList<>();
currentRange.add(dateRanges.get(0).get(0));
String end = dateRanges.get(0).get(1);
for (int i = 1; i < dateRanges.size(); i++) {
if (dateRanges.get(i).get(0).compareTo(end) <= 0) {
end = max(end, dateRanges.get(i).get(1));
} else {
currentRange.add(end);
result.add(new ArrayList<>(currentRange));
currentRange.clear();
currentRange.add(dateRanges.get(i).get(0));
end = dateRanges.get(i).get(1);
}
}
currentRange.add(end);
result.add(new ArrayList<>(currentRange));
return result;
}
private String max(String a, String b) {
return a.compareTo(b) > 0 ? a : b;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 1225 (Report Contiguous Dates)?
This page provides optimized solutions for LeetCode problem 1225 (Report Contiguous Dates) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 1225 (Report Contiguous Dates)?
The time complexity for LeetCode 1225 (Report Contiguous Dates) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 1225 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1225 in Java, C++, or Python.