-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray_Partition_I.java
More file actions
77 lines (57 loc) · 2.2 KB
/
Array_Partition_I.java
File metadata and controls
77 lines (57 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
public class Array_Partition_I {
public static void main(String[] args){
Array_Partition_I partition= new Array_Partition_I();
int[] nums = {1, 4, 3, 2};
int ret_val;
ret_val = partition.arrayPairSum(nums);
System.out.println("return value: " + ret_val);
}
//insertion sort exceeds time limit
//course中的代码应该有bug(当lo之后的元素均比s[lo]小)
//没有bug!!!
private static int partition2(int[] s, int lo, int hi){
//以lo为pivot,初始时i指向lo+1,j指向hi,i和j**交替从两侧向中间扫描**,终止条件是二者cross(即i>=j)
int i = lo,
j = hi+1;
while(true){
while (s[++i] < (s[lo]))//退出时,i位置处s[i]>=s[lo]
//此处break是退出内部的循环,刚才理解出错,认为有bug!!!
if (i == hi) break;
while (s[--j] > s[lo])//退出时,j位置处s[j]<=s[lo]
if (j == lo) break;
if (i>=j) break;
swap(s, i, j);
}
swap(s, lo, j);
return j;
}
static void swap(int[] a, int i, int j){
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
private static void sort(int[] s, int lo, int hi) {
if (hi - lo >= 1) {
int r = partition2(s, lo, hi);
sort(s, lo, r - 1);
sort(s, r + 1, hi);
}
}
public int arrayPairSum(int[] nums) {
sort(nums, 0, nums.length - 1);
int max_sum = 0;
for (int i = 0; i < nums.length; i += 2)
max_sum += nums[i];
return max_sum;
}
}
// Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.
//
// Example 1:
// Input: [1,4,3,2]
//
// Output: 4
// Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
// Note:
// n is a positive integer, which is in the range of [1, 10000].
// All the integers in the array will be in the range of [-10000, 10000].