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 = 10weight1 = 5value2 = 6weight2 = 4, and maxW = 8, the output should be
      knapsackLight(value1, weight1, value2, weight2, maxW) = 10.

      You can only carry the first item.

    • For value1 = 10weight1 = 5value2 = 6weight2 = 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 = 5weight1 = 3value2 = 7weight2 = 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.

我的解答:

def knapsackLight(value1, weight1, value2, weight2, maxW):
dic = {weight1:value1, weight2:value2}
if min(weight1, weight2) > maxW:
return 0
elif max(weight1, weight2) > maxW:
return dic[min(weight1, weight2)]
elif weight1 + weight2 > maxW:
return max(value1, value2)
else:
return value1 + value2
def knapsackLight(v1, w1, v2, w2, W):
return max(int(w1 <= W) * v1, int(w2 <= W) * v2, int(w1 + w2 <= W) * (v1 + v2)) # 这都是些什么脑子...

膜拜大佬

Code Signal_练习题_Knapsack Light的更多相关文章

  1. Code Signal_练习题_digitDegree

    Let's define digit degree of some positive integer as the number of times we need to replace this nu ...

  2. Code Signal_练习题_growingPlant

    Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...

  3. Code Signal_练习题_arrayMaxConsecutiveSum

    Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...

  4. Code Signal_练习题_differentSymbolsNaive

    Given a string, find the number of different characters in it. Example For s = "cabca", th ...

  5. Code Signal_练习题_firstDigit

    Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...

  6. Code Signal_练习题_extractEachKth

    Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...

  7. Code Signal_练习题_stringsRearrangement

    Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...

  8. Code Signal_练习题_absoluteValuesSumMinimization

    Given a sorted array of integers a, find an integer x from a such that the value of abs(a[0] - x) + ...

  9. Code Signal_练习题_depositProfit

    You have deposited a specific amount of money into your bank account. Each year your balance increas ...

随机推荐

  1. 网络基础、多线程、ftp任务铺垫

    一.网络基础 学习网络编程,了解一些网络基础的知识是必不可少的,下面学习一些基础的网络知识: 1.我们的电脑里有网卡,网卡里有mac地址: 2.我到某个地方插上网线,路由器或交换机中的DHCP服务为我 ...

  2. win7 docker 挂载共享目录

    在 win7 下用 docker 不像 win10 那样方便,安装包都不一样. 在 win7 下共享一个目录的方法如下: 1. 先设置 win7 到 VirtualBox 中 docker 用的那个虚 ...

  3. Swift5 语言参考(八) 模式

    模式表示单个值或复合值的结构.例如,元组的结构是两个元素的逗号分隔列表.因为模式表示值的结构而不是任何一个特定值,所以可以将它们与各种值匹配.例如,模式匹配元组和任何其他两元素元组.除了将模式与值匹配 ...

  4. HTML引入JS文件

    浏览器解析HTML文件时,先判断script 标签中是否有src属性,有则执行指定路径下的JS文件,没有则执行script标签中的js脚本. 1. HTML内嵌JS head里面添加script元素, ...

  5. ajaxsubmit 上传文件 在IE中返回的内容 提示下载文件

    在ajaxSubmit提交表单的时候,如果表单内有文件上传的话,会判断参数是否配置的iframe为false参数,如果没有,会用创建隐藏iframe方式提交表单,如果设定了iframe为false,则 ...

  6. Hadoop集群搭建中时间同步步骤

        一.设置主节点时间服务器的时区     二.在每一个节点上检查是否安装时间服务ntp     三.在主节点上配置时间同步的相关文件     四.在其他从节点上配置与主节点时间同步的脚本 一.设 ...

  7. google浏览器高清壁纸保存

    谷歌浏览器 扩展程序里边 有一个主题壁纸 好多不错的,并且是高清大图!!! 主题应用市场: https://chrome.google.com/webstore/category/themes?hl= ...

  8. TensorFlow.js之安装与核心概念

    TensorFlow.js是通过WebGL加速.基于浏览器的机器学习js框架.通过tensorflow.js,我们可以在浏览器中开发机器学习.运行现有的模型或者重新训练现有的模型. 一.安装     ...

  9. c++之window.h

    在c++中引入window.h头文件. Sleep函数,此函数接受一个时间参数,单位是ms.即使得程序在一段时间后继续运行.如下: 在hello输出之后3000ms,才会继续输出world字符串. M ...

  10. C# 高效率创建字符串类(StringBuilder)

    1.为什么需要StringBuilder类? 因为String类型代表不可变字符串,所以无法对当前String类型实例进行处理.所以FCL提供了System.Text.StringBuilder类型, ...