-
Notifications
You must be signed in to change notification settings - Fork 39
/
0202_Problem_1.py
47 lines (36 loc) · 1.18 KB
/
0202_Problem_1.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
# code_report Solution
# Problem Link (Contest): https://leetcode.com/contest/weekly-contest-202/problems/three-consecutive-odds/
# Problem Link (Practice): https://leetcode.com/problems/three-consecutive-odds/
# Note this problem is very similar to MCO (Max Consecutive Ones)
# Solution 1 (awful code)
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
i, ans = 0, 0
for e in arr:
if e % 2 == 1:
i += 1
ans = max(ans, i)
else:
i = 0
return ans >= 3
# Solution 2
def group(l):
return [list(x) for _, x in itertools.groupby(l)]
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
return max(map(sum, group(map(lambda x: x % 2, arr)))) >= 3
# Wishful thinking (this syntax doesn't work)
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
return arr
|> map(lambda x: x % 2)
|> group
|> map(sum)
|> max
>= 3
# Solution #3 (closest we can get)
def group(l):
return [list(x) for _, x in itertools.groupby(l)]
def threeConsecutiveOdds(self, arr: List[int]) -> bool:
a = map(lambda x: x % 2, arr)
b = group(a)
c = map(b, sum)
d = max(c)
return d >= 3