-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sliding Subarray Beauty.py
50 lines (38 loc) · 1.59 KB
/
Sliding Subarray Beauty.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
'''
Given an integer array nums containing n integers, find the beauty of each subarray of size k.
The beauty of a subarray is the xth smallest integer in the subarray if it is negative, or 0 if there are fewer than x negative integers.
Return an integer array containing n - k + 1 integers, which denote the beauty of the subarrays in order from the first index in the array.
A subarray is a contiguous non-empty sequence of elements within an array.
'''
class Solution:
def find_x_smallest(self, count_map, x):
count = 0
for num in range(-50, 0, 1):
if count_map.get(num, 0) != 0:
count += count_map[num]
if count >= x:
return num
return 0
def getSubarrayBeauty(self, nums: List[int], k: int, x: int) -> List[int]:
start = 0
neg = 0
count_map = {}
ans = []
for end in range(len(nums)):
# Remove contribution of start element if its negative.
if start > len(nums)-k+1:
break
if nums[end] < 0:
neg += 1
count_map[nums[end]] = count_map.get(nums[end], 0) + 1
window_size = end - start + 1
if window_size == k:
temp = 0
if neg >= x:
temp = self.find_x_smallest(count_map, x)
if nums[start] < 0:
neg -= 1
count_map[nums[start]] -= 1
start += 1
ans.append(temp)
return ans