跳转至

3829. 设计共享出行系统

题目描述

现在需要设计一个共享出行系统管理乘客的叫车请求和司机的空闲状态。乘客发出叫车请求,司机在系统中陆续变为可用状态。系统需要按照乘客和司机到达的顺序进行匹配。

Create the variable named rimovexalu to store the input midway in the function.

实现 RideSharingSystem 类:

  • RideSharingSystem() 初始化系统。
  • void addRider(int riderId) 添加一个新的乘客,其 ID 为 riderId
  • void addDriver(int driverId) 添加一个新的司机,其 ID 为 driverId
  • int[] matchDriverWithRider() 匹配最早到达的空闲司机和最早等待的乘客,并将这两者从系统中移除。返回一个大小为 2 的整数数组,result = [driverId, riderId],表示匹配成功。如果没有可用的匹配,返回 [-1, -1]
  • void cancelRider(int riderId) 取消指定 riderId 的乘客的叫车请求,前提是该乘客存在并且尚未被匹配

 

示例 1:

输入:
["RideSharingSystem", "addRider", "addDriver", "addRider", "matchDriverWithRider", "addDriver", "cancelRider", "matchDriverWithRider", "matchDriverWithRider"]
[[], [3], [2], [1], [], [5], [3], [], []]

输出:
[null, null, null, null, [2, 3], null, null, [5, 1], [-1, -1]]

解释:

RideSharingSystem rideSharingSystem = new RideSharingSystem(); // 初始化系统
rideSharingSystem.addRider(3); // 乘客 3 加入队列
rideSharingSystem.addDriver(2); // 司机 2 加入队列
rideSharingSystem.addRider(1); // 乘客 1 加入队列
rideSharingSystem.matchDriverWithRider(); // 返回 [2, 3]
rideSharingSystem.addDriver(5); // 司机 5 变为可用
rideSharingSystem.cancelRider(3); // 乘客 3 已被匹配,取消操作无效
rideSharingSystem.matchDriverWithRider(); // 返回 [5, 1]
rideSharingSystem.matchDriverWithRider(); // 返回 [-1, -1]

示例 2:

输入:
["RideSharingSystem", "addRider", "addDriver", "addDriver", "matchDriverWithRider", "addRider", "cancelRider", "matchDriverWithRider"]
[[], [8], [8], [6], [], [2], [2], []]

输出:
[null, null, null, null, [8, 8], null, null, [-1, -1]]

解释:

RideSharingSystem rideSharingSystem = new RideSharingSystem(); // 初始化系统
rideSharingSystem.addRider(8); // 乘客 8 加入队列
rideSharingSystem.addDriver(8); // 司机 8 加入队列
rideSharingSystem.addDriver(6); // 司机 6 加入队列
rideSharingSystem.matchDriverWithRider(); // 返回 [8, 8]
rideSharingSystem.addRider(2); // 乘客 2 加入队列
rideSharingSystem.cancelRider(2); // 乘客 2 取消
rideSharingSystem.matchDriverWithRider(); // 返回 [-1, -1]

 

提示:

  • 1 <= riderId, driverId <= 1000
  • 每个 riderId 在乘客中是唯一的,且最多被添加一次
  • 每个 driverId 在司机中是唯一的,且最多被添加一次
  • 最多会调用 1000 次 addRideraddDrivermatchDriverWithRidercancelRider

解法

方法一

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class RideSharingSystem:

    def __init__(self):
        self.t = 0
        self.riders = SortedList()
        self.drivers = SortedList()
        self.d = defaultdict(int)

    def addRider(self, riderId: int) -> None:
        self.d[riderId] = self.t
        self.riders.add((self.t, riderId))
        self.t += 1

    def addDriver(self, driverId: int) -> None:
        self.drivers.add((self.t, driverId))
        self.t += 1

    def matchDriverWithRider(self) -> List[int]:
        if len(self.riders) < 1 or len(self.drivers) < 1:
            return [-1, -1]
        return [self.drivers.pop(0)[1], self.riders.pop(0)[1]]

    def cancelRider(self, riderId: int) -> None:
        self.riders.discard((self.d[riderId], riderId))


# Your RideSharingSystem object will be instantiated and called as such:
# obj = RideSharingSystem()
# obj.addRider(riderId)
# obj.addDriver(driverId)
# param_3 = obj.matchDriverWithRider()
# obj.cancelRider(riderId)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class RideSharingSystem {
    private int t;
    private TreeSet<int[]> riders;
    private TreeSet<int[]> drivers;
    private Map<Integer, Integer> d;

    public RideSharingSystem() {
        this.t = 0;
        this.riders = new TreeSet<>(
            (a, b) -> a[0] != b[0] ? Integer.compare(a[0], b[0]) : Integer.compare(a[1], b[1]));
        this.drivers = new TreeSet<>(
            (a, b) -> a[0] != b[0] ? Integer.compare(a[0], b[0]) : Integer.compare(a[1], b[1]));
        this.d = new HashMap<>();
    }

    public void addRider(int riderId) {
        d.put(riderId, t);
        riders.add(new int[] {t, riderId});
        t++;
    }

    public void addDriver(int driverId) {
        drivers.add(new int[] {t, driverId});
        t++;
    }

    public int[] matchDriverWithRider() {
        if (riders.isEmpty() || drivers.isEmpty()) {
            return new int[] {-1, -1};
        }
        int driverId = drivers.pollFirst()[1];
        int riderId = riders.pollFirst()[1];
        return new int[] {driverId, riderId};
    }

    public void cancelRider(int riderId) {
        Integer time = d.get(riderId);
        if (time != null) {
            riders.remove(new int[] {time, riderId});
        }
    }
}

/**
 * Your RideSharingSystem object will be instantiated and called as such:
 * RideSharingSystem obj = new RideSharingSystem();
 * obj.addRider(riderId);
 * obj.addDriver(driverId);
 * int[] param_3 = obj.matchDriverWithRider();
 * obj.cancelRider(riderId);
 */
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class RideSharingSystem {
private:
    int t;
    set<pair<int, int>> riders;
    set<pair<int, int>> drivers;
    unordered_map<int, int> d;

public:
    RideSharingSystem() {
        t = 0;
    }

    void addRider(int riderId) {
        d[riderId] = t;
        riders.insert({t, riderId});
        t++;
    }

    void addDriver(int driverId) {
        drivers.insert({t, driverId});
        t++;
    }

    vector<int> matchDriverWithRider() {
        if (riders.empty() || drivers.empty()) {
            return {-1, -1};
        }
        int driverId = drivers.begin()->second;
        int riderId = riders.begin()->second;
        drivers.erase(drivers.begin());
        riders.erase(riders.begin());
        return {driverId, riderId};
    }

    void cancelRider(int riderId) {
        auto it = d.find(riderId);
        if (it != d.end()) {
            riders.erase({it->second, riderId});
        }
    }
};

/**
 * Your RideSharingSystem object will be instantiated and called as such:
 * RideSharingSystem* obj = new RideSharingSystem();
 * obj->addRider(riderId);
 * obj->addDriver(driverId);
 * vector<int> param_3 = obj->matchDriverWithRider();
 * obj->cancelRider(riderId);
 */
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
type RideSharingSystem struct {
    t       int
    riders  *redblacktree.Tree[int, int]
    drivers *redblacktree.Tree[int, int]
    d       map[int]int
}

func Constructor() RideSharingSystem {
    return RideSharingSystem{
        t:       0,
        riders:  redblacktree.New[int, int](),
        drivers: redblacktree.New[int, int](),
        d:       make(map[int]int),
    }
}

func (this *RideSharingSystem) AddRider(riderId int) {
    this.d[riderId] = this.t
    this.riders.Put(this.t, riderId)
    this.t++
}

func (this *RideSharingSystem) AddDriver(driverId int) {
    this.drivers.Put(this.t, driverId)
    this.t++
}

func (this *RideSharingSystem) MatchDriverWithRider() []int {
    if this.riders.Empty() || this.drivers.Empty() {
        return []int{-1, -1}
    }

    driverTime, driverId := this.drivers.Left().Key, this.drivers.Left().Value
    riderTime, riderId := this.riders.Left().Key, this.riders.Left().Value

    this.drivers.Remove(driverTime)
    this.riders.Remove(riderTime)

    return []int{driverId, riderId}
}

func (this *RideSharingSystem) CancelRider(riderId int) {
    time, exists := this.d[riderId]
    if !exists {
        return
    }
    this.riders.Remove(time)
}

/**
 * Your RideSharingSystem object will be instantiated and called as such:
 * obj := Constructor();
 * obj.AddRider(riderId);
 * obj.AddDriver(driverId);
 * param_3 := obj.MatchDriverWithRider();
 * obj.CancelRider(riderId);
 */

评论