diff --git a/solution/2700-2799/2793.Status of Flight Tickets/README.md b/solution/2700-2799/2793.Status of Flight Tickets/README.md new file mode 100644 index 0000000000000..fa0c31b275cd4 --- /dev/null +++ b/solution/2700-2799/2793.Status of Flight Tickets/README.md @@ -0,0 +1,102 @@ +# [2793. 航班机票状态](https://leetcode.cn/problems/status-of-flight-tickets) + +[English Version](/solution/2700-2799/2793.Status%20of%20Flight%20Tickets/README_EN.md) + +## 题目描述 + + + +
表: Flights
++-------------+------+ +| 列名 | 类型 | ++-------------+------+ +| flight_id | int | +| capacity | int | ++-------------+------+ +flight_id 是该表的主键列。 +每行包含航班 id 和座位容量。 ++ +
表:Passengers
++--------------+----------+ +| 列名 | 类型 | ++--------------+----------+ +| passenger_id | int | +| flight_id | int | +| booking_time | datetime | ++--------------+----------+ +passenger_id 是该表的主键。 +booking_time 包含不同的值。 +每行包含乘客 id、预订时间和所预订的航班 id。 ++ +
乘客提前预订航班机票。如果乘客预订了一张航班机票,并且航班上还有空座位,则乘客的机票将 得到确认 。然而,如果航班已经满员,乘客将被列入 等候名单 。
+ +编写一个 SQL 查询来确定每个乘客航班机票的当前状态。
+ +按 passenger_id
升序排序 返回结果表。
查询结果的格式如下所示。
+ ++ +
示例 1:
+ ++输入: +Flights 表: ++-----------+----------+ +| flight_id | capacity | ++-----------+----------+ +| 1 | 2 | +| 2 | 2 | +| 3 | 1 | ++-----------+----------+ +Passengers 表: ++--------------+-----------+---------------------+ +| passenger_id | flight_id | booking_time | ++--------------+-----------+---------------------+ +| 101 | 1 | 2023-07-10 16:30:00 | +| 102 | 1 | 2023-07-10 17:45:00 | +| 103 | 1 | 2023-07-10 12:00:00 | +| 104 | 2 | 2023-07-05 13:23:00 | +| 105 | 2 | 2023-07-05 09:00:00 | +| 106 | 3 | 2023-07-08 11:10:00 | +| 107 | 3 | 2023-07-08 09:10:00 | ++--------------+-----------+---------------------+ +输出: ++--------------+-----------+ +| passenger_id | Status | ++--------------+-----------+ +| 101 | Confirmed | +| 102 | Waitlist | +| 103 | Confirmed | +| 104 | Confirmed | +| 105 | Confirmed | +| 106 | Waitlist | +| 107 | Confirmed | ++--------------+-----------+ +解释: +- 航班 1 的容量为 2 位乘客。乘客 101 和乘客 103 是最先预订机票的,已经确认他们的预订。然而,乘客 102 是第三位预订该航班的乘客,这意味着没有更多的可用座位。乘客 102 现在被列入等候名单。 +- 航班 2 的容量为 2 位乘客,已经有两位乘客预订了机票,乘客 104 和乘客 105。由于预订机票的乘客数与可用座位数相符,这两个预订都得到了确认。 +- 航班 3 的容量为 1 位乘客,乘客 107 先预订并获得了唯一的可用座位,确认了他们的预订。预订时间在乘客 107 之后的乘客 106 被列入等候名单。+ +## 解法 + + + + + +### **SQL** + + + +```sql + +``` + + diff --git a/solution/2700-2799/2793.Status of Flight Tickets/README_EN.md b/solution/2700-2799/2793.Status of Flight Tickets/README_EN.md new file mode 100644 index 0000000000000..c0fe24b264643 --- /dev/null +++ b/solution/2700-2799/2793.Status of Flight Tickets/README_EN.md @@ -0,0 +1,96 @@ +# [2793. Status of Flight Tickets](https://leetcode.com/problems/status-of-flight-tickets) + +[中文文档](/solution/2700-2799/2793.Status%20of%20Flight%20Tickets/README.md) + +## Description + +
Table: Flights
++-------------+------+
+| Column Name | Type |
++-------------+------+
+| flight_id | int |
+| capacity | int |
++-------------+------+
+flight_id
is the primary key column for this table.
+Each row of this table contains flight id and capacity.
+
+
+Table: Passengers
++--------------+----------+ +| Column Name | Type | ++--------------+----------+ +| passenger_id | int | +| flight_id | int | +| booking_time | datetime | ++--------------+----------+ +passenger_id is the primary key column for this table. +booking_time column contains distinct values. +Each row of this table contains passenger id, booking time, and their flight id. ++ +
Passengers book tickets for flights in advance. If a passenger books a ticket for a flight and there are still empty seats available on the flight, the passenger's ticket will be confirmed. However, the passenger will be on a waitlist if the flight is already at full capacity.
+ +Write an SQL query to determine the current status of flight tickets for each passenger.
+ +Return the result table ordered by passenger_id
in ascending order.
The query result format is in the following example.
+ ++
Example 1:
+ ++Input: +Flights table: ++-----------+----------+ +| flight_id | capacity | ++-----------+----------+ +| 1 | 2 | +| 2 | 2 | +| 3 | 1 | ++-----------+----------+ +Passengers table: ++--------------+-----------+---------------------+ +| passenger_id | flight_id | booking_time | ++--------------+-----------+---------------------+ +| 101 | 1 | 2023-07-10 16:30:00 | +| 102 | 1 | 2023-07-10 17:45:00 | +| 103 | 1 | 2023-07-10 12:00:00 | +| 104 | 2 | 2023-07-05 13:23:00 | +| 105 | 2 | 2023-07-05 09:00:00 | +| 106 | 3 | 2023-07-08 11:10:00 | +| 107 | 3 | 2023-07-08 09:10:00 | ++--------------+-----------+---------------------+ +Output: ++--------------+-----------+ +| passenger_id | Status | ++--------------+-----------+ +| 101 | Confirmed | +| 102 | Waitlist | +| 103 | Confirmed | +| 104 | Confirmed | +| 105 | Confirmed | +| 106 | Waitlist | +| 107 | Confirmed | ++--------------+-----------+ +Explanation: +- Flight 1 has a capacity of 2 passengers. Passenger 101 and Passenger 103 were the first to book tickets, securing the available seats. Therefore, their bookings are confirmed. However, Passenger 102 was the third person to book a ticket for this flight, which means there are no more available seats. Passenger 102 is now placed on the waitlist, +- Flight 2 has a capacity of 2 passengers, Flight 2 has exactly two passengers who booked tickets, Passenger 104 and Passenger 105. Since the number of passengers who booked tickets matches the available seats, both bookings are confirmed. +- Flight 3 has a capacity of 1 passenger. Passenger 107 booked earlier and secured the only available seat, confirming their booking. Passenger 106, who booked after Passenger 107, is on the waitlist. ++ +## Solutions + + + +### **SQL** + +```sql + +``` + + diff --git a/solution/2700-2799/2794.Create Object from Two Arrays/README.md b/solution/2700-2799/2794.Create Object from Two Arrays/README.md new file mode 100644 index 0000000000000..f52aef92eff80 --- /dev/null +++ b/solution/2700-2799/2794.Create Object from Two Arrays/README.md @@ -0,0 +1,66 @@ +# [2794. 从两个数组中创建对象](https://leetcode.cn/problems/create-object-from-two-arrays) + +[English Version](/solution/2700-2799/2794.Create%20Object%20from%20Two%20Arrays/README_EN.md) + +## 题目描述 + + + +
给定两个数组 keysArr
和 valuesArr
,返回一个新的对象 obj
。obj
中的每个键值对都来自 keysArr[i]
和 valuesArr[i]
。
- 如果前面的索引中存在重复的键,则应该跳过该键值对。换句话说,只有第一次出现的键会被添加到对象中。
+ +- 如果键不是字符串,则应通过调用 String()
方法将其转换为字符串。
+ +
示例 1:
+ ++输入:arr1 = ["a", "b", "c"], arr2 = [1, 2, 3] +输出:{"a": 1, "b": 2, "c": 3} +解释:键 "a"、"b" 和 "c" 分别与值 1、2 和 3 配对。 ++ +
示例 2:
+ ++输入:arr1 = ["1", 1, false], arr2 = [4, 5, 6] +输出:{"1": 4, "false": 6} +解释:首先,将 arr1 中的所有元素转换为字符串。我们可以看到有两个 "1" 的出现。使用第一次出现 "1" 的关联值:4。 ++ +
示例 3:
+ ++输入:arr1 = [], arr2 = [] +输出:{} +解释:没有键,因此返回一个空对象。 ++ +
+ +
提示:
+ +arr1 和 arr2 都是有效的 JSON 数组
2 <= JSON.stringify(arr1).length <= 5 * 105
2 <= JSON.stringify(arr2).length <= 5 * 105
arr1.length === arr2.length
Given two arrays keysArr
and valuesArr
, return a new object obj
. Each key-value pair in obj
should come from keysArr[i]
and valuesArr[i]
.
- If a duplicate key exists at a previous index, that key-value should be excluded. In other words, only the first key should be added to the object.
+ +- If the key is not a string, it should be converted into a string by calling `String()` on it.
+ ++
Example 1:
+ ++Input: arr1 = ["a", "b", "c"], arr2 = [1, 2, 3] +Output: {"a": 1, "b": 2, "c": 3} +Explanation: The keys "a", "b", and "c" are paired with the values 1, 2, and 3 respectively. ++ +
Example 2:
+ ++Input: arr1 = ["1", 1, false], arr2 = [4, 5, 6] +Output: {"1": 4, "false": 6} +Explanation: First, all the elements in arr1 are converted into strings. We can see there are two occurrences of "1". The value associated with the first occurrence of "1" is used: 4. ++ +
Example 3:
+ ++Input: arr1 = [], arr2 = [] +Output: {} +Explanation: There are no keys so an empty object is returned. ++ +
+
Constraints:
+ +arr1 and arr2 are valid JSON arrays
2 <= JSON.stringify(arr1).length <= 5 * 105
2 <= JSON.stringify(arr2).length <= 5 * 105
arr1.length === arr2.length
给定一个数组 functions
,返回一个 promise 对象 promise
。functions
是一个返回多个 promise 对象 fnPromise
的函数数组。每个 fnPromise
可以被解析(resolved)或拒绝(rejected)。
如果 fnPromise
被解析:
obj = { status: "fulfilled", value: resolved value}
如果 fnPromise
被拒绝:
obj = { status: "rejected", reason: 拒绝的原因(捕获的错误消息)}
该 promise
应该返回一个包含这些对象 obj
的数组。数组中的每个 obj
应该对应原始函数数组中的多个 promise 对象,并保持相同的顺序。
请在不使用内置方法 Promise.allSettled()
的情况下实现它。
+ +
示例 1:
+ ++输入:functions = [ + () => new Promise(resolve => setTimeout(() => resolve(15), 100)) +] +输出:{"t":100,"values":[{"status":"fulfilled","value":15}]} +解释: +const time = performance.now() +const promise = promiseAllSettled(functions); + +promise.then(res => { + const out = {t: Math.floor(performance.now() - time), values: res} + console.log(out) // {"t":100,"values":[{"status":"fulfilled","value":15}]} +}) + +返回的 promise 在 100 毫秒内解析。由于函数数组中的 promise 被解析,返回的 promise 的解析值设置为[{"status":"fulfilled","value":15}]。 ++ +
示例 2:
+ ++输入:functions = [ + () => new Promise(resolve => setTimeout(() => resolve(20), 100)), + () => new Promise(resolve => setTimeout(() => resolve(15), 100)) +] +输出: +{ + "t":100, + "values": [ + {"status":"fulfilled","value":20}, + {"status":"fulfilled","value":15} + ] +} +解释:返回的 promise 在 100 毫秒内解析,因为解析时间取决于需要最长时间来解析的 promise。由于函数数组中的 promises 被解析,返回的 promise 的解析值设置为[{"status":"fulfilled","value":20},{"status":"fulfilled","value":15}]。 ++ +
示例 3:
+ ++输入:functions = [ + () => new Promise(resolve => setTimeout(() => resolve(30), 200)), + () => new Promise((resolve, reject) => setTimeout(() => reject("Error"), 100)) +] +输出: +{ + "t":200, + "values": [ + {"status":"fulfilled","value":30}, + {"status":"rejected","reason":"Error"} + ] +} +解释:返回的 promise 在 200 毫秒内解析,因为解析时间取决于需要最长时间来解析的 promise。由于函数数组中的一个 promise 被解析,另一个被拒绝,返回的 promise 的解析值设置为[{"status":"fulfilled","value":30},{"status":"rejected","reason":"Error"}]。数组中的每个对象对应原始函数数组中的 promise,并保持相同的顺序。 ++ +
+ +
提示:
+ +1 <= functions.length <= 10
Given an array functions
, return a promise promise
. functions
is an array of functions that return promises fnPromise.
Each fnPromise
can be resolved or rejected.
If fnPromise
is resolved:
obj = { status: "fulfilled", value: resolved value}
If fnPromise
is rejected:
obj = { status: "rejected", reason: reason of rejection (catched error message)}
The promise
should resolve with an array of these objects obj
. Each obj
in the array should correspond to the promises in the original array function, maintaining the same order.
Try to implement it without using the built-in method Promise.allSettled()
.
+
Example 1:
+ ++Input: functions = [ + () => new Promise(resolve => setTimeout(() => resolve(15), 100)) +] +Output: {"t":100,"values":[{"status":"fulfilled","value":15}]} +Explanation: +const time = performance.now() +const promise = promiseAllSettled(functions); + +promise.then(res => { + const out = {t: Math.floor(performance.now() - time), values: res} + console.log(out) // {"t":100,"values":[{"status":"fulfilled","value":15}]} +}) + +The returned promise resolves within 100 milliseconds. Since promise from the array functions is fulfilled, the resolved value of the returned promise is set to [{"status":"fulfilled","value":15}]. ++ +
Example 2:
+ ++Input: functions = [ + () => new Promise(resolve => setTimeout(() => resolve(20), 100)), + () => new Promise(resolve => setTimeout(() => resolve(15), 100)) +] +Output: +{ + "t":100, + "values": [ + {"status":"fulfilled","value":20}, + {"status":"fulfilled","value":15} + ] +} +Explanation: The returned promise resolves within 100 milliseconds, because the resolution time is determined by the promise that takes the longest time to fulfill. Since promises from the array functions are fulfilled, the resolved value of the returned promise is set to [{"status":"fulfilled","value":20},{"status":"fulfilled","value":15}]. ++ +
Example 3:
+ ++Input: functions = [ + () => new Promise(resolve => setTimeout(() => resolve(30), 200)), + () => new Promise((resolve, reject) => setTimeout(() => reject("Error"), 100)) +] +Output: +{ + "t":200, + "values": [ + {"status":"fulfilled","value":30}, + {"status":"rejected","reason":"Error"} + ] +} +Explanation: The returned promise resolves within 200 milliseconds, as its resolution time is determined by the promise that takes the longest time to fulfill. Since one promise from the array function is fulfilled and another is rejected, the resolved value of the returned promise is set to an array containing objects in the following order: [{"status":"fulfilled","value":30}, {"status":"rejected","reason":"Error"}]. Each object in the array corresponds to the promises in the original array function, maintaining the same order. ++ +
+
Constraints:
+ +1 <= functions.length <= 10
编写代码实现字符串方法 string.replicate(x)
,它将返回重复的字符串 x
次。
请尝试在不使用内置方法 string.repeat
的情况下实现它。
+ +
示例 1:
+ ++输入:str = "hello", times = 2 +输出:"hellohello" +解释:"hello" 被重复了 2 次 ++ +
示例 2:
+ ++输入:str = "code", times = 3 +输出:codecodecode" +Explanation: "code" 被重复了 3 次 ++ +
示例 3:
+ ++输入:str = "js", times = 1 +输出:"js" +解释:"js" 被重复了 1 次 ++ +
+ +
提示:
+ +1 <= str.length <= 1000
1 <= times <= 1000
Write code that enhances all strings such that you can call the string.replicate(x)
method on any string and it will return repeated string x
times.
Try to implement it without using the built-in method string.repeat
.
+
Example 1:
+ ++Input: str = "hello", times = 2 +Output: "hellohello" +Explanation: "hello" is repeated 2 times ++ +
Example 2:
+ ++Input: str = "code", times = 3 +Output: "codecodecode" +Explanation: "code" is repeated 3 times ++ +
Example 3:
+ ++Input: str = "js", times = 1 +Output: "js" +Explanation: "js" is repeated 1 time ++ +
+
Constraints:
+ +1 <= str.length <= 1000
1 <= times <= 1000
给定函数 fn
和数组 args
,返回一个函数 partialFn
。
args
中的占位符 "_"
需要用 restArgs
中索引从 0
开始的值替换。 restArgs
中剩余的值则添加到 args
的末尾。
partialFn
应该返回 fn
的结果。fn
应该使用修改后的 args
的元素作为单独的参数调用。
+ +
示例 1:
+ ++输入:fn = (...args) => args, args = [2,4,6], restArgs = [8,10] +输出:[2,4,6,8,10] +解释: +const partialFn = partial(fn, args) +const result = partialFn(...restArgs) +console.log(result) // [2,4,6,8,10] + +args 中没有占位符 "_",因此 restArgs 只是添加到 args 的末尾。然后将 args 的元素作为单独的参数传递给 fn,fn 返回传递的参数作为数组。 ++ +示例 2: + +
+输入:fn = (...args) => args, args = [1,2,"_",4,"_",6], restArgs = [3,5] +输出:[1,2,3,4,5,6] +解释: +const partialFn = partial(fn, args) +const result = partialFn(...restArgs) +console.log(result) // [1,2,3,4,5,6] + +占位符 "_" 被 restArgs 中的值替换。然后将 args 的元素作为单独的参数传递给 fn,fn 返回传递的参数作为数组。 ++ +
示例 3:
+ ++输入:fn = (a, b, c) => b + a - c, args = ["_", 5], restArgs = [5, 20] +输出:-10 +解释: +const partialFn = partial(fn, args) +const result = partialFn(...restArgs) +console.log(result) // -10 + +占位符 "_" 被替换为 5,并将 20 添加到 args 的末尾。然后将 args 的元素作为单独的参数传递给 fn,fn 返回 -10(5 + 5 - 20)。 ++ +
+ +
提示:
+ +fn 是一个函数
args 和 都是有效的 JSON 数组
1 <= args.length <= 5 * 104
1 <= restArgs.length <= 5 * 104
0 <= number of placeholders <= restArgs.length
Given a function fn
and an array args
, return a function partialFn
.
Placeholders "_"
in the args
should be replaced with values from restArgs
starting from index 0
. Any remaining values in the restArgs
should be added at the end of the args
.
partialFn
should return a result of fn
. fn
should be called with the elements of the modified args
passed as separate arguments.
+
Example 1:
+ ++Input: fn = (...args) => args, args = [2,4,6], restArgs = [8,10] +Output: [2,4,6,8,10] +Explanation: +const partialFn = partial(fn, args) +const result = partialFn(...restArgs) +console.log(result) // [2,4,6,8,10] + +There are no placeholders "_" in args therefore restArgs is just added at the end of args. Then the elements of the args are passed as separate arguments to fn, which returns passed arguments as an array. ++ +Example 2: + +
+Input: fn = (...args) => args, args = [1,2,"_",4,"_",6], restArgs = [3,5] +Output: [1,2,3,4,5,6] +Explanation: +const partialFn = partial(fn, args) +const result = partialFn(...restArgs) +console.log(result) // [1,2,3,4,5,6] + +Placeholders "_" are replaced with values from the restArgs. Then the elements of the args are passed as separate arguments to fn, which returns passed arguments as an array. ++ +
Example 3:
+ ++Input: fn = (a, b, c) => b + a - c, args = ["_", 5], restArgs = [5, 20] +Output: -10 +Explanation: +const partialFn = partial(fn, args) +const result = partialFn(...restArgs) +console.log(result) // -10 + +Placeholder "_" is replaced with 5 and 20 is added at the end of args. Then the elements of the args are passed as separate arguments to fn, which returns -10 (5 + 5 - 20). ++ +
+
Constraints:
+ +fn is a function
args and restArgs are valid JSON arrays
1 <= args.length <= 5 * 104
1 <= restArgs.length <= 5 * 104
0 <= number of placeholders <= restArgs.length