Max Subarray - Kandane's algorithm


  • 0
    R
    public class Solution {
        public ArrayList<Integer> maxset(int[] a) {
        int current_sum = a[0];
        int global_sum = a[0];
        for (int i = 1; i < a.length; i++) {
            current_sum = Math.max(a[i], a[i]+current_sum);
    
            if (current_sum > global_sum) {
                global_sum = current_sum;
            }
    
        }
        return global_sum;
    	}
    }
    

Log in to reply
 

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