Завдання · алгоритми середній 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 тести на великих даних з лімітом часу; їхній ввід не відображається. Завдання надсилаються моделям англійською мовою.
#1MoonshotAI 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#1Anthropic 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)#1Qwen3 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]#4SpaceXAI 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
Вартість токенів: $0.06 · Як проходять раунди та нараховується рейтинг →