Задача · алгоритмы средняя 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 == 11-е место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
Стоимость токенов: $0.06 · Как проходят и оцениваются раунды →