Campus Bikes II

Similar Problems:

- LeetCode: Campus Bikes
- CheatSheet: Leetcode For Code Interview
- CheatSheet: Common Code Problems & Follow-ups
- Tag: #backtracking, #manhattandis

On a campus represented as a 2D grid, there are N workers and M bikes, with N <= M. Each worker and bike is a 2D coordinate on this grid.

We assign one unique bike to each worker so that the sum of the Manhattan distances between each worker and their assigned bike is minimized.

The Manhattan distance between two points p1 and p2 is Manhattan(p1, p2) = |p1.x – p2.x| + |p1.y – p2.y|.

Return the minimum possible sum of Manhattan distances between each worker and their assigned bike.

Input: workers = [[0,0],[2,1]], bikes = [[1,2],[3,3]] Output: 6 Explanation: We assign bike 0 to worker 0, bike 1 to worker 1. The Manhattan distance of both assignments is 3, so the output is 6.

Input: workers = [[0,0],[1,1],[2,0]], bikes = [[1,0],[2,2],[2,1]] Output: 4 Explanation: We first assign bike 0 to worker 0, then assign bike 1 to worker 1 or worker 2, bike 2 to worker 2 or worker 1. Both assignments lead to sum of the Manhattan distances as 4.

Note:

- 0 <= workers[i][0], workers[i][1], bikes[i][0], bikes[i][1] < 1000
- All worker and bike locations are distinct.
- 1 <= workers.length <= bikes.length <= 10

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.

- Solution:

// https://code.dennyzhang.com/campus-bikes-ii // Basic Ideas: backtracking // // Complexity: Time O(m!*n), Space O(1) func getDis(point1 []int, point2 []int) int { v1 := point1[0]-point2[0] v2 := point1[1]-point2[1] if v1<0 { v1 = -v1 } if v2<0 { v2 = -v2 } return v1+v2 } func dfs(i int, presum int, workers [][]int, bikes [][]int, bikes_taken []bool, res *int) { // fast quit to speed up if presum >= * res { return } if i == len(workers) { // get a condidate if presum < *res { *res = presum } return } // check with each worker for j:=0; j<len(bikes); j++ { if bikes_taken[j] { continue } // assign bikes[j] to worker[i] bikes_taken[j] = true d := getDis(workers[i], bikes[j]) dfs(i+1, presum+d, workers, bikes, bikes_taken, res) // backtrack bikes_taken[j] = false } } func assignBikes(workers [][]int, bikes [][]int) int { res := 1<<31-1 bikes_taken := make([]bool, len(bikes)) dfs(0, 0, workers, bikes, bikes_taken, &res) return res }