0% completed
.....
.....
.....
Carol Lisbon
· a month ago
For the Java solution it is overcomplicating a relatively straightforward problem. Why do we use LinkedLists and Iterators when we can just emulate the Python solution and iterate by ints? The code below would be much more straightfoward from a beginner's perspective.
public List<Interval> merge(List<Interval> intervals) { if (intervals.size() < 2) return intervals; Collections.sort(intervals, (a, b) -> Integer.compare(a.start, b.start)); List<Interval> mergedIntervals = new ArrayList<>(); int start = intervals.get(0).start; int end = intervals.get(0).end; for (int i = 1; i < intervals.size(); i++) { Interval interval = intervals.get(i); if (interval.start <= end) { end = Math.max(interval.end, end); } else {
pankajaddi A
· 2 months ago
After studying and understanding more about the problems based on Merge Intervals , I expected an approach via Line sweep as many Problems are solved using the same approach . class Solution { public List<Interval> merge(List<Interval> intervals) { List<Interval> mergedIntervals = new LinkedList<Interval>(); // step 1 Build events List<int[]> events = new ArrayList<>(); for(Interval in : intervals){ events.add(new int[]{in.start,+1}); events.add(new int[]{in.end,-1}); } //step 2 // Sort events Collections.sort(events,(a,b)-> { if(a[0]==b[0]) return b[1]-a[1]; return a[0]-b[0]; }); //step 3 sweep int active =0 ; Integer start = null; for(int[] e: events){ int time =e[0],delta=e[1];
Spencer Lan
· 2 years ago
is the Interval class working in the back despite being commented out in the solution?
Mohammed Dh Abbas
· 2 years ago
#class Interval: # def __init__(self, start, end): # self.start = start # self.end = end # def print_interval(self): # print("[" + str(self.start) + ", " + str(self.end) + "]", end='') class Solution: def merge(self, intervals): result = [] # sort the intervals in-order to merge them intervals.sort(key = lambda i: i.start) for interval in intervals: if not result: result.append(interval) else: # [a, b] is the previous interval in the result array # [c, d] is the current internal from the loop a, b = result[-1].start, result[-1].end c, d = interval.start, interval.end # if overlap if c <= b: # merge then update the result array merged = Interval(a, max
camelBack
· 3 years ago
I am getting an error using python, trying to sort the list:
TypeError: 'Interval' object is not subscriptable
when I use either:
intervals.sort(key=lambda i: i[0])
OR
sorted(intervals, key=lambda i: i[0])
I can do this operation in my personal terminal with no issues
Satya Pandya
· 3 years ago
public List<Interval> merge(List<Interval> intervals) { List<Interval> mergedIntervals = new LinkedList<Interval>(); // TODO: Write your code here if(intervals.size()<2) return intervals; Interval l; Interval r; mergedIntervals.add(0, intervals.get(0)); intervals.remove(0); int ls; int le; Iterator<Interval> intervalItr = intervals.iterator(); while (intervalItr.hasNext()) { l=mergedIntervals.get(0); r=intervalItr.next(); if(overlap(l, r)){ ls = l.start; le = l.end; mergedIntervals.remove(0); mergedIntervals.add(0, new Interval(Math.min(ls, r.start), Math.max(le, r.end))); } else mergedIntervals.add(new Interval(r.start, r.end)); } return mergedInter
sweetykumari
· 3 years ago
Anyone have implemented this problem in C#?I tried but facing some problem , logic is quite complex
Laurensius Hans Santoso
· 3 years ago
As described in the title, I don't understand why we add the last interval to mergedIntervals outside the loop? Don't we also evaluate the last interval in intervals inside the for loop? Thank you!
Patrick Reid
· 4 years ago
This is a wildly over complicated way of handling this in JavaScript! Why would you use process.stdout.write? I am very disappointed with this answer.
Adam Sweeney
· 4 years ago
The C++ code in this course is generally lacking, but this example was particularly over-complicated. While I can understand certain design decisions, there are a couple others that really should not have been made. And a few times, outdated practices are employed.
The biggest offender in this lesson is the Interval itself. It should be a struct, and the constructor should utilize the initialization section for more efficient, and more concise code.
My next gripe is the expression(s) *intervalItr++. The dereference and increment are close enough in precedence that I have to look it up every now and again. While what it accomplishes in a single expression is neat, it hurts readability. A similar tack can be used to simplify the while loop while increasing readability and eliminating
Reading Progress
0%