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
- 브라이틱스 서포터즈
- 삼성SDS Brigthics
- 추천시스템
- 캐글
- 모델링
- Brigthics를 이용한 분석
- 포스코 아카데미
- 삼성SDS Brightics
- 혼공머신러닝딥러닝
- 직원 이직률
- 삼성SDS
- 포스코 청년
- Brigthics Studio
- Brigthics
- 데이터 분석
- 노코드AI
- 영상제작기
- 혼공학습단
- Brightics Studio
- 브라이틱스
- 혼공
- Brightics
- 삼성 SDS Brigthics
- 혼공머신
- Brightics를 이용한 분석
- 삼성 SDS
- 데이터분석
- 개인 의료비 예측
- 직원 이직여부
- 팀 분석
Archives
- Today
- Total
데이터사이언스 기록기📚
[프로그래머스] Level 2_구명보트(Greedy) 본문
📌문제 유형
Greedy
📌문제
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
📌나의 문제풀이
1차 시도(실패)
- 효율성 오래걸림
def solution(people, limit):
answer = 0
rescue = []
real_limit = limit
people.sort(reverse = True)
#[80,70,50,50]
for i in range(len(people)):
rescue.append(people[i])
limit = real_limit
limit -= people[i]
answer += 1
for j in range(i+1,len(people)):
if people[j] <= limit: # 숫자가 같을때
rescue.append(people[j])
limit -= people[j]
if rescue == people:
break
return answer
2차 시도(정확성 : 60, 효율성: 0)
def solution(people, limit):
answer = 0
list_ = []
people.sort(reverse = True)
while len(people) > 0:
list_.clear()
sum_ = 0
sum_ = people[0]
list_.append(0)
for i in range(1,len(people)):
if sum_ + people[i] <= limit:
sum_ += people[i]
list_.append(i)
else:
continue
answer += 1
for i in list_:
if i != 0:
i -= 1
del people[i]
return answer
3차 시도(정확성 : 통과, 효율성 : 0)
- del과 for문을 사용해서 효율성 0
def solution(people, limit):
answer = 0
real_limit = limit
list_ = []
while len(people) > 0:
people.sort(reverse = True)
limit = real_limit
list_.clear()
limit -= people[0]
people.pop(0)
people.sort()
answer += 1
for i in range(len(people)):
if people[i] <= limit:
limit -= people[i]
list_.append(i)
else:
break
for j in range(len(list_)):
if j != 0:
j -= 1
del people[list_[j]]
return answer
4차 시도(통과, 다른사람 풀이 이용)
[프로그래머스] 구명보트 in python
파이썬으로 프로그래머스 풀기 :: 구명보트 문제 설명 무인도에 갇힌 사람들을 구명보트를 이용하여 구출하려고 합니다. 구명보트는 작아서 한 번에 최대 2명씩 밖에 탈 수 없고, 무게 제한도
leedakyeong.tistory.com
def solution(people, limit):
answer = 0
people.sort()
i, j = 0, len(people)-1
while i <= j:
answer += 1
if people[j] + people[i] <= limit :
i += 1
j -= 1
return answer
📌다른 사람들의 풀이
1) deque 이용
- deque의 특징
- deque에는 sort가 없음!
- deque.reverse()로 뒤집는 것만 가능
- deque.pop, deque.popleft만 가능(특정 인덱스만 꺼내지 못함)
- 정렬 후 큰 값, 작은 값만 더하고 limit와 판단
- 더한 값이 limit보다 크면, 작은 값인 left 다시 넣기
from collections import deque
def solution(people, limit):
result = 0
deque_people = deque(sorted(people))
while deque_people:
left = deque_people.popleft()
if not deque_people:
return result + 1
right = deque_people.pop()
if left + right > limit:
deque_people.appendleft(left)
result += 1
return result
📌리뷰
- 삭제하지 않고 하나의 리스트로 문제에 접근할 수 있다
- 그리디 : 정렬, 리스트 앞과 뒤의 값 빼내서 비교하는 방법 생각하기
728x90
'Coding Test > 프로그래머스(Python)' 카테고리의 다른 글
[프로그래머스] Level 2_예상 대진표(2017 팁스타운) (1) | 2022.10.08 |
---|---|
[프로그래머스] Level 2_N개의 최소공배수(연습문제) (1) | 2022.10.08 |
[프로그래머스] Level 2_짝지어 제거하기(2017 팁스타운) (0) | 2022.10.06 |
[프로그래머스] Level 2_영어 끝말잇기(Summer/Winter Coding(~2018)) (0) | 2022.10.06 |
[프로그래머스] Level 2_피보나치 수(연습문제) (0) | 2022.10.05 |
Comments