【LeetCode】611. Valid Triangle Number 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/valid-triangle-number/description/
题目描述:
Given an array consists of non-negative integers, your task is to count the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle.
Example 1:
Input: [2,2,3,4]
Output: 3
Explanation:
Valid combinations are:
2,3,4 (using the first 2)
2,3,4 (using the second 2)
2,2,3
Note:
- The length of the given array won’t exceed 1000.
- The integers in the given array are in the range of [0, 1000].
题目大意
给出了一个数组,求在这个数组中能组成多少个三角形。
解题方法
看了下,这个规模是 1000,那么用 O(n^3) 的暴力解法肯定就TLE了。那么只能用更高效的算法。
这个方法是这样的,我们先对这个数组进行排序,排序之后我们来指出这个三角形中最长的边,那么剩下两条边的要求是:两边之和大于最长边。
所以,我们只需要对最长边进行变遍历,对两个短边再遍历即可。短边的遍历方法是:一个从 0 开始的短边 l ,一个从比最长边的短的一个边 r 开始向中间遍历。如果能够成三角形,那么说明比l长的短边和r结合也都能组成三角形。如果不能组成三角形,那么说明l太小了,需要向中间移动。
排序算法时间复杂度是 O(nlogn),for循环时间复杂度为O(n),while循环虽然有两个变量,但是这两个变量是同时向中间移动的关系,所以时间复杂度不会超过O(n)。总体的时间复杂度是O(n^2)。空间复杂度是O(1)。
注意,sorted() 函数不是原地排序,如果原地排序需要使用 nums.sort()。这就是我第一次提交失败的地方。
代码如下:
class Solution:
def triangleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
nums.sort()
count = 0
for i in range(len(nums) - 1, 1, -1):
l, r = 0, i - 1
while l < r:
if nums[l] + nums[r] > nums[i]:
count += r - l
r -= 1
else:
l += 1
return count
参考资料:
https://leetcode.com/problems/valid-triangle-number/discuss/104174/Java-O(n2)-Time-O(1)-Space
日期
2018 年 9 月 11 日 ———— 天好阴啊
【LeetCode】611. Valid Triangle Number 解题报告(Python)的更多相关文章
- leetcode 611. Valid Triangle Number 、259. 3Sum Smaller(lintcode 918. 3Sum Smaller)
这两个题几乎一样,只是说611. Valid Triangle Number满足大于条件,259. 3Sum Smaller满足小于条件,两者都是先排序,然后用双指针的方式. 611. Valid T ...
- LeetCode 611. Valid Triangle Number有效三角形的个数 (C++)
题目: Given an array consists of non-negative integers, your task is to count the number of triplets c ...
- 【LeetCode】306. Additive Number 解题报告(Python)
[LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- Leetcode 之 Valid Triangle Number
611. Valid Triangle Number 1.Problem Given an array consists of non-negative integers, your task is ...
- 【LeetCode】263. Ugly Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 除去2,3,5因子 日期 [LeetCode] 题目 ...
- 【LeetCode】136. Single Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 异或 字典 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】507. Perfect Number 解题报告(Python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】 202. Happy Number 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 [LeetCode] 题目地址:h ...
- 【LeetCode】268. Missing Number 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 求和 异或 日期 题目地址:https://leet ...
随机推荐
- jQuery添加html绑定事件
jQuery添加html绑定事件 $("#xxx").on("click",".dev",function(){ });
- GraphScope 集群部署
GraphScope 集群部署 1 k8s集群搭建 大致步骤如下: 安装docker.在ubuntu上,可以简单的通过命令sudo apt install docker.io来安装. 安装kubele ...
- 巩固javaweb的第二十二天
巩固内容: 使用表单数据 : 要对用户输入的信息进行验证,需要先获取输入信息.每个表单元素都属于一个 form 表单,要获取信息,需要先获取 form,然后访问表单元素的值. 有两种方式可以获取 fo ...
- Windows端口被占用解决方法
Error 场景 启动 Java 项目失败,控制台显示 Error starting ApplicationContext. To display the conditions report`re-r ...
- tomcat结合nginx
相信很多人都听过nginx,这个小巧的东西慢慢地在吞食apache和IIS的份额.那究竟它有什么作用呢?可能很多人未必了解. 说到反向代理,可能很多人都听说,但具体什么是反向代理,很多人估计就不清楚了 ...
- Swift3.0 延时执行
//延时1s执行 DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(1*NSEC_PER_SEC))/ ...
- 转Android Canvas和Paint基本使用
Android Canvas和Paint基本使用 这篇文章主要介绍下画笔Paint和画布Canvas的基本使用 1.Paint 创建对象Paint mPaint = new Paint(); 常 ...
- Linux基础命令---mput上传ftp文件
mput 使用lftp登录ftp服务器之后,可以使用put指令将文件上传到服务器.mput指令可以使用通配符,而put指令则不可以. 1.语法 mput [-c] [-d] [-a] ...
- 【Linux】【Commands】文本查看类
分屏查看命令:more和less more命令: more FILE 特点:翻屏至文件尾部后自动退出: less命令: less FILE head命令: 查看文件的前n行: head [option ...
- 什么是javaScript闭包
闭包是与函数有着紧密的关系,它是函数的代码在运行过程中的一个动态环境,是一个运行期的概念. 所谓闭包,是指词法表示包括不必计算的变量的函数.也就是说,该函数能够使用函数外定义的变量. 在程序语言中,所 ...