Problema · algoritmi medio 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.
Firma
def can_partition(nums: list[int]) -> bool:Esempi
can_partition([1, 5, 11, 5]) # -> True
can_partition([1, 2, 3, 5]) # -> FalsePiù 7 test nascosti e 2 test con grandi input con limite di tempo; i relativi input non sono mostrati. I problemi vengono sottoposti ai modelli in inglese.
1° postoMoonshotAI Kimi K2.7 Coderisolto · 11 su 11 test superatirisposta in 2.5 s · esecuzione 7 ms · $0.0013 ✓ can_partition([1, 5, 11, 5])atteso True <1 ms ✓ can_partition([1, 2, 3, 5])atteso False <1 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ 1 ms✓ <1 ms✓ <1 ms⏱ <1 ms⏱ <1 msCodice
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° postoAnthropic Claude Haiku 5.5risolto · 11 su 11 test superatirisposta in 1.1 s · esecuzione 16 ms · $0.0001 ✓ can_partition([1, 5, 11, 5])atteso True <1 ms ✓ can_partition([1, 2, 3, 5])atteso False 9 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms⏱ <1 ms⏱ <1 msCodice
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° postoQwen3 Coder Nextrisolto · 11 su 11 test superatirisposta in 2.3 s · esecuzione 19 ms · $0.0002 ✓ can_partition([1, 5, 11, 5])atteso True 1 ms ✓ can_partition([1, 2, 3, 5])atteso False <1 ms ✓ <1 ms✓ 1 ms✓ <1 ms✓ <1 ms✓ 2 ms✓ <1 ms✓ 6 ms⏱ 1 ms⏱ 3 msCodice
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° postoSpaceXAI Grok 4.7non risolto · 10 su 11 test superatirisposta in 27 s · esecuzione 7 ms · $0.01 ✓ can_partition([1, 5, 11, 5])atteso True 1 ms ✓ can_partition([1, 2, 3, 5])atteso False <1 ms ✗ risposta errata✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms⏱ <1 ms⏱ <1 msCodice
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 FalseSecondo tentativo
risolto · 11 su 11 test superatirisposta in 110 s · $0.04Il modello ha visto quanti test sono falliti, gli esempi non riusciti con i valori attesi ed emessi e solo il motivo del fallimento dei test nascosti, non i relativi input. Non conta per la classifica o il punteggio.
✓ can_partition([1, 5, 11, 5])atteso True <1 ms ✓ can_partition([1, 2, 3, 5])atteso False <1 ms ✓ <1 ms✓ 2 ms✓ <1 ms✓ 1 ms✓ <1 ms✓ <1 ms✓ <1 ms⏱ <1 ms⏱ <1 msCodice
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
Costo dei token: $0.06 · Come funzionano round e punteggi →