0

It sounds strange but I copied pasted and used the same input for the method, I get the correct result on my machine but leetcode is producing different result for the same code. this is the code on leetcode for Q-377:

class Solution:
    def combinationSum4(self, nums: List[int], target: int,memo={}) -> int:
       
        if target in memo:
            return memo[target]
        
        if target==0:
            return 1
        if target<0:
            return 0 
        count=0
        for num in nums: 
            remainder=target-num
            count+=self.combinationSum4(nums,remainder)
        
        memo[target]=count
        return count

this is the result I am getting for nums=[9] target=3

enter image description here

this is the result on jupyter and pycharm. same code. I copied from leetcode:

enter image description here

this pycahrmenter image description here

I also tried this solution got same issue:

enter image description here

3

1 Answer 1

3

The question in Leetcode doesn't have the argument memo = {}. This is from LC code.

def combinationSum4(self, nums: List[int], target: int) -> int:

Since you are changing the functions arguments, it works for you only in PyCharm or Jupyter. But Leetcode will only pass the required arguments i.e nums and target and not memo = {}. You need to write your code the LC's way.

If you intend to change the arguments of function. You can write a separate functions with the arguments you need and call it from within combinationSum4() function.

Here is the Code:

class Solution:
    def solve(self, nums, target, memo):
            if target in memo:
                return memo[target]

            if target==0:
                return 1
            if target<0:
                return 0 
            count=0
            for num in nums: 
                remainder=target-num
                count += self.solve(nums,remainder,memo)

            memo[target]=count
            return count
    
    def combinationSum4(self, nums: List[int], target: int) -> int:
        # Calling the function
        return self.solve(nums, target, {})
Sign up to request clarification or add additional context in comments.

8 Comments

Ram, your code has same issue. I run the code with nums=[9] and target=3. It gives me 0 on my machine but 4 on leetcode
I just ran this code on LC, I am getting a 0 (correct answer). This is the problem. right ?
i am going to update the the question with the image of your solution. I also tried on firefox and got same result
I added the result of your solution
I just ran the code and it gave a 0 (status was Accepted). But when I submit, it's giving a 4 as answer (status Wrong Answer). Strange!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.