Advertisement
coachdarek

4/15

Apr 15th, 2022
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.70 KB | None | 0 0
  1. '''
  2. nums = [1,5,11,5]
  3.  
  4. base case:
  5. len=0->true
  6. len=1->False
  7.  
  8.  
  9. Recursive:
  10. col->target=sum(num)//2 target->10 --> dp
  11. rows->len(nums)
  12.  
  13. matrix
  14.  
  15.  0 1                           11
  16. 0   T F  F F F
  17. 1          t  
  18. 2
  19. 3
  20.  
  21.  0 1
  22. 0 t F
  23. 1 F x
  24. dp[target-nums[1]]
  25.  
  26. matrix[i][j] = True, what does this mean about the problem?
  27.  
  28. with numbers till index i of nums-> I can genertae j value
  29.  
  30. matrix[n][target] -> answer to your code.
  31.  
  32. let's say you know matrix[i][j] for all i,j <= m,n
  33. matrix[i][j+1], matrix[i+1][j]
  34.  
  35. Given this, how can you solve matrix[i+1][j+1]
  36.  
  37.  
  38. case1->subtract the target with my index at 1 and then  check for the remaining value within dp
  39. dp[i][j]->dp[i][target-nums[i]]
  40.  
  41. case2->dp[i-1][target] I will not consider the number at index 1 and hence I am lookimng at previous calculated indexes.
  42.  
  43. dp[i][j] = dp[i-1][target-nums[i]] OR dp[i-1][target]
  44.  
  45.  
  46.  
  47.  
  48. 1. Establish base case
  49. 2. Establish recursive call.
  50. 3. write outhe basic example on paper with your method. Double check that it works.
  51. 4. Then write the code.
  52. '''
  53.  
  54. def carPartition(nums):
  55.     if sum(nums) % 2 == 1:
  56.         return False
  57.  
  58.     # Initialize DP Array.
  59.     # Recall dp[i][j] = True if with numbers till index i of nums-> I can genertae j value
  60.     dp = []
  61.     for _ in range(len(nums)):
  62.         dp.append([False for _ in range(sum(nums) // 2 + 1)])
  63.  
  64.     # Base Case
  65.     # len=0->true
  66.     # This is the only base-case becuase dp[0][nums[0]] = True because 0 should include the first number. Everything else False because there's only one number.
  67.    
  68.     dp[0][0] = True
  69.     if nums[0] <= sum(nums) // 2:
  70.         dp[0][nums[0]] = True
  71.  
  72.     # Recursive step. Compute with DP.
  73.     for i in range(1, len(nums)):
  74.         for j in range(sum(nums) // 2 + 1) :
  75.             print(i,j, nums[i])
  76.             for row in dp:
  77.                 print(row)
  78.             print("\n")
  79.             if nums[i] <= j and dp[i-1][j-nums[i]]:
  80.                 dp[i][j] = True
  81.             if i > 0 and dp[i-1][j]:
  82.                 dp[i][j] = True
  83.  
  84.  
  85.  
  86.     return dp[-1][-1]
  87.  
  88. print("Answer should be false, but is : ", carPartition([1,2,5])) # Should be false.
  89.  
  90. # carParition(
  91.  
  92.  
  93. # def canPartition(nums):
  94. #     sumation=sum(nums)
  95. #     if sumation%2 !=0:
  96. #         return False
  97. #     target=sumation//2
  98. #     dp=[[-1 for i in range(target+1)] for j in range(len(nums))]
  99. #     def _can_partition(sum,indx):
  100. #         if indx>=len(nums) :
  101. #             return False
  102. #         if dp[indx][sum]==-1:
  103. #             inclu=_can_partition(sum-nums[indx],indx+1)
  104. #             exclu=_can_partition(sum,indx+1)
  105. #             dp[indx][sum]=inclu or exclu
  106. #         return dp[indx][sum]
  107. #     _can_partition(target,0)
  108. #     print(dp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement