查看: 5652| 回复: 6
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] 求谷歌高频题1057.Campus Bikes的答案

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
非利口会员,求大佬share一下高票答案和解释

上一篇:求推荐的leetcode课程
下一篇:LeetCode 341 求IDE完整代码 (包括test case)
推荐
337845818 2019-9-28 08:29:01 | 只看该作者
全局:
本帖最后由 337845818 于 2019-9-28 08:33 编辑

题目细节很多,这题是简化的。

差别在于如果有另外一对人跟同一辆车车等同距离出现时, 你怎么做。

比如人[0, 0], [2,0], 车[1,0], [100,0]

本质是一个二分图匹配,标准解法是最大流。

n数字不大时可以选择tsp,数位dp。
回复

使用道具 举报

🔗
xiangzhongdi 2019-7-14 08:53:28 | 只看该作者

回帖奖励 +1

全局:
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.

Our goal is to assign a bike to each worker. Among the available bikes and workers, we choose the (worker, bike) pair with the shortest Manhattan distance between each other, and assign the bike to that worker. (If there are multiple (worker, bike) pairs with the same shortest Manhattan distance, we choose the pair with the smallest worker index; if there are multiple ways to do that, we choose the pair with the smallest bike index). We repeat this process until there are no available workers.

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

Return a vector ans of length N, where ans[i] is the index (0-indexed) of the bike that the i-th worker is assigned to.



Example 1:

Input: workers = [[0,0],[2,1]], bikes = [[1,2],[3,3]]
Output: [1,0]
Explanation:
Worker 1 grabs Bike 0 as they are closest (without ties), and Worker 0 is assigned Bike 1. So the output is [1, 0].

Example 2:

Input: workers = [[0,0],[1,1],[2,0]], bikes = [[1,0],[2,2],[2,1]]
Output: [0,2,1]
Explanation:
Worker 0 grabs Bike 0 at first. Worker 1 and Worker 2 share the same distance to Bike 2, thus Worker 1 is assigned to Bike 2, and Worker 2 will take Bike 1. So the output is [0,2,1].



Note:

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


直接贪心法一个一个算就好了
回复

使用道具 举报

🔗
xiangzhongdi 2019-7-14 08:58:45 | 只看该作者
全局:
高票答案

    We are able to solve this question using a greedy approach.

    initiate a priority queue of bike and worker pairs. The heap order should be Distance ASC, WorkerIndex ASC, Bike ASC
    Loop through all workers and bikes, calculate their distance, and then throw it to the queue.
    Initiate a set to keep track of the bikes that have been assigned.
    initiate a result array and fill it with -1. (unassigned)
    poll every possible pair from the priority queue and check if the person already got his bike or the bike has been assigned.
    early exist on every people got their bike.

This is my first post on LeetCode.
Let me know if you got any questions :D

   public int[] assignBikes(int[][] workers, int[][] bikes) {
        int n = workers.length;
        
        // order by Distance ASC, WorkerIndex ASC, BikeIndex ASC
        PriorityQueue<int[]> q = new PriorityQueue<int[]>((a, b) -> {
            int comp = Integer.compare(a[0], b[0]);
            if (comp == 0) {
                if (a[1] == b[1]) {
                    return Integer.compare(a[2], b[2]);
                }
               
                return Integer.compare(a[1], b[1]);
            }
            
            return comp;
        });
            
        // loop through every possible pairs of bikes and people,
        // calculate their distance, and then throw it to the pq.
        for (int i = 0; i < workers.length; i++) {
            
            int[] worker = workers[i];
            for (int j = 0; j < bikes.length; j++) {
                int[] bike = bikes[j];
                int dist = Math.abs(bike[0] - worker[0]) + Math.abs(bike[1] - worker[1]);
                q.add(new int[]{dist, i, j});
            }
        }
        
        // init the result array with state of 'unvisited'.
        int[] res = new int[n];
        Arrays.fill(res, -1);
        
        // assign the bikes.
        Set<Integer> bikeAssigned = new HashSet<>();
        while (bikeAssigned.size() < n) {
            int[] workerAndBikePair = q.poll();
            if (res[workerAndBikePair[1]] == -1
                && !bikeAssigned.contains(workerAndBikePair[2])) {   
               
                res[workerAndBikePair[1]] = workerAndBikePair[2];
                bikeAssigned.add(workerAndBikePair[2]);
            }
        }
        
        return res;
    }

评分

参与人数 1大米 +1 收起 理由
sherry715 + 1 很有用的信息!

查看全部评分

回复

使用道具 举报

🔗
 楼主| crazycodyman 2019-7-14 09:00:10 | 只看该作者
全局:
xiangzhongdi 发表于 2019-7-14 08:53
On a campus represented as a 2D grid, there are N workers and M bikes, with N

求贴一个可以ac的答案,java的最好
回复

使用道具 举报

🔗
luhongyuan999 2019-9-27 23:17:47 | 只看该作者
全局:
xiangzhongdi 发表于 2019-7-14 08:58
高票答案

    We are able to solve this question using a greedy approach.

heap order slower than priority queue
回复

使用道具 举报

🔗
Michael.Z 2019-12-8 15:35:43 | 只看该作者
全局:
337845818 发表于 2019-9-28 08:29
题目细节很多,这题是简化的。

差别在于如果有另外一对人跟同一辆车车等同距离出现时, 你怎么做。

感谢大神

补充一下,具体来讲是用KM算法,并把顶标值与边值设为负的,这样就可以得到最小流

MD LeetCode的题都好嗨难啊,什么时候才是个头啊TMD
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表