2286. Booking Concert Tickets in Groups Hard

@problem@discussion
#Binary Search#Design#Binary Indexed Tree#Segment Tree



1/**
2 * [2286] Booking Concert Tickets in Groups
3 *
4 * A concert hall has n rows numbered from 0 to n - 1, each with m seats, numbered from 0 to m - 1. You need to design a ticketing system that can allocate seats in the following cases:
5 * 
6 * 	If a group of k spectators can sit together in a row.
7 * 	If every member of a group of k spectators can get a seat. They may or may not sit together.
8 * 
9 * Note that the spectators are very picky. Hence:
10 * 
11 * 	They will book seats only if each member of their group can get a seat with row number less than or equal to maxRow. maxRow can vary from group to group.
12 * 	In case there are multiple rows to choose from, the row with the smallest number is chosen. If there are multiple seats to choose in the same row, the seat with the smallest number is chosen.
13 * 
14 * Implement the BookMyShow class:
15 * 
16 * 	BookMyShow(int n, int m) Initializes the object with n as number of rows and m as number of seats per row.
17 * 	int[] gather(int k, int maxRow) Returns an array of length 2 denoting the row and seat number (respectively) of the first seat being allocated to the k members of the group, who must sit together. In other words, it returns the smallest possible r and c such that all [c, c + k - 1] seats are valid and empty in row r, and r <= maxRow. Returns [] in case it is not possible to allocate seats to the group.
18 * 	boolean scatter(int k, int maxRow) Returns true if all k members of the group can be allocated seats in rows 0 to maxRow, who may or may not sit together. If the seats can be allocated, it allocates k seats to the group with the smallest row numbers, and the smallest possible seat numbers in each row. Otherwise, returns false.
19 * 
20 *  
21 * Example 1:
22 * 
23 * Input
24 * ["BookMyShow", "gather", "gather", "scatter", "scatter"]
25 * [[2, 5], [4, 0], [2, 0], [5, 1], [5, 1]]
26 * Output
27 * [null, [0, 0], [], true, false]
28 * Explanation
29 * BookMyShow bms = new BookMyShow(2, 5); // There are 2 rows with 5 seats each 
30 * bms.gather(4, 0); // return [0, 0]
31 *                   // The group books seats [0, 3] of row 0. 
32 * bms.gather(2, 0); // return []
33 *                   // There is only 1 seat left in row 0,
34 *                   // so it is not possible to book 2 consecutive seats. 
35 * bms.scatter(5, 1); // return True
36 *                    // The group books seat 4 of row 0 and seats [0, 3] of row 1. 
37 * bms.scatter(5, 1); // return False
38 *                    // There is only one seat left in the hall.
39 * 
40 *  
41 * Constraints:
42 * 
43 * 	1 <= n <= 5 * 10^4
44 * 	1 <= m, k <= 10^9
45 * 	0 <= maxRow <= n - 1
46 * 	At most 5 * 10^4 calls in total will be made to gather and scatter.
47 * 
48 */
49pub struct Solution {}
50
51// problem: https://leetcode.com/problems/booking-concert-tickets-in-groups/
52// discuss: https://leetcode.com/problems/booking-concert-tickets-in-groups/discuss/?currentPage=1&orderBy=most_votes&query=
53
54// submission codes start here
55
56struct BookMyShow {
57        false
58    }
59
60
61/** 
62 * `&self` means the method takes an immutable reference.
63 * If you need a mutable reference, change it to `&mut self` instead.
64 */
65impl BookMyShow {
66
67    fn new(n: i32, m: i32) -> Self {
68        
69    }
70    
71    fn gather(&self, k: i32, max_row: i32) -> Vec<i32> {
72        
73    }
74    
75    fn scatter(&self, k: i32, max_row: i32) -> bool {
76        
77    }
78}
79
80/**
81 * Your BookMyShow object will be instantiated and called as such:
82 * let obj = BookMyShow::new(n, m);
83 * let ret_1: Vec<i32> = obj.gather(k, maxRow);
84 * let ret_2: bool = obj.scatter(k, maxRow);
85 */
86
87// submission codes end
88
89#[cfg(test)]
90mod tests {
91    use super::*;
92
93    #[test]
94    fn test_2286() {
95    }
96}
97


Back
© 2025 bowen.ge All Rights Reserved.