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

 class Solution {
int minDis = Integer.MAX_VALUE; public int assignBikes(int[][] workers, int[][] bikes) {
dfs(workers, bikes, new boolean[bikes.length], 0, 0);
return minDis;
} public void dfs(int[][] workers, int[][] bikes, boolean[] visited, int pos, int distance) {
if (pos == workers.length) {
minDis = Math.min(minDis, distance);
return;
}
if (distance > minDis) return;
for (int i = 0; i < visited.length; i ++) {
if (visited[i]) continue;
visited[i] = true;
dfs(workers, bikes, visited, pos + 1, distance + manhattanDis(workers[pos], bikes[i]));
visited[i] = false;
}
} public int manhattanDis(int[] worker, int[] bike) {
return Math.abs(worker[0] - bike[0]) + Math.abs(worker[1] - bike[1]);
}
}

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

   public int assignBikes(int[][] workers, int[][] bikes) {
int n = workers.length;
int m = bikes.length;
int[][] dp = new int[n + 1][1 << m];
for (int[] d : dp) {
Arrays.fill(d, Integer.MAX_VALUE / 2);
}
dp[0][0] = 0;
int min = Integer.MAX_VALUE;
for (int i = 1; i <= n; i++) {
for (int s = 1; s < (1 << m); s++) {
for (int j = 0; j < m; j++) {
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
continue;
}
int prev = s ^ (1 << j); // previously s at j should be 0
dp[i][s] = Math.min(dp[i - 1][prev] + dis(workers[i - 1], bikes[j]), dp[i][s]) ;
if (i == n) {
min = Math.min(min, dp[i][s]);
}
}
}
}
return min;
} public int dis(int[] p1, int[] p2) {
return Math.abs(p1[0] - p2[0]) + Math.abs(p1[1] - p2[1]);
}

Leetcode: Campus Bikes II的更多相关文章

  1. [Swift]LeetCode1066. 校园自行车分配 II | Campus Bikes II

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  2. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  3. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

  4. LeetCode:课程表II【210】

    LeetCode:课程表II[210] 题目描述 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一 ...

  5. LeetCode:全排列II【47】

    LeetCode:全排列II[47] 参考自天码营题解:https://www.tianmaying.com/tutorial/LC47 题目描述 给定一个可包含重复数字的序列,返回所有不重复的全排列 ...

  6. LeetCode:子集 II【90】

    LeetCode:子集 II[90] 题目描述 给定一个可能包含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集). 说明:解集不能包含重复的子集. 示例: 输入: [1,2,2] 输出: ...

  7. [LeetCode]丑数 II&C++中priority_queue和unordered_set的使用

    [LeetCode]丑数 II&C++中priority_queue和unordered_set的使用 考虑到现实因素,LeetCode每日一题不再每天都写题解了(甚至有可能掉题目?--)但对 ...

  8. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  9. LeetCode:Subsets I II

    求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...

随机推荐

  1. 剑指offer:从尾到头打印链表

    题目 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 解题思路 在不改变链表结构的前提下,因为单向链表本身的结构是从头到尾的,现在用从尾到头遍历打印,可以联想到“先进后出”, 因此我 ...

  2. 《少年先疯队》第八次团队作业:Alpha冲刺第四天

    前言    第四天冲刺会议    时间:2019.6.17    地点:宿舍 4.1 今日完成任务情况以及遇到的问题.   4.1.1今日完成任务情况 姚玉婷:管理员功能模块中,收费管理功能的实现. ...

  3. 「NOI2012」骑行川藏

    「NOI2012」骑行川藏 题目描述 蛋蛋非常热衷于挑战自我,今年暑假他准备沿川藏线骑着自行车从成都前往拉萨. 川藏线的沿途有着非常美丽的风景,但在这一路上也有着很多的艰难险阻,路况变化多端,而蛋蛋的 ...

  4. 导出到CSV文件

    一.手工导出导出 1.winform void DataGridViewToExcel(DataGridView dataGridView1) { SaveFileDialog saveFileDia ...

  5. bzoj1784: [Usaco2010 Jan]island

    现在居然出现一道题只有\(pascal\)题解没有\(C++\)题解的情况,小蒟蒻要打破它. 思维题:分类讨论 回归正题,此题十分考验思维,首先我们要考虑如何把不会走的地方给填上,使最后只用求一遍这个 ...

  6. Djiango权限组件

    一. login中注册 权限url def login(request): if request.method == "POST": username = request.POST ...

  7. 10分钟教你用python 30行代码搞定简单手写识别!

    欲直接下载代码文件,关注我们的公众号哦!查看历史消息即可! 手写笔记还是电子笔记好呢? 毕业季刚结束,眼瞅着2018级小萌新马上就要来了,老腊肉小编为了咱学弟学妹们的学习,绞尽脑汁准备编一套大学秘籍, ...

  8. Java 日期工具类(日期,月份加减等)--转

    package util; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.C ...

  9. AtCoder Grand Contest 013题解

    传送门 \(A\) 先把相同的缩一起,然后贪心就可以了 //quming #include<bits/stdc++.h> #define R register #define fp(i,a ...

  10. 洛谷P3522 TEM-temperature

    题目 单调队列+阅读理解 简化题意. 找到一个最长的区间使得区间每个点的r要大于该点之前的点的l. 然后可以用单调队列维护单调递减的l.最后尺取法O(n)枚举所有区间并取最大值. 单调队列可以快速找某 ...