【LeetCode】825. Friends Of Appropriate Ages 解题报告(Python)
作者: 负雪明烛
 id: fuxuemingzhu
 个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/friends-of-appropriate-ages/description/
题目描述:
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[A]
- age[B] > 100 && age[A] < 100
Otherwise, A will friend request B.
Note that if A requests B, B does not necessarily request A. Also, people will not friend request themselves.
How many total friend requests are made?
Example 1:
Input: [16,16]
Output: 2
Explanation: 2 people friend request each other.
Example 2:
Input: [16,17,18]
Output: 2
Explanation: Friend requests are made 17 -> 16, 18 -> 17.
Example 3:
Input: [20,30,100,110,120]
Output:
Explanation: Friend requests are made 110 -> 100, 120 -> 110, 120 -> 100.
Notes:
- 1 <= ages.length <= 20000.
- 1 <= ages[i] <= 120.
题目大意
这个题让我们找出有多少个好友申请。好友申请不能成立的条件:
- age[B] <= 0.5 * age[A] + 7
- age[B] > age[A]
- age[B] > 100 && age[A] < 100
总结一下:A只能向年龄小于等于自己的人(B)申请,并且也不能太小,总之要满足0.5 * age[A] + 7 < B <= A.
第三个条件是第二个条件的子集,只要满足条件二一定满足条件三。
解题方法
把上面的约束条件理解清楚之后就很简单了。首先我们需要统计个数,然后肯定需要进行排序,然后遍历标记为A,查找满足他的申请条件的B有多少。
对于A,B他们之间有多少对申请?如果A!=B,那么A要向每个B进行申请;如果A==B,那么A要向和他年龄一样大的其他人申请。
时间复杂度是O(120 * N),空间复杂度是O(120).
class Solution(object):
    def numFriendRequests(self, ages):
        """
        :type ages: List[int]
        :rtype: int
        """
        count = collections.Counter(ages)
        ages = sorted(count.keys())
        N = len(ages)
        res = 0
        for A in ages:
            for B in range(int(0.5 * A) + 7 + 1, A + 1):
                res += count[A] * (count[B] - int(A == B))
        return res
日期
2018 年 10 月 19 日 —— 自古逢秋悲寂寥,我言秋日胜春朝
【LeetCode】825. Friends Of Appropriate Ages 解题报告(Python)的更多相关文章
- 【LeetCode】206. Reverse Linked List 解题报告(Python&C++&java)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 递归 日期 [LeetCode] 题目地址:h ... 
- 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ... 
- 【LeetCode】784. Letter Case Permutation 解题报告 (Python&C++)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 循环 日期 题目地址:https://leet ... 
- 【LeetCode】760. Find Anagram Mappings 解题报告
		[LeetCode]760. Find Anagram Mappings 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/find ... 
- 【LeetCode】Pascal's Triangle II 解题报告
		[LeetCode]Pascal's Triangle II 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/pascals-tr ... 
- 【LeetCode】299. Bulls and Cows 解题报告(Python)
		[LeetCode]299. Bulls and Cows 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题 ... 
- 【LeetCode】743. Network Delay Time 解题报告(Python)
		[LeetCode]743. Network Delay Time 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: ht ... 
- 【LeetCode】518. Coin Change 2 解题报告(Python)
		[LeetCode]518. Coin Change 2 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目 ... 
- 【LeetCode】474. Ones and Zeroes 解题报告(Python)
		[LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ... 
随机推荐
- C语言 字符串指针和字符串数组使用区别
			字符串指针和字符串数组使用区别 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 ... 
- Linux命令行批量删除文件(目录)
			快速-批量删除文件或目录 1-1.快速删除大文件夹(注意目录后的结束符'/')(对于含有海量文件的目录,不能直接rm -rf删除,这样效率很慢:) rsync -a --delete blank/ t ... 
- Linux 中的五种 IO 模型
			Linux 中的五种 IO 模型 在正式开始讲Linux IO模型前,比如:同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案是不同的.所以先限定一 ... 
- 18-Rotate Array-Leetcode
			Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ... 
- 字符scanf 的输入注意
			1.注意scanf 不能有空格,如果有空格会将空格给输入进去 scanf("d "):---有空格 和scanf("d");--没有空格 有很大的区别 
- kubernetes部署haproxy、keepalived为kube-apiserver做集群
			也可以用nginx.keepalived做负载均衡,看大家的需求. # yum -y install haproxy keepalived haproxy的配置文件(三台一样): cat > / ... 
- Ubuntu apt代理apt-cacher-ng配置及使用
			apt-cacher-ng是更强大的apt代理服务器的替代方案,例如squid-deb-proxy.如果您正在运行小型家庭或办公室网络,那就别无所求.它可能缺少一些更高级的功能,但是可以立即进行配置, ... 
- A Child's History of England.30
			CHAPTER 10 ENGLAND UNDER HENRY THE FIRST, CALLED FINE-SCHOLAR Fine-scholar, on hearing of the Red Ki ... 
- HDFS【概述、数据流】
			目录 概述 定义 优缺点 HDFS组成架构 HDFS文件块大小 HDFS数据流 写数据 读数据 网络拓扑-节点距离计算 机架感知(写数据的副本存储节点选择) 概述 定义 HDFS是一个分布式文件管理系 ... 
- 零基础学习java------day10------带包编译,权限修饰符,内部类,调式,junitTest
			0. 带包编译 解决使用notepad++编写的java类中如果有package的解决方案,如下代码 package com._51doit.test; class HelloWorld{ publ ... 
