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 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] = -2one 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:
function solution(A);
that, given a non-empty 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] = -2the function should return 8, as explained above.
Write an efficient algorithm for the following assumptions:
- 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].
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const MIN_
}
NR_POSSIBLE_ROLLS = 6
MIN_VALUE = -10000000001
def solution(A):
sub_solutions = [MIN_VALUE] * (len(A)+NR_POSSIBLE_ROLLS)
sub_solutions[NR_POSSIBLE_ROLLS] = A[0]
# iterate over all steps
for idx in xrange(NR_POSSIBLE_ROLLS+1, len(A)+NR_POSSIBLE_ROLLS):
max_previous = MIN_VALUE
for previous_idx in xrange(NR_POSSIBLE_ROLLS):
max_previous = max(max_previous, sub_solutions[idx-previous_idx-1])
# the value for each iteration is the value at the A array plus the best value from which this index can be reached
sub_solutions[idx] = A[idx-NR_POSSIBLE_ROLLS] + max_previous
return sub_solutions[len(A)+NR_POSSIBLE_ROLLS-1]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sub_solutions =
}
NR_POSSIBLE_ROLLS = 6
MIN_VALUE = -10000000001
def solution(A):
sub_solutions = [MIN_VALUE] * (len(A)+NR_POSSIBLE_ROLLS)
sub_solutions[NR_POSSIBLE_ROLLS] = A[0]
# iterate over all steps
for idx in xrange(NR_POSSIBLE_ROLLS+1, len(A)+NR_POSSIBLE_ROLLS):
max_previous = MIN_VALUE
for previous_idx in xrange(NR_POSSIBLE_ROLLS):
max_previous = max(max_previous, sub_solutions[idx-previous_idx-1])
# the value for each iteration is the value at the A array plus the best value from which this index can be reached
sub_solutions[idx] = A[idx-NR_POSSIBLE_ROLLS] + max_previous
return sub_solutions[len(A)+NR_POSSIBLE_ROLLS-1]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sub_solutions = new Array(A.length+ROLL).fill(-Infinity);
console.log(sub_solutions)
}
NR_POSSIBLE_ROLLS = 6
MIN_VALUE = -10000000001
def solution(A):
sub_solutions = [MIN_VALUE] * (len(A)+NR_POSSIBLE_ROLLS)
sub_solutions[NR_POSSIBLE_ROLLS] = A[0]
# iterate over all steps
for idx in xrange(NR_POSSIBLE_ROLLS+1, len(A)+NR_POSSIBLE_ROLLS):
max_previous = MIN_VALUE
for previous_idx in xrange(NR_POSSIBLE_ROLLS):
max_previous = max(max_previous, sub_solutions[idx-previous_idx-1])
# the value for each iteration is the value at the A array plus the best value from which this index can be reached
sub_solutions[idx] = A[idx-NR_POSSIBLE_ROLLS] + max_previous
return sub_solutions[len(A)+NR_POSSIBLE_ROLLS-1]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sub_solutions = new Array(A.length+ROLL).fill(-Infinity);
console.log(sub_solutions)
}
// NR_POSSIBLE_ROLLS = 6
// MIN_VALUE = -10000000001
// def solution(A):
// sub_solutions = [MIN_VALUE] * (len(A)+NR_POSSIBLE_ROLLS)
// sub_solutions[NR_POSSIBLE_ROLLS] = A[0]
// # iterate over all steps
// for idx in xrange(NR_POSSIBLE_ROLLS+1, len(A)+NR_POSSIBLE_ROLLS):
// max_previous = MIN_VALUE
// for previous_idx in xrange(NR_POSSIBLE_ROLLS):
// max_previous = max(max_previous, sub_solutions[idx-previous_idx-1])
// # the value for each iteration is the value at the A array plus the best value from which this index can be reached
// sub_solutions[idx] = A[idx-NR_POSSIBLE_ROLLS] + max_previous
// return sub_solutions[len(A)+NR_POSSIBLE_ROLLS-1]
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ -Infinity, -Infinity, -Infinity, -Infinity, -Infinity, -Infinity, -Infinity, -Infinity, -Infinity, -Infinity, -Infinity, -Infinity ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sub_solutions = new Array(A.length+ROLL).fill(-Infinity);
console.log(sub_solutions[0])
}
// NR_POSSIBLE_ROLLS = 6
// MIN_VALUE = -10000000001
// def solution(A):
// sub_solutions = [MIN_VALUE] * (len(A)+NR_POSSIBLE_ROLLS)
// sub_solutions[NR_POSSIBLE_ROLLS] = A[0]
// # iterate over all steps
// for idx in xrange(NR_POSSIBLE_ROLLS+1, len(A)+NR_POSSIBLE_ROLLS):
// max_previous = MIN_VALUE
// for previous_idx in xrange(NR_POSSIBLE_ROLLS):
// max_previous = max(max_previous, sub_solutions[idx-previous_idx-1])
// # the value for each iteration is the value at the A array plus the best value from which this index can be reached
// sub_solutions[idx] = A[idx-NR_POSSIBLE_ROLLS] + max_previous
// return sub_solutions[len(A)+NR_POSSIBLE_ROLLS-1]
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
-Infinity
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sub_solutions = new Array(A.length+ROLL).fill(-Infinity);
console.log(sub_solutions[0])
}
NR_POSSIBLE_ROLLS = 6
MIN_VALUE = -10000000001
def solution(A):
sub_solutions = [MIN_VALUE] * (len(A)+NR_POSSIBLE_ROLLS)
sub_solutions[NR_POSSIBLE_ROLLS] = A[0]
# iterate over all steps
for idx in xrange(NR_POSSIBLE_ROLLS+1, len(A)+NR_POSSIBLE_ROLLS):
max_previous = MIN_VALUE
for previous_idx in xrange(NR_POSSIBLE_ROLLS):
max_previous = max(max_previous, sub_solutions[idx-previous_idx-1])
# the value for each iteration is the value at the A array plus the best value from which this index can be reached
sub_solutions[idx] = A[idx-NR_POSSIBLE_ROLLS] + max_previous
return sub_solutions[len(A)+NR_POSSIBLE_ROLLS-1]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sub_solutions = new Array(A.length+ROLL).fill(-Infinity);
for(let index of )
}
NR_POSSIBLE_ROLLS = 6
MIN_VALUE = -10000000001
def solution(A):
sub_solutions = [MIN_VALUE] * (len(A)+NR_POSSIBLE_ROLLS)
sub_solutions[NR_POSSIBLE_ROLLS] = A[0]
# iterate over all steps
for idx in xrange(NR_POSSIBLE_ROLLS+1, len(A)+NR_POSSIBLE_ROLLS):
max_previous = MIN_VALUE
for previous_idx in xrange(NR_POSSIBLE_ROLLS):
max_previous = max(max_previous, sub_solutions[idx-previous_idx-1])
# the value for each iteration is the value at the A array plus the best value from which this index can be reached
sub_solutions[idx] = A[idx-NR_POSSIBLE_ROLLS] + max_previous
return sub_solutions[len(A)+NR_POSSIBLE_ROLLS-1]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sub_solutions = new Array(A.length+ROLL).fill(-Infinity);
for(let i =ROLL+1; i<len)
}
NR_POSSIBLE_ROLLS = 6
MIN_VALUE = -10000000001
def solution(A):
sub_solutions = [MIN_VALUE] * (len(A)+NR_POSSIBLE_ROLLS)
sub_solutions[NR_POSSIBLE_ROLLS] = A[0]
# iterate over all steps
for idx in xrange(NR_POSSIBLE_ROLLS+1, len(A)+NR_POSSIBLE_ROLLS):
max_previous = MIN_VALUE
for previous_idx in xrange(NR_POSSIBLE_ROLLS):
max_previous = max(max_previous, sub_solutions[idx-previous_idx-1])
# the value for each iteration is the value at the A array plus the best value from which this index can be reached
sub_solutions[idx] = A[idx-NR_POSSIBLE_ROLLS] + max_previous
return sub_solutions[len(A)+NR_POSSIBLE_ROLLS-1]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sub_solutions = new Array(A.length+ROLL).fill(-Infinity);
for(let i =ROLL+1; i<A.length+ROLL; i++){
let max = -Infinity;
for(let pre)
}
}
NR_POSSIBLE_ROLLS = 6
MIN_VALUE = -10000000001
def solution(A):
sub_solutions = [MIN_VALUE] * (len(A)+NR_POSSIBLE_ROLLS)
sub_solutions[NR_POSSIBLE_ROLLS] = A[0]
# iterate over all steps
for idx in xrange(NR_POSSIBLE_ROLLS+1, len(A)+NR_POSSIBLE_ROLLS):
max_previous = MIN_VALUE
for previous_idx in xrange(NR_POSSIBLE_ROLLS):
max_previous = max(max_previous, sub_solutions[idx-previous_idx-1])
# the value for each iteration is the value at the A array plus the best value from which this index can be reached
sub_solutions[idx] = A[idx-NR_POSSIBLE_ROLLS] + max_previous
return sub_solutions[len(A)+NR_POSSIBLE_ROLLS-1]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sub_solutions = new Array(A.length+ROLL).fill(-Infinity);
for(let i =ROLL+1; i<A.length+ROLL; i++){
let max = -Infinity;
for(let pre of )
}
}
NR_POSSIBLE_ROLLS = 6
MIN_VALUE = -10000000001
def solution(A):
sub_solutions = [MIN_VALUE] * (len(A)+NR_POSSIBLE_ROLLS)
sub_solutions[NR_POSSIBLE_ROLLS] = A[0]
# iterate over all steps
for idx in xrange(NR_POSSIBLE_ROLLS+1, len(A)+NR_POSSIBLE_ROLLS):
max_previous = MIN_VALUE
for previous_idx in xrange(NR_POSSIBLE_ROLLS):
max_previous = max(max_previous, sub_solutions[idx-previous_idx-1])
# the value for each iteration is the value at the A array plus the best value from which this index can be reached
sub_solutions[idx] = A[idx-NR_POSSIBLE_ROLLS] + max_previous
return sub_solutions[len(A)+NR_POSSIBLE_ROLLS-1]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sub_solutions = new Array(A.length+ROLL).fill(-Infinity);
for(let i =ROLL+1; i<A.length+ROLL; i++){
let max = -Infinity;
for(let pre =0; pre<ROLL; pre++){
max = Math.max(max,)
}
}
}
NR_POSSIBLE_ROLLS = 6
MIN_VALUE = -10000000001
def solution(A):
sub_solutions = [MIN_VALUE] * (len(A)+NR_POSSIBLE_ROLLS)
sub_solutions[NR_POSSIBLE_ROLLS] = A[0]
# iterate over all steps
for idx in xrange(NR_POSSIBLE_ROLLS+1, len(A)+NR_POSSIBLE_ROLLS):
max_previous = MIN_VALUE
for previous_idx in xrange(NR_POSSIBLE_ROLLS):
max_previous = max(max_previous, sub_solutions[idx-previous_idx-1])
# the value for each iteration is the value at the A array plus the best value from which this index can be reached
sub_solutions[idx] = A[idx-NR_POSSIBLE_ROLLS] + max_previous
return sub_solutions[len(A)+NR_POSSIBLE_ROLLS-1]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sub_solutions = new Array(A.length+ROLL).fill(-Infinity);
for(let i =ROLL+1; i<A.length+ROLL; i++){
let max = -Infinity;
for(let pre =0; pre<ROLL; pre++){
max = Math.max(max, sub_solutions[i-pre-1]);
}
}
}
NR_POSSIBLE_ROLLS = 6
MIN_VALUE = -10000000001
def solution(A):
sub_solutions = [MIN_VALUE] * (len(A)+NR_POSSIBLE_ROLLS)
sub_solutions[NR_POSSIBLE_ROLLS] = A[0]
# iterate over all steps
for idx in xrange(NR_POSSIBLE_ROLLS+1, len(A)+NR_POSSIBLE_ROLLS):
max_previous = MIN_VALUE
for previous_idx in xrange(NR_POSSIBLE_ROLLS):
max_previous = max(max_previous, sub_solutions[idx-previous_idx-1])
# the value for each iteration is the value at the A array plus the best value from which this index can be reached
sub_solutions[idx] = A[idx-NR_POSSIBLE_ROLLS] + max_previous
return sub_solutions[len(A)+NR_POSSIBLE_ROLLS-1]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sub_solutions = new Array(A.length+ROLL).fill(-Infinity);
for(let i =ROLL+1; i<A.length+ROLL; i++){
let max = -Infinity;
for(let pre =0; pre<ROLL; pre++){
max = Math.max(max, sub_solutions[i-pre-1]);
}
sub_solutions[i] = A[i-ROLL] + max;
}
}
NR_POSSIBLE_ROLLS = 6
MIN_VALUE = -10000000001
def solution(A):
sub_solutions = [MIN_VALUE] * (len(A)+NR_POSSIBLE_ROLLS)
sub_solutions[NR_POSSIBLE_ROLLS] = A[0]
# iterate over all steps
for idx in xrange(NR_POSSIBLE_ROLLS+1, len(A)+NR_POSSIBLE_ROLLS):
max_previous = MIN_VALUE
for previous_idx in xrange(NR_POSSIBLE_ROLLS):
max_previous = max(max_previous, sub_solutions[idx-previous_idx-1])
# the value for each iteration is the value at the A array plus the best value from which this index can be reached
sub_solutions[idx] = A[idx-NR_POSSIBLE_ROLLS] + max_previous
return sub_solutions[len(A)+NR_POSSIBLE_ROLLS-1]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;sol
const sol = new Array(A.length+ROLL).fill(-Infinity);
for(let i =ROLL+1; i<A.length+ROLL; i++){
let max = -Infinity;
for(let pre =0; pre<ROLL; pre++){
max = Math.max(max, sol[i-pre-1]);
}
sol[i] = A[i-ROLL] + max;
}
console.log(sol)
}
ReferenceError: sol is not defined
at solution (solution.js:6:20)
at solutionWrapper (/tmp/exec.js:402:28)
at Promise.resolve.then (/tmp/exec.js:428:24)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
at Function.Module.runMain (module.js:686:11)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sol = new Array(A.length+ROLL).fill(-Infinity);
for(let i =ROLL+1; i<A.length+ROLL; i++){
let max = -Infinity;
for(let pre =0; pre<ROLL; pre++){
max = Math.max(max, sol[i-pre-1]);
}
sol[i] = A[i-ROLL] + max;
}
console.log(sol)
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ -Infinity, -Infinity, -Infinity, -Infinity, -Infinity, -Infinity, -Infinity, -Infinity, -Infinity, -Infinity, -Infinity, -Infinity ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sol = new Array(A.length+ROLL).fill(-Infinity);
for(let i =ROLL+1; i<A.length+ROLL; i++){
let max = -Infinity;
for(let pre =0; pre<ROLL; pre++){
max = Math.max(max, sol[i-pre-1]);
}
sol[i] = A[i-ROLL] + max;
}
sub_solutions[len(A)+NR_POSSIBLE_ROLLS-1]
console.log(sol)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sol = new Array(A.length+ROLL).fill(-Infinity);
for(let i =ROLL+1; i<A.length+ROLL; i++){
let max = -Infinity;
for(let pre =0; pre<ROLL; pre++){
max = Math.max(max, sol[i-pre-1]);
}
sol[i] = A[i-ROLL] + max;
}
console.log(sol[A.length+ROLL-1])
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
-Infinity
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sol = new Array(A.length+ROLL).fill(-Infinity);
sub_solutions[NR_POSSIBLE_ROLLS] = A[0]
for(let i =ROLL+1; i<A.length+ROLL; i++){
let max = -Infinity;
for(let pre =0; pre<ROLL; pre++){
max = Math.max(max, sol[i-pre-1]);
}
sol[i] = A[i-ROLL] + max;
}
console.log(sol[A.length+ROLL-1])
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sol = new Array(A.length+ROLL).fill(-Infinity);
sol[ROLL] = A[0];
for(let i =ROLL+1; i<A.length+ROLL; i++){
let max = -Infinity;
for(let pre =0; pre<ROLL; pre++){
max = Math.max(max, sol[i-pre-1]);
}
sol[i] = A[i-ROLL] + max;
}
console.log(sol[A.length+ROLL-1])
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
8
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sol = new Array(A.length+ROLL).fill(-Infinity);
sol[ROLL] = A[0];
for(let i =ROLL+1; i<A.length+ROLL; i++){
let max = -Infinity;
for(let pre =0; pre<ROLL; pre++){
max = Math.max(max, sol[i-pre-1]);
}
sol[i] = A[i-ROLL] + max;
}
console.log()
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sol = new Array(A.length+ROLL).fill(-Infinity);
sol[ROLL] = A[0];
for(let i =ROLL+1; i<A.length+ROLL; i++){
let max = -Infinity;
for(let pre =0; pre<ROLL; pre++){
max = Math.max(max, sol[i-pre-1]);
}
sol[i] = A[i-ROLL] + max;
}
return sol[A.length+ROLL-1];
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sol = new Array(A.length+ROLL).fill(-Infinity);
sol[ROLL] = A[0];
for(let i =ROLL+1; i<A.length+ROLL; i++){
let max = -Infinity;
for(let pre =0; pre<ROLL; pre++){
max = Math.max(max, sol[i-pre-1]);
}
sol[i] = A[i-ROLL] + max;
}
return sol[A.length+ROLL-1];
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sol = new Array(A.length+ROLL).fill(-Infinity);
sol[ROLL] = A[0];
for(let i =ROLL+1; i<A.length+ROLL; i++){
let max = -Infinity;
for(let pre =0; pre<ROLL; pre++){
max = Math.max(max, sol[i-pre-1]);
}
sol[i] = A[i-ROLL] + max;
}
return sol[A.length+ROLL-1];
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const ROLL = 6;
const sol = new Array(A.length+ROLL).fill(-Infinity);
sol[ROLL] = A[0];
for(let i =ROLL+1; i<A.length+ROLL; i++){
let max = -Infinity;
for(let pre =0; pre<ROLL; pre++){
max = Math.max(max, sol[i-pre-1]);
}
sol[i] = A[i-ROLL] + max;
}
return sol[A.length+ROLL-1];
}
The solution obtained perfect score.