-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlgorithm0001.java
More file actions
126 lines (112 loc) · 4.49 KB
/
Algorithm0001.java
File metadata and controls
126 lines (112 loc) · 4.49 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
public class Algorithm0001 {
// 有n个动物重量分别a1,a2,a3....an,
//
// 这群动物一起玩叠罗汉游戏
//
// 规定从左往右选择动物,每只动物左边动物的总重量不超过自己的重量
//
// 返回最多能选多少个动物,求一个高效的算法
//
// 比如有7个动物,从左往右重量依次为 1,3,5,7,9,11,21
//
// 最多能选5个动物 1,3,5,9,21
//
// 注意:
//
// - 实际给定的数组可能是无序的
// - 要求从左往右选动物,且不能打乱原始数组
public static int[] maxAnimal(int[] srcArray) {
int index = 0; // 按顺序从第一个动物开始
// 存储累计重量
int[] ends = new int[srcArray.length];
// 存储纳入的动物重量
int[] valids = new int[srcArray.length];
// 纳入的动物数量
int validCount = 0;
// 0- index, 1-validCount
int[] opt = new int[2];
opt[0] = index;
opt[1] = validCount;
conquer(srcArray, valids, ends, opt);
index = opt[0];
validCount = opt[1];
int[] mArray = new int[validCount];
for (int i = 0; i < validCount; i++) {
mArray[i] = valids[i];
}
return mArray;
}
public static void conquer(int[] srcArray, int[] valids, int[] ends, int[] opt) {
int index = opt[0];
int validCount = opt[1];
if (index < srcArray.length) {
if (index == 0) {
valids[validCount] = srcArray[index];
ends[validCount] = srcArray[index];
validCount++;
} else {
if (ends[validCount - 1] <= srcArray[index]) {
valids[validCount] = srcArray[index];
ends[validCount] = ends[validCount - 1] + srcArray[index];
validCount++;
} else if (ends[validCount - 1] > srcArray[index]) {
if (validCount >= 2) {
// 比较累计重量数组 倒数第二项的累计重量+当前索引的动物重量 与 最后一项累计重量 的大小
if (ends[validCount - 1] > ends[validCount - 2] + srcArray[index]) {
// 说明 当前索引的动物 与 当前已纳入的最后一只动物 相比, 替换已纳入的最后一只动物 更好
valids[validCount - 1] = srcArray[index];
ends[validCount - 1] = ends[validCount - 2] + srcArray[index];
}
// else 说明 当前索引的动物 与 当前已纳入的最后一只动物 相比, 保持当前 已纳入的动物 更好, 继续索引下一只
} else {
// 当前只有1只动物纳入, 但是当前索引的动物重量 比 已纳入的唯一动物重量 小,应该把小的重量的动物纳入,替换掉原来的
valids[validCount - 1] = srcArray[index];
ends[validCount - 1] = srcArray[index];
}
}
}
opt[0] = ++index;
opt[1] = validCount;
conquer(srcArray, valids, ends, opt);
}
}
public static void main(String[] args) {
int n = 1000;
// 随机一些数
List<Integer> mArrayList = new ArrayList();
for (int i = 0; i < n; i++) {
mArrayList.add((int) (Math.random() * 2 * n) + 1);
}
mArrayList.sort(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
return (Integer) o1 - (Integer) o2;
}
});
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = mArrayList.get(i);
}
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
System.out.print(" ");
if (i % 10 == 0 && i > 0) {
System.out.print("\n");
}
}
System.out.print("\n");
System.out.print("\n");
int[] mArray = maxAnimal(array);
for (int i = 0; i < mArray.length; i++) {
System.out.print(mArray[i]);
System.out.print(" ");
if (i % 10 == 0 && i > 0) {
System.out.print("\n");
}
}
}
}