|
|
|
@ -1,6 +1,5 @@
|
|
|
|
|
import collections.abc
|
|
|
|
|
import copy
|
|
|
|
|
import itertools
|
|
|
|
|
import math
|
|
|
|
|
import random
|
|
|
|
|
import typing
|
|
|
|
|
|
|
|
|
@ -581,3 +580,48 @@ def accum_sub_array_maxes(nums: list[int]) -> list[int]:
|
|
|
|
|
accum.append(max(nums[i], prev))
|
|
|
|
|
|
|
|
|
|
return accum
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def neighborly_node_from_list(inlist: list[list[int]]):
|
|
|
|
|
# Alias "Node" type for leetcode compat
|
|
|
|
|
Node = stdlib.NeighborlyNodeNicely
|
|
|
|
|
|
|
|
|
|
if len(inlist) == 0:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
outlist = [Node(i + 1, []) for i in range(len(inlist))]
|
|
|
|
|
|
|
|
|
|
for i in range(len(inlist)):
|
|
|
|
|
outlist[i].neighbors[:] = []
|
|
|
|
|
|
|
|
|
|
for neighbor_val in inlist[i]:
|
|
|
|
|
outlist[i].neighbors.append(outlist[neighbor_val - 1])
|
|
|
|
|
|
|
|
|
|
return outlist[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def neighborly_node_to_list(node) -> list[list[int]]:
|
|
|
|
|
serialized: dict[int, list[int]] = {}
|
|
|
|
|
|
|
|
|
|
for cur in traverse_neighborly_node(node, serialized):
|
|
|
|
|
if cur is None:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
serialized[cur.val] = [n.val for n in cur.neighbors]
|
|
|
|
|
|
|
|
|
|
return [v for _, v in sorted(serialized.items())]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def traverse_neighborly_node(
|
|
|
|
|
node: stdlib.NeighborlyNodeNicely, memo: collections.abc.Container[int]
|
|
|
|
|
) -> typing.Iterator[stdlib.NeighborlyNodeNicely | None]:
|
|
|
|
|
yield node
|
|
|
|
|
|
|
|
|
|
if node is None:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
for neighbor in node.neighbors:
|
|
|
|
|
if neighbor.val in memo:
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
yield from traverse_neighborly_node(neighbor, memo)
|
|
|
|
|