力扣-1501.可以放心投资的国家

目录

题目:

要求:

测试数据:

创建数据库:

表数据图:

分析:

代码实现:


题目:

表 Person:

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| id             | int     |
| name           | varchar |
| phone_number   | varchar |
+----------------+---------+
id 是该表具有唯一值的列.
该表每一行包含一个人的名字和电话号码.
电话号码的格式是:'xxx-yyyyyyy', 其中 xxx 是国家码(3 个字符), yyyyyyy 是电话号码(7 个字符), x 和 y 都表示数字. 同时, 国家码和电话号码都可以包含前导 0.

表 Country:

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| name           | varchar |
| country_code   | varchar |
+----------------+---------+
country_code 是该表具有唯一值的列.
该表每一行包含国家名和国家码. country_code 的格式是'xxx', x 是数字.

表 Calls:

+-------------+------+
| Column Name | Type |
+-------------+------+
| caller_id   | int  |
| callee_id   | int  |
| duration    | int  |
+-------------+------+
该表无主键, 可能包含重复行.
每一行包含呼叫方 id, 被呼叫方 id 和以分钟为单位的通话时长. caller_id != callee_id

要求:

一家电信公司想要投资新的国家。该公司想要投资的国家是:  该国的平均通话时长要严格地大于全球平均通话时长。

写一个解决方案,  找到所有该公司可以投资的国家。

返回的结果表 无顺序要求

测试数据:

创建数据库:

Create table If Not Exists Person (id int, name varchar(15), phone_number varchar(11))
Create table If Not Exists Country (name varchar(15), country_code varchar(3))
Create table If Not Exists Calls (caller_id int, callee_id int, duration int)
Truncate table Person
insert into Person (id, name, phone_number) values ('3', 'Jonathan', '051-1234567')
insert into Person (id, name, phone_number) values ('12', 'Elvis', '051-7654321')
insert into Person (id, name, phone_number) values ('1', 'Moncef', '212-1234567')
insert into Person (id, name, phone_number) values ('2', 'Maroua', '212-6523651')
insert into Person (id, name, phone_number) values ('7', 'Meir', '972-1234567')
insert into Person (id, name, phone_number) values ('9', 'Rachel', '972-0011100')
Truncate table Country
insert into Country (name, country_code) values ('Peru', '051')
insert into Country (name, country_code) values ('Israel', '972')
insert into Country (name, country_code) values ('Morocco', '212')
insert into Country (name, country_code) values ('Germany', '049')
insert into Country (name, country_code) values ('Ethiopia', '251')
Truncate table Calls
insert into Calls (caller_id, callee_id, duration) values ('1', '9', '33')
insert into Calls (caller_id, callee_id, duration) values ('2', '9', '4')
insert into Calls (caller_id, callee_id, duration) values ('1', '2', '59')
insert into Calls (caller_id, callee_id, duration) values ('3', '12', '102')
insert into Calls (caller_id, callee_id, duration) values ('3', '12', '330')
insert into Calls (caller_id, callee_id, duration) values ('12', '3', '5')
insert into Calls (caller_id, callee_id, duration) values ('7', '9', '13')
insert into Calls (caller_id, callee_id, duration) values ('7', '1', '3')
insert into Calls (caller_id, callee_id, duration) values ('9', '7', '1')
insert into Calls (caller_id, callee_id, duration) values ('1', '7', '7')

表数据图:

输入:
Person 表:
+----+----------+--------------+
| id | name     | phone_number |
+----+----------+--------------+
| 3  | Jonathan | 051-1234567  |
| 12 | Elvis    | 051-7654321  |
| 1  | Moncef   | 212-1234567  |
| 2  | Maroua   | 212-6523651  |
| 7  | Meir     | 972-1234567  |
| 9  | Rachel   | 972-0011100  |
+----+----------+--------------+
Country 表:
+----------+--------------+
| name     | country_code |
+----------+--------------+
| Peru     | 051          |
| Israel   | 972          |
| Morocco  | 212          |
| Germany  | 049          |
| Ethiopia | 251          |
+----------+--------------+
Calls 表:
+-----------+-----------+----------+
| caller_id | callee_id | duration |
+-----------+-----------+----------+
| 1         | 9         | 33       |
| 2         | 9         | 4        |
| 1         | 2         | 59       |
| 3         | 12        | 102      |
| 3         | 12        | 330      |
| 12        | 3         | 5        |
| 7         | 9         | 13       |
| 7         | 1         | 3        |
| 9         | 7         | 1        |
| 1         | 7         | 7        |
+-----------+-----------+----------+

分析:

代码实现:

with t1 as (select caller_id cid, duration
            from calls
            union all
            select callee_id cid, duration
            from calls),
     t2 as (select cid, sum(duration) sm, count(duration) cnt
            from t1
            group by cid),
     t3 as (select id,
                   country.name,
                   country_code,
                   sm,
                   cnt
            FROM country
                     join person on phone_number = country_code + '%'
                     join t2 on id = cid)
select name
from t3
group by country_code, name
having (sum(sm) / sum(cnt))> (select sum(sm)/sum(cnt) from t3);

### 如何使用力扣平台 #### 平台概述 力扣是一个专注于算法和数据结构练习的学习平台,提供了丰富的题目资源以及在线评测功能。通过该平台,用户能够针对不同难度级别的题目进行训练,从而提高自身的编程能力和逻辑思维能力。 #### 刷题策略建议 为了高效利用时间并取得更好的学习效果,在力扣上刷题时可以选择特定范围内的高质量题目集中攻克。例如,优先完成前200道经典题目是非常明智的选择[^1]。这些题目涵盖了大部分基础知识点,并且与HOT 100榜单中的高频考点高度吻合。 #### 数据结构专项练习 对于某些具体的数据结构领域如链表操作,则可以通过专门挑选一系列相关联的经典习题来进行深入研究。比如下面列举了一些典型的关于链表处理方面的挑战编号列表供参考[^2]: - 反转单链表 (No. 206) - 移除链表元素 (No. 203) #### 实际案例解析 以一道具体的加油站在环路上行驶场景为例说明如何应用动态规划或者模拟法解决问题。假设存在一组加油站及其对应的燃油供应量数组gas[]及消耗成本数组cost[], 我们要找到一个起点使得车辆能顺利完成一圈旅行而不会中途耗尽燃料。以下是基于上述描述的一个解决方案思路[^3]: ```python def canCompleteCircuit(gas, cost): n = len(gas) total_tank, curr_tank = 0, 0 starting_station = 0 for i in range(n): total_tank += gas[i] - cost[i] curr_tank += gas[i] - cost[i] if curr_tank < 0: starting_station = i + 1 curr_tank = 0 return starting_station if total_tank >= 0 else -1 ``` #### 职业发展导向 另外值得注意的是,除了个人兴趣驱动外,很多人也会因为求职需求加入到每日打卡刷题大军之中。确实如此,无论是BAT还是其他知名互联网公司都会把候选人的算法功底当作重要考核指标之一[^4]。因此合理安排复习计划显得尤为重要。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值