Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Get max sum from 2D array example
- * Use Kadane's algorithm
- * Maximum subarray problem: https://en.wikipedia.org/wiki/Maximum_subarray_problem
- * Author: https://github.com/InterVi
- */
- import java.util.AbstractMap.SimpleEntry;
- import java.util.Map.Entry;
- public class MaxSumExample {
- public static void main(String[] args) { //testing
- int[][] test = {
- {-1, -2, -1},
- {-20},
- {-40, -50},
- {-1, 2, 3, -4, 5}, //6
- {4, -1, 2, 1}, //6, least summands, this will print
- {1, 1},
- {2, -4, -1},
- {1, 1}
- };
- System.out.print(maxSum2D(test, false, true));
- }
- /**
- * @param zeroNegative true - return 0 or positive result
- */
- public static SumData maxSum(int[] a, boolean zeroNegative) {
- int result = a[0]; //get first value for correct comparison
- int sum = a[0];
- int start = 0, end = 0;
- for (int i = 1; i < a.length; i++) {
- //sum = Math.max(sum + a[i], a[i]); //first step getting max sum, temporary value
- int newSum = sum + a[i]; //new sum
- if (newSum > a[i]) { //get max sum
- sum = newSum;
- } else {
- sum = a[i];
- start = i;
- end = i;
- }
- if (sum > result) { //save current max sum;
- end = i;
- result = sum;
- }
- }
- if (zeroNegative && result < 0) result = 0;
- return new SumData(start, end, result);
- }
- /**
- * @param array
- * @param useLength true - use length least value, false - use summands least value for get index
- * @param zeroNegative true - return 0 or positive result
- * @return key - index in array with max sum, value - SumData for found subarray
- */
- public static Entry<Integer, SumData> maxSum2D(int array[][], boolean useLength, boolean zeroNegative){
- int lastIndex = 0; //index with max sum
- int lastMax = Integer.MIN_VALUE; //max sum
- int lastLength = 0; //last array[i].length or summands, for get index with the least value (length or summands)
- SumData maxData = null; //current max sum data
- for (int i = 0; i < array.length; i++) {
- SumData data = maxSum(array[i], zeroNegative); //get sum and other data for subarray
- int newLength = useLength ? array[i].length : data.SUMMANDS; //use length or summands
- if (data.SUM == lastMax && newLength <= lastLength) { //save index with the least value (length or summands)
- lastIndex = i;
- maxData = data;
- } else if (data.SUM > lastMax) { //update index and max sum value
- lastIndex = i;
- lastMax = data.SUM;
- maxData = data;
- }
- lastLength = newLength; //save length or summands for use next iteration
- }
- return new SimpleEntry<>(lastIndex, maxData);
- }
- }
- public class SumData {
- public final int INDEX_START, INDEX_END, SUM, SUMMANDS;
- public SumData(int start, int end, int sum) {
- INDEX_START = start;
- INDEX_END = end;
- SUM = sum;
- SUMMANDS = end - start + 1; //because count from zero (inclusively)
- }
- @Override
- public String toString() {
- return "Start index: " + INDEX_START + ", end index: " + INDEX_END + ", sum: " + SUM + ", summands: " + SUMMANDS;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement