> For the complete documentation index, see [llms.txt](https://timmybeeflin.gitbook.io/cracking-leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://timmybeeflin.gitbook.io/cracking-leetcode/array_problem/sweep-line/1094.-car-pooling.md).

# 1094. Car Pooling

T: O(nlogn)

S: O(n)

```java
class Solution {

    public boolean carPooling(int[][] trips, int capacity) {
        TreeMap<Integer, Integer> map = new TreeMap<>();
        for (int[] trip : trips) {
            map.put(trip[1], map.getOrDefault(trip[1], 0) + trip[0]);
            map.put(trip[2], map.getOrDefault(trip[2], 0) - trip[0]);
        }
        int max = 0;
        int count = 0;
        for (int value : map.values()) {
            count += value;
            max = Math.max(max, count);
            if (max > capacity) {
                return false;
            }
        }
        return true;
    }
}

/*
car goes with one direction

capacity
an array trips where trip[i] = [numPassengersi, fromi, toi]


from to pick and drop

  in here the sweep line shows, it takes 2 + 3 = 5 people, 5 > 4, so it's fail
    |
  2 |
1.  |  5
  3.|     7
    |3
when 3, pick up 3 more people, it's fail

  2+3
*/
```

## `bucket sort idea`

because this Q's time range is small

* `1 <= trips.length <= 1000`

`so we can use bucket sort idea`

`T: O(n + 1001),  n is trips length`

`S: O(n)`

```java
class Solution {
    public boolean carPooling(int[][] trips, int capacity) {
        int[] bucket = new int[1001];
        for (int i = 0; i < trips.length; i++) {
            int[] trip = trips[i];
            bucket[trip[1]] += trip[0];
            bucket[trip[2]] -= trip[0];
        }
        int max = 0;
        int count = 0;
        for (int i = 0; i < bucket.length; i++) {
            count += bucket[i];
            max = Math.max(max, count);
            if (max > capacity) {
                return false;
            }
        }
        return true;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://timmybeeflin.gitbook.io/cracking-leetcode/array_problem/sweep-line/1094.-car-pooling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
