LintCode Triangle Count

原题链接在这里:http://www.lintcode.com/en/problem/triangle-count/#

题目:

Given an array of integers, how many three numbers can be found in the array, so that we can build an triangle whose three edges length is the three numbers that we find?

Example

Given array S = [3,4,6,7], return 3. They are:

[3,4,6]
[3,6,7]
[4,6,7]

Given array S = [4,4,4,4], return 4. They are:

[4(1),4(2),4(3)]
[4(1),4(2),4(4)]
[4(1),4(3),4(4)]
[4(2),4(3),4(4)]

题解:

3Sum Smaller类似.

也是Two Pointers, 构成三角要求三条边成a + b > c. 设S[i] 为c, 前面的数中选出S[l]为a, S[r]为b.

若是S[l] + S[r] > c则符合要求,还会有会有r-l种组合若继续向右移动l. 所以res += r-l. r左移一位.

若是S[l] + S[r] <= c, 则向右移动l.

Time Complexity: O(n^2). sort 用时O(nlogn), 对于每一个c, Two Points用时O(n). n = S.length.

Space: O(1).

AC Java:

 1 public class Solution {
 2     public int triangleCount(int S[]) {
 3         if(S == null || S.length < 3){
 4             return 0;
 5         }
 6
 7         int res = 0;
 8         Arrays.sort(S);
 9         for(int i = 2; i<S.length; i++){
10             int l = 0;
11             int r = i-1;
12             while(l<r){
13                 if(S[l] + S[r] > S[i]){
14                     res += r-l;
15                     r--;
16                 }else{
17                     l++;
18                 }
19             }
20         }
21         return res;
22     }
23 }
时间: 2024-07-28 18:03:10

LintCode Triangle Count的相关文章

【Lintcode】382.Triangle Count

题目: Given an array of integers, how many three numbers can be found in the array, so that we can build an triangle whose three edges length is the three numbers that we find? Example Given array S = [3,4,6,7], return 3. They are: [3,4,6] [3,6,7] [4,6

Triangle Count

Given an array of integers, how many three numbers can be found in the array, so that we can build an triangle whose three edges length is the three numbers that we find? Given array S = [3,4,6,7], return 3. They are: [3,4,6] [3,6,7] [4,6,7] Given ar

【lintcode】Count of Smaller Number before itself

http://www.lintcode.com/en/problem/count-of-smaller-number-before-itself/ 这道题目是动态添加线段树的元素,然后再查询. 数据集和描述不相符,坑 class Solution { public: /** * @param A: An integer array * @return: Count the number of element before this element 'ai' is * smaller than i

刷题--双指针(2)

Two Sum类 首先是基本的Two Sum题解 用hashmap 时间复杂度O(n),空间复杂度O(n),每一次首先找hashmap中有没有target - nums[i], 如果没有将nums[i]入map 用双指针法,时间复杂度O(n + nlogn), 空间复杂度O(1) 首先要对数组进行排序,如果要求的是返回两个数的索引,那么就不能用这个方法 例 lintcode  56. Two Sum https://www.lintcode.com/problem/two-sum/descrip

九章算法 基础算法 强化算法 系统设计 大数据 安卓 leetcode 高清视频

leetcode 直播视频讲座录像 九章算法视频录像,PPT 算法班,算法强化班,Java入门与基础算法班,big data项目实战班,Andriod项目实战班 九章算法下载 九章算法面试 九章算法leetcode 九章算法答案 九章算法mitbbs 九章算法班 九章算法ppt 九章算法录像 九章算法培训 九章算法微博 leetcode 视频 九章算法偷录 算法培训 算法班课程大纲: 1 从strStr谈面试技巧与Coding Style(免费试听) 2 二分搜索与旋转排序数组 Binary S

微软Hololens学院教程-Hologram 230-空间映射(Spatial mapping )

空间映射地图是将真实环境的环境信息扫描到设备中,使得全息对象可以识别真实场景环境,从而达到可以将虚拟对象与真实世界相结合的效果.这节教程主要学习内容如下: 使用Hololens扫描空间环境并将空间数据导入到开发计算机中. 学习利用shader给空间网格赋予材质以便其更容易被发现. 使用网格处理方法将网格变成简单的平面. 对全息对象可以放置的位置进行放置提醒,使得用户更容易的放置. 开发遮挡效果,即当全息对象被真实场景中的物体遮挡时,你仍然可以看见它,只不过它是线框模式的. 项目文件: Downl

明风:分布式图计算的平台Spark GraphX 在淘宝的实践

快刀初试:Spark GraphX在淘宝的实践 作者:明风 (本文由团队中梧苇和我一起撰写,并由团队中的林岳,岩岫,世仪等多人Review,发表于程序员的8月刊,由于篇幅原因,略作删减,本文为完整版) 对于网络科学而言,世间万物都可以抽象成点,而事物之间的关系都可以抽象成边,并根据不同的应用场景,生成不同的网络,因此整个世界都可以用一个巨大的复杂网络来代表.有关复杂网络和图算法的研究,在最近的十几年取得了巨大的进展,并在多个领域有重要的应用. 作为最大的电商平台,淘宝上数亿买家和卖家,每天产生数

Spark Graphx编程指南

问题导读 1.GraphX提供了几种方式从RDD或者磁盘上的顶点和边集合构造图?2.PageRank算法在图中发挥什么作用?3.三角形计数算法的作用是什么? Spark中文手册-编程指南Spark之一个快速的例子Spark之基本概念Spark之基本概念Spark之基本概念(2)Spark之基本概念(3)Spark-sql由入门到精通Spark-sql由入门到精通续spark GraphX编程指南(1) Pregel API 图本身是递归数据结构,顶点的属性依赖于它们邻居的属性,这些邻居的属性又依

Project Euler:Problem 61 Cyclical figurate numbers

Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygonal) numbers and are generated by the following formulae: Triangle   P3,n=n(n+1)/2   1, 3, 6, 10, 15, ... Square   P4,n=n2   1, 4, 9, 16, 25, ... Penta