A zero-indexed array A consisting of N integers is given. A triplet (P, Q, R) is triangular if it is possible to build a triangle with sides of lengths A[P], A[Q] and A[R]. In other words, triplet (P, Q, R) is triangular if 0 ≤ P < Q < R < N and:…
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…
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…
首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which correspond to the types of successive nucleotides in the sequence. Each nucleotide has an impact factor, which is an integer. Nucleotides of types A,…
https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/17491791 其实可以做到O(n) #include <iostream> #include <sstream> using namespace std; int solution(vector<int> &A) { int n = A.size(); int prev…
https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个节点记录以该节点结束的最长路径,这样加入新的路径时去更新.注意路径是双向的~ #include <vector> #include <algorithm> using namespace std; struct Road { int start; int end; int val; }…
https://codility.com/programmers/challenges/upsilon2012 求笛卡尔树的高度,可以用单调栈来做. 维持一个单调递减的栈,每次进栈的时候记录下它之后有多少元素,就是以它为根的子树的高度.出栈的时候再更新一次供新进栈者使用. int solution(vector<int> &A) { A.push_back(1000000001); // an element larger than any one in A int size = A.…