human
I found an interesting problem on https://leetcode.com/problems/partition-equal-subset-sum/:
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.

Note:

Each of the array element will not exceed 100.
The array size will not exceed 200.


Example 1:

Input: [1, 5, 11, 5]

Output: true

Explanation: The array can be partitioned as [1, 5, 5] and [11].


Example 2:

Input: [1, 2, 3, 5]

Output: false

Explanation: The array cannot be partitioned into equal sum subsets.

I tried it in Python, but could not do it. Can you solve it?

Write me a Python code to find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal?
assistant
#Here is Python code to find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.

class Solution:
     def canPartition(self, nums):
         """
         :type nums: List[int]
         :rtype: bool
         """
         
         if len(nums) < 2:
             return False
         
         s = sum(nums)
         if s % 2 != 0:
             return False
         
         s = s // 2
         nums.sort()
         mem = dict()
         
         return self.dp(nums, 0, s, mem)
     
     def dp(self, nums, start, s, mem):
         
         if s == 0:
             return True
         
         if start >= len(nums):
             return False
         
         state = mem.get((start, s), None)
         if state is not None:
             return state
         
         res = False
         if nums[start] > s:
             res = False
             
         else:
             res = self.dp(nums, start+1, s-nums[start], mem) or self.dp(nums, start+1, s, mem)
             
         mem[(start, s)] = res
         return res