Problem Description
There is an array A
of N
integers and three tiles. Each tile can cover two neighboring numbers from the array but cannot intersect with another tile. It also cannot be placed outside the array, even partially.
Write a function:
def solution(A)
that, given an array A
of N
integers, returns the maximum sum of numbers that can be covered using at most three tiles.
Examples
Example 1:
Input:A = [2, 3, 5, 2, 3, 4, 6, 4, 1]
Output:25
Explanation:
There is only one optimal placement of tiles: (3, 5), (3, 4), (6, 4).
Example 2:
Input:A = [1, 5, 3, 2, 6, 6, 10, 4, 7, 2, 1]
Output:35
Explanation:
One of the three optimal placements of tiles is (5, 3), (6, 10), (4, 7).
Example 3:
Input:A = [1, 2, 3, 3, 2]
Output:10
Explanation:
There is one optimal placement of tiles: (2, 3), (3, 2). Only two tiles can be used because A
is too small to contain another one.
Example 4:
Input:A = [5, 10, 3]
Output:15
Explanation:
Only one tile can be used.
Problem Description
There is an array A
of N
integers, which may contain both positive and negative values. You need to choose two fragments:
- One of length
K
- One of length
L
The goal is to maximize the total sum of the elements that belong to the two fragments.
If any individual element belongs to both fragments, it is added to the final sum only once with its sign changed (positive values become negative, and vice versa).
Write a function:
def solution(A, K, L)
that, given an array A
of N
integers and integers K
and L
, returns the maximum total sum that can be obtained.
Examples
Example 1:
Input:A = [1, 3, -4, 2, -1]
, K = 3
, L = 2
Output:10
Explanation:
For A = [1, 3, -4, 2, -1]
, K = 3
, and L = 2
, you can choose the fragment [1, 3, -4]
and the fragment [-4, 2]
.
The third element of A
belongs to both fragments, so its sign is changed in the final sum.
Final sum:1 + 3 + -(-4) + 2 = 10
.
Example 2:
Input:A = [-5, -3, -4]
, K = 1
, L = 3
Output:-2
Explanation:
For A = [-5, -3, -4]
, K = 1
, and L = 3
, the segment of length L
will contain each element of A
.
Choosing -5
as the only element of the K
segment will change its sign in the final sum.
Final sum:-3 + -4 + -(-5) = -2
.
我们长期稳定承接各大科技公司如TikTok、Google、Amazon等的OA笔试代写服务,确保满分通过。如有需求,请随时联系我们。
We consistently provide professional online assessment services for major tech companies like TikTok, Google, and Amazon, guaranteeing perfect scores. Feel free to contact us if you're interested.