find the subset of maximal sum in which the distance between consecutive elements is at most 6 from a given array

Chaklader Asfak Arefe

I am trying to solve a Codility problem for the dynamic programming section provided.

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] = -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:

class Solution { public int solution(int[] 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] = -2 the 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].

I get low performance with the online judge.

enter image description here

My solution is provided below.

public static int solution(int[] A) {

        int N = A.length;

        int[] result = new int[N];
        result[0] = A[0];

        for (int i = 1; i < N; i++) {

            result[i] = result[i - 1];

            for (int j = 2; j <= 6; j++) {

                if (j > i) {
                    break;
                }

                // 0, 1, 2, 3, 4
                result[i] = Math.max(result[i], result[j - 2]);
            }

            result[i] += A[i];
        }

        return result[N - 1];
    }

How do I improve the correctness and performance?

Yola

You can do it in O(N) with O(N) additional space.

Allocate array dp of length N and for every next item in the array find out the biggest possible value of the game by following formula:

dp[k] = max(dp[k-6], dp[k-5], .., dp[k - 1]) + data[k]

Some special handling is needed in the beginning of the array. The answer will be stored in dp[N-1].

EDIT: As properly noted @גלעד ברקן in the comment, we can do it with O(1) space. To do this we can use circular buffer of length 7.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

find the subset of maximal sum in which the distance between consecutive elements is at most 6 from a given array

From Dev

Maximal subset sum smaller than a given value

From Dev

Write a program, which finds the maximal sequence of consecutive equal elements in an array. c#

From Dev

Array consecutive elements sum

From Dev

How to find a distance between elements in numpy array?

From Dev

Python: Find distance between characters of elements of array

From Dev

Find the positions in which the elements located in these have the least distance between them

From Dev

Sum consecutive groups of elements of an array

From Dev

find sum of diagonal elements from given index in 2d array

From Dev

Distance to non consecutive elements in a numpy array

From Dev

given array find all combinations of elements that give sum

From Dev

Find sum from combination of array elements

From Dev

Given an unsorted array, Find the maximum subtraction between two elements in the array

From Dev

Find elements that sum to a given number

From Dev

Calculating distance between each consecutive element of an array

From Dev

maximum sum of n consecutive elements of array

From Dev

Find maximal subset of all non connected elements if Elements swapped in bubble sort are connected

From Dev

Find the largest sum subarray from the given array using segment trees

From Dev

How to find the maximal (longest) sequence of increasing elements in an array arr[n]?

From Dev

Find values in list which sum to a given value

From Dev

How to find if there's arithmatic mean of contiguous elements in a sorted array that equals to a given value in the most efficient running time?

From Dev

Sum elements from array

From Dev

Sum elements from array

From Dev

Find distance between list's elements

From Dev

Find subset of size k such that the minimum distance between values is maximum

From Dev

How to find the maximum possible sum from any two elements in an array

From Dev

Find the sum of array elements recursively

From Dev

Find the sum of array elements recursively

From Dev

Find the most vowel words in the sentence (which can be several maximal words that are equivalent to each other and are located neighbor)

Related Related

  1. 1

    find the subset of maximal sum in which the distance between consecutive elements is at most 6 from a given array

  2. 2

    Maximal subset sum smaller than a given value

  3. 3

    Write a program, which finds the maximal sequence of consecutive equal elements in an array. c#

  4. 4

    Array consecutive elements sum

  5. 5

    How to find a distance between elements in numpy array?

  6. 6

    Python: Find distance between characters of elements of array

  7. 7

    Find the positions in which the elements located in these have the least distance between them

  8. 8

    Sum consecutive groups of elements of an array

  9. 9

    find sum of diagonal elements from given index in 2d array

  10. 10

    Distance to non consecutive elements in a numpy array

  11. 11

    given array find all combinations of elements that give sum

  12. 12

    Find sum from combination of array elements

  13. 13

    Given an unsorted array, Find the maximum subtraction between two elements in the array

  14. 14

    Find elements that sum to a given number

  15. 15

    Calculating distance between each consecutive element of an array

  16. 16

    maximum sum of n consecutive elements of array

  17. 17

    Find maximal subset of all non connected elements if Elements swapped in bubble sort are connected

  18. 18

    Find the largest sum subarray from the given array using segment trees

  19. 19

    How to find the maximal (longest) sequence of increasing elements in an array arr[n]?

  20. 20

    Find values in list which sum to a given value

  21. 21

    How to find if there's arithmatic mean of contiguous elements in a sorted array that equals to a given value in the most efficient running time?

  22. 22

    Sum elements from array

  23. 23

    Sum elements from array

  24. 24

    Find distance between list's elements

  25. 25

    Find subset of size k such that the minimum distance between values is maximum

  26. 26

    How to find the maximum possible sum from any two elements in an array

  27. 27

    Find the sum of array elements recursively

  28. 28

    Find the sum of array elements recursively

  29. 29

    Find the most vowel words in the sentence (which can be several maximal words that are equivalent to each other and are located neighbor)

HotTag

Archive