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. dpkg: error processing package bluez (--configure) 解决方法【转】

    转自:http://blog.csdn.net/heray1990/article/details/47803541 在 Ubuntu 执行 sudo apt-get upgrade 时,出现了如下的 ...

  2. python 删除字符串中的连续空格只保留一个

    目标是要去掉多余的空格字符,在相邻字符串中,只保留一个空格 紫梧桐 - 蛋壳公寓朝阳门店                                                 郑田力 可以利 ...

  3. PHP将两个二维数组合并为一个二维数组的方法

    $a = array(0 => Array(id => 66,class_name => math),1 => Array(id => 67,class_name =&g ...

  4. jquery导出Excel表格

    1.引用js插件 <script src="tableExport.js"></script> <script src="jquery.ba ...

  5. 程序员面试京东前端,现场JavaScript代码写出魔方特效

    程序员面试京东前端,现场JS代码写出魔方特效,成功搞定20K月薪 今天小编我逛论坛,看到了一位程序员小伙子,因为是有了两年工作经验,然后去京东面试前端岗,一面二面轻松就过了,到了技术面这一块,小伙干脆 ...

  6. schema get_ddl

    select dbms_metadata.get_ddl('INDEX','INDEX_CC_TAXID','CACS9DBSIT1') from dual; select dbms_metadata ...

  7. Java-静态代码块,构造代码块,构造函数

    静态代码块:用staitc声明,jvm加载类时执行,仅执行一次 构造代码块:类中直接用{}定义,每一次创建对象时执行. 执行顺序优先级:静态块, main(),函数,构造块,构造方法. 构造函数 pu ...

  8. 【分享】· 图床&在线分享演示文稿

    关于图床 什么是图床? 这并不是一个多么高大上的名词概念!用比较通俗的话来说,当你在撰写新文章时,你需要去插入图片以使得你的文章内容更加直观.易懂,这个时候有以下几种办法: 在博客根目录的 sourc ...

  9. Systemd入门教程:实战篇(转)

    作者: 阮一峰 日期: 2016年3月 8日 上一篇文章,我介绍了 Systemd 的主要命令,今天介绍如何使用它完成一些基本的任务. 一.开机启动 对于那些支持 Systemd 的软件,安装的时候, ...

  10. Linux守护进程的启动方法(转)

    作者: 阮一峰 日期: 2016年2月28日 "守护进程"(daemon)就是一直在后台运行的进程(daemon). 本文介绍如何将一个 Web 应用,启动为守护进程. 一.问题的 ...