- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1066.java
45 lines (39 loc) · 1.38 KB
/
_1066.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
packagecom.fishercoder.solutions.secondthousand;
publicclass_1066 {
publicstaticclassSolution1 {
intminSum = Integer.MAX_VALUE;
publicintassignBikes(int[][] workers, int[][] bikes) {
backtracking(workers, bikes, 0, newboolean[bikes.length], 0);
returnminSum;
}
privatevoidbacktracking(
int[][] workers,
int[][] bikes,
intworkersIndex,
boolean[] bikesAssigned,
intcurrentSum) {
if (workersIndex >= workers.length) {
minSum = Math.min(minSum, currentSum);
return;
}
if (currentSum > minSum) {
return;
}
for (intj = 0; j < bikes.length; j++) {
if (!bikesAssigned[j]) {
bikesAssigned[j] = true;
backtracking(
workers,
bikes,
workersIndex + 1,
bikesAssigned,
currentSum + dist(workers[workersIndex], bikes[j]));
bikesAssigned[j] = false;
}
}
}
privateintdist(int[] worker, int[] bike) {
returnMath.abs(worker[0] - bike[0]) + Math.abs(worker[1] - bike[1]);
}
}
}