Problem Solving/LeetCode

2023.01.16 Today's Challenge

fabichoi 2023. 1. 16. 23:45

https://leetcode.com/problems/insert-interval/

 

Insert Interval - LeetCode

Insert Interval - You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the ith interval and intervals is sorted in ascending order by starti. You are also given an interval new

leetcode.com

머지 인터벌을 활용해서 문제 풂

class Solution:
    def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
        insert_i = bisect_left(intervals, newInterval)
        intervals.insert(insert_i, newInterval)
        stack = []
        for s,e in intervals:
            if stack and stack[-1][1] >= s:
                last_s, last_e = stack.pop()
                stack.append([last_s, max(last_e,e)])
            else:
                stack.append([s,e])
        return stack
반응형