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. Django自带的后台管理样式找不到的问题。

    今天发现自己用uwsgi,nginx部署完服务器后,又想用自带的Django服务器进行后台管理调试,发现Django后代管理页面样式找不到.又查看了路径发现是正确的.网上看了很多方法.最后才发现自己把 ...

  2. python的datetime常用方法

    把datetime转成字符串 datetime.strftime("%Y-%m-%d-%H") 把字符串转成datetime datetime.strptime(datetime, ...

  3. [Leetcode]123.买卖股票的最佳时机3

    [原题链接][https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/] 分析:动态规划+二分法.以第i天为分界线,计 ...

  4. odoo开发 相关知识点

    (1)导入模块可以起别名: (2) 新的模型前端要调用显示有关联的另一个模型的相关字段 (3) 传递上下文 搜索视图打开默认按照接收的参数搜索显示: 发起端视图 上下文写法: 目标端 触发显示,搜索视 ...

  5. Java实现二叉树先序,中序,后序,层次遍历

    一.以下是我要解析的一个二叉树的模型形状.本文实现了以下方式的遍历: 1.用递归的方法实现了前序.中序.后序的遍历: 2.利用队列的方法实现层次遍历: 3.用堆栈的方法实现前序.中序.后序的遍历. . ...

  6. 七、Framework类库

    1.Framework类库简介 .Net Framework类库包含Framework类库(Framework Class Library,FCL).FCL是一组DLL程序集的统称,其中含有数千个类型 ...

  7. Android 安装时报错INSTALL_FAILED_NO_MATCHING_ABIS 的解决办法

    $ adb push D:\AndroidstudioTestWorkSpace\app\build\outputs\apk\app-debug.apk       /data/local/tmp/c ...

  8. Android_EditText 打勾显示输入的密码 --EditText与setTransformationMethod

    实现目标: 实现原理: 为CheckBox添加一个监听器事件; 实现的源码: package edu.cquptzx.showPassword; import android.app.Activity ...

  9. 使用ffmpeg实现对h264视频解码 -- (实现了一个易于使用的c++封装库)

    H264是当今流行的视频压缩格式:ffmpeg是一个开源库,实现了对h264视频文件的解压缩. 为了降低使用ffmpeg的复杂性,尽量隐藏实现细节,我写了一个封装库.c#也可以很方便的使用此库.解压后 ...

  10. 用Filter作用户授权的例子

    public class LoginFilter implements Filter { private String permitUrls[] = null; private String goto ...