|
|
|
@ -6,47 +6,47 @@ import java.util.Arrays;
|
|
|
|
|
|
|
|
|
|
public class TestC {
|
|
|
|
|
|
|
|
|
|
public static String[] sortByName(String[] names, int[] heights) {
|
|
|
|
|
quickSort(names, heights, 0, heights.length - 1);
|
|
|
|
|
return names;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void quickSort(String[] names, int[] heights, int start, int end) {
|
|
|
|
|
if (start >= end) {
|
|
|
|
|
return;
|
|
|
|
|
public static int maxSumTwoNoOverlap(int[] nums, int firstLen, int secondLen) {
|
|
|
|
|
int n = nums.length;
|
|
|
|
|
int[] prefixSum = new int[n + 1];
|
|
|
|
|
for (int i = 1; i <= n; i++) {
|
|
|
|
|
prefixSum[i] = prefixSum[i - 1] + nums[i - 1];
|
|
|
|
|
}
|
|
|
|
|
int pivot = partition(names, heights, start, end);
|
|
|
|
|
quickSort(names, heights, start, pivot - 1);
|
|
|
|
|
quickSort(names, heights, pivot + 1, end);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static int partition(String[] names, int[] heights, int start, int end) {
|
|
|
|
|
int pivotHeight = heights[end];
|
|
|
|
|
int i = start - 1;
|
|
|
|
|
for (int j = start; j < end; j++) {
|
|
|
|
|
if (heights[j] >= pivotHeight) {
|
|
|
|
|
i++;
|
|
|
|
|
int tmpHeight = heights[i];
|
|
|
|
|
heights[i] = heights[j];
|
|
|
|
|
heights[j] = tmpHeight;
|
|
|
|
|
String tmpName = names[i];
|
|
|
|
|
names[i] = names[j];
|
|
|
|
|
names[j] = tmpName;
|
|
|
|
|
int maxSum = 0;
|
|
|
|
|
int[] maxSumLeft = new int[n];
|
|
|
|
|
int[] maxSumRight = new int[n];
|
|
|
|
|
int maxLeft = 0, maxRight = 0;
|
|
|
|
|
for (int i = firstLen - 1; i <= n - secondLen; i++) {
|
|
|
|
|
int sum = prefixSum[i + 1] - prefixSum[i - firstLen + 1];
|
|
|
|
|
if (sum > maxLeft) {
|
|
|
|
|
maxLeft = sum;
|
|
|
|
|
}
|
|
|
|
|
maxSumLeft[i] = maxLeft;
|
|
|
|
|
}
|
|
|
|
|
for (int i = n - secondLen; i >= firstLen - 1; i--) {
|
|
|
|
|
int sum = prefixSum[i + secondLen] - prefixSum[i];
|
|
|
|
|
if (sum >= maxRight) {
|
|
|
|
|
maxRight = sum;
|
|
|
|
|
}
|
|
|
|
|
maxSumRight[i] = maxRight;
|
|
|
|
|
}
|
|
|
|
|
int tmpHeight = heights[i + 1];
|
|
|
|
|
heights[i + 1] = heights[end];
|
|
|
|
|
heights[end] = tmpHeight;
|
|
|
|
|
String tmpName = names[i + 1];
|
|
|
|
|
names[i + 1] = names[end];
|
|
|
|
|
names[end] = tmpName;
|
|
|
|
|
return i + 1;
|
|
|
|
|
for (int i = firstLen - 1; i <= n - secondLen; i++) {
|
|
|
|
|
int sum = prefixSum[i + 1] - prefixSum[i - firstLen + 1];
|
|
|
|
|
maxSum = Math.max(maxSum, sum + maxSumRight[i + 1]);
|
|
|
|
|
maxSum = Math.max(maxSum, sum + maxSumLeft[i - 1]);
|
|
|
|
|
}
|
|
|
|
|
return maxSum;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
String[] names = {"Mary", "John", "Emma"};
|
|
|
|
|
int[] heights = {180, 165, 170};
|
|
|
|
|
String[] sortedNames = sortByName(names, heights);
|
|
|
|
|
System.out.println(Arrays.toString(sortedNames));
|
|
|
|
|
int[] nums1 = { 0, 6, 5, 2, 2, 5, 1, 9, 4 };
|
|
|
|
|
int firstLen1 = 1;
|
|
|
|
|
int secondLen1 = 2;
|
|
|
|
|
System.out.println(maxSumTwoNoOverlap(nums1, firstLen1, secondLen1)); // expect 20
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|