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 ...
随机推荐
- entitymanager 进行数据序列化
场景:同一个方法里,需要将前一部分执行的数据保存到数据库.后半部分读取数据时从数据库里获取,而不是获取到缓存里的数据. 理解eneityManager的这三个方法的作用和区别,首先需要分清楚Persi ...
- (1)Java Spring
Spring 4种关键策略: 基于POJO的轻量级和最小侵入编程 通过依赖注入和面向接口实现松耦合 基于切面和惯例进行声明式编程 通过切面和模板减少样板式代码
- 陕西师范大学第七届程序设计竞赛网络同步赛 J 黑猫的小老弟【数论/法拉数列/欧拉函数】
链接:https://www.nowcoder.com/acm/contest/121/J来源:牛客网 题目描述 大家知道,黑猫有很多的迷弟迷妹,当然也有相亲相爱的基友,这其中就有一些二五仔是黑猫的小 ...
- Codeforces Round #321 (Div. 2) A. Kefa and First Steps【暴力/dp/最长不递减子序列】
A. Kefa and First Steps time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- (寒假集训)Roadblock(最短路)
Roadblock 时间限制: 1 Sec 内存限制: 64 MB提交: 9 解决: 5[提交][状态][讨论版] 题目描述 Every morning, FJ wakes up and walk ...
- [CF623E]Transforming Sequence
$\newcommand{\align}[1]{\begin{align*}#1\end{align*}}$题意:对于一个序列$a_{1\cdots n}(a_i\in[1,2^k-1])$,定义序列 ...
- 【bzoj3261】【最大异或和】可持久化trie树+贪心
[pixiv] https://www.pixiv.net/member_illust.php?mode=medium&illust_id=61705397 Description 给定一个非 ...
- 四. Java继承和多态9. 类与类之间的关系
类与类之间最常见的关系主要有三种:依赖(uses-a).聚合(has-a)和继承(is-a). 下面以在线书店订单系统为例,来详细的讲述这三种关系的概念. 在线书店订单系统的主要功能是:注册用户可以登 ...
- SqlServer发布订阅错误收集
原文:SqlServer发布订阅错误收集 目录 1. SqlServer发布订阅错误收集 1.1. Message:脚本对于表"dbo.table"失败. 1.1.1. 错误消息 ...
- JAVA常见算法题(十六)
package com.xiaowu.demo; //猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不过瘾,又多吃了一个: //第二天早上又将剩下的桃子吃掉一半,而且又多吃了一个. //以后 ...