https://codility.com/demo/take-sample-test/missing_integer本题是找出数组里第一个非负的整数,要求复杂度O(n)。那么首先想到的做法是排序,但这样负责度就上去了。要从nlogn降到n,常见的一个做法是用hashtable,这里就可以用set记录。要注意的是全负的情况,所以这里用了maxVal=0作为初值。
Question: https://codility.com/demo/take-sample-test/flags Question Name: boron2013 or Flags The official solution is here.UPDATE 2014-10-02: thanks to Piotrek Martynowicz, a bug is found and fixed.Solution to boron2013 (Flags) by codility Python1...
Question: https://codility.com/demo/take-sample-test/min_abs_sum_of_two Question Name: Min-Abs-Sum-Of-Two or MinAbsSumOfTwo There are two O(nlogn) solutions for this question. Both solutions have the same overall time complexity and space complexity. And both solutions need to sort the...
https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍。注意其中一段可以为0。还有最后和最前面一个不可能取到~ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 #include <vector> usingnamespacestd; intsolution...
https://codility.com/demo/take-sample-test/stone_wall 拼石块。用最少的方块。一开始想了想用贪心,是可行的,就是尽量每次把当前的高度往右延伸到最多,比如7,8,7,那么7往右延伸可以覆盖到第二个7,这样减掉,后面的就变成1,0了,问题仍然等价。
http://codility.com/demo/take-sample-test/tapeequilibrium 简单题。记录到i为止的sum就可以了。O(n)。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 // you can also use includes, for example: // #include <algorithm> intsolution(vector<int> &A) { ...
Question: https://codility.com/demo/take-sample-test/min_max_division Question Name: Min-Max-Division or MinMaxDivision Logically, it seems to be similar with the boron2013 (Flags) challenge by Codility. And the following is my Python solution.UPDATE...
https://codility.com/demo/take-sample-test/fish 一开始习惯性使用单调栈,后来发现一个普通栈就可以了。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #include <stack> usingnamespacestd; ...
http://codility.com/demo/take-sample-test/maxcounters简单题。注意要记录两个max,一个是最大值,一个是已经生效的最大值。// you can also use includes, for example:// #include vector so