gpt4 book ai didi

Maximum sum of contiguous sub-sequence with length at most k(长度不超过k的邻接子序列的最大和)

转载 作者:bug小助手 更新时间:2023-10-25 14:10:36 24 4
gpt4 key购买 nike



I am trying to modify the Kadane Algorithm in order to solve a more specific problem.

我正在尝试修改Kadane算法,以解决更具体的问题。



def max_Sum(arr, length, k):
if length < k:
print('length of array should be greater than k')

res = 0
for i in range(0, k):
res += arr[i]

current_sum = res

for i in range(k, n):
if k == n:
for j in range(0, k-1):
res += arr[j]
current_sum = res
else:
current_sum += arr[i] - arr[i-k]
print(res, current_sum)
res = max(res, current_sum)

return res


This is the code for the maximum subarray problem. What I want to do is find the maximum subarray with length at most K.

这是最大子数组问题的代码。我要做的是找出最大长度为K的子数组。



Example: We have an array A = [3,-5 1 2,-1 4,-3 1,-2] and we want to find the maximum subarray of length at most K = 9. Length of subarray should not be restricted at K, if there is another length L < K that provides a greater sum, the algorithm should return sum[:L].

例如:我们有一个数组A=[3,-5 1 2,-1 4,-3 1,-2],我们希望找到最大长度为K=9的子数组。子数组的长度不应限制在K,如果存在另一个长度L



In this case, the algorithm will return 0. It should return 6, following the sum of A[2:5].

在这种情况下,算法将返回0。它应该返回6,跟在A[2:5]的和之后。


更多回答
优秀答案推荐

Well, a solution that works in O(n * K) is to use sliding windows for every possible length <= K. I have tried to find a O(n) correct solution modifying Kadane, but I couldn't.

嗯,在O(n*K)中有效的一个解决方案是对每个可能的长度使用滑动窗口<=K。我试图找到修改Kadane的O(N)个正确的解决方案,但我做不到。



def best_array_fixed_k( arr, length, k ):
total_sum = 0
best = 0
for x in xrange( 0, length ):
total_sum = total_sum + arr[x]
if x >= k:
total_sum = total_sum - arr[x - k]
if x >= k - 1:
best = max( best, total_sum )
# this makes sure that we are considering a window with length = k
return best

def max_sum( arr, length, k):
best = 0
for x in xrange( 1, k + 1):
best = max( best, best_array_for_fixed_k(arr, length, x ) )
return best


For those who are seeking an answer in O(n), you can easily adapt the answer to this question over at CS StackExchange. The answer solves the same problem in O(n) where subsequence's length must be in a given range. For this problem, just set the range to [0, k].

对于那些在O(n)中寻求答案的人,你可以在CS StackExchange中轻松地调整这个问题的答案。在O(n)的情况下,时间复杂度为O(n)。对于这个问题,只需将范围设置为[0,k]。



Solution in java with time complexity : O(n*k)

时间复杂度为O(n*k)的Java语言解决方案


public static int maxSumforFixedSizeK(int[] arr, int k){

// Using simple window sliding technique
int best_sum;
int curr_sum=0;

for( int i=0;i<k;i++)
curr_sum += arr[i];

best_sum = curr_sum;

for( int i=k; i<arr.length; i++) {
curr_sum += (arr[i] - arr[i - k]);
best_sum = Math.max(best_sum, curr_sum);
}

return best_sum;
}

public static int maxSumforSizeAtMostK(int[] arr, int k){
int best_sum = Integer.MIN_VALUE;

// Calculate maximum sum for every window size in interval [1,k]
for( int i=1; i<=k; i++ )
best_sum = Math.max( best_sum, maxSumforFixedSizeK(arr, i) );

return best_sum;
}

更多回答

Exactly what I was looking for!! Thank you!

这正是我要找的!!谢谢!

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com