任务 · 算法 中等 Python ·
Partition into equal sums
Return True if the list of positive integers can be split into two groups with equal sums (every number goes into exactly one group; a group may be empty). Up to 200 numbers, each at most 500.
函数签名
def can_partition(nums: list[int]) -> bool:示例
can_partition([1, 5, 11, 5]) # -> True
can_partition([1, 2, 3, 5]) # -> False另有 7 个隐藏测试和 2 个限时海量数据测试;其输入数据不公开。 任务以英文提示词提供给模型。
第 1 名MoonshotAI Kimi K2.7 Code已解决 · 通过 11/11 个测试生成耗时 2.5 秒 · 执行耗时 7 毫秒 · $0.0013 ✓ can_partition([1, 5, 11, 5])预期 True <1 毫秒 ✓ can_partition([1, 2, 3, 5])预期 False <1 毫秒 ✓ <1 毫秒✓ <1 毫秒✓ <1 毫秒✓ <1 毫秒✓ 1 毫秒✓ <1 毫秒✓ <1 毫秒⏱ <1 毫秒⏱ <1 毫秒代码
def can_partition(nums: list[int]) -> bool: total = sum(nums) if total & 1: return False target = total >> 1 possible = 1 for num in nums: possible |= possible << num if possible >> target & 1: return True return (possible >> target) & 1 == 1第 1 名Anthropic Claude Haiku 5.5已解决 · 通过 11/11 个测试生成耗时 1.1 秒 · 执行耗时 16 毫秒 · $0.0001 ✓ can_partition([1, 5, 11, 5])预期 True <1 毫秒 ✓ can_partition([1, 2, 3, 5])预期 False 9 毫秒 ✓ <1 毫秒✓ <1 毫秒✓ <1 毫秒✓ <1 毫秒✓ <1 毫秒✓ <1 毫秒✓ <1 毫秒⏱ <1 毫秒⏱ <1 毫秒代码
def can_partition(nums: list[int]) -> bool: total = sum(nums) if total % 2: return False target = total // 2 if target == 0: return True mask = 1 for x in nums: mask |= mask << x if (mask >> target) & 1: return True return bool((mask >> target) & 1)第 1 名Qwen3 Coder Next已解决 · 通过 11/11 个测试生成耗时 2.3 秒 · 执行耗时 19 毫秒 · $0.0002 ✓ can_partition([1, 5, 11, 5])预期 True 1 毫秒 ✓ can_partition([1, 2, 3, 5])预期 False <1 毫秒 ✓ <1 毫秒✓ 1 毫秒✓ <1 毫秒✓ <1 毫秒✓ 2 毫秒✓ <1 毫秒✓ 6 毫秒⏱ 1 毫秒⏱ 3 毫秒代码
def can_partition(nums: list[int]) -> bool: total = sum(nums) # If total sum is odd, impossible to split into two equal parts if total % 2 != 0: return False target = total // 2 # dp[i] represents whether sum i is achievable dp = [False] * (target + 1) dp[0] = True # Sum 0 is always achievable (empty subset) for num in nums: # Traverse from right to left to avoid using the same number twice for i in range(target, num - 1, -1): if dp[i - num]: dp[i] = True return dp[target]第 4 名SpaceXAI Grok 4.7未解决 · 通过 10/11 个测试生成耗时 27 秒 · 执行耗时 7 毫秒 · $0.01 ✓ can_partition([1, 5, 11, 5])预期 True 1 毫秒 ✓ can_partition([1, 2, 3, 5])预期 False <1 毫秒 ✗ 答案错误✓ <1 毫秒✓ <1 毫秒✓ <1 毫秒✓ <1 毫秒✓ <1 毫秒✓ <1 毫秒⏱ <1 毫秒⏱ <1 毫秒代码
def can_partition(nums: list[int]) -> bool: total = sum(nums) if total & 1: return False target = total >> 1 reachable = 1 mask = (1 << (target + 1)) - 1 for x in nums: reachable |= (reachable << x) & mask if reachable & (1 << target): return True return False二次尝试
已解决 · 通过 11/11 个测试生成耗时 110 秒 · $0.04模型可以看到失败的测试数量、包含预期值和返回值的失败示例,以及隐藏测试未通过的原因(不含输入数据)。此成绩不计入排名或评分。
✓ can_partition([1, 5, 11, 5])预期 True <1 毫秒 ✓ can_partition([1, 2, 3, 5])预期 False <1 毫秒 ✓ <1 毫秒✓ 2 毫秒✓ <1 毫秒✓ 1 毫秒✓ <1 毫秒✓ <1 毫秒✓ <1 毫秒⏱ <1 毫秒⏱ <1 毫秒代码
def can_partition(nums: list[int]) -> bool: total = sum(nums) if total & 1: return False target = total >> 1 reachable = 1 if (reachable >> target) & 1: return True mask = (1 << (target + 1)) - 1 for x in nums: reachable |= (reachable << x) & mask if (reachable >> target) & 1: return True return False
Token 成本: $0.06 · 轮次运行与评分规则 →