GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/python/416_Partition_Equal_Subset_Sum.py at master · qiyuangong/leetcode

Expand file tree

/

416_Partition_Equal_Subset_Sum.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
35 lines (34 loc) · 1.11 KB
/

416_Partition_Equal_Subset_Sum.py

Copy path

File metadata and controls

35 lines (34 loc) · 1.11 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution(object):
# def canPartition(self, nums):
# """
# :type nums: List[int]
# :rtype: bool
# """
# if nums is None or len(nums) == 0:
# return True
# volumn = 0
# for num in nums:
# volumn += num
# # if sum is odd then false
# if volumn % 2 != 0:
# return False
# # if sum of some elements can be half of total sum then true
# volumn /= 2
# dp = [False] * (volumn + 1)
# dp[0] = True
# for i in range(1, len(nums) + 1):
# for j in range(volumn, nums[i - 1] - 1, -1):
# dp[j] = dp[j] or dp[j - nums[i - 1]]
# return dp[volumn]
def canPartition(self, nums):
total_sum = sum(nums)
if total_sum & 1:
return False
# if sum of some elements can be half of total sum then true
target = total_sum >> 1
dp = [0] * (target + 1)
dp[0] = 1
for num in nums:
for i in range(target, num - 1, -1):
dp[i] = dp[i] | dp[i - num]
return dp[target] == 1
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.