Practices on Codility (15) and practices on codility (15)

Source: Internet
Author: User

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



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.