Beats 81% Java concise solution


  • 0
    public boolean canPartition(int[] nums) {
            int sum = 0, n = nums.length;
            for (int num : nums) sum += num;
            if ((sum & 1) != 0) return false;
            sum >>>= 1;
            boolean[] dp = new boolean[sum + 1];
            dp[0] = true;
            int curSum = 0;
            for (int x : nums) {
                int max = Math.min(sum, curSum += x);
                for (int j = max; j >= x; j--) {
                    dp[j] = dp[j] || dp[j - x];
                }
                if (dp[sum]) return true;
            }
            return dp[sum];
        }
    

Log in to reply
 

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