• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
neverlandly
博客园    首页    新随笔    联系   管理    订阅  订阅

Leetcode: Campus Bikes II

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.
Example 1:



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.
Example 2:


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

 

Basic: Backtracking + pruning

 1 class Solution {
 2     int minDis = Integer.MAX_VALUE;
 3     
 4     public int assignBikes(int[][] workers, int[][] bikes) {
 5         dfs(workers, bikes, new boolean[bikes.length], 0, 0);    
 6         return minDis;
 7     }
 8     
 9     public void dfs(int[][] workers, int[][] bikes, boolean[] visited, int pos, int distance) {
10         if (pos == workers.length) {
11             minDis = Math.min(minDis, distance);
12             return;
13         }
14         if (distance > minDis) return;
15         for (int i = 0; i < visited.length; i ++) {
16             if (visited[i]) continue;
17             visited[i] = true;
18             dfs(workers, bikes, visited, pos + 1, distance + manhattanDis(workers[pos], bikes[i]));
19             visited[i] = false;
20         }
21     }
22     
23     public int manhattanDis(int[] worker, int[] bike) {
24         return Math.abs(worker[0] - bike[0]) + Math.abs(worker[1] - bike[1]);
25     }
26 }

 

DP: refer to https://leetcode.com/problems/campus-bikes-ii/discuss/305218/DFS-%2B-Pruning-And-DP-Solution

state : dp[i][s] = the min distance for first i workers to build the state s ,
transit: dp[i][s] = min(dp[i][s], dp[i - 1][prev] + dis(worker[i -1], bike[j)) | 0 < j <m, prev = s ^ (1 << j)
init:dp[0][0] = 0;
result: dp[n][s] s should have n bit

 1   public int assignBikes(int[][] workers, int[][] bikes) {
 2         int n = workers.length;
 3         int m = bikes.length;
 4         int[][] dp = new int[n + 1][1 << m];
 5         for (int[] d : dp) {
 6             Arrays.fill(d, Integer.MAX_VALUE / 2);
 7         }
 8         dp[0][0] = 0;
 9         int min = Integer.MAX_VALUE;
10         for (int i = 1; i <= n; i++) {
11             for (int s = 1; s < (1 << m); s++) {
12                 for (int j = 0; j < m; j++) {
13                     if ((s & (1 << j)) == 0) { // s is current state after the operation of taking bike at j, so s at j should be 1 already 
14                         continue;
15                     }
16                     int prev = s ^ (1 << j);   // previously s at j should be 0
17                     dp[i][s] = Math.min(dp[i - 1][prev] + dis(workers[i - 1], bikes[j]), dp[i][s]) ;
18                     if (i == n) {
19                         min = Math.min(min, dp[i][s]);
20                     }
21                 }
22             }
23         }
24         return min;
25     }
26   
27     public int dis(int[] p1, int[] p2) {
28         return Math.abs(p1[0] - p2[0]) + Math.abs(p1[1] - p2[1]);
29     }

 

posted @ 2019-10-02 16:13  neverlandly  阅读(1538)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3