-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFinding-Maximum-Permutation.py
59 lines (48 loc) · 1.02 KB
/
Finding-Maximum-Permutation.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
# !/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Finding a Maximum Permutation
"""
__author__ = "prakashsellathurai"
__copyright__ = "Copyright 2021"
__version__ = "1.0.1"
__email__ = "prakashsellathurai@gmail.com"
def naive_max_perm(M, A=None):
"""
>>> M = [2, 2, 0, 5, 3, 5, 7, 4]
>>> print(naive_max_perm(M))
{0, 2, 5}
"""
if A is None:
A = set(range(len(M)))
if len(A) == 1:
return A
B = {M[i] for i in A}
C = A - B
if C:
A.remove(C.pop())
return naive_max_perm(M, A)
return A
def max_perm(M):
"""
>>> M = [2, 2, 0, 5, 3, 5, 7, 4]
>>> print(max_perm(M))
{0, 2, 5}
"""
n = len(M)
A = set(range(n))
count = [0] * n
for i in M:
count[i] += 1
Q = [i for i in A if count[i] == 0]
while Q:
i = Q.pop()
A.remove(i)
j = M[i]
count[j] -= 1
if count[j] == 0:
Q.append(j)
return A
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=1)