Hi, this is the second problem that I am experiencing Python3 submission failure for a test case that passed just fine if I tested on the user test case.
Referring to this problem:
https://leetcode.com/problems/target-sum/
Submission result:
Input:
[1]
1
Output:
5
Expected:
1In user mode, this test case passed with Output = 1 so I am baffled. I experienced another problem yesterday too, same issue, user mode passed, but submission failed.
Did I miss anything?
My code:
class Solution:
memo = [[-99999999] * 2001 for i in range(2001)]
def helper(self, nums: List[int], S: int, curr_total: int, idx: int) -> int:
if idx == len(nums):
if curr_total == S:
return 1
else:
return 0
if self.memo[idx][curr_total] != -99999999:
return self.memo[idx][curr_total]
temp = curr_total + nums[idx]
total = self.helper(nums, S, temp, idx+1)
temp = curr_total - nums[idx]
total += self.helper(nums, S, temp, idx+1)
self.memo[idx][curr_total] = total
return total
def findTargetSumWays(self, nums: List[int], S: int) -> int:
return self.helper(nums, S, 0, 0)