Codility adds exercise lesson 2. There are three questions: (1) Perm-Check the given integer array has N numbers. Ask if it is an arrangement of 1-N, that is, whether each number is 1-N and only appears once. Output 1 and 0 indicate whether or not, input range N [1 .. 10 ^ 5], an integer in the array [1 .. 10 ^ 5], requires the complexity of the time space is O (N ). Analysis: the space complexity O (N) algorithm is very simple. We can create a bool array to indicate 1-N, whether or not each number has appeared. The Code is as follows: [cpp] // you can also use provided des, for example: // # include <algorithm> int solution (vector <int> &) {// write your code here... vector <bool> have; int I, n =. size (); have. resize (n, false); for (I = 0; I <n; ++ I) {if (-- A [I]> = n) | (have [A [I]) {return 0;} have [A [I] = true;} return 1;} In fact, we have an algorithm with a spatial complexity of O (1. Use the Perm-Missing-Elem method in exercise (1) to change A [I] to the position of A [A [I]-1]. The Code is as follows: [cpp] // you can also use provided des, for example: // # include <algorithm> int solution (vector <int> &) {// write your code here... int n =. size (), I, x, t; for (I = 0; I <n; ++ I) {for (x = A [I]; (x <= n) & (A [x-1]! = X);) {t = A [x-1]; A [x-1] = x; x = t;} if (x> n) {return 0 ;}for (I = 0; I <n; ++ I) {if (A [I]! = I + 1) {return 0;} return 1;} we can also make it more beautiful. Suppose we try to make A [0 .. i] is 1 .. I + 1, for the current A [I]! = I + 1, from A [0 .. i-1] is already 1 .. i. If A [I] <I + 1, it indicates that it is repeated, and A [I]> n does not work, because all numbers can only be 1 .. n. Check whether the number at the position of A [A [I]-1] is repeated or not, if you do not need to repeat it, exchange it until A [I] meets the requirements. The Code is as follows: [cpp] // you can also use provided des, for example: // # include <algorithm> int solution (vector <int> & A) {// write your code here... int n =. size (), I, t; for (I = 0; I <n; ++ I) {while (A [I]! = I + 1) {if (A [I]> n) | (A [I] <I + 1) | (A [A [I]-1] = A [I]) {return 0;} t = A [I]; A [I] = A [A [I]-1]; A [t-1] = t;} return 1;} although there is A while loop, but in fact it is O (n), because each exchange should at least change a number to the position where it should be. We don't need this while loop. We have the same idea. We can try to modify the I value of the loop variable ...... The code is as follows: [cpp] int solution (vector <int> & A) {// write your code here... int n =. size (), I, t; for (I = 0; I <n;) {if (A [I] = I + 1) {++ I ;} else {if (A [I]> n) | (A [I] <I + 1) | (A [A [I]-1] = A [I]) {return 0;} t = A [I]; A [I] = A [A [I]-1]; A [t-1] = t ;}} return 1 ;}( 2) the Frog-River-One background is very interesting. The essence is to ask whether an integer array with a length of N contains all integers from. Both N and X are [1 .. 10 ^ 5], and the number in the array is 1 .. X. If array A [0.. r] contains all numbers of 1. X, return r; otherwise, return-1. Complexity time O (N) and space O (X) are required ). This question is actually similar to the previous one. In terms of space, we can also create a bool array with the length of X to indicate that it has never appeared. In fact, we still have the space for O (1. First, if there are at least X items in the solution, we need to exchange the first X items of the array to the position where the array should be placed using the exchange mentioned above. Then, let's take a look at X .. whether the values of N-1 items are 0 .. the location of X-1 is required. If you need to release it and update r, the Code is a little troublesome, but the idea remains unchanged: [cpp] // you can also use between des, for example: // # include <algorithm> int solution (int X, vector <int> & A) {// write your code here... int I, r, t, n =. size (); if (X> n) {return-1 ;}for (I = 0; I <X;) {if (A [I]> X) | (A [A [I]-1] = A [I]) {++ I;} else {t = A [I]; A [I] = A [A [I ]-1]; A [t-1] = t;/* if (t <I + 1) {+ I;} */} r = X-1; for (I = X; I <n; ++ I) {if (A [I] <= X) & (A [A [I]-1]! = A [I]) {A [A [I]-1] = A [I]; r = I ;}} for (I = 0; I <X; ++ I) {if (A [I]! = I + 1) {return-1 ;}} return r ;}( 3) an array of Max-Counters with a length of N. At first, all numbers are 0. There are two operations, one is to add 1 to an element, and the other is to change all elements to the current maximum value. After several such operations, the final state of the array is obtained. The length of the array N and the number of operations M are both in [1 .. 10 ^ 5]. Complexity O (N + M) and space O (N) are required ). We maintain a timestamp for each operation in the array. The timestamp determines whether the value of the array element is the previous maximum value or the current value, and records the maximum value before each second operation, the Code is as follows: [cpp] // you can also use provided des, for example: // # include <algorithm> vector <int> solution (int N, vector <int> &) {// write your code here... int I, m =. size (), lastv = 0, lastt =-1, v = 0; vector <int> r, t; r. resize (N, 0); t. resize (N,-1); for (I = 0; I <m; ++ I) {if (-- A [I] <N) {v = max (r [A [I] = (t [A [I]> lastt )? R [A [I]: lastv) + 1, v); t [A [I] = I;} else {lastv = v; lastt = I ;}} for (I = 0; I <N; ++ I) {if (lastt> t [I]) {r [I] = lastv ;}} return r ;} in fact, the timestamp can be omitted. The Code is as follows: [cpp] // you can also use between des, for example: // # include <algorithm> vector <int> solution (int N, vector <int> & A) {// write your code here... int I, m =. size (), lastv = 0, v = 0; vector <int> r; r. resize (N, 0); for (I = 0; I <m; ++ I) {if (-- A [I] <N) {v = max (v, r [A [I] = max (r [A [I], lastv) + 1) ;}else {lastv = v ;}} for (I = 0; I <N; ++ I) {r [I] = max (r [I], lastv);} return r ;}