-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
67 lines (53 loc) · 1.64 KB
/
solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import bisect
#
# Brute force solution:
# For each window with "k" elements, sort the window and return the median.
# Time complexity: O(n*klog(k))
# Space complexity: O(n) (for the output array)
#
#
# Optimized solution:
# Initialize the window with the "k" first elements using bisect insert to place the element while keeping the
# window sorted. For every new window, remove the oldest element and add the new one, using bisect insert.
# Return the median of each window as usual
# Time complexity: O(n*k)
# Space complexity: O(n) (for the output array)
#
# Time complexity: O(n*k) (n = number of elements in the array)
# Space complexity: O(n) (for the output buffer)
#
def subarr_mean(arr, k):
# Populate the buffer with the first "k" elements of the array,
# adding them in order, to keep the array sorted: O(k)
buffer = []
for i in range(0, k):
bisect.insort(buffer, arr[i])
oldest = arr[0]
output = [median(buffer, k)]
for i in range(k, len(arr)):
# Remove the oldest value: O(k)
buffer.remove(oldest)
# Insert the new value on the right place, in
# order to keep the buffer sorted: O(k)
bisect.insort(buffer, arr[i])
# Add the median to the output array: O(1)
output.append(median(buffer, k))
# Update the oldest value: O(1)
oldest = arr[i - k + 1]
return output
#
# Time complexity: O(1)
# Space complexity: O(1)
#
def median(subarr, k):
"""
Return the median of the subarray based on the value ok "k"
"""
even_k = (k % 2 == 0)
if even_k:
right = k / 2
left = right - 1
return (subarr[left] + subarr[right]) / 2
else:
id = k // 2
return subarr[id]