-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathMinfuds.java
More file actions
149 lines (130 loc) · 3.93 KB
/
Minfuds.java
File metadata and controls
149 lines (130 loc) · 3.93 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class Minfuds {
static final int INFINITE = 2001;
static final double ERROR = 1E-9;
public double solution(int[] A, int[] B) {
Line[] lines = new Line[A.length];
for (int i = 0; i < lines.length; i++) {
lines[i] = new Line(A[i], B[i]);
}
List<Point> maxPoints = computeExtremePoints(lines,
(line1, line2) -> (int) Math.signum(line1.a - line2.a),
yDiffLeftEnd -> yDiffLeftEnd < 0);
List<Point> minPoints = computeExtremePoints(lines,
(line1, line2) -> (int) Math.signum(line2.a - line1.a),
yDiffLeftEnd -> yDiffLeftEnd > 0);
return computeMinDiff(maxPoints, minPoints);
}
double computeMinDiff(List<Point> maxPoints, List<Point> minPoints) {
double minDiff = Double.MAX_VALUE;
int maxPointsIndex = 0;
int minPointsIndex = 0;
while (maxPointsIndex < maxPoints.size()
|| minPointsIndex < minPoints.size()) {
double diff;
if (minPointsIndex == minPoints.size()
|| (maxPointsIndex < maxPoints.size() && maxPoints
.get(maxPointsIndex).x <= minPoints
.get(minPointsIndex).x)) {
int leftEndIndex = findLeftEndIndex(minPointsIndex,
minPoints.size());
Line minLine = new Line(minPoints.get(leftEndIndex),
minPoints.get(leftEndIndex + 1));
diff = maxPoints.get(maxPointsIndex).y
- minLine.getY(maxPoints.get(maxPointsIndex).x);
maxPointsIndex++;
} else {
int leftEndIndex = findLeftEndIndex(maxPointsIndex,
maxPoints.size());
Line maxLine = new Line(maxPoints.get(leftEndIndex),
maxPoints.get(leftEndIndex + 1));
diff = maxLine.getY(minPoints.get(minPointsIndex).x)
- minPoints.get(minPointsIndex).y;
minPointsIndex++;
}
minDiff = Math.min(minDiff, diff);
}
return minDiff;
}
int findLeftEndIndex(int pointsIndex, int pointsSize) {
return Math.min(Math.max(pointsIndex - 1, 0), pointsSize - 2);
}
List<Point> computeExtremePoints(Line[] lines, Comparator<Line> comparator,
ExtremeChecker extremeChecker) {
Arrays.sort(lines, comparator);
List<Point> extremePoints = new ArrayList<Point>();
for (Line line : lines) {
while (true) {
if (extremePoints.isEmpty()) {
extremePoints
.add(new Point(-INFINITE, line.getY(-INFINITE)));
extremePoints.add(new Point(INFINITE, line.getY(INFINITE)));
break;
}
Point lastSecondPoint = extremePoints
.get(extremePoints.size() - 2);
Point lastPoint = extremePoints.get(extremePoints.size() - 1);
double yDiffLeftEnd = line.getY(lastSecondPoint.x)
- lastSecondPoint.y;
if (extremeChecker.check(yDiffLeftEnd)) {
if (!isParallel(lastSecondPoint, lastPoint, line)) {
extremePoints.remove(extremePoints.size() - 1);
extremePoints.add(intersect(line, new Line(
lastSecondPoint, lastPoint)));
extremePoints.add(new Point(INFINITE, line
.getY(INFINITE)));
}
break;
}
extremePoints.remove(extremePoints.size() - 1);
if (extremePoints.size() == 1) {
extremePoints.remove(0);
}
}
}
return extremePoints;
}
boolean isParallel(Point lastSecondPoint, Point lastPoint, Line line) {
return Math.abs(new Line(lastSecondPoint, lastPoint).a - line.a) < ERROR;
}
Point intersect(Line line1, Line line2) {
double x = (line2.b - line1.b) / (line1.a - line2.a);
double y = line1.a * x + line1.b;
return new Point(x, y);
}
}
@FunctionalInterface
interface ExtremeChecker {
boolean check(double yDiffLeftEnd);
}
class Point {
double x;
double y;
Point(double x, double y) {
this.x = x;
this.y = y;
}
}
// y = a * x + b
class Line implements Comparable<Line> {
double a;
double b;
Line(double a, double b) {
this.a = a;
this.b = b;
}
Line(Point p1, Point p2) {
a = (p1.y - p2.y) / (p1.x - p2.x);
b = p1.y - a * p1.x;
}
@Override
public int compareTo(Line other) {
return (int) Math.signum(a - other.a);
}
double getY(double x) {
return a * x + b;
}
}