A zero-indexed array A consisting of N integers is given. A triplet (P, Q, R) is triangular if it is possible to build a triangle with sides of lengths A[P], A[Q] and A[R]. In other words, triplet (P, Q, R) is triangular if 0 ≤ P < Q < R < N and:

  • A[P] + A[Q] > A[R],
  • A[Q] + A[R] > A[P],
  • A[R] + A[P] > A[Q].

For example, consider array A such that:

  A[0] = 10    A[1] = 2    A[2] = 5
A[3] = 1 A[4] = 8 A[5] = 12

There are four triangular triplets that can be constructed from elements of this array, namely (0, 2, 4), (0, 2, 5), (0, 4, 5), and (2, 4, 5).

Write a function:

int solution(vector<int> &A);

that, given a zero-indexed array A consisting of N integers, returns the number of triangular triplets in this array.

For example, given array A such that:

  A[0] = 10    A[1] = 2    A[2] = 5
A[3] = 1 A[4] = 8 A[5] = 12

the function should return 4, as explained above.

Assume that:

  • N is an integer within the range [0..1,000];
  • each element of array A is an integer within the range [1..1,000,000,000].

Complexity:

  • expected worst-case time complexity is O(N2);
  • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).

给定正整数数组A,长度为N,下标从0开始,求(P,Q,R),满足0<=P<Q<R<N 并且 A[P] + A[Q] > A[R], A[Q] + A[R] > A[P], A[P] + A[R] > A[Q]的三元组个数。

数据范围 N [0..1000], 数组元素[1..10^9]。

要求复杂度 时间O(N ^ 2) ,空间 O(1)。

分析: 显然我们不能枚举……我们可以把数组排序 O(NlogN),甚至O(N^2)的排序都可以。然后还是枚举,只不过枚举两条较小的边A[x] , A[y], 然后我们考虑最大边A[z],设想假设我们固定x, 当y变大时A[x] + A[y]也变大,我们需要A[x] + A[y] > A[z], y变大之前的那些z值现在依然也满足条件,所以我们只要接着上次满足条件的最大的z,继续循环就可以了。所以对于同一个x来说,y和z的变化都是O(N)的。总复杂度O(N^2)。

 // you can use includes, for example:
#include <algorithm> // you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl; int solution(vector<int> &A) {
// write your code in C++11
sort(A.begin(), A.end());
int a, b, c;
int res = ;
for (a = ; a < (int)A.size() - ; ++a) {
c = a + ;
for (b = a + ; b < (int)A.size() - ; ++b) {
for (c = max(c, b + ); c < A.size() && A[a] + A[b] > A[c]; ++c);
res += c - b - ;
}
}
return res;
}

[Codility] CountTriangles的更多相关文章

  1. Codility NumberSolitaire Solution

    1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...

  2. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  3. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  4. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  5. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  6. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...

  7. *[codility]MaxDoubleSliceSum

    https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...

  8. *[codility]Fish

    https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> us ...

  9. *[codility]CartesianSequence

    https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有 ...

随机推荐

  1. 通过WebRTC实现实时视频通信(一)

    通过WebRTC实现实时视频通信(一) 通过WebRTC实现实时视频通信(二) 通过WebRTC实现实时视频通信(三) WebRTC,名称源自网页实时通信(Web Real-Time Communic ...

  2. VS2010已停止工作,framework 4.0安装失败

    好久没在台式机上写程序了,今天打开一个解决方案,报错: 从来没有遇到这种情况,重新关闭解决方案,在菜单栏中打开解决方案说没有framework 4.0,其实我安装的是4.5 ,也有4.0,于是全部卸载 ...

  3. Windows 之 手机访问 PC 端本地部署的站点

    测试网页在手机上的显示工具我们可以使用谷歌内核的浏览器,打开开发者工具(F12),在device那里选择设备,然后刷新来查看网页在手机上的显示效果. 但毕竟是模拟的,如果想要在真机上调试该怎么办呢. ...

  4. base64 图片编码之再优化

    首先进入网站: http://b64.io/ 最多可减少图片体积容量近70%,建议不要优化base 64 图片格式为gif , 已实测如果用gif的话会增加容量.

  5. 自我分析-Spring IOC

    Spring IOC容器实现原理大致是容器(Map)+反射(Java反射和cglib).Spring提供丰富的ApplicationContext.以FileSystemXmlApplicationC ...

  6. Fiddler Session标志

    Each Session object in Fiddler contains a collection of string flags, in the Session.oFlags[] collec ...

  7. Spring中依赖注入的四种方式

    在Spring容器中为一个bean配置依赖注入有三种方式: · 使用属性的setter方法注入  这是最常用的方式: · 使用构造器注入: · 使用Filed注入(用于注解方式). 使用属性的sett ...

  8. c#实现统计代码运行时间

    方法一: //实例化一个计时器 Stopwatch watch = new Stopwatch(); //開始计时 watch.Start(); //此处为要计算的执行代码 for (int i = ...

  9. 【Docker】安装并测试安装成功

    1.环境描述 Centos 7 2.安装步骤 通过命令yum install docker安装 等待下载安装-,出现下图,按y继续 继续等待-出现下图按y继续 再继续等待- 知道出现上图表示安装完毕 ...

  10. 转:Windows消息机制要点

    Windows消息机制要点 1. 窗口过程     每个窗口会有一个称为窗口过程的回调函数(WndProc),它带有四个参数,分别为:窗口句柄(Window Handle),消息ID(Message ...