MaxDoubleSliceSum Coding Algorithm

I came across this problem in Codility tutorials, here is a description:

Given a non-empty null-indexed array A, consisting of N integers.

A triplet (X, Y, Z), such that 0 ≤ X <Y <Z <N, is called a double slice.

The double cut sum (X, Y, Z) is the sum of A [X + 1] + A [X + 2] + ... + A [Y - 1] + A [Y + 1] + A [Y + 2] + ... + A [Z - 1].

For example, an array A such that:

A[0] = 3
A[1] = 2
A[2] = 6
A[3] = -1
A[4] = 4
A[5] = 5
A[6] = -1
A[7] = 2

contains the following examples of double fragments:

double slice (0, 3, 6), the sum is 2 + 6 + 4 + 5 = 17,

double slice (0, 3, 7), the sum is 2 + 6 + 4 + 5 - 1 = 16,

double slice (3, 4, 5), the sum is 0.

The goal is to find the maximum amount of any double fragment.

Write a function:

int solution (vector & A);

- A, N , .

, :

A[0] = 3
A[1] = 2
A[2] = 6
A[3] = -1
A[4] = 4
A[5] = 5
A[6] = -1
A[7] = 2

17, A 17.

, :

N - [3,00,000]; A [-10,000..10,000].

:

- O (N); - O (N), ( > , ).

.

MaxSum, i, , . , MaxSum, i, 0..i. :

int solution(vector<int> &A) {
    int n = A.size();

    int end = 2;   

    int ret = 0;
    int sum = 0;

    int min = A[1];

    while (end < n-1)
    {
        if (A[end] < min)
        {
            sum = max(0, sum + min);
            ret = max(ret, sum);
            min = A[end];
            ++end;
            continue;
        }
        sum = max(0, sum + A[end]);
        ret = max(ret, sum);
        ++end;
    }

    return ret;
}

, !

+4
4

. . 100/100.

public int solution(int[] A) {
  int N = A.length;
  int[] K1 = new int[N];
  int[] K2 = new int[N];

  for(int i = 1; i < N-1; i++){
    K1[i] = Math.max(K1[i-1] + A[i], 0);
  }
  for(int i = N-2; i > 0; i--){
    K2[i] = Math.max(K2[i+1]+A[i], 0);
  }

  int max = 0;

  for(int i = 1; i < N-1; i++){
    max = Math.max(max, K1[i-1]+K2[i+1]);
  }

  return max;
}
+4

:

int get_max_sum(const vector<int>& a) {
    int n = a.size();
    vector<int> best_pref(n);
    vector<int> best_suf(n);
    //Compute the best sum among all x values assuming that y = i.
    int min_pref = 0;
    int cur_pref = 0;
    for (int i = 1; i < n - 1; i++) {
        best_pref[i] = max(0, cur_pref - min_pref);
        cur_pref += a[i];
        min_pref = min(min_pref, cur_pref);
    }
    //Compute the best sum among all z values assuming that y = i.
    int min_suf = 0;
    int cur_suf = 0;
    for (int i = n - 2; i > 0; i--) {
        best_suf[i] = max(0, cur_suf - min_suf);
        cur_suf += a[i];
        min_suf = min(min_suf, cur_suf);
    }
    //Check all y values(y = i) and return the answer.
    int res = 0;
    for (int i = 1; i < n - 1; i++)
        res = max(res, best_pref[i] + best_suf[i]);
    return res;
 }

 int get_max_sum_dummy(const vector<int>& a) {
    //Try all possible values of x, y and z.
    int res = 0;
    int n = a.size();
    for (int x = 0; x < n; x++)
        for (int y = x + 1; y < n; y++)
            for (int z = y + 1; z < n; z++) {
                int cur = 0;
                for (int i = x + 1; i < z; i++)
                    if (i != y)
                        cur += a[i];
                res = max(res, cur);
            }
    return res;
 }

bool test() {
    //Generate a lot of small test cases and compare the output of 
    //a brute force and the actual solution.
    bool ok = true;
    for (int test = 0; test < 10000; test++) {
        int size = rand() % 20 + 3;
        vector<int> a(size);
        for (int i = 0; i < size; i++)
            a[i] = rand() % 20 - 10;
        if (get_max_sum(a) != get_max_sum_dummy(a))
            ok = false;
    }
    for (int test = 0; test < 10000; test++) {
        int size = rand() % 20 + 3;
        vector<int> a(size);
        for (int i = 0; i < size; i++)
            a[i] = rand() % 20;
        if (get_max_sum(a) != get_max_sum_dummy(a))
            ok = false;
    }
    return ok;
}

- get_max_sum ( - , , ).

, , - i i - 1, (best_pref[i] best_suf[i], ). i best_pref[i] + best_suf[i]. , best_pref[y] x y, best_suf[y] z y y.

+1
def solution(A):
    n = len(A)
    K1 = [0] * n
    K2 = [0] * n
    for i in range(1,n-1,1):
        K1[i] = max(K1[i-1] + A[i], 0)

    for i in range(n-2,0,-1):
        K2[i] = max(K2[i+1]+A[i], 0)

    maximum = 0;
    for i in range(1,n-1,1):
        maximum = max(maximum, K1[i-1]+K2[i+1])

    return maximum

def main():
    A = [3,2,6,-1,4,5,-1,2]
    print(solution(A))

if __name__ == '__main__': main()
+1

100%

def solution(a)
  max_starting =(a.length - 2).downto(0).each.inject([[],0]) do |(acc,max), i|
    [acc, acc[i]= [0, a[i] + max].max ]
  end.first

  max_ending =1.upto(a.length - 3).each.inject([[],0]) do |(acc,max), i|
    [acc, acc[i]= [0, a[i] + max].max ]
  end.first

  max_ending.each_with_index.inject(0) do |acc, (el,i)|
    [acc, el.to_i + max_starting[i+2].to_i].max
  end
end
0

All Articles