> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Campus Bikes Python Solution with Tests

> Tested Python solution for LeetCode 1057 with 11 pytest cases. Generate a practice environment with lcpy.

LeetCode 1057, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Greedy](/catalog/topics/greedy), [Sorting](/catalog/topics/sorting), [Heap (Priority Queue)](/catalog/topics/heap-priority-queue). [View on LeetCode](https://leetcode.com/problems/campus-bikes/description/).

Generate this problem as a practice environment: tested reference solution, 11 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1057   # by problem number
lcpy gen -s campus_bikes   # by problem name
```

## Problem

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

### Examples

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

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

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

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/campus_bikes/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/campus_bikes/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from itertools import product


class Solution:
    # Time: O(n * m * log(n * m))
    # Space: O(n * m)
    def assign_bikes(self, workers: list[list[int]], bikes: list[list[int]]) -> list[int]:
        n, m = len(workers), len(bikes)
        pairs = sorted(
            (abs(w[0] - b[0]) + abs(w[1] - b[1]), i, j)
            for (i, w), (j, b) in product(enumerate(workers), enumerate(bikes))
        )
        used_workers = [False] * n
        used_bikes = [False] * m
        ans = [0] * n
        for _, i, j in pairs:
            if not used_workers[i] and not used_bikes[j]:
                used_workers[i] = used_bikes[j] = True
                ans[i] = j
        return ans
```

## Complexity

| Time | Space |
| - | - |
| O(n \* m \* log(n \* m)) | O(n \* m) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.