返回介绍

solution / 2100-2199 / 2153.The Number of Passengers in Each Bus II / README_EN

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

2153. The Number of Passengers in Each Bus II

中文文档

Description

Table: Buses

+--------------+------+
| Column Name  | Type |
+--------------+------+
| bus_id     | int  |
| arrival_time | int  |
| capacity   | int  |
+--------------+------+
bus_id contains unique values.
Each row of this table contains information about the arrival time of a bus at the LeetCode station and its capacity (the number of empty seats it has).
No two buses will arrive at the same time and all bus capacities will be positive integers.

 

Table: Passengers

+--------------+------+
| Column Name  | Type |
+--------------+------+
| passenger_id | int  |
| arrival_time | int  |
+--------------+------+
passenger_id contains unique values.
Each row of this table contains information about the arrival time of a passenger at the LeetCode station.

 

Buses and passengers arrive at the LeetCode station. If a bus arrives at the station at a time tbus and a passenger arrived at a time tpassenger where tpassenger <= tbus and the passenger did not catch any bus, the passenger will use that bus. In addition, each bus has a capacity. If at the moment the bus arrives at the station there are more passengers waiting than its capacity capacity, only capacity passengers will use the bus.

Write a solution to report the number of users that used each bus.

Return the result table ordered by bus_id in ascending order.

The result format is in the following example.

 

Example 1:

Input: 
Buses table:
+--------+--------------+----------+
| bus_id | arrival_time | capacity |
+--------+--------------+----------+
| 1    | 2      | 1    |
| 2    | 4      | 10     |
| 3    | 7      | 2    |
+--------+--------------+----------+
Passengers table:
+--------------+--------------+
| passenger_id | arrival_time |
+--------------+--------------+
| 11       | 1      |
| 12       | 1      |
| 13       | 5      |
| 14       | 6      |
| 15       | 7      |
+--------------+--------------+
Output: 
+--------+----------------+
| bus_id | passengers_cnt |
+--------+----------------+
| 1    | 1        |
| 2    | 1        |
| 3    | 2        |
+--------+----------------+
Explanation: 
- Passenger 11 arrives at time 1.
- Passenger 12 arrives at time 1.
- Bus 1 arrives at time 2 and collects passenger 11 as it has one empty seat.

- Bus 2 arrives at time 4 and collects passenger 12 as it has ten empty seats.

- Passenger 12 arrives at time 5.
- Passenger 13 arrives at time 6.
- Passenger 14 arrives at time 7.
- Bus 3 arrives at time 7 and collects passengers 12 and 13 as it has two empty seats.

Solutions

Solution 1

# Write your MySQL query statement below
WITH
  T AS (
    SELECT
      *,
      SUM(cnt) OVER (ORDER BY dt, bus_id) AS cur,
      IF(@t > 0, @t := cnt, @t := @t + cnt) AS cur_sum
    FROM
      (
        SELECT bus_id, arrival_time AS dt, capacity AS cnt FROM Buses
        UNION ALL
        SELECT -1, arrival_time AS dt, -1 FROM Passengers
      ) AS a JOIN (SELECT @t := 0 x) AS b
  )
SELECT
  bus_id,
  IF(cur_sum > 0, cnt - cur_sum, cnt) AS passengers_cnt
FROM T
WHERE bus_id > 0
ORDER BY bus_id;

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

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

发布评论

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