返回介绍

solution / 1000-1099 / 1057.Campus Bikes / README_EN

发布于 2024-06-17 01:03:31 字数 6772 浏览 0 评论 0 收藏 0

1057. Campus Bikes

中文文档

Description

On a campus represented on the X-Y plane, there are n workers and m bikes, with n <= m.

You are given an array workers of length n where workers[i] = [xi, yi] is the position of the ith worker. You are also given an array bikes of length m where bikes[j] = [xj, yj] is the position of the jth bike. All the given positions are unique.

Assign a bike to each worker. Among the available bikes and workers, we choose the (workeri, bikej) pair with the shortest Manhattan distance between each other and assign the bike to that worker.

If there are multiple (workeri, bikej) 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. Repeat this process until there are no available workers.

Return _an array _answer_ of length _n_, where _answer[i]_ is the index (0-indexed) of the bike that the _ith_ worker is assigned to_.

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

 

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].

 

Constraints:

  • n == workers.length
  • m == bikes.length
  • 1 <= n <= m <= 1000
  • workers[i].length == bikes[j].length == 2
  • 0 <= xi, yi < 1000
  • 0 <= xj, yj < 1000
  • All worker and bike locations are unique.

Solutions

Solution 1

class Solution:
  def assignBikes(
    self, workers: List[List[int]], bikes: List[List[int]]
  ) -> List[int]:
    n, m = len(workers), len(bikes)
    arr = []
    for i, j in product(range(n), range(m)):
      dist = abs(workers[i][0] - bikes[j][0]) + abs(workers[i][1] - bikes[j][1])
      arr.append((dist, i, j))
    arr.sort()
    vis1 = [False] * n
    vis2 = [False] * m
    ans = [0] * n
    for _, i, j in arr:
      if not vis1[i] and not vis2[j]:
        vis1[i] = vis2[j] = True
        ans[i] = j
    return ans
class Solution {
  public int[] assignBikes(int[][] workers, int[][] bikes) {
    int n = workers.length, m = bikes.length;
    int[][] arr = new int[m * n][3];
    for (int i = 0, k = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        int dist
          = Math.abs(workers[i][0] - bikes[j][0]) + Math.abs(workers[i][1] - bikes[j][1]);
        arr[k++] = new int[] {dist, i, j};
      }
    }
    Arrays.sort(arr, (a, b) -> {
      if (a[0] != b[0]) {
        return a[0] - b[0];
      }
      if (a[1] != b[1]) {
        return a[1] - b[1];
      }
      return a[2] - b[2];
    });
    boolean[] vis1 = new boolean[n];
    boolean[] vis2 = new boolean[m];
    int[] ans = new int[n];
    for (var e : arr) {
      int i = e[1], j = e[2];
      if (!vis1[i] && !vis2[j]) {
        vis1[i] = true;
        vis2[j] = true;
        ans[i] = j;
      }
    }
    return ans;
  }
}
class Solution {
public:
  vector<int> assignBikes(vector<vector<int>>& workers, vector<vector<int>>& bikes) {
    int n = workers.size(), m = bikes.size();
    vector<tuple<int, int, int>> arr(n * m);
    for (int i = 0, k = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        int dist = abs(workers[i][0] - bikes[j][0]) + abs(workers[i][1] - bikes[j][1]);
        arr[k++] = {dist, i, j};
      }
    }
    sort(arr.begin(), arr.end());
    vector<bool> vis1(n), vis2(m);
    vector<int> ans(n);
    for (auto& [_, i, j] : arr) {
      if (!vis1[i] && !vis2[j]) {
        vis1[i] = true;
        vis2[j] = true;
        ans[i] = j;
      }
    }
    return ans;
  }
};
func assignBikes(workers [][]int, bikes [][]int) []int {
  n, m := len(workers), len(bikes)
  type tuple struct{ d, i, j int }
  arr := make([]tuple, n*m)
  for i, k := 0, 0; i < n; i++ {
    for j := 0; j < m; j++ {
      d := abs(workers[i][0]-bikes[j][0]) + abs(workers[i][1]-bikes[j][1])
      arr[k] = tuple{d, i, j}
      k++
    }
  }
  sort.Slice(arr, func(i, j int) bool {
    if arr[i].d != arr[j].d {
      return arr[i].d < arr[j].d
    }
    if arr[i].i != arr[j].i {
      return arr[i].i < arr[j].i
    }
    return arr[i].j < arr[j].j
  })
  vis1, vis2 := make([]bool, n), make([]bool, m)
  ans := make([]int, n)
  for _, e := range arr {
    i, j := e.i, e.j
    if !vis1[i] && !vis2[j] {
      vis1[i], vis2[j] = true, true
      ans[i] = j
    }
  }
  return ans
}

func abs(x int) int {
  if x < 0 {
    return -x
  }
  return x
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文