Advertisement
InterVi

Maximum subarray problem: 2D Kadane's algorithm (indexes)

Mar 31st, 2018
744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. /**
  2. * Get max sum from 2D array example
  3. * Use Kadane's algorithm
  4. * Maximum subarray problem: https://en.wikipedia.org/wiki/Maximum_subarray_problem
  5. * Author: https://github.com/InterVi
  6. */
  7. import java.util.AbstractMap.SimpleEntry;
  8. import java.util.Map.Entry;
  9.  
  10. public class MaxSumExample {
  11.   public static void main(String[] args) { //testing
  12.     int[][] test = {
  13.       {-1, -2, -1},
  14.       {-20},
  15.       {-40, -50},
  16.       {-1, 2, 3, -4, 5}, //6
  17.       {4, -1, 2, 1}, //6, least summands, this will print
  18.       {1, 1},
  19.       {2, -4, -1},
  20.       {1, 1}
  21.     };
  22.     System.out.print(maxSum2D(test, false, true));
  23.   }
  24.  
  25.   /**
  26.   * @param zeroNegative true - return 0 or positive result
  27.   */
  28.   public static SumData maxSum(int[] a, boolean zeroNegative) {
  29.     int result = a[0]; //get first value for correct comparison
  30.     int sum = a[0];
  31.     int start = 0, end = 0;
  32.     for (int i = 1; i < a.length; i++) {
  33.       //sum = Math.max(sum + a[i], a[i]); //first step getting max sum, temporary value
  34.       int newSum = sum + a[i]; //new sum
  35.       if (newSum > a[i]) { //get max sum
  36.         sum = newSum;
  37.       } else {
  38.         sum = a[i];
  39.         start = i;
  40.         end = i;
  41.       }
  42.       if (sum > result) { //save current max sum;
  43.         end = i;
  44.         result = sum;
  45.       }
  46.     }
  47.     if (zeroNegative && result < 0) result = 0;
  48.     return new SumData(start, end, result);
  49.   }
  50.  
  51.   /**
  52.   * @param array
  53.   * @param useLength true - use length least value, false - use summands least value for get index
  54.   * @param zeroNegative true - return 0 or positive result
  55.   * @return key - index in array with max sum, value - SumData for found subarray
  56.   */
  57.   public static Entry<Integer, SumData> maxSum2D(int array[][], boolean useLength, boolean zeroNegative){
  58.     int lastIndex = 0; //index with max sum
  59.     int lastMax = Integer.MIN_VALUE; //max sum
  60.     int lastLength = 0; //last array[i].length or summands, for get index with the least value (length or summands)
  61.     SumData maxData = null; //current max sum data
  62.     for (int i = 0; i < array.length; i++) {
  63.       SumData data = maxSum(array[i], zeroNegative); //get sum and other data for subarray
  64.       int newLength = useLength ? array[i].length : data.SUMMANDS; //use length or summands
  65.       if (data.SUM == lastMax && newLength <= lastLength) { //save index with the least value (length or summands)
  66.         lastIndex = i;
  67.         maxData = data;
  68.       } else if (data.SUM > lastMax) { //update index and max sum value
  69.         lastIndex = i;
  70.         lastMax = data.SUM;
  71.         maxData = data;
  72.       }
  73.       lastLength = newLength; //save length or summands for use next iteration
  74.     }
  75.     return new SimpleEntry<>(lastIndex, maxData);
  76.   }
  77. }
  78.  
  79. public class SumData {
  80.   public final int INDEX_START, INDEX_END, SUM, SUMMANDS;
  81.  
  82.   public SumData(int start, int end, int sum) {
  83.     INDEX_START = start;
  84.     INDEX_END = end;
  85.     SUM = sum;
  86.     SUMMANDS = end - start + 1; //because count from zero (inclusively)
  87.   }
  88.  
  89.   @Override
  90.   public String toString() {
  91.     return "Start index: " + INDEX_START + ", end index: " + INDEX_END + ", sum: " + SUM + ", summands: " + SUMMANDS;
  92.   }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement