Codility_MaxDoubleSliceSum

functionMan·2024년 8월 28일

Codility

목록 보기
27/32
post-thumbnail

9-3. MaxDoubleSliceSum

A non-empty array A consisting of N integers is given.

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

The sum of double slice (X, Y, Z) is the total of A[X + 1] + A[X + 2] + ... + A[Y − 1] + A[Y + 1] + A[Y + 2] + ... + A[Z − 1].

For example, 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 example double slices:

double slice (0, 3, 6), sum is 2 + 6 + 4 + 5 = 17,
double slice (0, 3, 7), sum is 2 + 6 + 4 + 5 − 1 = 16,
double slice (3, 4, 5), sum is 0.
The goal is to find the maximal sum of any double slice.

Write a function:

class Solution { public int solution(int[] A); }

that, given a non-empty array A consisting of N integers, returns the maximal sum of any double slice.

For example, given:

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

the function should return 17, because no double slice of array A has a sum of greater than 17.

Write an efficient algorithm for the following assumptions:

N is an integer within the range [3..100,000];
each element of array A is an integer within the range [−10,000..10,000].

비어 있지 않은 N개의 정수로 구성된 배열 A가 주어집니다.

0 ≤ X < Y < Z < N을 만족하는 세 개의 요소 (X, Y, Z)를 더블 슬라이스라고 합니다.

더블 슬라이스 (X, Y, Z)의 합은 A[X + 1] + A[X + 2] + … + A[Y − 1] + A[Y + 1] + A[Y + 2] + … + A[Z − 1]의 총합입니다.

예를 들어, 배열 A가 다음과 같다면:

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

다음과 같은 예시 더블 슬라이스가 포함됩니다:

더블 슬라이스 (0, 3, 6), 합은 2 + 6 + 4 + 5 = 17,
더블 슬라이스 (0, 3, 7), 합은 2 + 6 + 4 + 5 − 1 = 16,
더블 슬라이스 (3, 4, 5), 합은 0.
목표는 모든 더블 슬라이스 중 최대 합을 찾는 것입니다.

다음과 같은 함수를 작성하세요:

class Solution { 
    public int solution(int[] A); 
}

비어 있지 않은 N개의 정수로 구성된 배열 A가 주어지면, 최대 합을 가지는 더블 슬라이스의 합을 반환하는 함수를 작성하세요.

예를 들어, 주어진 배열이 다음과 같다면:

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…100,000] 범위 내의 정수입니다.
배열 A의 각 요소는 [−10,000…10,000] 범위 내의 정수입니다.

문제풀이

class Solution {
    public int solution(int[] A) {
        int N = A.length;
        int[] maxEnd = new int[N];
        int[] maxStart = new int[N];
        
        for (int i = 1; i < N - 1; i++) {
            maxEnd[i] = Math.max(0, maxEnd[i - 1] + A[i]);
        }
        
        for (int i = N - 2; i > 0; i--) {
            maxStart[i] = Math.max(0, maxStart[i + 1] + A[i]);
        }

        int maxSlice = 0;
        for (int i = 1; i < N - 1; i++) {
            maxSlice = Math.max(maxSlice, maxEnd[i - 1] + maxStart[i + 1]);
        }
        return maxSlice;
    }
}

2개의 배열을 생성:
끝점부터 중간점 사이의 값의 합을 저장할 배열01
시작점부터 중간점 사이의 값의 합을 저장할 배열02

반복문으로 해당 배열에 계산 값을 저장해줌

ex) A(0,3,6)일 경우
배열01 -> A[1,2]를 더한값을 배열로 저장
배열02 -> A[4,5]를 더한값을 배열로 저장,

배열에 다 저장이 되면

다시 반복문을 통해 지점이 변경되었을때
배열1과 배열2의 합을 maxSlice라는 변수에 담고 Math.max를 통해
해당 값을 업데이트하여 리턴값을 계산

제출결과

문제풀어보기 -> https://app.codility.com/programmers/lessons/9-maximum_slice_problem/max_double_slice_sum/

profile
functionMan

0개의 댓글