[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

  

原文地址:https://www.cnblogs.com/lightwindy/p/9698958.html

时间: 2024-10-11 05:35:51

[LeetCode] 597. Friend Requests I: Overall Acceptance Rate 朋友请求 I: 全部的接受率的相关文章

python+requests实现接口测试 - get与post请求使用

简介:Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTTP 测试需求.Requests 的哲学是以 PEP 20 的习语为中心开发的,所以它比 urllib 更加 Pythoner.更重要的一点是它支持 Python3 哦! 一.安装 使pip安装: pip install requests 安装完后,运行一个简单的例子查看是否安装成功: impo

python 爬虫 基于requests模块发起ajax的get请求

基于requests模块发起ajax的get请求 需求:爬取豆瓣电影分类排行榜 https://movie.douban.com/中的电影详情数据 用抓包工具捉取 使用ajax加载页面的请求 鼠标往下下滚轮拖动页面,会加载更多的电影信息,这个局部刷新是当前页面发起的ajax请求, 用抓包工具捉取页面刷新的ajax的get请求,捉取滚轮在最底部时候发起的请求 这个get请求是本次发起的请求的url ajax的get请求携带参数 获取响应内容不再是页面数据,是json字符串,是通过异步请求获取的电影

python+requests——不带参数的get请求

import requests url = "http://www.baidu.com" #请求网址 yctime = 0.05 #不能超过的时间 resp = requests.get(url,timeout=yctime) print("#-----------------------------------------------------------------------------------------------1") print(type(res

在requests模块中使用代理发送请求

1. 代理概述 玩爬虫为什么我们不能使用一个固定IP发送请求 你使用一个固定IP发送每秒向对方服务器发送10几个请求,对方认为这样操作不是人干的, 就把你IP给封了 服务器端的人可以根据你IP很快锁定你, 要求你对这种窃取行为赔偿. 代理 正向代理与反向代理 正向代理与反向代理的区别 反向代理: 服务器端知道代理的存在,反向代理是为了保护服务器或负责负载均衡 但是客户端不知道代理的存在的 正向代理: 客户端知道代理的存在,正向代理是为保护客户端,防止追究责任. 但是服务端不知道真实的客户端 2.

[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 ith person. Person A will NOT friend request person B (B != A) if any of the following conditions are true: age[B] <= 0.5 * age[A] + 7 age[B] > age[

python中requests库get方法带参数请求

起因是想爬五等分的花嫁的漫画.这是其中的一个坑 先上代码 data={ 'cid':567464, 'page':1, 'key':'', 'language':1, 'gtk':6, '_cid':567464, '_mid':34949, '_dt':'2019-05-03 13:03:08', '_sign':'e74c8c52618a64a454dd7f12aff3cc1c' }def getFun(url,data): ret=requests.get(url,params=data)

python+requests——发送带参数的get请求

Requests 允许你使用 params 关键字参数,以一个字符串字典来提供这些参数. 举例来说,如果你想传递 key1=value1 和 key2=value2 到 httpbin.org/get ,那么你可以使用如下代码: payload = {'key1': 'value1', 'key2': 'value2'} resp = requests.get("http://httpbin.org/get", params=payload,timeout=0.5) 原文地址:http

Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

Sorted by frequency of problems that appear in real interviews.Last updated: October 2, 2017Google (214)534 Design TinyURL388 Longest Absolute File Path683 K Empty Slots340 Longest Substring with At Most K Distinct Characters681 Next Closest Time482

【sql】leetcode习题 (共 42 题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [175]Combine Two Tables [176]Second Highest Salary [177]Nth Highest Salary [178]Rank Scores [180]Consecutive Numbers [181]Employees Earning More Than Their Managers [182]Duplicate Email