-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathminimum-number-of-taps-to-open-to-water-a-garden.py
More file actions
42 lines (30 loc) · 1.19 KB
/
minimum-number-of-taps-to-open-to-water-a-garden.py
File metadata and controls
42 lines (30 loc) · 1.19 KB
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
from functools import cache
from typing import List, Tuple
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
taps = n + 1
intervals: List[Tuple[int, int]] = []
for tap in range(taps):
intervals.append((tap - ranges[tap], tap + ranges[tap]))
intervals.sort()
# Dummy interval outside the garden
intervals.append((n, n + 1))
@cache
def dp(interval: int) -> int:
if interval == taps:
# Hit the final dummy interval
return 0
min_taps = taps + 1
for next_interval in range(interval + 1, len(intervals)):
if intervals[interval][1] >= intervals[next_interval][0]:
# Take
min_taps = min(min_taps, dp(next_interval) + 1)
else:
# Skip this and everything after, as there is no overlap
break
return min_taps
min_taps = taps + 1
for interval in range(taps):
if intervals[interval][0] <= 0:
min_taps = min(min_taps, dp(interval))
return min_taps if min_taps < taps + 1 else -1