[LeetCode] 597. Friend Requests I: Overall Acceptance Rate 朋友请求 I: 全部的接受率
In social network like Facebook or Twitter, people send friend requests and accept others’ requests as well. Now given two tables as below:
Table: friend_request
| sender_id | send_to_id |request_date|
|-----------|------------|------------|
| 1 | 2 | 2016_06-01 |
| 1 | 3 | 2016_06-01 |
| 1 | 4 | 2016_06-01 |
| 2 | 3 | 2016_06-02 |
| 3 | 4 | 2016-06-09 |
Table: request_accepted
| requester_id | accepter_id |accept_date |
|--------------|-------------|------------|
| 1 | 2 | 2016_06-03 |
| 1 | 3 | 2016-06-08 |
| 2 | 3 | 2016-06-08 |
| 3 | 4 | 2016-06-09 |
| 3 | 4 | 2016-06-10 |
Write a query to find the overall acceptance rate of requests rounded to 2 decimals, which is the number of acceptance divide the number of requests.
For the sample data above, your query should return the following result.
|accept_rate|
|-----------|
| 0.80|
Note:
- The accepted requests are not necessarily from the table
friend_request. In this case, you just need to simply count the total accepted requests (no matter whether they are in the original requests), and divide it by the number of requests to get the acceptance rate. - It is possible that a sender sends multiple requests to the same receiver, and a request could be accepted more than once. In this case, the ‘duplicated’ requests or acceptances are only counted once.
- If there is no requests at all, you should return 0.00 as the accept_rate.
Explanation: There are 4 unique accepted requests, and there are 5 requests in total. So the rate is 0.80.
Follow-up:
- Can you write a query to return the accept rate but for every month?
- How about the cumulative accept rate for every day?
Intuition
Count the accepted requests and then divides it by the number of all requests.
Algorithm
To get the distinct number of accepted requests, we can query from the request_accepted table.
select count(*) from (select distinct requester_id, accepter_id from request_accepted;
With the same technique, we can have the total number of requests from the friend_request table:
select count(*) from (select distinct sender_id, send_to_id from friend_request;
At last, divide these two numbers and round it to a scale of 2 decimal places to get the required acceptance rate.
Wait! The divisor (total number of requests) could be '0' if the table friend_request is empty. So, we have to utilize ifnull to deal with this special case.
解法1:
select
round(
ifnull(
(select count(*) from (select distinct requester_id, accepter_id from request_accepted) as A)
/
(select count(*) from (select distinct sender_id, send_to_id from friend_request) as B),
0)
, 2) as accept_rate;
解法2:
select coalesce(round
(count(distinct requester_id, accepter_id)
/
count(distinct sender_id, send_to_id),2),
0)
as accept_rate
from friend_request, request_accepted
All LeetCode Questions List 题目汇总
[LeetCode] 597. Friend Requests I: Overall Acceptance Rate 朋友请求 I: 全部的接受率的更多相关文章
- python+requests实现接口测试 - get与post请求使用(转载)
转自:http://www.cnblogs.com/nizhihong/p/6567928.html 简介:Requests 是用Python语言编写,基于 urllib,采用 Apache2 Lic ...
- python+requests实现接口测试 - get与post请求使用
简介:Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 ...
- python 爬虫 基于requests模块发起ajax的post请求
基于requests模块发起ajax的post请求 需求:爬取肯德基餐厅查询http://www.kfc.com.cn/kfccda/index.aspx中指定某个城市地点的餐厅数据 点击肯德基餐厅查 ...
- python 爬虫 基于requests模块发起ajax的get请求
基于requests模块发起ajax的get请求 需求:爬取豆瓣电影分类排行榜 https://movie.douban.com/中的电影详情数据 用抓包工具捉取 使用ajax加载页面的请求 鼠标往下 ...
- [LeetCode] 602. Friend Requests II: Who Has Most Friend? 朋友请求 II: 谁有最多的朋友?
In social network like Facebook or Twitter, people send friend requests and accept others' requests ...
- [LeetCode] Friends Of Appropriate Ages 适合年龄段的朋友
Some people will make friend requests. The list of their ages is given and ages[i] is the age of the ...
- 6-使用requests库封装类处理get/post请求
1.request安装 1)pip安装,直接pip install requests 2)下载离线包安装,加压后,命令行进入路径,执行python setup.py install 2.创建工程 注意 ...
- requests库入门11-重定向和请求历史
默认情况下,除了head请求,requests会自动处理重定向 重定向就是会把url重新指定到另一个.比如github,使用http会自动重定向到https.一些公司也会使用网关啥的做重定向. r = ...
- Python3下requests库发送multipart/form-data类型请求
[本文出自天外归云的博客园] 要模拟multipart/form-data类型请求,可以用python3的requests库完成.代码示例如下: #请求的接口url url = "url&q ...
随机推荐
- 对 Jenkins+ANT+Jmeter 接口测试的实践
转载地址:https://testerhome.com/topics/5262 1.前言 最近感觉大家都在讲Jenkins+jmeter+ant或maven的使用,但没有说到具体怎么投入到项目使用,只 ...
- 12.基于vue-router的案例
案例分析 用到的路由技术要点: 路由的基础用法 嵌套路由 路由重定向 路由传参 编程式导航 根据项目的整体布局划分好组件结构,通过路由导航控制组件的显示 1.抽离并渲染 App根组件 2.将左侧菜 单 ...
- vscode——常用插件记录
前言 本人vscode中使用的插件列表,记录下. 列表 Auto Rename Tag 自动重命名成对的超文本标记语言/可扩展标记语言 background-cover 为vscode设置背景图片 C ...
- 六.深浅copy
先问问大家,什么是拷贝?拷贝是音译的词,其实他是从copy这个英文单词音译过来的,那什么是copy? copy其实就是复制一份,也就是所谓的抄一份.深浅copy其实就是完全复制一份,和部分复制一份的意 ...
- 算法笔记求序列A每个元素左边比它小的数的个数(树状数组和离散化)
#include <iostream> #include <algorithm> #include <cstring> using namespace std ; ...
- Java中多态
多态:把子类看成是父类,把实现类看成是接口,这样类就具有多种形态,称为多态. 在多态中访问成员变量,访问的是父类中的成员变量. 在多态中访问成员方法,先访问的是子类,看看子类有没有覆盖重写要访问的父类 ...
- 文件搜索命令find
1.路径加文件名搜索(find): 查找的是etc目录下的以init为名字的文件. 加通配符后为模糊搜索,只要文件名中含有init即可. 查找etc目录下以init开头的七位文件名. 2.搜索时不区分 ...
- linux命令之------More命令
More命令 1)作用:命令类似cat,不过会以一页一页的形式显示,更方便使用者逐页阅读. 2)-num:一次显示的行数 3)-d:提示使用者,在画面下方显示[Press space to conti ...
- sublime text 3插件改造之AutoFileName去掉.vue文件中img标签后面的width和height,完全去掉!!
在.vue文件中img标签使用autofilename提示引入文件时,会在文件后面插入宽度高度,如下图: 文件后面会自动插入height和width,其实这两玩意儿在大多数时候并没卵用,然后就开始了百 ...
- 【luoguP2371】 [国家集训队]墨墨的等式
题目链接 考虑将所有的\(a_1x_1+a_2x_2+--+a_nx_n=B\)对\(a_1\)取模,那么所有可达到的B就分为了\(0\)~\(a_1-1\)类 如果对\(a_1\)取模为\(k\)的 ...