Loading Cargo

"Look Stephen, here's a list of the items that need to be loaded onto the ship. We're going to need a lot of batteries." Nikola handed him a notepad.

"What are the numbers next to the items?"

"That is the weight of each item."

"Er, why?"

"So you can see how much your trading cards and comic book collection will weigh us down during flight." Rang Sofias voice from the phone tube.

"What is she talking about?” asked Nikola “Ooooh, nevermind, that was sarcasm. It’s important because your load-stabilizing belt is broken and there is no way that we can find a new one right now. That’s why, when you carry the things on the list, you’ll have to redistribute their weights in order to get the minimal weight in each arm."

"Okay, so I have to figure out how many batteries I should hold in each hand to prevent them from breaking when they inevitably fall to the ground. Got it."

You have been given a list of integer weights. You should help Stephen distribute these weights into two sets, such that the difference between the total weight of each set is as low as possible.

Input data: A list of the weights as a list of integers.

Output data: The number representing the lowest possible weight difference as a positive integer.

原题链接: http://www.checkio.org/mission/loading-cargo/

题目大意: 将数组中的数分为两组, 使得两组和之差最小

思路: 暴力法, 计算出所有分组和, 求最小差值; 时间复杂度N^2

 def checkio(data):
#cal all possible subset sum
minmum_gap = 100
sub_sum = {0}
total_sum = sum(data) for each in data:
tmp_list = [] for pre in sub_sum:
tmp_sum = each + pre if tmp_sum not in sub_sum:
tmp_list.append(tmp_sum) tmp_gap = abs((tmp_sum << 1) - total_sum) if tmp_gap < minmum_gap:
minmum_gap = tmp_gap for new_sum in tmp_list:
sub_sum.add(new_sum) #replace this for solution
return minmum_gap

Loading Cargo的更多相关文章

  1. Ferry Loading III[HDU1146]

    Ferry Loading IIITime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...

  2. Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem C. Cargo Transportation 暴力

    Problem C. Cargo Transportation 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed ...

  3. 步入angularjs directive(指令)--点击按钮加入loading状态

    今天我终于鼓起勇气写自己的博客了,激动与害怕并存,希望大家能多多批评指导,如果能够帮助大家,也希望大家点个赞!! 用angularjs 工作也有段时间了,总体感觉最有挑战性的还是指令,因为没有指令的a ...

  4. 《动手实现一个网页加载进度loading》

    loading随处可见,比如一个app经常会有下拉刷新,上拉加载的功能,在刷新和加载的过程中为了让用户感知到 load 的过程,我们会使用一些过渡动画来表达.最常见的比如"转圈圈" ...

  5. eclipse 突然 一直在loading descriptor for XXX (XXX为工程名)Cancel Requested

    问题: eclipse 启动后,啥也不干,就一直在loading descriptor for XXX (XXX为工程名),,其他什么操作都不能操作. 如下图所示,保存文件也无法保存.  这个怎么办? ...

  6. linux使用wkhtmltopdf报错error while loading shared libraries:

    官网提示 linux需要这些动态库.depends on: zlib, fontconfig, freetype, X11 libs (libX11, libXext, libXrender) 在li ...

  7. solr定时更新索引遇到的问题(SolrDataImportProperties Error loading DataImportScheduler properties java.lang.NullPointerException)

    问题描述 报如下错误,很显然,问题原因:空指针异常: ERROR (localhost-startStop-1) [   ] o.a.s.h.d.s.SolrDataImportProperties ...

  8. python3: error while loading shared libraries: libpython3.5m.so.1.0: cannot open shared object file: No such file or directory

    安装python3遇到报错: wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz ./configure --prefix=/u ...

  9. x01.os.21: print "Loading..."

    Linux Inside 是中文版,值得下载一读. 先把目标设低点,开机进入后,在屏幕上打印“Loading..."即可.由于要在 bochs 中运行,首先就是安装 bochs.Oldlin ...

随机推荐

  1. [LeetCode] 55. Jump Game 解题思路

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  2. SRM 583 DIV1

    A 裸最短路. class TravelOnMars { public: int minTimes(vector <int>, int, int); }; vector<int> ...

  3. tungsten开机启动及进程开启停止

  4. Java虚拟机内存优化实践

    前面一篇文章介绍了Java虚拟机的体系结构和内存模型,既然提到内存,就不得不说到内存泄露.众所周知,Java是从C++的基础上发展而来的,而C++程序的很大的一个问题就是内存泄露难以解决,尽管Java ...

  5. hdu 1402 A * B Problem Plus FFT

    /* hdu 1402 A * B Problem Plus FFT 这是我的第二道FFT的题 第一题是完全照着别人的代码敲出来的,也不明白是什么意思 这个代码是在前一题的基础上改的 做完这个题,我才 ...

  6. 多次绑定click及ajax提交常用方法

    <script> $(document).ready(function() { //绑定click $(".exchange_ecv").bind("clic ...

  7. 轻量级mvvm Web开发框架 postby:http://zhutty.cnblogs.com

    今天特别郁闷,怎么说呢,之前一直就用angular,然后这两天用的是avalon这东西,反正,一开始没时间去玩它,第一个任务就是封装个jq插件,实现一个小功能.反正呢,就是越写越郁闷.用过angula ...

  8. android中正确保存view的状态

    英文原文: http://trickyandroid.com/saving-android-view-state-correctly/ 转载此译文须注明出处. 今天我们聊一聊安卓中保存和恢复view状 ...

  9. log4net根据日志类型写入到不同的文件中

    <?xml version="1.0"?> <configuration> <configSections> <!--log4net配置安 ...

  10. (转)回车 执行button点击

    功能说明:当我们注册或者执行搜索时,输入内容后,不用单击按钮来执行按钮单击事件,而直接通过回车来执行按钮单击事件.只要在text框中onkeydown事件中加入执行按钮的onclick事件就OK了 代 ...