按照提示写了暴力,过不了啊。。怎么看都是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]相关推荐

  1. 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 ...

  2. Leetcode 1925. Count Square Sum Triples [Python]

    基本思路是暴力破解.但是注意sqrt给出的是float,转成int后检查下是否还是原来的数值,随后检查给出的sqrt是不是小于等于n的. class Solution:def countTriples ...

  3. LeetCode:Largest Number - 求整型数组中各元素可拼合成的最大数字

    2019独角兽企业重金招聘Python工程师标准>>> 1.题目名称 Largest Number(求整型数组中各元素可拼合成的最大数字) 2.题目地址 https://leetco ...

  4. leetcode 《简单》 数学部分 Python实现

    leetcode <简单> 数学部分 Python实现 ''' Fizz Buzz 写一个程序,输出从 1 到 n 数字的字符串表示.1. 如果 n 是3的倍数,输出"Fizz& ...

  5. [勇者闯LeetCode] 136. Single Number

    [勇者闯LeetCode] 136. Single Number Description Given an array of integers, every element appears twice ...

  6. 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) ...

  7. LeetCode 38. Count and Say

    问题链接 LeetCode 38. Count and Say 题目解析 找规律,每一个数字串是上一个数字串的"读法".比如:n=1时为"1",读作" ...

  8. leetcode 《简单》 设计问题 Python实现

    leetcode <简单> 设计问题 Python实现''' Shuffle an Array 打乱一个没有重复元素的数组.示例: // 以数字集合 1, 2 和 3 初始化数组. int ...

  9. LeetCode 排序和搜索简单部分 Python实现

    #2018-06-07 June Thursday the 23 week, the 158 day SZ #LeetCode 排序和搜索 简单部分 Python实现 '''并两个有序数组 注意题目要 ...

最新文章

  1. 【linux练习】基础作业一
  2. Verilog设计分频器(面试必看)
  3. SpringCloud学习2-Springboot监控模块(actuator)
  4. 如何重启_消费市场按下重启键,企业该如何提前布局
  5. 查看Oracle实例的EM端口
  6. (字符串)字符串中首先出现两次的字符
  7. 下一次装mysql 试一下这个方法
  8. 解决UE4 Launcher启动速度极慢的方法
  9. 计算机网络——自顶向下
  10. network secruity studay day2
  11. arduino uno r3单片机封装图_第十二节 电路识图 从Arduino开始讲起
  12. 计算机音乐我的天空,南征北战NZBZ《我的天空》[FLAC/MP3-320k]
  13. 2022护眼产品展,视力康复展,山东国际眼健康展会,护眼仪展
  14. CentOS下连VisualSVN服务器时报SSL handshake failed: SSL error: Key usage violation in certificate has been d
  15. 软件开发基于JavaScript实现快速转换文本语言
  16. redisson-spring-boot-starter
  17. 关于thinkpad t400 安装Ubuntu 12.04 后的几个问题
  18. git tag 操作
  19. Intel software developer manual
  20. 牛客挑战赛68-A芥川龙之介的河童

热门文章

  1. 还有两天 Symbian Belle 就要和我们见面了!
  2. 《纯干货-6》Stanford University 2017年最新《Tensorflow与深度学习实战》视频课程分享
  3. 基于Android的五子棋游戏APP设计
  4. PMP项目管理-[第十章]沟通管理
  5. CookieJar下载并使用cookies
  6. 「GoTeam 招聘时间」长亭科技 Golang 后端开发实习生工程师(杭州)
  7. Windows句柄剖析
  8. 为何面试时都会问你的职业规划呢?该如何回答呢?
  9. 【APIO2016】划艇
  10. Latex自定义序号