본문 바로가기
CodeSignal/Arcade

CodeSignal [39/60] knapsackLight

by 벤진[Benzene] 2020. 5. 26.

Problem:

You found two items in a treasure chest! The first item weighs weight1 and is worth value1, and the second item weighs weight2 and is worth value2. What is the total maximum value of the items you can take with you, assuming that your max weight capacity is maxW and you can't come back for the items later?

Note that there are only two items and you can't bring more than one item of each type, i.e. you can't take two first items or two second items.

 

Example

  • For value1 = 10, weight1 = 5, value2 = 6, weight2 = 4, and maxW = 8, the output should be
    knapsackLight(value1, weight1, value2, weight2, maxW) = 10.

    You can only carry the first item.

  • For value1 = 10, weight1 = 5, value2 = 6, weight2 = 4, and maxW = 9, the output should be
    knapsackLight(value1, weight1, value2, weight2, maxW) = 16.

    You're strong enough to take both of the items with you.

  • For value1 = 5, weight1 = 3, value2 = 7, weight2 = 4, and maxW = 6, the output should be
    knapsackLight(value1, weight1, value2, weight2, maxW) = 7.

    You can't take both items, but you can take any of them.

-Summary-

1. 경우의 수를 만들어 그대로 코딩에 구현

- 두개의 weight 이 maxW보다 적어 둘다 가지고 갈수 있는 경우.

- 두개의 무게가 maxW보다 크지만, 각각의 무게가 maxW보다는 작아 둘중 하나를 선택해야 하는경우.

- 하나의 무게가 maxW보다 커서 무조건 한개만 선택해야 하는 경우.

 

2. 저 3개의 경우를 조건 구문으로 나눠 해당하는 경우로 따라가 max_val 값을 return

 

모든 문제에 대한 저작권은 CodeSignal 회사에 있습니다. [Copyright © 2020 BrainFights Inc. All rights reserved]

https://codesignal.com/

댓글