TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: With N numbers, determine if a tie is possible?

2 点作者 syberslidder超过 12 年前
So I was thinking about the election and I was wondering if you had N numbers, what type of algorithm would best be able to tell you if a tie is possible? I know that the sum being divisible by an even number doesn't necessarily mean there is a way to tie. This was inspired by thinking of the up coming election. You can assume all numbers in N are positive and that there may be numbers repeated, think electoral college as an example.

2 条评论

friggeri超过 12 年前
This is the partition problem[1]. So to answer your question: the problem is NP-complete, but there are pseudo-polynomial time dynamic programming solutions (see the wikipedia entry for details).<p>[1]: <a href="http://en.wikipedia.org/wiki/Partition_problem" rel="nofollow">http://en.wikipedia.org/wiki/Partition_problem</a>
chacham15超过 12 年前
Off the top of my head:<p><pre><code> 1. sum all the numbers 2. if the sum is odd, then return false 3. else determine if any set of numbers adds to half the sum </code></pre> To determine if any set of numbers adds to half the sum:<p><pre><code> 1. choose a number and add it to the set 2. if the sum of the set is less than the number, add another number to the set 3. else remove a number from the set </code></pre> which numbers to add and remove can be done via brute force, but there is probably a better way.
评论 #4739348 未加载