saurus2
Saurus2
saurus2
전체 방문자
오늘
어제
  • 분류 전체보기
    • 개발
      • AJAX
    • ML Ops
    • Profile
    • 음식점
    • 배낭여행
    • 컴퓨터공학
      • 알고리즘 공부
      • C++
      • Sever 스터디
      • Java spring
      • 알고리즘 _ 문제해결
      • 딥러닝
      • Java 정리
      • Python
      • LeetCode 1000
      • Machine Learning Study
      • Sign language Detection Pro..
      • LeetCode Solutions
    • 비콘
    • 데일리 리포트
    • 유학일기
      • 영어 공부
      • Daily
    • AI Master Degree
      • Data Mining
      • AI and Data engineering
      • Math Foundations for Decisi..
      • Natural Language Processing

블로그 메뉴

  • 홈
  • 태그
  • 미디어로그
  • 위치로그
  • 방명록

공지사항

인기 글

태그

  • 파이썬
  • BFS
  • c++
  • 알고리즘문제해결
  • 알고리즘
  • 개발자 취업준비
  • 온라인저지
  • 리트코드
  • 릿코드
  • 취업준비
  • 개발자
  • 딥러닝
  • DFS
  • 문제해결능력
  • two pointer
  • 백준
  • LeetCode
  • 취준
  • Python
  • 딕셔너리

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
saurus2

Saurus2

[LeetCode] 1272. Remove Interval
컴퓨터공학/LeetCode 1000

[LeetCode] 1272. Remove Interval

2022. 9. 23. 02:45

1272. Remove Interval

Medium

A set of real numbers can be represented as the union of several disjoint intervals, where each interval is in the form [a, b). A real number x is in the set if one of its intervals [a, b) contains x (i.e. a <= x < b).

You are given a sorted list of disjoint intervals intervals representing a set of real numbers as described above, where intervals[i] = [ai, bi] represents the interval [ai, bi). You are also given another interval toBeRemoved.

Return the set of real numbers with the interval toBeRemoved removed from intervals. In other words, return the set of real numbers such that every x in the set is in intervals but not in toBeRemoved. Your answer should be a sorted list of disjoint intervals as described above.

 

Example 1:

Input: intervals = [[0,2],[3,4],[5,7]], toBeRemoved = [1,6]
Output: [[0,1],[6,7]]

Example 2:

Input: intervals = [[0,5]], toBeRemoved = [2,3]
Output: [[0,2],[3,5]]

Example 3:

Input: intervals = [[-5,-4],[-3,-2],[1,2],[3,5],[8,9]], toBeRemoved = [-1,4]
Output: [[-5,-4],[-3,-2],[4,5],[8,9]]

 

Constraints:

  • 1 <= intervals.length <= 104
  • -109 <= ai < bi <= 109

문제 풀이

  1. 인터벌들이 주어진다. 지워야하는 구간이 주어진다. 
  2. 인터벌들 중에 지워야하는 구간내에 포함이 된다면 지워야하는 부분을 잘라 답을 완성해야한다.
  3. 제한은 10^4 이기 때문에 N^2 알고리즘을 사용해도 문제가 없어보인다. 
  4. 전체 인터벌들을 하나씩 확인한다.
  5. 지우는 구간의 조건은 총 세개로 지정할 수 있다. 
  6. 이 조건 세가지 말고도, 만약에 지우는 구간 밖에 인터벌이 있는 조건도 처리해줘야한다.

조건

  1. 인터벌 사이에 지우는 구간 첫 포인트가 포함될때
    인터벌    [    ]
    지움          [    ]
  2. 인터벌 사이에 지우는 구간 마지막 포인트가 포함될때
    인터벌    [    ]
    지움    [    ]
  3. 인터벌이 지우는 구간 안에 들어 갈때 
    인터벌    [    ]
    지움     [         ]

소스 코드

class Solution:
    def removeInterval(self, intervals: List[List[int]], toBeRemoved: List[int]) -> List[List[int]]:
        result = []
        for interval in intervals:
            if interval[1] < toBeRemoved[0] or toBeRemoved[1] < interval[0]:
                result.append(interval)
            elif toBeRemoved[0] <= interval[0] and toBeRemoved[1] < interval[1]:
                result.append([toBeRemoved[1], interval[1]])
            elif interval[0] < toBeRemoved[0] and toBeRemoved[1] < interval[1]:
                result.append([interval[0], toBeRemoved[0]])
                result.append([toBeRemoved[1], interval[1]])
            elif interval[0] < toBeRemoved[0] and interval[1] <= toBeRemoved[1]:
                result.append([interval[0], toBeRemoved[0]])
        return result
저작자표시 (새창열림)

'컴퓨터공학 > LeetCode 1000' 카테고리의 다른 글

[LeetCode] 159. Longest Substring with At Most Two Distinct Characters  (1) 2022.09.24
[LeetCode] 1680. Concatenation of Consecutive Binary Numbers  (0) 2022.09.24
[LeetCode] 557. Reverse Words in a String III  (1) 2022.09.23
[LeetCode]1338. Reduce Array Size to The Half  (0) 2022.08.20
[LeetCode] 1570. Dot Product of Two Sparse Vectors  (0) 2022.08.18
    '컴퓨터공학/LeetCode 1000' 카테고리의 다른 글
    • [LeetCode] 159. Longest Substring with At Most Two Distinct Characters
    • [LeetCode] 1680. Concatenation of Consecutive Binary Numbers
    • [LeetCode] 557. Reverse Words in a String III
    • [LeetCode]1338. Reduce Array Size to The Half
    saurus2
    saurus2
    Simple is Best

    티스토리툴바