-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_396.java
73 lines (65 loc) · 1.32 KB
/
sort_396.java
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
public class sort_396 {
// public static int maxRotateFunction(int[] A) {
// if (A.length == 0) {
// return 0;
// }
// int n = A.length;
// int[] f = new int[n];
// for (int k = 0; k < n; k++) {
// int[] b = construct_B(A, k);
// for (int i = 0; i < n; i++) {
// f[k] += i * b[i];
// }
// }
// Arrays.sort(f);
// return f[f.length - 1];
//
// }
public static int[] construct_B(int[] A, int k) {
int n = A.length;
int[] B = new int[n];
if (k == 0) {
B = A;
return B;
}
for (int i = 0; i < k; i++) {
B[i] = A[n - k + i];
}
int h = 0;
for (int j = k; j < n; j++) {
B[j] = A[h];
h++;
}
return B;
}
public static int maxRotateFunction(int[] A) {
int max = Integer.MIN_VALUE;
int n = A.length;
int sum = 0;
if (n == 0) {
return 0;
}
int f[] = new int[n];
for (int i = 0; i < n; i++) {
sum += A[i];
f[0] += i * A[i];
}
max = Math.max(max, f[0]);
for (int i = 1; i < n; i++) {
f[i] = f[i - 1] + sum - n * A[n - i];
max = Math.max(max, f[i]);
}
return max;
}
public static void main(String[] args) {
int[] a = { 4, 3, 2, 6 };
System.out.println(maxRotateFunction(a));
// int[] b = construct_B(a, 1);
// for (int i : b) {
// System.out.println(i);
// }
// Arrays.sort(a);
// System.out.println(a[0]);
System.out.println(Integer.MIN_VALUE);
}
}