-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q2.py
28 lines (21 loc) · 928 Bytes
/
Q2.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
def calculate_blankets_needed(n, d, animals):
# Sort the sleeping positions of animals
animals.sort()
# Initialize the number of blankets needed
blankets_needed = 1
# Initialize the rightmost position covered by the first blanket
rightmost_covered = animals[0] + d
# Iterate over the sleeping positions of animals
for i in range(1, n):
# If the animal's sleeping position is not covered by the current blanket
if animals[i] > rightmost_covered:
# Increment the number of blankets needed
blankets_needed += 1
# Update the rightmost position covered by the new blanket
rightmost_covered = animals[i] + d
return blankets_needed
# Read input
n, d = map(int, input().split())
animals = list(map(int, input().split()))
# Calculate and print the minimum number of blankets needed
print(calculate_blankets_needed(n, d, animals))