Skip to content

Commit e646e7f

Browse files
authored
Update minimum-number-of-days-to-eat-n-oranges.py
1 parent 87c4a0d commit e646e7f

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

Python/minimum-number-of-days-to-eat-n-oranges.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,28 @@ def memoization(lookup, i):
1818

1919
lookup = {}
2020
return memoization(lookup, n)
21+
22+
23+
# Time: O((logn)^2)
24+
# Space: O((logn)^2)
25+
class Solution2(object):
26+
def minDays(self, n):
27+
result = 0
28+
q, lookup = [n], set([n])
29+
while q:
30+
new_q = []
31+
for i in q:
32+
if not i:
33+
return result
34+
if i-1 not in lookup:
35+
lookup.add(i-1)
36+
new_q.append(i-1)
37+
if i%2 == 0 and i//2 not in lookup:
38+
lookup.add(i//2)
39+
new_q.append(i//2)
40+
if i%3 == 0 and i//3 not in lookup:
41+
lookup.add(i//3)
42+
new_q.append(i//3)
43+
result += 1
44+
q = new_q
45+
return result

0 commit comments

Comments
 (0)