-
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
package com.fishercoder.solutions.secondthousand;
public class _1066 {
public static class Solution1 {
int minSum = Integer.MAX_VALUE;
public int assignBikes(int[][] workers, int[][] bikes) {
backtracking(workers, bikes, 0, new boolean[bikes.length], 0);
return minSum;
}
private void backtracking(
int[][] workers,
int[][] bikes,
int workersIndex,
boolean[] bikesAssigned,
int currentSum) {
if (workersIndex >= workers.length) {
minSum = Math.min(minSum, currentSum);
return;
}
if (currentSum > minSum) {
return;
}
for (int j = 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;
}
}
}
private int dist(int[] worker, int[] bike) {
return Math.abs(worker[0] - bike[0]) + Math.abs(worker[1] - bike[1]);
}
}
}