Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
Tags
- python
- 다이나믹 프로그래밍
- 브루트포스
- programmers
- 백준
- 스택
- 코딩테스트
- dict
- level1
- level0
- 그리디
- MYSQL
- 운영체제
- 재귀
- 수학
- 다익스트라
- 가상메모리 관리
- 파이썬
- level2
- 힙
- DP
- BFS
- 가상메모리
- 딕셔너리
- N과M
- 구현
- 프로그래머스
- BOJ
- dfs
- level3
Archives
- Today
- Total
동캄의 코딩도장
프로그래머스 level0 [겹치는 선분의 길이] 파이썬 본문
https://school.programmers.co.kr/learn/courses/30/lessons/120876
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
# 프로그래머스 겹치는 선분의 길이
def solution(lines):
answer = 0
lines.sort()
curr = -101
for i in range(len(lines)):
end = lines[i][1]
for j in range(i+1, len(lines)):
start = lines[j][0]
if start < end:
end_ = min(end, lines[j][1])
curr = max(curr, start)
if (end_ >= curr):
answer += end_-curr
curr = end_
else:
break
print(answer)
return answer
우선, 선들을 시작점을 기준으로 정렬해주고, 그리디하게 겹치는 선분의 길이 값을 계속 더 해 간다.
def solution(lines):
sets = [set(range(min(l), max(l))) for l in lines]
return len(sets[0] & sets[1] | sets[0] & sets[2] | sets[1] & sets[2])
깔끔하게 푸신 분의 풀이가 있어 첨부한다.
'코테 > 프로그래머스 level0' 카테고리의 다른 글
프로그래머스 level0 [연속된 수의 합] 파이썬 (0) | 2023.03.05 |
---|---|
프로그래머스 level0 [안전지대] 파이썬 (0) | 2023.03.05 |
프로그래머스 level0 [평행] 파이썬 (0) | 2023.03.05 |
프로그래머스 level0 [숫자 찾기] 파이썬 (0) | 2023.03.05 |
프로그래머스 level0 [세균 증식] 파이썬 (2) | 2023.03.05 |