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;
}
时间: 2024-12-19 11:44:35

Solution to Triangle by Codility的相关文章

the solution of CountNonDivisible by Codility

question:https://codility.com/programmers/lessons/9 To solve this question , I get each element's divsors which appearing in input Array A using Sieve of Eratosthenes method. Time complexity is O(nlogn); Then  we iterate array A to get the ith non-di

Solution of NumberOfDiscIntersections by Codility

question:https://codility.com/programmers/lessons/4 trap: int overflow code: #include <algorithm> int solution(vector<int> &A) { // write your code in C++11 int size = A.size(); if (size <2) return 0; vector<long> begin; vector<

Triangle Leetcode Python

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ] The minimum path sum from top to bottom is 11 (i

[leetcode]Triangle @ Python

原题地址:https://oj.leetcode.com/problems/triangle/ 题意: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3

Triangle---minimum path sum

动态规划 class Solution: # @param triangle, a list of lists of integers # @return an integer def minimumTotal(self, triangle): depth=len(triangle) dp=[] if depth==0: return 0 for i in range(depth): if i==0: dp.append([triangle[i][i]]) else: for j in rang

Solution of Codility

Solution of Codility codility.com is another great place to improve your programming skill. Train myself , and record here. Lesson 1: Time Complexity Lesson 2: Counting Elements Lesson 3: Prefix Sums Lesson 4: Sorting MaxProductOfThree: * Distinct: *

codility flags solution

How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers is given. A peak is an array element which is larger than its neighbours. More precisely, it is an index P such that 0 < P < N − 1 and A[P − 1] < A

Codility MaxAbsSliceSum Solution

1.题目(HARD) A non-empty zero-indexed array A of N integers is given. A pair of integers (P, Q), such that 0 ≤ P ≤ Q < N, is called a slice of array A. Thesum of a slice (P, Q) is the total of A[P] + A[P+1] + ... + A[Q]. A min abs slice is a slice whos

Codility NumberSolitaire Solution

1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from 0 to N − 1. There is a number written on each square. A non-empty zero-indexed array A of N integers contains the numbers written on the squares. More