question: https://codility.com/programmers/lessons/4

we need two parts to prove our solution.

on one hand,

there is no false triangular. Given the array has been sorted, if A[i]+A[i+1]>A[i+2], we can prove the existence of the triangle. for array A is sorted , we can easily confirm that A[i+2] + A[i] > A[i+1] and A[i+1]+A[i+2] >A[i]. So we just need to check
this condition.

on the other hand,there is no underreporting triangular. If
the inequality can hold for three out-of-order elements, to say, A[index]+A[index+m] > A[index+n], where n>m>1. because array A is sorted, we can reach that A[index+m-1]>=A[index] and A[index+n]>= A[index + m+1]; after simplification , we infer that A[index+m-1]+A[index+m]
> A[index+m+1].
if we have any inequality holding for out-of-order elements, we MUST have AT
LEAST an inequality holding for three consecutive elements.

some
trap:

  • forget to check A[i] >0;
  • need to judge if A.size() <3; rather than left these to the condition in for loop.   because A.size() return size_t type . if A.size()==1,A.size()-2 may get a very large positive num, than lead to error.

C++
Solution

#include <algorithm>
#include <vector>
#include <map>
int solution(vector<int> &A) {
// write your code in C++11
if (A.size()<3)
return 0;
sort(A.begin(),A.end());
for(int i=0; i< A.size()-2&& i<A.size(); i++){
if(A[i]>0 && A[i]>A[i+2]-A[i+1])
return 1;
}
return 0;
}

Solution to Triangle by Codility的更多相关文章

  1. Solution of NumberOfDiscIntersections by Codility

    question:https://codility.com/programmers/lessons/4 this question is seem like line intersections qu ...

  2. the solution of CountNonDivisible by Codility

    question:https://codility.com/programmers/lessons/9 To solve this question , I get each element's di ...

  3. Leetcode OJ : Triangle 动态规划 python solution

    Total Accepted: 31557 Total Submissions: 116793     Given a triangle, find the minimum path sum from ...

  4. 【LeetCode OJ】Triangle

    Problem Link: http://oj.leetcode.com/problems/triangle/ Let R[][] be a 2D array where R[i][j] (j < ...

  5. 做了codility网站上一题:CountBoundedSlices

    在写上一随笔之前,在Codility网站上还做了一个道题(非Demo题):CountBoundedSlices,得了60分(害臊呀).今天又重新做了一下这个算法,性能提高了不少,但由于此题不是Demo ...

  6. [LeetCode] 120. Triangle _Medium tag: Dynamic Programming

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

  7. [leetcode]Triangle @ Python

    原题地址:https://oj.leetcode.com/problems/triangle/ 题意: Given a triangle, find the minimum path sum from ...

  8. 数字三角形 · Triangle

    从上到下用DP. [抄题]: 给定一个数字三角形,找到从顶部到底部的最小路径和.每一步可以移动到下面一行的相邻数字上. 比如,给出下列数字三角形: [ [2], [3,4], [6,5,7], [4, ...

  9. leetcode 【 Triangle 】python 实现

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

随机推荐

  1. shell编程学习笔记【原创】

    本文为本人学习笔记,如有转载请注明出处,谢谢 一.Bourne Shell 有如下四种变量: 用户自定义变量 位置变量,即命令行参数 预定义变量 环境变量 二.位置变量 $ 与键入的命令行一样,包含脚 ...

  2. Strlcpy和strlcat——一致的、安全的字符串拷贝和串接函数【转】

    转自:http://blog.csdn.net/kailan818/article/details/6731772 英文原文: http://www.gratisoft.us/todd/papers/ ...

  3. sysbench(mysql测试工具 )

    目录 一.基准测试简介 1.什么是基准测试 2.基准测试的作用 3.基准测试的指标 4.基准测试的分类 二.sysbench 1.sysbench简介 2.sysbench安装 3.sysbench语 ...

  4. Xamarin XAML语言教程使用方法设置进度条进度

    Xamarin XAML语言教程使用方法设置进度条进度 在ProgressBar中定义了一个ProgressTo方法,此方法也可以用来对进度条当前的进行进行设置,ProgressTo与Progress ...

  5. 初步接触LVS

    今天整理下思绪,定下要掌握LVS原理和使用方法.于是,看了部分关于LVS的概述和文章. 章博士在2002年写的LVS的几篇文章,在我看来,今天都值得一看.http://www.linuxvirtual ...

  6. 初识交替最小二乘ALS

    ALS是alternating least squares的缩写 , 意为交替最小二乘法:而ALS-WR是alternating-least-squares with weighted-λ -regu ...

  7. RMAN BACKUP

    转自 RMAN BACKUP backup terminology Using the RMAN BACKUP Command to Create Backups Server-Managed Con ...

  8. 常见java异常

    1. java.lang.NullPointerException(空指针异常)  调用了未经初始化的对象或者是不存在的对象 经常出现在创建图片,调用数组这些操作中,比如图片未经初始化,或者图片创建时 ...

  9. ES,ZK,Mysql相关参数优化

    1.ES 内存调优: vi config/jvm.options -Xms16g -Xmx16g 2.Zookeeper参数配置调优 2.1\在conf目录下 vi java.env export J ...

  10. wp8使用mvvm模式简单例子(二)---登陆功能,事件触发

    首先,还是需要一个Model类来为UI层的元素提供数据源 public class LoginModel:DependencyObject { public string Uid { get { re ...