1. Title:
A game for one player was played on a board consisting of N consecutive squares and 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. Moreover, some squares can be marked during the game.
At the beginning of the game, there are a pebble on square number 0 and this is the only square on the board which is Marke D. The goal of the game is to move the pebble to square number n−1.
During Each turn we throw a six-sided die, with numbers from 1 to 6 on its faces, and consider the number K, which shows O n the upper face after the die comes to rest. Then we move the pebble standing in square number I to square number i + K, providing that square number i + K exists. If Square number I + K does not exist, we throw the die again until we obtain a valid move. Finally, we mark Square number I + K.
After the game finishes (when the Pebble was standing on square number n−1), we calculate the result. The result of the game is the sum of the numbers written on all marked squares.
For example, given the following array:
A[0] = 1 a[1] =-2 a[2] = 0 a[3] = 9 a[4] =-1 a[5] = 2
One possible game could be as follows:
- The Pebble is on square number 0, which is marked;
- We throw 3; The Pebble moves from square number 0 to square number 3; We mark square number 3;
- We throw 5; The Pebble does not move, since there are no square number 8 on the board;
- We throw 2; The Pebble moves to square number 5; We mark this square and the game ends.
The marked squares is 0, 3 and 5, so the result of the game is 1 + 9 + (−2) = 8. This is the maximal possible result, can be achieved on this board.
Write a function:
int solution (int a[], int N);
That is, given a non-empty zero-indexed array A of N integers, returns the maximal result that can is achieved on the Board R Epresented by Array A.
For example, given the array
A[0] = 1 a[1] =-2 a[2] = 0 a[3] = 9 a[4] =-1 a[5] = 2
The function should return 8, as explained above.
Assume that:
- N is an integer within the range [2.. 100,000];
- Each element of within the range [−10,000. ].
Complexity:
- Expected worst-case time complexity is O (N);
- Expected worst-case space complexity is O (N), beyond input storage (not counting the storage required for input arguments) .
Elements of input arrays can be modified.
Copyright 2009–2015 by Codility Limited. All rights Reserved. Unauthorized copying, publication or disclosure prohibited.2. Topic analysis
This topic is very complex, in fact, the content is very simple, is that we roll the dice, 1~6, representing a few steps forward. So the length of a chessboard is N, and there is a number on each node, and we're going to pass the dice just to the last lattice, and in the process we go through X points. The problem is that we have to output the maximum possible number of these X points.
This question if not read about the introduction of dynamicprogramming, all of a sudden silly forced. This may be much smaller ah, jump to all the positive is no words, but because there are negative, how to choose I jump into that negative number, avoid which negative? How do I jump in if I have a long list of negative numbers? Not to mention the time complexity required for O (N). This.. The head is so big.
But with the dynamic algorithm, we can change the idea to the problem.
We're not looking backwards, we're moving forward. A little bright of mathematical inductive methods.
First, we casually stand on the position W. So, if you want to reach this point, you can only jump from its first 6 positions, because the dice is the largest of 6.
So if the problem ends at this point because the number on the W position is fixed, then to jump to this point and maximum, you need to find the maximum value in the first six points. And so on, it will eventually go back to the No. 0 position. The maximum value of this position is fixed and is itself. We can push forward the maximum value of all locations ~
And we are in every position, the maximum number of loops to find is 6, so even if I have two layers of loops, then the time complexity is only 6n~=o (N). Linear.
We also need an array to store the maximum value for each location that requires n space.
3. Code
intMaxlastsix (intA[],intPOS) { intstep=1; intresult = a[pos-Step]; while((Pos-step) >=0) { if(step>6) { returnresult; } result= (Result>a[pos-step])? result:a[pos-Step]; Step++; } returnresult;}intSolution (intA[],intN) {//Write your code in C99 intDp[n]; intI=0; dp[0]=a[0]; for(i=1; i<n;i++) { inttemp =Maxlastsix (dp,i); Dp[i]=a[i]+temp; //printf ("%d\n", temp); } returndp[n-1];}
Codility Numbersolitaire Solution