Triangle
2015-11-26 20:45
295 查看
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.
解答。
因为有 O(N*log(N))的要求,一般就要先sort一下。
sort 之后是从小到大的,除非reverse = True
然后我们就要到sorted array里面找到all possible i
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.
解答。
因为有 O(N*log(N))的要求,一般就要先sort一下。
sort 之后是从小到大的,除非reverse = True
然后我们就要到sorted array里面找到all possible i
import java.util.*; class Solution { public int solution(int[] A) { Arrays.sort(A); for(int i = 0; i< A.length-2; i++){ if(A[i] > A[i+2] - A[i+1]){//use subtraction to avoid overflow return 1; } } return 0; } }
相关文章推荐
- HD_2037今年暑假不AC
- Delphi利用Webbrowser登陆QQ群文档
- C语言基础第三篇--控制语句
- 错误:Unsupported major.minor version 51.0(jdk版本错误)
- 二叉树层序遍历
- AdminCMD
- 程序员都会的 35 个 jQuery 小技巧
- Javascript设计模式之观察者模式
- vim recording
- syslinux
- Delphi在Webbrowser上绘制图像
- leetcode_Peeking Iterator
- jquery+递归自动生成无限级菜单
- [leetcode] 233. Number of Digit One
- hive与hbase关联表的创建,外表方式
- PagerSlidingTabStrip介绍及使用,让ViewPager更绚丽
- POJ.2420.A Star not a Tree.模拟退火.0ms
- 安装完成ubuntu系统后的基本的相关配置---Linux总结笔记
- HDU 1062:Text Reverse【水】
- HDU 4562 守护雅典娜(动态规划)