경쟁 프로그래밍쉬움

0-1 배낭

'0-1 Knapsack' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

무게가 wt이고 값이 val인 항목을 사용하여 용량이 W인 배낭에 넣을 수 있는 최대 값을 찾는 함수 knapsack(W, wt, val, n)을 작성하세요. 항목을 분할할 수 없습니다. 아이템을 가져가거나 버리거나 둘 중 하나입니다.

제약
  • 1 <= n <= 100
  • 1 <= W <= 1000
  • 1 <= wt[i], val[i] <= 1000

Example 1
Input
knapsack(50, [10, 20, 30], [60, 100, 120], 3)
Output
220
Explanation

By taking items with weight 20 and 30, the total weight is 50, and the value is 100 + 120 = 220.

Example 2
Input
knapsack(10, [5, 4, 6, 3], [10, 40, 30, 50], 4)
Output
90
Explanation

By taking items of weight 4 and 3, the total weight is 7, and the value is 40 + 50 = 90.

Need a Hint?
세트나 힙과 같은 동적 프로그래밍 관련 데이터 구조를 사용해 보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.