Practices on Codility (15) and practices on codility (15)
(1) NumberSolitaire
A game starts with N grids in a row, with the number 0 .. n-1. At first, the pawn piece is in A [0], and each lattice has an integer (which may be positive or negative ). You are in the grid I, you throw the dice and get the points X = [1 .. 6], and then go to the grid I + X. If the grid does not exist, roll the dice again until the grid I + X exists. When you go to the grid N-1, the game is over. The sum of the integers in the lattice is your score. How can I calculate the maximum possible score?
Data range: N [2 .. 10 ^ 5]. The range of the number in the grid is [-10000, + 10000].
Required complexity: time O (N), Space O (N)
Analysis: A clear dp [I] = A [I] + max (dp [I-x]) 1 <= x <= min (6, I)
Code:
// you can use includes, for example:// #include <algorithm>// you can write to stdout for debugging purposes, e.g.// cout << "this is a debug message" << endl;int solution(vector<int> &A) { // write your code in C++11 const int inf = 2000000000; int n = A.size(); vector<int> dp(n); dp[0] = A[0]; for (int i = 1; i < n; ++i) { dp[i] = -inf; for (int j = min(6 , i); j; --j) { dp[i] = max(dp[i], dp[i - j]); } dp[i] += A[i]; } return dp[n - 1];}
(2) MinAbsSum
Http://blog.csdn.net/caopengcs/article/details/10028269