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. python3使用urllib获取set-cookies

    #!/usr/bin/env python # encoding: utf-8 import urllib.request from collections import defaultdict re ...

  2. aiohttp/asyncio测试代理是否可用

    #!/usr/bin/env python # encoding: utf-8 from proxyPool.db import RedisClient import asyncio import a ...

  3. CF985A Chess Placing【思维】

    [链接]:CF985A [题意]:给你n和n/2个数ai,每个ai和奇数.偶数比较距离(注意选了奇数,偶数的距离就不要算了,反之同理),求最小的答案. [代码]: #include <iostr ...

  4. 网络爬虫框架Webmagic

    1 谈谈网络爬虫 1.1 什么是网络爬虫 在大数据时代,信息的采集是一项重要的工作,而互联网中的数据是海量的,如果单纯靠人力进行信息采集,不仅低效繁琐,搜集的成本也会提高.如何自动高效地获取互联网中我 ...

  5. 模拟【P1650】 田忌赛马

    顾z 你没有发现两个字里的blog都不一样嘛 qwq 题目描述--->p1650 田忌赛马 先%dalao sto GMPotlc orz 他教给的我,征求意见后终于来水题解. 分析 我们需要知 ...

  6. IntelliJ IDEA插件-翻译插件

    说明:这个翻译插件应该是最好的了. 官网:https://github.com/YiiGuxing/TranslationPlugin 但是这个有个缺点就是使用收费的API,基于有道,截止今天使用的是 ...

  7. USING CHARLES FROM AN IPHONE

    USING CHARLES FROM AN IPHONE 从系统偏好->高级来查看ip地址即可 To use Charles as your HTTP proxy on your iPhone ...

  8. easyui numberbox precision属性

    //设置easyui numbox 最小值为0,保留2为小数 <input id="payPrice" type="text" name="pa ...

  9. 如何让mysql的自动递增的字段重新从1开始呢?(

    数据库表自动递增字段在用过一段时间后清空,还是继续从清空后的自动编号开始.如何才能让这个字段自动从1开始自动递增呢? 下面两个方法偶都试过,很好用: 1 清空所有数据,将自增去掉,存盘,在加上自增,存 ...

  10. 转: java语法与ide级入门介绍 from: IBM dev

    点评: 讲的比较初级,但是有教你使用ide (Eclipse) frrom:http://www.ibm.com/developerworks/java/tutorials/j-introtojava ...