This is a problem from leetcode 56. The problem is: 'Given a collection of intervals, merge all overlapping intervals.'
I copied other's successful solution but when I run it it shows error:
AttributeError: 'list' object has no attribute 'start'
Why???
class Solution(object):
def merge(self, intervals):
"""
:type intervals: List[Interval]
:rtype: List[Interval]
"""
if len(intervals) == 0: return []
intervals = sorted(intervals, key = lambda x: x.start)
res = [intervals[0]]
for n in intervals[1:]:
if n.start <= res[-1].end: res[-1].end = max(n.end, res[-1].end)
else: res.append(n)
return res
AttributeError: 'list' object has no attribute 'start'
Line 8 in <lambda> (Solution.py)
Line 8 in merge (Solution.py)
Line 32 in main (Solution.py)
Line 40 in <module> (Solution.py)
Interval
appears to be an actual class, not simply a list of two items. Your input is in the incorrect format. – Carcigenicate Apr 20 '19 at 0:47start
andend
attributes. To answer your question, no, lists don't have those attributes. Did they include the class to be used in the challenge? – Carcigenicate Apr 20 '19 at 0:50