7 kyu

Simple Fun #5: Knapsack Light

302 of 689myjinxin2015

Description:

Task

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/max_w and you can't come back for the items later?

Example

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

You can only carry the first item.

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

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

For value1 = 10, weight1 = 10, value2 = 6, weight2 = 10 and maxW = 9, the output should be 0.

Unfortunately, you're not strong enough to take any one :(

Input/Output

Arguments

  • integer value1
  • integer weight1
  • integer value1
  • integer weight1
  • integer maxW/max_w

Contraints

  • 2valueN502 \leq valueN \leq 50
  • 2weightN302 \leq weightN \leq 30
  • 1maxW501 \leq maxW \leq 50

Output

  • integer representing the maximum value of the items that can be carried
Puzzles

Stats:

CreatedJan 22, 2017
PublishedJan 22, 2017
Warriors Trained1143
Total Skips29
Total Code Submissions2553
Total Times Completed689
JavaScript Completions252
C# Completions86
Python Completions302
PHP Completions55
Ruby Completions45
Crystal Completions9
Haskell Completions6
Total Stars18
% of votes with a positive feedback rating89% of 207
Total "Very Satisfied" Votes167
Total "Somewhat Satisfied" Votes35
Total "Not Satisfied" Votes5
Total Rank Assessments12
Average Assessed Rank
7 kyu
Highest Assessed Rank
5 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • myjinxin2015 Avatar
  • donaldsebleung Avatar
  • Dentzil Avatar
  • kazk Avatar
  • user9644768 Avatar
  • hobovsky Avatar
  • dfhwze Avatar
  • tobeannouncd Avatar
  • saudiGuy Avatar
Ad