Python time limit exceed


  • 3
    Z
    def findTargetSumWays(self, nums, S):
        if not nums:
            return 0
        return helper(nums,0,0,S,len(nums))
    
    def helper(nums,now,p,t,l):
        print(now,t)
        if p == l:
            if now == t:
                return 1
            return 0
    
        return helper(nums,now + int(nums[p]),p+1,t,l) + helper(nums,now - int(nums[p]),p+1,t,l)
    

    Can admin change time limit for python in these question. the alg works in other languages but shows time limit exceed.


  • 1
    W

    I think this problem intentionally forces you to use DP.

    Simple brute-force recursion should not be accepted because otherwise there is no fun doing the practice.

    @administrators

    However, the admin might need to think about the inequality here... For a same algorithm, if python don't get AC, why should Java/C++?


  • 0
    Z

    I think the DP method is really hard to come up with this question. The reason is send this is that I basically translate the top solution wrote in java but translate it into python. The top solution's definitely been accepted. But the python version doesnt. As we all know, python sacrifice it speed to improve its flexibility. I think it is reasonable to change time limit for python in this one


  • 0
    F

    @zhang435 I use the same method as you do, just use memorization to store the intermediate result and it is accepted. See my solution, https://discuss.leetcode.com/topic/76650/python-intuitive-dfs-solution-with-memorization


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.