Code Signal_练习题_Knapsack Light
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, andmaxW = 8, the output should beknapsackLight(value1, weight1, value2, weight2, maxW) = 10.You can only carry the first item.
For
value1 = 10,weight1 = 5,value2 = 6,weight2 = 4, andmaxW = 9, the output should beknapsackLight(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, andmaxW = 6, the output should beknapsackLight(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的更多相关文章
- Code Signal_练习题_digitDegree
Let's define digit degree of some positive integer as the number of times we need to replace this nu ...
- Code Signal_练习题_growingPlant
Each day a plant is growing by upSpeed meters. Each night that plant's height decreases by downSpeed ...
- Code Signal_练习题_arrayMaxConsecutiveSum
Given array of integers, find the maximal possible sum of some of its k consecutive elements. Exampl ...
- Code Signal_练习题_differentSymbolsNaive
Given a string, find the number of different characters in it. Example For s = "cabca", th ...
- Code Signal_练习题_firstDigit
Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int&qu ...
- Code Signal_练习题_extractEachKth
Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6 ...
- Code Signal_练习题_stringsRearrangement
Given an array of equal-length strings, check if it is possible to rearrange the strings in such a w ...
- 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) + ...
- Code Signal_练习题_depositProfit
You have deposited a specific amount of money into your bank account. Each year your balance increas ...
随机推荐
- markdown的学习记录
一.markdown用处: 写博客,写微信公众号等等 二.准备工作: 工具使用:由于代码经常使用idea,所以继续使用idea编写markdown,但需要安装插件:MarkDown Navigator ...
- D3.js(v3)+react框架 基础部分之数据绑定及其工作过程与绑定顺序
数据绑定: 将数据绑定到Dom上,是D3最大的特色.d3.select和d3.selectAll返回的元素的选择集.选择集上是没有数据的. 数据绑定就是使被选择元素里“含有”数据. 相关函数有两个: ...
- 人工智能-机器学习之seaborn(读取xlsx文件,小提琴图)
我们不止可以读取数据库的内容,还可以读取xlsx文件的内容,这个库有在有些情况还是挺实用的 首先我们想读取这个文件的时候必须得现有个seaborn库 下载命令就是: pip install seab ...
- Python如何判断字符串中是否有中文
解决:Python如何判断字符串中是否有中文 In [240]: s Out[240]: '你好aa' In [241]: for i in s: ...: if u'\u4e00' <= i ...
- [原创]Chorme密码读取工具\Firefox密码读取工具
工具: getBrowserPWD编译: VC作者: K8哥哥博客: http://qqhack8.blog.163.com发布: 2017/11/24 16:16:17 简介: 有时为了方便我们会让 ...
- POJ 2840
#include<iostream> #include<stdio.h> #include<string> using namespace std; int mai ...
- POJ 2501
#include<iostream> #include<iomanip> #include<stdio.h> #include<string> #inc ...
- (转载)elasticsearch 查询(match和term)
原文地址:https://www.cnblogs.com/yjf512/p/4897294.html elasticsearch 查询(match和term) es中的查询请求有两种方式,一种是简易版 ...
- (转载)es进行聚合操作时提示Fielddata is disabled on text fields by default
原文地址:http://blog.csdn.net/u011403655/article/details/71107415 根据es官网的文档执行 GET /megacorp/employee/_se ...
- VMware虚拟机克隆Linux(CentOS)系统后找不到eth0网卡的问题(图文详解)
问题现象: 有时候,会使用VMware虚拟机的的克隆功能,快速的复制已安装好的Linux系统. 可是克隆完之后,会发现没有eth0网卡. 解决办法: 1.编辑/etc/udev/rules.d/70 ...