用于查询的 Python3 程序在旋转数组中查找给定长度的最大和连续子数组
给定一个包含N个整数的数组arr[]和以下两种类型的{X, Y}形式的Q个查询:
- 如果X = 1 ,将给定数组向左旋转Y个位置。
- 如果X = 2 ,则在数组的当前状态下打印长度为Y的最大和子数组。
例子:
Input: N = 5, arr[] = {1, 2, 3, 4, 5}, Q = 2, Query[][] = {{1, 2}, {2, 3}}
Output:
Query 1: 3 4 5 1 2
Query 2: 12
Explanation:
Query 1: Shift array to the left 2 times: {1, 2, 3, 4, 5} -> {2, 3, 4, 5, 1} -> {3, 4, 5, 1, 2}
Query 2: Maximum sum subarray of length 3 is {3, 4, 5} and the sum is 12
Input: N = 5, arr[] = {3, 4, 5, 1, 2}, Q = 3, Query[][] = {{1, 3}, {1, 1}, {2, 4}}
Output:
Query 1: 1 2 3 4 5
Query 2: 2 3 4 5 1
Query 3: 14
Explanation:
Query 1: Shift array to the left 3 times: {3, 4, 5, 1, 2} -> {4, 5, 1, 2, 3} -> {5, 1, 2, 3, 4} -> {1, 2, 3, 4, 5}
Query 2: Shift array to the left 1 time: {1, 2, 3, 4, 5} -> {2, 3, 4, 5, 1}
Query 3: Maximum sum subarray of length 4 is {2, 3, 4, 5} and sum is 14
天真的方法:最简单的方法是通过将元素一个一个移动到距离Y来旋转数组,查询类型为 1 ,并生成长度为 Y 的所有子数组的总和,如果查询类型为 2 ,则打印最大总和.
时间复杂度: O(Q*N*Y)
辅助空间: O(N)
有效方法:为了优化上述方法,我们的想法是使用杂耍算法进行数组旋转并找到长度为Y的最大和子数组,使用滑动窗口技术。请按照以下步骤解决问题:
- 如果X = 1 ,使用杂耍算法将数组旋转Y 。
- 否则,如果X = 2 ,则使用滑动窗口技术找到长度为 Y 的最大和子数组。
- 如果查询X为1 ,则打印数组。
- 否则,打印大小为Y的最大和子数组。
下面是上述方法的实现:
Python3
# Python3 program for the above approach
# Function to calculate the maximum
# sum of length k
def MaxSum(arr, n, k):
i, max_sum = 0, 0
sum = 0
# Calculating the max sum for
# the first k elements
while i < k:
sum += arr[i]
i += 1
max_sum = sum
# Find subarray with maximum sum
while (i < n):
# Update the sum
sum = sum - arr[i - k] + arr[i]
if (max_sum < sum):
max_sum = sum
i += 1
# Return maximum sum
return max_sum
# Function to calculate gcd of the
# two numbers n1 and n2
def gcd(n1, n2):
# Base Case
if (n2 == 0):
return n1
# Recursively find the GCD
else:
return gcd(n2, n1 % n2)
# Function to rotate the array by Y
def RotateArr(arr, n, d):
# For handling k >= N
i = 0
j = 0
d = d % n
# Dividing the array into
# number of sets
no_of_sets = gcd(d, n)
for i in range(no_of_sets):
temp = arr[i]
j = i
# Rotate the array by Y
while (True):
k = j + d
if (k >= n):
k = k - n
if (k == i):
break
arr[j] = arr[k]
j = k
# Update arr[j]
arr[j] = temp
# Return the rotated array
return arr
# Function that performs the queries
# on the given array
def performQuery(arr, Q, q):
N = len(arr)
# Traverse each query
for i in range(q):
# If query of type X = 1
if (Q[i][0] == 1):
arr = RotateArr(arr, N, Q[i][1])
# Print the array
for t in arr:
print(t, end = " ")
print()
# If query of type X = 2
else:
print(MaxSum(arr, N, Q[i][1]))
# Driver Code
if __name__ == '__main__':
# Given array arr[]
arr = [ 1, 2, 3, 4, 5 ]
q = 5
# Given Queries
Q = [ [ 1, 2 ], [ 2, 3 ],
[ 1, 3 ], [ 1, 1 ],
[ 2, 4 ] ]
# Function call
performQuery(arr, Q, q)
# This code is contributed by mohit kumar 29
3 4 5 1 2
12
1 2 3 4 5
2 3 4 5 1
14
时间复杂度: O(Q*N),其中 Q 是查询数, N 是给定数组的大小。
辅助空间: O(N)
请参阅有关查询的完整文章以在旋转数组中查找给定长度的最大和连续子数组以获取更多详细信息!