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实现 '''并两个有序数组 注意题目要 ...
最新文章
- 携号转网:欢迎比阻挠更为有效
- 看看那些来自电路造型挑战赛中的作品
- A Beginner's Guide To btrfs
- 树莓派python实例_使用Python实现树莓派WiFi断线自动重连实例(附代码)
- wxWidgets:wxSocketEvent类用法
- OpenCV imgproc直方图的实例(附完整代码)
- docker下安装Pillow模块
- mysql语法学习(一)__Instances__表
- 禁止访问磁盘的注册表
- 爬虫python编程与cvi编程_编程小白如何写爬虫程序
- 2022年视频号的五大机会,教育商家该如何上车?
- docker安装gamit_科学网—ubuntu下安装gamit 安装 - 贺小星的博文
- CTGU 2021春-MySQL数据库实验2:基本查询3-4关,共7小题全代码+信息表+通关截图!
- 哈夫曼树详解及其应用(哈夫曼编码)
- 苹果4s怎么越狱教程_苹果iOS 11.4-11.4.1越狱发布:附越狱教程
- 迎接“119”消防宣传日|助力企事业单位实现消防安全,前景互联在行动
- 求一个3*3矩阵对角线元素之和
- 图像处理----图像模糊(高斯模糊)
- 第1期-通过携程爬取机票价格
- IOS第二十九天——HTTP下载一个图片