Exercises on codility (15)

Source: Internet
Author: User

(1) Numbersolitaire

A game starts with a row of n squares, with a lattice number 0. N-1, at first, the pieces in a[0], each lattice has an integer (possibly positive, possibly negative). You are in lattice I, you throw the dice, get the points x = [1..6], and then go to the lattice numbered i + x, if this lattice does not exist, throw a dice again until i + x lattice exists. When you go to the N-1th grid, the game is over. The number of integers you pass through the lattice is your score, and the maximum possible score?

Data range: N [2..10^5], the range of the number of squares [-10000, +10000].

Complexity of requirements: Time O (n), space O (n)

Analysis: An obvious dp 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 was 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



Exercises on codility (15)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.