Java Solution Any Suggestion?


  • 0
    Y

    New in Java. I know it will be more effective to use DP method. Just wanna get some suggestion or recommend from what I did. Thank you.

    This is my Java solution and have no idea if it is really works, though get the correct answer from the given array.

    class Solution {
        public int maxSubArray(int[] nums) {
            int n = nums.length;
            int max = nums[0];
            
            for(int j = 0;j < n;j++) {
                for (int i = 0;i<n-j; i++) {
                     int sum = 0;
                    for(int z = 0;z<j+1;z++) {
                        sum = sum + nums[i+z];
                             if(sum > max) {
                                max = sum;
                        }
                    }
                }
            }
            return max;
        }
    }
    

  • 0
    F

    Most of this kind of question can be deal in O(n)


Log in to reply
 

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