上贴想法的 Python 实现
class ListNode(object):
def __init__(self, c):
self.c = c
self.next = None
self.prev = None
def max_count(lst):
# 返回最大count
if len(lst) <= 1:
return 0
lst.sort()
# 创建链表
head = node = ListNode(0)
pre_num = None
for n in lst:
if pre_num == n:
node.c += 1
else:
nnode = ListNode(1)
nnode.prev = node
node.next = nnode
node = nnode
pre_num = n
count = 0
while head.next:
thead = head.next
ncount = 0
while thead:
ncount += 1
thead.c -= 1
nnode = thead.next
del_node(thead)
thead = nnode
count += ncount - 1
return count
def del_node(tmp):
if tmp.c == 0:
tmp.prev.next = tmp.next
if tmp.next:
tmp.next.prev = tmp.prev