Codility--- Triangle
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.
// 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的更多相关文章
- Solution to Triangle by Codility
question: https://codility.com/programmers/lessons/4 we need two parts to prove our solution. on one ...
- [LeetCode] Triangle 三角形
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [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, ...
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- 【leetcode】Pascal's Triangle II
题目简述: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Retur ...
- 【leetcode】Pascal's Triangle
题目简述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...
- POJ 1163 The Triangle(简单动态规划)
http://poj.org/problem?id=1163 The Triangle Time Limit: 1000MS Memory Limit: 10000K Total Submissi ...
- Triangle - Delaunay Triangulator
Triangle - Delaunay Triangulator eryar@163.com Abstract. Triangle is a 2D quality mesh generator an ...
随机推荐
- git 修改仓库地址
公司服务器地址换了 , 原来的git代码地址也跟着需要变 , git remote rm origin 执行该操作 , 删除原来的git地址 git remote -v 查看一下有没有删除成功 , ( ...
- vue项目 下载表格 java后台返回的是信息流表格如何下载解决乱码
主要是在请求参数后面加上{responseType: 'blob'}或者arrayBuffer this.$http.get(this.api.export, { params: this.info, ...
- 最近更新电脑管家开机速度约慢很多木有?$计算机管理-废话$.qmgc
最近更新电脑管家开机速度约慢很多木有? 一旦开机速度是几十秒,由于前几天更新电脑管家,大约几十秒钟成为一个点开机时间! 同样在一个多垃圾清理桌面图标! 右键菜单也看不到属性 须要到下面位置查看: wa ...
- 将枚举转成SelectListItem
代码如下: /// <summary> /// 将一个枚举转化成一个List<SelectListItem> /// </summary> /// <type ...
- 使用MVVM DataTemplate在WPF XAML视图之间切换
原文 使用MVVM DataTemplate在WPF XAML视图之间切换 更新:这个技术的改进版本,一个不创建视图,可以在以下链接找到: http://www.technical-recipes.c ...
- 3DTools TrackballDecorator实现3D漫游
原文:3DTools TrackballDecorator实现3D漫游 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/m0_37591671/art ...
- Android 百度定位SDK
原文:Android 百度定位SDK 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/shui1025701856/article/details/7 ...
- c# 守护进程,WPF程序自守护
原文:c# 守护进程,WPF程序自守护 版权声明:本文为博主原创文章,转载请注明出处. https://blog.csdn.net/lwwl12/article/details/79035246 如何 ...
- Gtk+/Gtkmm介绍与安装(有继承关系图)
GTK+是一套跨平台的,以C语言编写的GUI类库,它起源于GNU的图像处理程序"GIMP",因而被起名为"The GIMP Toolkit",从一开始它就被设计 ...
- 用 cmake 构建Qt工程(对比qmake进行学习)
cmake vs qmake qmake 是为 Qt 量身打造的,使用起来非常方便 cmake 使用上不如qmake简单直接,但复杂换来的是强大的功能 内置的 out-of source 构建.(目前 ...