https://school.programmers.co.kr/learn/courses/30/lessons/42885?language=python3
ํ๋ก๊ทธ๋๋จธ์ค
SW๊ฐ๋ฐ์๋ฅผ ์ํ ํ๊ฐ, ๊ต์ก์ Total Solution์ ์ ๊ณตํ๋ ๊ฐ๋ฐ์ ์ฑ์ฅ์ ์ํ ๋ฒ ์ด์ค์บ ํ
programmers.co.kr
์๋ 1 ๊ธฐ์กด ์ฝ๋ - ์๊ฐ ์ด๊ณผ
๋ชจ๋ ์กฐํฉ์ ๊ณ ๋ คํด์ผ ํ๋ ํ์ง๋ง (combination),,, ์๊ฐ ์ด๊ณผ๊ฐ ๋์๋ฒ๋ฆผ
from itertools import combinations
def solution(people, limit):
answer = 0
processed_pp = []
for boat in combinations(people,2):
boat_weight = 0
for i, b in enumerate(boat):
if i in processed_pp:
continue
boat_weight += b
if boat_weight > limit:
continue
else:
answer += 1
for i, p in enumerate(people):
if p in boat:
processed_pp.append(people[i])
# print(processed_pp)
if processed_pp != people:
answer += len(people) - len(processed_pp)
return answer
์๋2 ํฌํฌ์ธํฐ ์๊ณ ๋ฆฌ์ฆ ํ์ต
def solution(people, limit):
people.sort()
answer = 0
left, right = 0, len(people) - 1
while left < right:
if people[left] + people[right] == limit:
left += 1; right -= 1; answer += 1
elif people[left] + people[right] < limit:
left += 1
else:
right -= 1
answer += len(people) - answer*2
return answer
์๋ 3
๋ฌด๊ฑฐ์ด ๊ฒ๊ณผ ๊ฐ๋ฒผ์ด ๊ฒ์ ์๋์ผ๋ก ๋ฐฐ๋ถํ ์ ์๋๋ก sort ์ ๋ ฌ
def solution(people, limit):
people.sort() # ๋ชธ๋ฌด๊ฒ ์ค๋ฆ์ฐจ์ ์ ๋ ฌ
left, right = 0, len(people) - 1
answer = 0
while left <= right:
# ๊ฐ์ฅ ๊ฐ๋ฒผ์ด ์ฌ๋ + ๊ฐ์ฅ ๋ฌด๊ฑฐ์ด ์ฌ๋์ ํ์๋ณธ๋ค
if people[left] + people[right] <= limit:
left += 1 # ๋ ๋ค ํ์
right -= 1 # ๋ฌด๊ฑฐ์ด ์ฌ๋์ ๋ฐ๋์ ํ์์ผ ํจ
answer += 1 # ๋ณดํธ 1์ฒ ์ฌ์ฉ
return answer'๐ Study > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ํ๋ก๊ทธ๋๋จธ์ค | dfs/bfs | ๋จ์ด๋ณํ (0) | 2025.10.09 |
|---|---|
| ํ๋ก๊ทธ๋๋จธ์ค | dfs/bfs | ๋คํธ์ํฌ (0) | 2025.10.09 |
| [Bronze I] 9093 - ๋จ์ด ๋ค์ง๊ธฐ (1) | 2025.07.20 |
| [Gold V] 2504 - ๊ดํธ์ ๊ฐ (1) | 2025.07.20 |
| [Silver II] 1780 - ์ข ์ด์ ๊ฐ์ (1) | 2025.07.20 |