【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 ...
随机推荐
- 商业创新不能等?用友低代码开发平台YonBuilder为您加速!
随着云计算.人工智能.物联网.大数据.5G等新一代技术的快速发展,越来越多的企业希望借助技术的力量加速数智化转型,期许通过更加敏捷和强大的应用系统推动企业的商业创新速度.但传统软件开发周期长.开发成本 ...
- 巩固java第五天
巩固内容: HTML 实例解析 <p> 元素: <p>这是第一个段落.</p> 这个 <p> 元素定义了 HTML 文档中的一个段落. 这个元素拥有一个 ...
- hadoop-uber作业模式
如果作业很小,就选择和自己在同一个JVM上运行任务,与在一个节点上顺序运行这些任务相比,当application master 判断在新的容器中的分配和运行任务的开销大于并行运行它们的开销时,就会发生 ...
- 【leetcode】986. Interval List Intersections (双指针)
You are given two lists of closed intervals, firstList and secondList, where firstList[i] = [starti, ...
- 【leetcode】917. Reverse Only Letters(双指针)
Given a string s, reverse the string according to the following rules: All the characters that are n ...
- mysql外键策略
1.外键 建表时添加外键:constraint 外键名 foreign key 从表字段 references 主表字段 级联操作 create table dage( create table xi ...
- Dockers启动Kafka
首先安装 Confluent Platform Quick Start for Confluent Platform (Local install) Use this quick start to g ...
- 【Linux】【Shell】【text】sed
sed [OPTION]... 'script' [input-file] ... script: 地址定界编辑命令 ...
- JS - 获取当前的时间,并且转换成年 - 月 - 日格式!
先获取当前时间,并转换成年月日格式! function getNowFormatDate() { var date = new Date(); var seperator1 = "-&quo ...
- js实现点击不同按钮切换内容
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...