-
Notifications
You must be signed in to change notification settings - Fork 7
/
Numbers in PI.py
64 lines (55 loc) · 2.59 KB
/
Numbers in PI.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
"""
Numbers In Pi ☆
Given a string representation of the first n digits of Pi and a list of positive integers (all in string format),
write a function that returns the smallest
number of spaces that can be added to the n digits of Pi such that all resulting numbers are found in the list of integers.
Not e that a single number can appear multiple times in the resulting numbers. For example, if Pi is "3141" and the numbers are
["1", "3", "4"] , the number "1" is allowed to appear twice in the list of resulting numbers after three spaces are added:
"3 | 1 | 4 | 1"
If no number of spaces to be added exists such that all resulting numbers are found in the list of integers, the function should return -1.
Sample Input
pi = "3141592653589793238462643383279",
numbers = ["314159265358979323846", "26433", "8", "3279", "314159265", "35897932384626433832", "79"]
Sample Output
2 // "314159265 | 35897932384626433832 | 79"
"""
# SOLUTION 1
# O(n^3 + m) time | O(n + m) space - where n is the number of digits in Pi and m is the number of favorite numbers
def numbersInPi(pi, numbers):
numbersTable = {number: True for number in numbers}
minSpaces = getMinSpaces (pi, numbersTable, {}, 0)
return -1 if minSpaces == float("inf") else minSpaces
def getMinSpaces(pi, numbersTable, cache, idx):
if idx == len(pi):
return -1
if idx in cache:
return cache[idx]
minSpaces = float("inf")
for i in range(idx, len(pi)):
prefix = pi[idx : i + 1]
if prefix in numbersTable:
minSpacesInSuffix = getMinSpaces(pi, numbersTable, cache, i + 1)
minSpaces = min(minSpaces, minSpacesInSuffix + 1)
cache[idx] = minSpaces
return cache[idx]
# SOLUTION 2
# O(n^3 + m) time | O(n + m) space where n is the number of digits in Pi and m is the number of favorite numbers
def numbersInPi(pi, numbers):
numbersTable = {number: True for number in numbers}
cache = {}
for i in reversed(range(len(pi))):
getMinSpaces(pi, numbersTable, cache, i)
return -1 if cache[0] == float("inf") else cache[0]
def getMinSpaces(pi, numbersTable, cache, idx):
if idx == len(pi):
return -1
if idx in cache:
return cache[idx]
minSpaces = float("inf")
for i in range(idx, len(pi)):
prefix = pi[idx : i + 1]
if prefix in numbersTable:
minSpacesInSuffix = getMinSpaces(pi, numbersTable, cache, i + 1)
minSpaces = min(minSpaces, minSpacesInSuffix + 1)
cache[idx] = minSpaces
return cache[idx]