## Similar Problems

Similar Problems not available

# Maximize Win From Two Segments - Leetcode Solution

## Companies:

LeetCode: Maximize Win From Two Segments Leetcode Solution

Difficulty: Medium

Topics: sliding-window binary-search array

The Maximize Win From Two Segments problem on LeetCode requires us to find the maximum point value that can be obtained by selecting two non-overlapping segments from a given array of integers. The segments can be of any length, but their indices must not overlap.

Let's first understand the problem statement with an example. Suppose we have an array `[2, 3, 1, 6, 7]`

and we are allowed to select two non-overlapping segments from it. The segments can be of any length and their indices must not overlap. The possible combinations of two segments are:

`[2, 3]`

and`[1, 6, 7]`

, with a total point value of 2+3+1+6+7 = 19.`[2, 3, 1]`

and`[6, 7]`

, with a total point value of 2+3+1+6+7 = 19.`[2, 3, 1, 6]`

and`[7]`

, with a total point value of 2+3+1+6+7 = 19.`[2]`

and`[3, 1, 6, 7]`

, with a total point value of 2+3+1+6+7 = 19.`[2, 3]`

and`[6, 7]`

, with a total point value of 2+3+6+7 = 18.`[2]`

and`[3, 1, 6]`

, with a total point value of 2+3+1+6 = 12.`[2]`

and`[3, 1]`

, with a total point value of 2+3+1 = 6.`[2]`

and`[1, 6]`

, with a total point value of 2+1+6 = 9.`[2]`

and`[6, 7]`

, with a total point value of 2+6+7 = 15.`[3, 1]`

and`[6, 7]`

, with a total point value of 3+1+6+7 = 17.

Therefore, the maximum point value that can be obtained by selecting two non-overlapping segments from the given array is 19.

Now, let's see how we can solve this problem. One approach is to use dynamic programming. We can maintain two 2D tables `dp1`

and `dp2`

, where `dp1[i][j]`

represents the maximum point value that can be obtained by selecting a segment from indices `0`

to `i`

and a segment from indices `i+1`

to `j`

, and `dp2[i][j]`

represents the maximum point value that can be obtained by selecting a segment from indices `i`

to `j`

and a segment from indices `j+1`

to `n-1`

, where `n`

is the length of the input array.

To fill in the tables `dp1`

and `dp2`

, we can use the following recurrence relations:

```
dp1[i][j] = max(dp1[i][j-1], dp1[i-1][j], dp2[i][j-1]) + arr[j]
dp2[i][j] = max(dp2[i][j+1], dp2[i+1][j], dp1[i+1][j]) + arr[i]
```

The base cases for `dp1`

and `dp2`

are when the length of the segment is `1`

, i.e., `dp1[i][i] = dp2[i][i] = arr[i]`

.

Once we fill in the tables `dp1`

and `dp2`

, we can iterate over all possible combinations of non-overlapping segments and calculate their total point value as `dp1[i][j] + dp2[i][j+1]`

, where `i`

is the starting index of the first segment and `j`

is the ending index of the first segment.

Finally, we return the maximum total point value obtained across all possible combinations of non-overlapping segments.

The time complexity of this approach is O(n^2), where n is the length of the input array, as we need to fill in the tables `dp1`

and `dp2`

. The space complexity is also O(n^2), as we need to maintain two tables of size n x n.

## Maximize Win From Two Segments Solution Code

`1`