Test case runs correctly when testing custom case, but not on submission


  • 0
    M

    I have an inefficient solution:

    public class Solution {
    static boolean flag = false;

    public boolean canPartition(int[] nums) {
        int totalSum = 0;
        
        for(int i = 0; i < nums.length; i++) {
            totalSum += nums[i];
        }
        
        canPartitionUtil(nums, 0, totalSum, 0);
        
        return flag;
    }
    
    public void canPartitionUtil(int[] nums, int curSum, int totalSum, int index) {
        if(curSum == totalSum) {
            flag = true;
            return;
        }
        
        if(curSum > totalSum) {
            return;
        }
        
        for(int i = index; i < nums.length; i++) {
            canPartitionUtil(nums, curSum + nums[i], totalSum - nums[i], i + 1);
        }
        
    }
    

    }

    On testing with the case [1,2,3,5], the custom testing works but fails while running on submission. I do not understand how there is a difference.


  • 0

Log in to reply
 

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