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

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
  • 1
    `memo={}`, I think it is the cause. – jizhihaoSAMA Aug 03 '21 at 10:05
  • the [leetcode] tag has been [burninated](https://meta.stackoverflow.com/questions/399117/we-need-to-de-leet-leetcode) before and should not be recreated. – jps Aug 03 '21 at 10:10
  • 1
    You might want to read: [“Least Astonishment” and the Mutable Default Argument](https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument) – Matthias Aug 03 '21 at 10:48

1 Answers1

1

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, {})
Ram
  • 4,724
  • 2
  • 14
  • 22
  • 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 – Yilmaz Aug 03 '21 at 10:26
  • I just ran this code on LC, I am getting a 0 (correct answer). [This](https://leetcode.com/problems/combination-sum-iv/) is the problem. right ? – Ram Aug 03 '21 at 10:28
  • i am going to update the the question with the image of your solution. I also tried on firefox and got same result – Yilmaz Aug 03 '21 at 10:31
  • I added the result of your solution – Yilmaz Aug 03 '21 at 10:33
  • 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! – Ram Aug 03 '21 at 10:40
  • I am glad you saw it. I thought i was losing my mind :) – Yilmaz Aug 03 '21 at 10:41
  • I got it! What is wrong is, we are using a default value for ```memo = {}```. That's the culprit. I have updated my answer please check. – Ram Aug 03 '21 at 10:43
  • Thank you. this question was the first question I solved on my own but leetcode gave me headache. – Yilmaz Aug 03 '21 at 10:48