LeetCode 솔루션 분류

[1/28] 352. Data Stream as Disjoint Intervals

컨텐츠 정보

본문

352. Data Stream as Disjoint Intervals 

Given a data stream input of non-negative integers a1, a2, ..., an, summarize the numbers seen so far as a list of disjoint intervals.

Implement the SummaryRanges class:

  • SummaryRanges() Initializes the object with an empty stream.
  • void addNum(int value) Adds the integer value to the stream.
  • int[][] getIntervals() Returns a summary of the integers in the stream currently as a list of disjoint intervals [starti, endi]. The answer should be sorted by starti.

 

Example 1:

Input
["SummaryRanges", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals", "addNum", "getIntervals"]
[[], [1], [], [3], [], [7], [], [2], [], [6], []]
Output
[null, null, [[1, 1]], null, [[1, 1], [3, 3]], null, [[1, 1], [3, 3], [7, 7]], null, [[1, 3], [7, 7]], null, [[1, 3], [6, 7]]]

Explanation
SummaryRanges summaryRanges = new SummaryRanges();
summaryRanges.addNum(1);      // arr = [1]
summaryRanges.getIntervals(); // return [[1, 1]]
summaryRanges.addNum(3);      // arr = [1, 3]
summaryRanges.getIntervals(); // return [[1, 1], [3, 3]]
summaryRanges.addNum(7);      // arr = [1, 3, 7]
summaryRanges.getIntervals(); // return [[1, 1], [3, 3], [7, 7]]
summaryRanges.addNum(2);      // arr = [1, 2, 3, 7]
summaryRanges.getIntervals(); // return [[1, 3], [7, 7]]
summaryRanges.addNum(6);      // arr = [1, 2, 3, 6, 7]
summaryRanges.getIntervals(); // return [[1, 3], [6, 7]]

 

Constraints:

  • 0 <= value <= 104
  • At most 3 * 104 calls will be made to addNum and getIntervals.

 

Follow up: What if there are lots of merges and the number of disjoint intervals is small compared to the size of the data stream?

Accepted
68.1K
Submissions
124.7K
<div class="bg-divider-2 dark:bg-dark-divider-2 h-full w-px border-divider-1 dark:border-dark-divider-1 mr-4 max-h-[14px]" style="border: 0px solid rgba(247, 250, 255, 0.24);
태그 ,

관련자료

댓글 1

학부유학생님의 댓글

  • 익명
  • 작성일
class SummaryRanges:

    def __init__(self):
        self.intervals = []

    def addNum(self, value: int) -> None:
        l, r = 0, len(self.intervals)-1
        while l <= r:
            mid = (l+r)//2
            if self.intervals[mid][0] <= value <= self.intervals[mid][1]: return
            elif value < self.intervals[mid][0]:
                r = mid - 1
            else:
                l = mid + 1

        pos = l
        self.intervals.insert(pos, [value,value])
        if pos + 1 < len(self.intervals) and self.intervals[pos+1][0] == value + 1:
            self.intervals[pos][1] = self.intervals[pos+1][1]
            del self.intervals[pos+1]
        if pos - 1 >= 0 and self.intervals[pos-1][1] == value - 1:
            self.intervals[pos][0] = self.intervals[pos-1][0]
            del self.intervals[pos-1]


    def getIntervals(self) -> List[List[int]]:
        return self.intervals


# Your SummaryRanges object will be instantiated and called as such:
# obj = SummaryRanges()
# obj.addNum(value)
# param_2 = obj.getIntervals()
LeetCode 솔루션 357 / 14 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 740 명
  • 오늘 방문자 3,306 명
  • 어제 방문자 6,598 명
  • 최대 방문자 14,831 명
  • 전체 회원수 1,507 명
알림 0