Solution to Triangle by Codility
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的更多相关文章
- Solution of NumberOfDiscIntersections by Codility
question:https://codility.com/programmers/lessons/4 this question is seem like line intersections qu ...
- the solution of CountNonDivisible by Codility
question:https://codility.com/programmers/lessons/9 To solve this question , I get each element's di ...
- Leetcode OJ : Triangle 动态规划 python solution
Total Accepted: 31557 Total Submissions: 116793 Given a triangle, find the minimum path sum from ...
- 【LeetCode OJ】Triangle
Problem Link: http://oj.leetcode.com/problems/triangle/ Let R[][] be a 2D array where R[i][j] (j < ...
- 做了codility网站上一题:CountBoundedSlices
在写上一随笔之前,在Codility网站上还做了一个道题(非Demo题):CountBoundedSlices,得了60分(害臊呀).今天又重新做了一下这个算法,性能提高了不少,但由于此题不是Demo ...
- [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 ...
- [leetcode]Triangle @ Python
原题地址:https://oj.leetcode.com/problems/triangle/ 题意: Given a triangle, find the minimum path sum from ...
- 数字三角形 · Triangle
从上到下用DP. [抄题]: 给定一个数字三角形,找到从顶部到底部的最小路径和.每一步可以移动到下面一行的相邻数字上. 比如,给出下列数字三角形: [ [2], [3,4], [6,5,7], [4, ...
- leetcode 【 Triangle 】python 实现
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
随机推荐
- libyuv编译(各平台)【转】
转自:http://blog.csdn.net/wszawsz33/article/details/51669719 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] Getti ...
- Hbase delete遇到的常见异常: Exception in thread "main" java.lang.UnsupportedOperationException
hbase 执行批量删除时出现错误: Exception in thread "main" java.lang.UnsupportedOperationException at j ...
- 简述web工程师的职责与学习
最近两年web突然很火,也有很多人涌入这一行,但这行实际上是进来的人很多,出去的人也很多. 在我眼里,Web前端开发工程师的职责有:1.Web前端表现层及与前后端交互的架构设计和开发2.配合后台开发人 ...
- Django和SQLAlchemy区别
译者注:本文首先介绍了什么是ORM,然后从多个方面对Python语言下的两个ORM库Django和SQLAlchemy进行比较,为ORM的选型提供了较为全面的指导建议.以下是译文. ORM是什么? 在 ...
- python每日一类(1):pathlib
每天学习一个python的类(大多数都是第三方的),聚沙成金. -------------------------------------------------------------------- ...
- springboot webapi 支持跨域 CORS
1.创建corsConfig 配置文件 @Configuration public class corsConfig { @Autowired varModel varModel_; @Bean pu ...
- ZOJ18th省赛 Lucky 7
[线上网址](http://acm.zju.edu.cn/onlinejudge/showContestProblems.do?contestId=378) BaoBao has just found ...
- 用swift开发自己的MacOS锁屏软件(一)
最近看到了NearLock这款软件,感觉还是很不错的,当我兴致勃勃的安装了体验之后,发现效果和自己所想的差太多了,所以,便想着自己写一个吧. 刚开始当然是查资料之类的,不查不知道,一查吓一跳,国内基本 ...
- Log4j记录日志到数据库
1.自定义输出消息 /** * 参数化消息 * @author Johnson.Lee * */ public interface ParameterizedMessage extends Seria ...
- 在前端页面调用sevlet的路径
1.路径:相对路径和绝对路径 <!-- 使用相对路径访问Servletpath --> <a href="servlet/ServletPath">这是Se ...