1.题目:

A game for one player is played on a board consisting of N consecutive squares, 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 is a pebble on square number 0 and this is the only square on the board which is marked. 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 on the upper face after the die comes to rest. Then we move the pebble standing on 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 is 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 is 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 are 0, 3 and 5, so the result of the game is 1 + 9 + (−2) = 8. This is the maximal possible result that can be achieved on this board.

Write a function:

int solution(int A[], int N);

that, given a non-empty zero-indexed array A of N integers, returns the maximal result that can be achieved on the board represented 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 array A is an integer within the range [−10,000..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.题目分析

这个题目写的超级复杂,其实说的内容很简单,就是我们掷骰子,1~6,代表向前走几步。那么一个棋盘的长度为N,每个节点上有一个数字,我们要通过掷色子刚好走到最后一个格子,在这个过程中会经过x个点。问题就是要我们输出这x个点最大可能的和是多少。

这个题如果不是看了关于dynamicprogramming的介绍的话一下子就蒙傻逼了。这个可能小多啊,跳到所有的正数是没话说,不过因为有负数,如何选择我跳入那个负数,避开哪个负数?如果有一长串的负数我如何跳入?更何况时间复杂度要求为O(N)。。这。。头好大。

但是有了dynamic这个算法,我们便可以换一个思路来向这个问题。

我们并不是要向后看,而是向前看。有点数学归纳法的赶脚。

首先,我们随便的站到位置W上。那么,如果要到达这个点,只能是从其前6个位置跳过来的,因为色子最大就到6呢。

那么如果问题到这个点结束,因为W位置上的数字是固定的,那么要跳到这个点时和为最大,则需要找到前六个点中的最大值即可。那么以此类推,最终会回到第0个位置。这个位置的最大值是固定的就是其本身。我们便可以递推的推出所有位置的最大值~

而且我们在每一个位置,内循环查找的最大次数为6,所以即使我有两层循环,那么时间复杂度也只是6N~=O(N)。线性。

我们还需要一个数组存储每一个位置的最大值需要N个空间。

3.代码

int maxLastSix(int A[],int pos)
{
int step=;
int result = A[pos-step];
while((pos-step)>=)
{
if(step>)
{
return result;
}
result = (result>A[pos-step])?result:A[pos-step];
step++;
}
return result;
} int solution(int A[], int N) {
// write your code in C99
int dp[N];
int i=; dp[]=A[]; for(i=;i<N;i++)
{
int temp = maxLastSix(dp,i);
dp[i]=A[i]+temp;
// printf("%d\n",temp);
} return dp[N-];
}

Codility NumberSolitaire Solution的更多相关文章

  1. codility flags solution

    How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...

  2. Solution of NumberOfDiscIntersections by Codility

    question:https://codility.com/programmers/lessons/4 this question is seem like line intersections qu ...

  3. Solution to Triangle by Codility

    question: https://codility.com/programmers/lessons/4 we need two parts to prove our solution. on one ...

  4. the solution of CountNonDivisible by Codility

    question:https://codility.com/programmers/lessons/9 To solve this question , I get each element's di ...

  5. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  6. *[codility]Peaks

    https://codility.com/demo/take-sample-test/peaks http://blog.csdn.net/caopengcs/article/details/1749 ...

  7. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...

  8. *[codility]AscendingPaths

    https://codility.com/programmers/challenges/magnesium2014 图形上的DP,先按照路径长度排序,然后依次遍历,状态是使用到当前路径为止的情况:每个 ...

  9. *[codility]MaxDoubleSliceSum

    https://codility.com/demo/take-sample-test/max_double_slice_sum 两个最大子段和相拼接,从前和从后都扫一遍.注意其中一段可以为0.还有最后 ...

随机推荐

  1. nginx服务傻瓜搭建

    nginx服务傻瓜搭建 安装步骤: 一.先准备好相关源码包和程序包,如下图 所有包都在云服务器的/src目录下. 二.安装 1.安装nginx服务器,支持vod stream.fileupload c ...

  2. 未能加载文件或程序集“System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。系统找不到指定的文件

    ASP.NET 运行时错误:针对类型System.Web.Mvc.PreApplicationStartCode的应用程序邓启动初始化方法Start 引发了异常,显示下列错误消息: 未能加载文件或程序 ...

  3. mysql benchmark基准测试

    git项目地址: https://github.com/akopytov/sysbench 利用sysbench很容易对mysql做性能基准测试(当然这个工具很强大,除了测试主流数据库性能,还能测试其 ...

  4. [LeetCode] Majority Element II 求众数之二

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...

  5. iOS 设置不同的字体颜色

    //设置不同字体颜色 -(void)fuwenbenLabel:(UILabel *)labell FontNumber:(UIFont *)font AndRange:(NSRange)range ...

  6. CSS你可能还不知道的一些知识点

    一.特殊选择器 1.* 用于匹配任何的标记 2.> 用于指定父子节点关系 3.E + F 毗邻元素选择器,匹配所有紧随E元素之后的同级元素F 4.E ~ F 匹配所有E元素之后的同级元素F 5. ...

  7. cocos2d-x屏幕分辨率,窗口大小总结

    这个东西很烦人,相信很多人都不理解 今天来总结一下,首先有很多概念都要事先弄得清楚明白 1.屏幕分辨率 所谓屏幕分辨率相信很多人都知道他的概念,不就是1280pxX720PX吗?不就是这种形式吗?有什 ...

  8. h5手机端下拉选择城市

    <!doctype html><html>    <head>            <meta http-equiv="Content-Type& ...

  9. 82.Android之MVP+Retrofit+RxJava实践小结

    转载:http://wuxiaolong.me/2016/06/12/mvpRetrofitRxjava/ 关于MVP.Retrofit.RxJava,之前已经分别做了分享,如果您还没有阅读过,可以猛 ...

  10. BZOJ 4205: 卡牌配对

    4205: 卡牌配对 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 173  Solved: 76[Submit][Status][Discuss] ...