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…
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…
question:https://codility.com/programmers/lessons/4 this question is seem like line intersections question. we can use similar method to solve this question. now i prove this solution. for each line, it has two endpoint. we call it left and right. as…
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 i…
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…
首先上题目: 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/fluorum2014 http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1273 http://blog.csdn.net/caopengcs/article/details/36872627 http://www.quora.com/How-do-I-determine-the-order-of-visiting-all-leave…
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/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后和最前面一个不可能取到~ #include <vector> using namespace std; int solution(vector<int> &A) { vector<int> maxEnd; maxEnd.resize(A.size()); maxEn…
https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了. #include <stack> using namespace std; int solution(vector<int> &A, vector<int> &B) { int size = A.size(); int deadNum = 0; stack<int> stk; // downst…
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.…
https://codility.com/demo/take-sample-test/count_div 此题比较简单,是在O(1)时间里求区间[A,B]里面能被K整除的数字,那么就计算一下就能得到. int solution(int A, int B, int K) { // write your code in C++11 int diff = (B-A+1); int result = diff / K; for (int i = A + result * K; i <= B; i++)…
https://codility.com/demo/take-sample-test/min_avg_two_slice 此题要求一个数组子段的最小的平均数(返回第一个数字的index).刚开始想记录sum,也没用,因为所有子段的组合是O(n^2)的.后来看解释发现,最小值必然存在于长度为2或3的子段中(长度为3的子段也无法分解).可以用反证法,如果有一个长度大于3的子段,均值小于之前找出的长度2和3的子段均值,那么必然可以在这个子段中找到均值更小的长度2和3的子段,矛盾. int soluti…
今天开始刷刷codility上respectable的题目,难度适中. https://codility.com/demo/take-sample-test/missing_integer 本题是找出数组里第一个非负的整数,要求复杂度O(n).那么首先想到的做法是排序,但这样负责度就上去了.要从nlogn降到n,常见的一个做法是用hashtable,这里就可以用set记录.要注意的是全负的情况,所以这里用了maxVal=0作为初值. #include <unordered_set> using…
在写上一随笔之前,在Codility网站上还做了一个道题(非Demo题):CountBoundedSlices,得了60分(害臊呀).今天又重新做了一下这个算法,性能提高了不少,但由于此题不是Demo题,不能重新在Codility网站测试了. 可以从http://codility.com/cert/view/certAWY5VP-D46CA7989XU4XEZT/details看到题目的详细信息. 这里把题目copy出来: An integer K and a non-empty zero-in…
https://codility.com/demo/take-sample-test/delta2011/ 0-1背包问题的应用.我自己一开始没想出来.“首先对数组做处理,负数转换成对应正数,零去掉,计数排序统计有多少个不同元素及其对应个数,并累加所有数的和sum,不妨记b=sum/2,不同元素个数为m,则目标转换为在m个不同元素中挑出若干个元素(每个元素可以重复多次,但少于它们的出现次数),使得它们的和不大于b并尽量接近.到了这里,应该有点熟悉的感觉了吧.对了,其实这就是0-1背包问题!” 参…
http://codility.com/demo/take-sample-test/pi2012 又是一道单调栈的题目.首先这道题目n^2是最朴素的做法.继续优化,因为和顺序有关就不好排序.然后,看到可以分解成求左方向最值和右方向最值的问题.此时要变成O(n)就要么贪心,要么DP,要么单调栈,要么有更多规律未发现. 这里有这么一个特点,考虑求左方向最值,当先有4,再有5之后,那么之前的4就没有意义了.所以用单调栈.如果用函数,代码可以更简洁. #include <stack> vector&l…
http://codility.com/demo/take-sample-test/hydrogenium2013 用Dijkstra求最短路径,同时和D[i]比较判断是不是能到.用了优先队列优化,复杂度是(m+n)*log(n).同时,写Dijkstra的时候一般要用dist数组,这里只拿它做访问标示.中间有个坑就是两个点之间可以多条路径,fail了半天. #include <queue> #include <functional> #define pp pair<int,…
http://codility.com/demo/take-sample-test/omega2013 这题有点意思.首先经过思考,想到从底部往上扫,去迎接掉下来的disc.但这样仍然是不行的.后来看了答案,需要转化一下.因为从上往下看时,井里面下面大的圆都被上面小的圆遮住了,所以可以削去. int solution(vector<int> &A, vector<int> &B) { // write your code in C++98 for (int i =…
http://codility.com/demo/take-sample-test/arrayinversioncount 求逆序对数,归并排序并记录逆序次数. // you can also use includes, for example: // #include <algorithm> int merge(vector<int> &A, int left, int right) { if (left >= right) return 0; int mid =…
Codility Certificate题目.求product最大值,product为长度*出现次数,例子"abababa"如下: "a", whose product equals 1 * 4 = 4,"ab", whose product equals 2 * 3 = 6,"aba", whose product equals 3 * 3 = 9,"abab", whose product equals…
http://codility.com/demo/take-sample-test/treeheight 非常非常简单的求树的深度.不忍直视. // you can also use includes, for example: // #include <algorithm> int get_height(tree * T) { if (T->l == NULL && T->r == NULL) return 0; int hl = 0; int hr = 0; i…
http://codility.com/demo/take-sample-test/equileader 一开始想到从左和右两边开始扫取众数,但求众数又要重新扫一遍,这样复杂度就是O(n^2)了.此题的关键在于Equi-Leader必然是众数,否则不可能左边和右边都是众数.所以先求出众数及其出现次数,再扫就行了. // you can also use includes, for example: // #include <algorithm> int solution(vector<i…
https://codility.com/demo/take-sample-test/stone_wall 拼石块.用最少的方块.一开始想了想用贪心,是可行的,就是尽量每次把当前的高度往右延伸到最多,比如7,8,7,那么7往右延伸可以覆盖到第二个7,这样减掉,后面的就变成1,0了,问题仍然等价.但这样要O(n^2).结果需要O(n)的复杂度.这个时候就想到了单调栈.于是栈中只记录递增的序列,发现比top当前的大就pop,因为这个对之后的已经没有作用了.因为每个元素都进栈出栈一次,平摊下来是O(n…
http://codility.com/demo/take-sample-test/beta2010/ 这题以前做的时候是先排序再二分,现在觉得没有必要.首先圆可以看成线段,把线段的进入作为一个事件,出去作为一个事件.注意根据题意,同样的点,进入要在出去之前.那么O(n)扫一遍就可以得到结果.注意的是A[i]+i可能会超出int. // you can also use includes, for example: #include <algorithm> int solution(const…
http://codility.com/demo/take-sample-test/genomicrangequery 这题有点意思.一开始以为是RMQ或者线段树,但这样要O(n*logn).考虑到只有四种字符,可以用数组记录每个字符i之前出现过几次.二,查询区间是闭区间,所以要处理off by one的问题. // you can also use includes, for example: // #include <algorithm> vector<int> solutio…
http://codility.com/demo/take-sample-test/maxcounters 简单题.注意要记录两个max,一个是最大值,一个是已经生效的最大值. // you can also use includes, for example: // #include <algorithm> vector<int> solution(int N, vector<int> &A) { // write your code in C++98 int…
http://codility.com/demo/take-sample-test/tapeequilibrium 简单题.记录到i为止的sum就可以了.O(n). // you can also use includes, for example: // #include <algorithm> int solution(vector<int> &A) { // write your code in C++98 int total = 0; vector<int&g…
codility出了lesson 5了. (1) 合法括号序列,包括( [ { ) ] }这6种字符的字符串,长度N在[0..200000]范围内,为其是否合法. 要求时间复杂度O(N),空间复杂度O(N). 用堆栈简单判断就可以了. // you can also use includes, for example: // #include <algorithm> #include <stack> int solution(const string &S) { // wr…