| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- 개발자
- 월간
- 30분
- Writing
- 매일
- 읽기
- Problem Solving
- leetcode
- 리얼 클래스
- 사이드
- 뭐든
- 운동
- FIT XR
- English
- 쓰릴오브파이트
- 괜찮음
- 프로젝트
- 화상영어
- realclass
- 영어원서읽기
- 미드시청
- 영어공부
- 스탭퍼
- 링피트
- Daily Challenge
- 잡생각
- 만화도
- 10분
- 3줄정리
- 파비최
Archives
- Today
- Total
파비의 매일매일 공부기록
2023.04.03 Today's Challenge 본문
https://leetcode.com/problems/boats-to-save-people/
Boats to Save People - LeetCode
Can you solve this real interview question? Boats to Save People - You are given an array people where people[i] is the weight of the ith person, and an infinite number of boats where each boat can carry a maximum weight of limit. Each boat carries at most
leetcode.com
탐욕법으로 풀면 될거 같은데, edge case 때문에 WA 날 듯.
그럴 땐 투 포인터 기법을 활용하면 됨.
class Solution:
def numRescueBoats(self, people: List[int], limit: int) -> int:
people.sort()
i, j = 0, len(people) - 1
ans = 0
while i <= j:
ans += 1
if people[i] + people[j] <= limit:
i+= 1
j -= 1
return ans반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
| 2023.04.05 Today's Challenge (0) | 2023.04.05 |
|---|---|
| 2023.04.04 Today's Challenge (0) | 2023.04.04 |
| 2023.04.02 Today's Challenge (0) | 2023.04.02 |
| 2023.04.01 Today's Challenge (0) | 2023.04.01 |
| 2023.03.31 Today's Challenge (0) | 2023.03.31 |
Comments