Leetcode 1395. Count Number of Teams [Python]
按照提示写了暴力,过不了啊。。怎么看都是n ^3的时间复杂度,n=1000,照道理说time complexity = o(1000000000)也是过不了的。尝试这样理解吧,选定中间的数字,看左侧有多少小于(或大于)中间数字的元素(记为increas_left, decreas_left),同理看右侧有多少大于(或小于)中间数字的元素(记为increas_right, decreas_right)。则以此数为中间数的满足条件的combination的数量就是increas_left *increas_righ + decreas_left *decreas_right。这样可以降低复杂度到n^2.
class Solution:def numTeams(self, rating: List[int]) -> int:res = 0for i in range(1, len(rating)-1):increase_left = 0increase_right = 0decrease_left = 0decrease_right = 0for j in range(0, i):if rating[j] < rating[i]:increase_left += 1elif rating[j] > rating[i]:decrease_left += 1for k in range(i+1, len(rating)):if rating[i] < rating[k]:increase_right += 1elif rating[i] > rating[k]:decrease_right += 1res += increase_left*increase_right#increase triple res += decrease_left*decrease_right#decrease triplereturn res
Leetcode 1395. Count Number of Teams [Python]相关推荐
- leetcode 1534. Count Good Triplets(python)
描述 Given an array of integers arr, and three integers a, b and c. You need to find the number of goo ...
- Leetcode 1925. Count Square Sum Triples [Python]
基本思路是暴力破解.但是注意sqrt给出的是float,转成int后检查下是否还是原来的数值,随后检查给出的sqrt是不是小于等于n的. class Solution:def countTriples ...
- LeetCode:Largest Number - 求整型数组中各元素可拼合成的最大数字
2019独角兽企业重金招聘Python工程师标准>>> 1.题目名称 Largest Number(求整型数组中各元素可拼合成的最大数字) 2.题目地址 https://leetco ...
- leetcode 《简单》 数学部分 Python实现
leetcode <简单> 数学部分 Python实现 ''' Fizz Buzz 写一个程序,输出从 1 到 n 数字的字符串表示.1. 如果 n 是3的倍数,输出"Fizz& ...
- [勇者闯LeetCode] 136. Single Number
[勇者闯LeetCode] 136. Single Number Description Given an array of integers, every element appears twice ...
- pandas使用nunique函数计算dataframe每个数据列的独特值的个数(count number of unique values in each column of dataframe)
pandas使用nunique函数计算dataframe每个数据列的独特值的个数(count number of unique values in each column of dataframe) ...
- LeetCode 38. Count and Say
问题链接 LeetCode 38. Count and Say 题目解析 找规律,每一个数字串是上一个数字串的"读法".比如:n=1时为"1",读作" ...
- leetcode 《简单》 设计问题 Python实现
leetcode <简单> 设计问题 Python实现''' Shuffle an Array 打乱一个没有重复元素的数组.示例: // 以数字集合 1, 2 和 3 初始化数组. int ...
- LeetCode 排序和搜索简单部分 Python实现
#2018-06-07 June Thursday the 23 week, the 158 day SZ #LeetCode 排序和搜索 简单部分 Python实现 '''并两个有序数组 注意题目要 ...
最新文章
- 【linux练习】基础作业一
- Verilog设计分频器(面试必看)
- SpringCloud学习2-Springboot监控模块(actuator)
- 如何重启_消费市场按下重启键,企业该如何提前布局
- 查看Oracle实例的EM端口
- (字符串)字符串中首先出现两次的字符
- 下一次装mysql 试一下这个方法
- 解决UE4 Launcher启动速度极慢的方法
- 计算机网络——自顶向下
- network secruity studay day2
- arduino uno r3单片机封装图_第十二节 电路识图 从Arduino开始讲起
- 计算机音乐我的天空,南征北战NZBZ《我的天空》[FLAC/MP3-320k]
- 2022护眼产品展,视力康复展,山东国际眼健康展会,护眼仪展
- CentOS下连VisualSVN服务器时报SSL handshake failed: SSL error: Key usage violation in certificate has been d
- 软件开发基于JavaScript实现快速转换文本语言
- redisson-spring-boot-starter
- 关于thinkpad t400 安装Ubuntu 12.04 后的几个问题
- git tag 操作
- Intel software developer manual
- 牛客挑战赛68-A芥川龙之介的河童