Solved LeetCode program · 953 of 1,000
#1319 · Number of Operations to Make Network Connected
Original English source
Keep a shared starts as an empty list of whole numbers. Keep a shared edges as an empty list of whole numbers. Keep a shared counts as an empty list of whole numbers. Keep a shared seen as an empty list of whole numbers. Keep a shared queue as an empty list of whole numbers. Keep a shared tail of 0. Place 0 into starts. Place 2 into starts. Place 4 into starts. Place 6 into starts. Place 1 into edges. Place 2 into edges. Place 0 into edges. Place 2 into edges. Place 0 into edges. Place 1 into edges. Place 2 into counts. Place 2 into counts. Place 2 into counts. Place 0 into counts. For every node from 0 to 3, Place 0 into seen. For every cell from 0 to 15, Place 0 into queue.
To reach from node (a whole number).
For every offset from 0 to item node of counts minus 1.
The base is item node of starts. The target is item (base plus offset) of edges. If item target of seen is 0, Set item target of seen to 1, Set item tail of queue to target, Add 1 to tail.
To explore from source (a whole number). The head is 0. Set tail to 1. Set item 0 of queue to source. Set item source of seen to 1.
Repeat while head is less than tail.
The node is item head of queue. Add 1 to head. Reach from node.
The parts is 0. For every source from 0 to 3, if item source of seen is 0, Explore from source, add 1 to parts. The links is 3. The needed is 3. If links is less than needed, Show -1, otherwise Show parts minus 1.
Expected stdout
1