-
Notifications
You must be signed in to change notification settings - Fork 0
/
5442_avoid_flood.py
147 lines (136 loc) · 5.1 KB
/
5442_avoid_flood.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
'''
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake.
Given an integer array rains where:
rains[i] > 0 means there will be rains over the rains[i] lake.
rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it.
Return an array ans where:
ans.length == rains.length
ans[i] == -1 if rains[i] > 0.
ans[i] is the lake you choose to dry in the ith day if rains[i] == 0.
If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array.
Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)
Example 1:
Input: rains = [1,2,3,4]
Output: [-1,-1,-1,-1]
Explanation: After the first day full lakes are [1]
After the second day full lakes are [1,2]
After the third day full lakes are [1,2,3]
After the fourth day full lakes are [1,2,3,4]
There's no day to dry any lake and there is no flood in any lake.
Example 2:
Input: rains = [1,2,0,0,2,1]
Output: [-1,-1,2,1,-1,-1]
Explanation: After the first day full lakes are [1]
After the second day full lakes are [1,2]
After the third day, we dry lake 2. Full lakes are [1]
After the fourth day, we dry lake 1. There is no full lakes.
After the fifth day, full lakes are [2].
After the sixth day, full lakes are [1,2].
It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario.
Example 3:
Input: rains = [1,2,0,1,2]
Output: []
Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day.
After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood.
Example 4:
Input: rains = [69,0,0,0,69]
Output: [-1,69,1,1,-1]
Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9
Example 5:
Input: rains = [10,20,20]
Output: []
Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.
Constraints:
1 <= rains.length <= 10^5
0 <= rains[i] <= 10^9
'''
class Solution:
def sub(self, rains):
res = []
hs = dict()
slow, fast = 0, 0
while fast < len(rains):
r = rains[fast]
if r != 0:
if r in hs:
while True:
if slow >= fast:
return []
r2 = rains[slow]
print(r,slow,r2)
if r2 == 0:
if slow > hs[r]:
res.append(r)
slow += 1
break
else:
res.append(1)
else:
res.append(-1)
slow += 1
hs[r] = fast
fast += 1
# print(slow,res)
while slow < len(rains):
r = rains[slow]
num = 1 if r == 0 else -1
res.append(num)
slow += 1
return res
def avoidFloodOLD(self, rains):
res = self.sub(rains)
if len(res) > 0:
return res
# res = self.sub(rains[::-1])
# if len(res) > 0:
# return res[::-1]
return []
def avoidFlood(self, rains):
dry_lands = []
last_flood = dict()
res = []
for i,r in enumerate(rains):
if r == 0:
dry_lands.append(i)
res.append(1)
else:
if r in last_flood:
print(i,r,dry_lands,last_flood,res)
dried = False
for j,dl in enumerate(dry_lands):
if dl > last_flood[r]:
res[dl] = r
last_flood[r] = i
del dry_lands[j]
dried = True
break
if not dried:
return []
else:
last_flood[r] = i
res.append(-1)
return res
s = Solution()
# rains = [1,2,3,4]
# assert s.avoidFlood(rains) == [-1,-1,-1,-1]
# rains = [1,2,0,0,2,1]
# print(s.avoidFlood(rains))
# assert s.avoidFlood(rains) == [-1,-1,2,1,-1,-1]
# rains = [1,2,0,1,2]
# assert s.avoidFlood(rains) == []
# # rains = [69,0,0,0,69]
# # assert s.avoidFlood(rains) == []
# rains = [10,20,20]
# assert s.avoidFlood(rains) == []
# rains = [0,1,1]
# assert s.avoidFlood(rains) == []
# rains = [1,0,2,0,2,1]
# rains = [1,0,2,0,1,2]
# rains = [1,2,0,2,3,0,1]
# assert s.avoidFlood(rains) == [-1,-1,2,-1,-1,1,-1]
# rains = [1,2,0,2,3,0,1][::-1]
# assert s.avoidFlood(rains) == [-1,-1,2,-1,-1,1,-1][::-1]
# rains = [1,1,0,0]
# assert s.avoidFlood(rains) == []
rains = [2,3,0,0,3,1,0,1,0,2,2]
print(s.avoidFlood(rains))