forked from netsetos/python_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinimum Distance
More file actions
25 lines (21 loc) · 810 Bytes
/
Minimum Distance
File metadata and controls
25 lines (21 loc) · 810 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def mindist(x,m,p): #Time Complexity - O(n2)
n=len(x)
min_dis=9999
for i in range(n):
for j in range(i+1,n):
if m==x[i] and p==x[j] or \
m==x[j] and p==x[i] and abs(j - i) > min_dis:
min_dis = abs(j - i)
return min_dis
def min_distance(x,a,b): #Time Complexity - O(n)
n=len(x)
last = -1
min_dis = 999
for i in range(n):
if x[i] == a or x[i] == b:
if last != -1:
min_dis=min(min_dis,i-last)
last=i #for updating 1st number's index
return min_dis
x=[1,3,5,7,9,2,4,6,8]
print(min_distance(x,5,8))