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 |
Tags
- visiontransformer
- 네이버 부스트캠프 ai tech 6기
- video editing
- segmenation map generation
- emerdiff
- diffusion model
- 논문리뷰
- Vit
- segmentation map
- diffusion models
- score distillation
- dreammotion
- BOJ
- masactrl
- controlnext
- 3d generation
- diffusion
- DP
- transformer
- 코테
- Programmers
- video generation
- VirtualTryON
- flow matching
- 프로그래머스
- 3d editing
- 코딩테스트
- rectified flow
- Python
- image editing
Archives
- Today
- Total
평범한 필기장
[Programmers_Python] 구명보트 본문
https://school.programmers.co.kr/learn/courses/30/lessons/42885
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
내 풀이
def solution(people, limit):
people = sorted(people, reverse = True)
cnt = 0
while people:
if len(people) >= 2:
n = limit - people[0]
for i in range(1, len(people)):
if people[i] <= n:
people.pop(i)
break
people.pop(0)
else:
people.pop(0)
cnt += 1
# print(people, cnt)
return cnt
내 코드로 하니 효율성 테스트에서 전부 시간 초과가 났다... 아마 while문 안에 for문이 하나 더 있어서 발생한 것 같다. 한번 저렇게 풀어야된다라 생각하니 다른 접근 방식이 생각나지 않았다. 그래서 다른 분들의 블로그를 참고 했는데 people리스트를 정렬하는건 맞았으나 그 다음 단계가 달랐다.
정답 풀이
def solution(people, limit):
people.sort()
answer = 0
start, end = 0, len(people) - 1
while start <= end:
answer += 1
if people[start] + people[end] <= limit:
start += 1
end -= 1
return answer
- answer = 0, start = 0, end = len(people)-1로 초기 설정
- people[start] + people[end] <= limit이면 start += 1, end -= 1
- people[start] + people[end] > limit이면 end -= 1
- 한번 할때마다 answer += 1
- start <= end 동안 while문 반복
이런 방식으로 하면 while문 하나로 문제를 풀 수 있어 시간초과가 나지 않는다!
그림 설명!
'코딩 테스트 준비 > Programmers' 카테고리의 다른 글
[Programmers_python] H-index (0) | 2023.06.26 |
---|---|
[Programmers_Python] 멀리 뛰기 (0) | 2023.06.24 |
[Programmers_Python] 다음 큰 숫자 (0) | 2023.06.22 |
[Programmers_Python] 파일명 정렬 (0) | 2023.06.20 |
[Programmers_Python] 단어 변환 (0) | 2023.06.19 |