Leetcode # 56. ⭐️ Merge Intervals

Problem

https://leetcode.com/problems/merge-intervals

Solution

Time Complexity: O(len(intervals))
Space Complexity: O(1)
(The input and output generally do not count towards the space complexity.)

class Solution:
  def merge(self, intervals: List[List[int]]) -> List[List[int]]:    
    intervals.sort()
    ans = intervals[:1]
    for interval in intervals[1:]:
      if interval[0] <= ans[-1][1]:
        ans[-1][1] = max(interval[1], ans[-1][1])
      else:
        ans.append(interval)
            
    return ans

Last Updated on 2023/09/10 by A1go

目錄
Bitnami