-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #627 from 0xff-dev/847
Add solution and test-cases for problem 847
- Loading branch information
Showing
5 changed files
with
57 additions
and
23 deletions.
There are no files selected for viewing
29 changes: 15 additions & 14 deletions
29
leetcode/801-900/0847.Shortest-Path-Visiting-All-Nodes/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 32 additions & 2 deletions
34
leetcode/801-900/0847.Shortest-Path-Visiting-All-Nodes/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,35 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
func Solution(graph [][]int) int { | ||
queue := [][]int{} | ||
size := len(graph) | ||
|
||
finalState := (1 << size) - 1 | ||
visited := make(map[[2]int]struct{}) | ||
for i := 0; i < size; i++ { | ||
queue = append(queue, []int{i, 1 << i}) | ||
visited[[2]int{i, 1 << i}] = struct{}{} | ||
} | ||
|
||
ans := -1 | ||
for len(queue) > 0 { | ||
nq := make([][]int, 0) | ||
ans++ | ||
for _, item := range queue { | ||
node := item[0] | ||
state := item[1] | ||
for _, next := range graph[node] { | ||
nextState := state | (1 << next) | ||
if nextState == finalState { | ||
return ans + 1 | ||
} | ||
if _, ok := visited[[2]int{next, nextState}]; !ok { | ||
nq = append(nq, []int{next, nextState}) | ||
visited[[2]int{next, nextState}] = struct{}{} | ||
} | ||
} | ||
} | ||
queue = nq | ||
} | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+8.93 KB
leetcode/801-900/0847.Shortest-Path-Visiting-All-Nodes/shortest1-graph.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+12.9 KB
leetcode/801-900/0847.Shortest-Path-Visiting-All-Nodes/shortest2-graph.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.