最大子序列和
阿新 • • 發佈:2019-02-11
private static int MaximunSunsequenceSumMethod3(int[] testArr, int left, int right) {
if (left == right) {
if (testArr[left] > 0)
return testArr[left];
return 0;
}
int center = (left + right) / 2;
int maxLeftSum = MaximunSunsequenceSumMethod3(testArr, left, center);
int maxRightSum = MaximunSunsequenceSumMethod3(testArr, center + 1, right);
int maxLeftBorderSum = 0, leftBorderSum = 0;
for (int i = center; i >= left; i--) {
leftBorderSum += testArr[i];
if (leftBorderSum > maxLeftBorderSum)
maxLeftBorderSum = leftBorderSum;
}
int maxRightBorderSum = 0, rightBorderSum = 0;
for (int i = center + 1; i <= right; i++) {
rightBorderSum += testArr[i];
if (rightBorderSum > maxRightBorderSum)
maxRightBorderSum = rightBorderSum;
}
return max(maxLeftSum, maxRightSum, maxLeftBorderSum + maxRightBorderSum);
}
private static int max(int maxLeftSum, int maxRightSum, int sum) {
int max = sum;
if (maxLeftSum > max)
max = maxLeftSum;
if (maxRightSum > max)
max = maxRightSum;
return max;
}
private static void MaximunSunsequenceSumMethod1(int[] testArr) {
long start = System.currentTimeMillis();
int maxSum = 0;
for (int i = 0; i < testArr.length; i++) {
for (int j = i; j < testArr.length; j++) {
int thisSum = 0;
for (int k = 0; k < j; k++) {
thisSum += testArr[k];
}
if (thisSum > maxSum) {
maxSum = thisSum;
}
}
}
System.out.println(maxSum);
long end = System.currentTimeMillis();
System.out.println("time:" + (end - start) + "ms");
}
private static void MaximunSunsequenceSumMethod2(int[] testArr) {
long start = System.currentTimeMillis();
int maxSum = 0;
for (int i = 0; i < testArr.length; i++) {
int thisSum = 0;
for (int j = i; j < testArr.length; j++) {
thisSum += testArr[j];
if (thisSum > maxSum) {
maxSum = thisSum;
}
}
}
System.out.println(maxSum);
long end = System.currentTimeMillis();
System.out.println("time:" + (end - start) + "ms");
if (left == right) {
if (testArr[left] > 0)
return testArr[left];
return 0;
}
int center = (left + right) / 2;
int maxLeftSum = MaximunSunsequenceSumMethod3(testArr, left, center);
int maxRightSum = MaximunSunsequenceSumMethod3(testArr, center + 1, right);
int maxLeftBorderSum = 0, leftBorderSum = 0;
for (int i = center; i >= left; i--) {
leftBorderSum += testArr[i];
if (leftBorderSum > maxLeftBorderSum)
maxLeftBorderSum = leftBorderSum;
}
int maxRightBorderSum = 0, rightBorderSum = 0;
for (int i = center + 1; i <= right; i++) {
rightBorderSum += testArr[i];
if (rightBorderSum > maxRightBorderSum)
maxRightBorderSum = rightBorderSum;
}
return max(maxLeftSum, maxRightSum, maxLeftBorderSum + maxRightBorderSum);
}
private static int max(int maxLeftSum, int maxRightSum, int sum) {
int max = sum;
if (maxLeftSum > max)
max = maxLeftSum;
if (maxRightSum > max)
max = maxRightSum;
return max;
}
private static void MaximunSunsequenceSumMethod1(int[] testArr) {
long start = System.currentTimeMillis();
int maxSum = 0;
for (int i = 0; i < testArr.length; i++) {
for (int j = i; j < testArr.length; j++) {
int thisSum = 0;
for (int k = 0; k < j; k++) {
thisSum += testArr[k];
}
if (thisSum > maxSum) {
maxSum = thisSum;
}
}
}
System.out.println(maxSum);
long end = System.currentTimeMillis();
System.out.println("time:" + (end - start) + "ms");
}
private static void MaximunSunsequenceSumMethod2(int[] testArr) {
long start = System.currentTimeMillis();
int maxSum = 0;
for (int i = 0; i < testArr.length; i++) {
int thisSum = 0;
for (int j = i; j < testArr.length; j++) {
thisSum += testArr[j];
if (thisSum > maxSum) {
maxSum = thisSum;
}
}
}
System.out.println(maxSum);
long end = System.currentTimeMillis();
System.out.println("time:" + (end - start) + "ms");
}