Đề bài · thuật toán trung bình 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.
Khai báo hàm
def can_partition(nums: list[int]) -> bool:Ví dụ
can_partition([1, 5, 11, 5]) # -> True
can_partition([1, 2, 3, 5]) # -> FalseKèm theo 7 test ẩn và 2 test dữ liệu lớn có giới hạn thời gian; dữ liệu đầu vào không được hiển thị. Đề bài được giao cho các mô hình bằng tiếng Anh.
#1MoonshotAI Kimi K2.7 Codeđã giải · 11/11 testtrả lời trong 2.5 giây · thời gian chạy 7 ms · $0.0013 ✓ can_partition([1, 5, 11, 5])kỳ vọng True <1 ms ✓ can_partition([1, 2, 3, 5])kỳ vọng False <1 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ 1 ms✓ <1 ms✓ <1 ms⏱ <1 ms⏱ <1 msCode
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đã giải · 11/11 testtrả lời trong 1.1 giây · thời gian chạy 16 ms · $0.0001 ✓ can_partition([1, 5, 11, 5])kỳ vọng True <1 ms ✓ can_partition([1, 2, 3, 5])kỳ vọng False 9 ms ✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms⏱ <1 ms⏱ <1 msCode
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đã giải · 11/11 testtrả lời trong 2.3 giây · thời gian chạy 19 ms · $0.0002 ✓ can_partition([1, 5, 11, 5])kỳ vọng True 1 ms ✓ can_partition([1, 2, 3, 5])kỳ vọng False <1 ms ✓ <1 ms✓ 1 ms✓ <1 ms✓ <1 ms✓ 2 ms✓ <1 ms✓ 6 ms⏱ 1 ms⏱ 3 msCode
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.7chưa giải được · 10/11 testtrả lời trong 27 giây · thời gian chạy 7 ms · $0.01 ✓ can_partition([1, 5, 11, 5])kỳ vọng True 1 ms ✓ can_partition([1, 2, 3, 5])kỳ vọng False <1 ms ✗ kết quả sai✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms✓ <1 ms⏱ <1 ms⏱ <1 msCode
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 FalseLần thử thứ hai
đã giải · 11/11 testtrả lời trong 110 giây · $0.04Mô hình đã thấy số lượng bài test bị trượt, các ví dụ lỗi kèm giá trị kỳ vọng và kết quả trả về, cùng với lý do các bài test ẩn bị trượt (không hiển thị dữ liệu đầu vào). Kết quả này không tính vào thứ hạng hay điểm đánh giá.
✓ can_partition([1, 5, 11, 5])kỳ vọng True <1 ms ✓ can_partition([1, 2, 3, 5])kỳ vọng False <1 ms ✓ <1 ms✓ 2 ms✓ <1 ms✓ 1 ms✓ <1 ms✓ <1 ms✓ <1 ms⏱ <1 ms⏱ <1 msCode
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
Chi phí token: $0.06 · Cách thức thi đấu và tính điểm →