Advertisement
InterVi

Maximum subarray problem: 2D Kadane's algorithm

Mar 25th, 2018
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 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.lang.Math;
  8. import java.util.AbstractMap.SimpleEntry;
  9. import java.util.Map.Entry;
  10.  
  11. public class MaxSumExample {
  12.   public static void main(String[] args) { //testing
  13.     int[][] test = {
  14.       {-1, -2, -1},
  15.       {-20},
  16.       {-40, -50},
  17.       {-1, 2, 3, -4, 5}, //6
  18.       {4, -1, 2, 1}, //6, least summands, this will print
  19.       {1, 1},
  20.       {2, -4, -1},
  21.       {1, 1}
  22.     };
  23.     System.out.print(maxSum2D(test, false));
  24.   }
  25.  
  26.   /**
  27.   * @return key - summands, value - result
  28.   */
  29.   public static Entry<Integer, Integer> maxSum(int[] a) {
  30.     int result = a[0]; //get first value for correct comparison
  31.     int sum = a[0];
  32.     int items = 1; //summands
  33.     for (int i = 1; i < a.length; i++) {
  34.       sum = Math.max(sum + a[i], a[i]); //first step getting max sum, temporary value
  35.       //result = Math.max(result, sum); //if no need summands
  36.       if (sum > result) { //save current max sum
  37.         result = sum;
  38.         items++; //update summands
  39.       }
  40.     }
  41.     return new SimpleEntry<>(items, result);
  42.   }
  43.  
  44.   /**
  45.   * @param array
  46.   * @param useLength true - use length least value, false - use summands least value for get index
  47.   * @return key - index in array with max sum, value - sum
  48.   */
  49.   public static Entry<Integer, Integer> maxSum2D(int array[][], boolean useLength){
  50.     int lastIndex = 0; //index with max sum
  51.     int lastMax = Integer.MIN_VALUE; //max sum
  52.     int lastLength = 0; //last array[i].length or summands, for get index with the least value (length or summands)
  53.     for (int i = 0; i < array.length; i++) {
  54.       Entry<Integer, Integer> entry = maxSum(array[i]); //get sum and summands in subarray
  55.       int newLength = useLength ? array[i].length : entry.getKey(); //use length or summands
  56.       if (entry.getValue() == lastMax && newLength <= lastLength) { //save index with the least value (length or summands)
  57.         lastIndex = i;
  58.       } else if (entry.getValue() > lastMax) { //update index and max sum value
  59.         lastIndex = i;
  60.         lastMax = entry.getValue();
  61.       }
  62.       lastLength = newLength; //save length or summands for use next iteration
  63.     }
  64.     return new SimpleEntry<>(lastIndex, lastMax);
  65.   }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement