1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-10-23 22:08:58 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
Files
leetcode-problemset/leetcode-cn/problem (Chinese)/寻找黄金时段客户 [find-golden-hour-customers].html
2025-10-19 23:12:56 +08:00

133 lines
6.4 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>表:<code>restaurant_orders</code></p>
<pre>
+------------------+----------+
| Column Name | Type |
+------------------+----------+
| order_id | int |
| customer_id | int |
| order_timestamp | datetime |
| order_amount | decimal |
| payment_method | varchar |
| order_rating | int |
+------------------+----------+
order_id 是这张表的唯一主键。
payment_method 可以是 cashcard 或 app。
order_rating 在 1 到 5 之间,其中 5 是最佳(如果没有评分则是 NULL
order_timestamp 同时包含日期和时间信息。
</pre>
<p>编写一个解决方案来寻找 <strong>黄金时间客户</strong>&nbsp;- 高峰时段持续订购且满意度高的客户。客户若满足以下所有条件,则被视为 <strong>黄金时段客户</strong></p>
<ul>
<li>进行 <strong>至少</strong>&nbsp;<code>3</code>&nbsp;笔订单。</li>
<li>他们有&nbsp;<strong>至少</strong>&nbsp;<code>60%</code>&nbsp;的订单在 <strong>高峰时间</strong>&nbsp;中(<code>11:00</code>-<code>14:00</code>&nbsp;<code>18:00</code>-<code>21:00</code>)。</li>
<li>他们的 <strong>平均评分</strong> 至少为 <code>4.0</code>,四舍五入到小数点后 <code>2</code> 位。</li>
<li>已评价至少 <code>50%</code> 的订单。</li>
</ul>
<p>返回结果表按&nbsp;<code>average_rating</code> <strong>降序</strong>&nbsp;排序,然后按&nbsp;<code>customer_id</code> <strong>降序&nbsp;</strong>排序。</p>
<p>结果格式如下所示。</p>
<p>&nbsp;</p>
<p><strong class="example">示例:</strong></p>
<div class="example-block">
<p><b>输入:</b></p>
<p>restaurant_orders 表:</p>
<pre class="example-io">
+----------+-------------+---------------------+--------------+----------------+--------------+
| order_id | customer_id | order_timestamp | order_amount | payment_method | order_rating |
+----------+-------------+---------------------+--------------+----------------+--------------+
| 1 | 101 | 2024-03-01 12:30:00 | 25.50 | card | 5 |
| 2 | 101 | 2024-03-02 19:15:00 | 32.00 | app | 4 |
| 3 | 101 | 2024-03-03 13:45:00 | 28.75 | card | 5 |
| 4 | 101 | 2024-03-04 20:30:00 | 41.00 | app | NULL |
| 5 | 102 | 2024-03-01 11:30:00 | 18.50 | cash | 4 |
| 6 | 102 | 2024-03-02 12:00:00 | 22.00 | card | 3 |
| 7 | 102 | 2024-03-03 15:30:00 | 19.75 | cash | NULL |
| 8 | 103 | 2024-03-01 19:00:00 | 55.00 | app | 5 |
| 9 | 103 | 2024-03-02 20:45:00 | 48.50 | app | 4 |
| 10 | 103 | 2024-03-03 18:30:00 | 62.00 | card | 5 |
| 11 | 104 | 2024-03-01 10:00:00 | 15.00 | cash | 3 |
| 12 | 104 | 2024-03-02 09:30:00 | 18.00 | cash | 2 |
| 13 | 104 | 2024-03-03 16:00:00 | 20.00 | card | 3 |
| 14 | 105 | 2024-03-01 12:15:00 | 30.00 | app | 4 |
| 15 | 105 | 2024-03-02 13:00:00 | 35.50 | app | 5 |
| 16 | 105 | 2024-03-03 11:45:00 | 28.00 | card | 4 |
+----------+-------------+---------------------+--------------+----------------+--------------+
</pre>
<p><strong>输出:</strong></p>
<pre class="example-io">
+-------------+--------------+----------------------+----------------+
| customer_id | total_orders | peak_hour_percentage | average_rating |
+-------------+--------------+----------------------+----------------+
| 103 | 3 | 100 | 4.67 |
| 101 | 4 | 75 | 4.67 |
| 105 | 3 | 100 | 4.33 |
+-------------+--------------+----------------------+----------------+
</pre>
<p><strong>解释:</strong></p>
<ul>
<li><strong>客户 101</strong>
<ul>
<li>总订单数4至少 3 笔)</li>
<li>高峰时间订单4 笔中有 3 笔12:3019:1513:45 和 20:30 在高峰时间)</li>
<li>高峰时间占比3/4 = 75%(至少 60%</li>
<li>已评分的订单4 笔中有 3 笔75% 评分完成率)</li>
<li>平均评分:(5+4+5)/3 = 4.67(至少 4.0</li>
<li>结果:<strong>黄金时段客户</strong></li>
</ul>
</li>
<li><strong>客户 102</strong>:
<ul>
<li>总订单数3至少 3 笔)</li>
<li>高峰时间订单3 笔中有 2 笔11:3012:00 都在高峰时间,但 15:30 不是)</li>
<li>高峰时间占比2/3 = 66.67%(至少 60%</li>
<li>已评分的订单3 笔中有 2 笔66.67% 评分完成率)</li>
<li>平均评分:(4+3)/2 = 3.5(少于 4.0</li>
<li>结果:<strong>不是黄金时段客户</strong>(平均评分太低)</li>
</ul>
</li>
<li><strong>客户 103</strong>:
<ul>
<li>总订单数3至少 3 笔)</li>
<li>高峰时间订单3 笔中有 3 19:0020:4518:30 都在傍晚高峰时间)</li>
<li>高峰时间占比3/3 = 100%(至少 60%</li>
<li>已评分的订单3 笔中有 3 笔100% 评分完成率)</li>
<li>平均评分:(5+4+5)/3 = 4.67(至少 4.0</li>
<li>结果:<strong>黄金时段客户</strong></li>
</ul>
</li>
<li><strong>客户 104</strong>:
<ul>
<li>总订单数3至少 3 笔)</li>
<li>高峰时间订单3 笔中有 0 笔10:0009:3016:00 都不在高峰时间)</li>
<li>高峰时间占比0/3 = 0%(至少 60%</li>
<li>结果:<strong>不是黄金时段客户</strong>(高峰时段订单不足)</li>
</ul>
</li>
<li><strong>客户 105</strong>:
<ul>
<li>总订单数3至少 3 笔)</li>
<li>高峰时间订单3 笔中有 3 笔12:1513:0011:45 都在中午高峰时间)</li>
<li>高峰时间占比3/3 = 100%(至少 60%</li>
<li>已评分的订单3 笔中有 3 笔100% 评分完成率)</li>
<li>平均评分:(4+5+4)/3 = 4.33(至少 4.0</li>
<li>结果:<strong>黄金时段客户</strong></li>
</ul>
</li>
</ul>
<p>结果表按 average_rating 降序排序,然后按 customer_id 降序排序。</p>
</div>