Task description

A zero-indexed array A consisting of N integers is given. A 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] = 20

Triplet (0, 2, 4) is triangular.

Write a function:

class Solution { public int solution(int[] A); }

that, given a zero-indexed array A consisting of N integers, returns 1 if there exists a triangular triplet for this array and returns 0 otherwise.

For example, given array A such that:

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

the function should return 1, as explained above. Given array A such that:

A[0] = 10 A[1] = 50 A[2] = 5 A[3] = 1

the function should return 0.

Assume that:

  • N is an integer within the range [0..100,000];
  • each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].

Complexity:

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

Elements of input arrays can be modified.

Solution

 
Programming language used: Java
Total time used: 4 minutes
Code: 23:47:57 UTC, java, final, score:  100
// you can also use imports, for example:
// import java.util.*; // you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.Arrays;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
Arrays.sort(A);
for(int i=0; i<A.length-2; i++) {
if(A[i] > 0 && A[i] > A[i+2] - A[i+1])
return 1;
}
return 0;
}
}
https://codility.com/demo/results/trainingKHBQYZ-75T/
 

Codility--- Triangle的更多相关文章

  1. Solution to Triangle by Codility

    question: https://codility.com/programmers/lessons/4 we need two parts to prove our solution. on one ...

  2. [LeetCode] Triangle 三角形

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  3. [LeetCode] Pascal's Triangle II 杨辉三角之二

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  4. [LeetCode] Pascal's Triangle 杨辉三角

    Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...

  5. Codility NumberSolitaire Solution

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

  6. codility flags solution

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

  7. 【leetcode】Pascal's Triangle II

    题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...

  8. 【leetcode】Pascal's Triangle

    题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...

  9. POJ 1163 The Triangle(简单动态规划)

    http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissi ...

  10. Triangle - Delaunay Triangulator

    Triangle - Delaunay Triangulator  eryar@163.com Abstract. Triangle is a 2D quality mesh generator an ...

随机推荐

  1. hdu3698 Let the light guide us dp+线段树优化

    http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java ...

  2. [Android]使用化名(alias)功能防止相同资源的重复

    在为一个应用匹配不同资源文件的时候,有时可能需要在不同适配类型的资源路径下使用相同的资源文件,这时使用alias方法可以防止相同资源文件的重复,提高效率.以下摘自Android开发文档http://d ...

  3. 关于MOVE 和 CopyMemory 的用法区别

    最近做了一个数据采集服务器, 根据程序的框架,使用了大量的指针结构体(内存块) 操作. 例子: PArrayByte = ^TArrayByte;  TArrayByte = packed recor ...

  4. Vim 写 iOS App

    Vim 写 iOS App 我们都知道 Vim 和 Emacs 都是文本编辑器中的上古神器,你也许用 ctags,cscopes 配合 Vim 完成过大型 C 或者 C++ 的开发,你也许配合过其他插 ...

  5. android游戏开发系列(2)——背景音乐播放技术

    背景音乐通常播放时间较长,且文件体积也相对较大.这类资源如果放在内存中,一方面给硬件资源本身就很紧缺的手机造成了负担,另一方面通常也没有这方面的需求,放在内存中,在调用时播放速度较快,而长时音乐文件通 ...

  6. Android于popWindow写弹出菜单

    1.什么是popWindow? popWindow这是对话的方式!文字解说android的方式来使用对话框,这就是所谓的popWindow. 2.popWindow特征 Android的对话框有两种: ...

  7. 在codefirst迁移的时候 为新增的属性添加一个默认值

    在模型中新增一个属性之后 通过add-migration ConsulationMsg_add_IsDel命令之后 会生成一个 日期+ConsulationMsg_add_IsDel的类文件 打开类文 ...

  8. python3实现域名查询和whois查询

    关键字:python3 域名查询 域名查询接口 whois查询原文:http://www.cnblogs.com/txw1958/archive/2012/08/31/python3-domain-w ...

  9. python中的验证码识别库PyTesser

    PyTesser PyTesser is an Optical Character Recognition module for Python. It takes as input an image ...

  10. HDU1728 从迷宫中逃脱 【方向BFS】

    从迷宫中逃脱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Sub ...