-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAirbnb.py
48 lines (36 loc) · 1.19 KB
/
Airbnb.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
def reverse(aString):
aList = [c for c in aString]
i = 0
j = len(aString)-1
while (i <= j):
aList[i], aList[j] = aList[j], aList[i]
i+=1
j-=1
return ''.join(aList)
# print(reverse('asd'))
def findMissing(aList, aListMissingOne):
"""
Ex)
Given aList := [4,12,9,5,6] and aListMissingOne := [4,9,12,6]
Find the missing element
Return 5
IDEA:
Brute force! Examine all tuples.
Time: O(n*m) and Space: O(1)
Smarter: Use a hashMap, instead!
Time: O(n+m) and Space: O(m)
"""
assert len(aList)-len(aListMissingOne) == 1
# n = len(aList)
# m = len(aListMissingOne)
# for i in range(n):
# for j in range(m):
# if (aList[i] == aListMissingOne[j]): break
# if (j == m-1): return aList[i]
_dict = {}
for i in range(len(aListMissingOne)): # O(len(aListMissingOne) + len(aList))
_dict[aListMissingOne[i]] = aListMissingOne[i]
for j in range(len(aList)):
if (aList[j] not in _dict): return aList[j]
return 'lists are permutations of each other'
print(findMissing([4,12,9,6],[4,9,12]))