-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCityTour.py
55 lines (40 loc) · 1.48 KB
/
CityTour.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
# https://www.interviewbit.com/problems/city-tour/
p = 1000000007
def divideModP(a, b):
return a * pow(b, p - 2, p)
def getC(facts, m, n):
res = divideModP(facts[m], facts[m - n]) % p
return divideModP(res, facts[n]) % p
def fillFactorials(facts):
for i in range(2, len(facts)):
facts[i] = (facts[i - 1] * i) % p
class Solution:
# @param A : integer
# @param B : list of integers
# @return an integer
def solve(self, A, B):
facts = [1] * (A + 1)
fillFactorials(facts)
result = 1
B.sort()
gaps = []
unvisitedCount = 0
for i in range(1, len(B)):
if B[i] != B[i - 1] + 1:
gaps.append((B[i - 1], B[i]))
unvisitedCount += (B[i] - B[i - 1] - 1)
if B[0] != 1:
unvisitedCount += B[0] - 1
if B[-1] != A:
unvisitedCount += A - B[-1]
if B[0] != 1:
result = (result * getC(facts, unvisitedCount, B[0] - 1)) % p
unvisitedCount -= (B[0] - 1)
if B[-1] != A:
result = (result * getC(facts, unvisitedCount, A - B[-1])) % p
unvisitedCount -= (A - B[-1])
for gap in gaps:
result = (result * pow(2, gap[1] - gap[0] - 2, p)) % p
result = (result * getC(facts, unvisitedCount, gap[1] - gap[0] - 1)) % p
unvisitedCount -= (gap[1] - gap[0] - 1)
return result