Skip to content

Commit 2032997

Browse files
committed
Day 24: add explanation
1 parent 62669d5 commit 2032997

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

day24/README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Today's challenge is a continuation of [yesterday's](../day23)!
2+
3+
## Ideas
4+
5+
I wanted to take a different approach to this question; instead of doing a
6+
full brute-force exploration on all combinations of the inputArray,
7+
construct a graph and then do DFS from every node to find a possible
8+
Hamiltonian path. The existence of a path would verify whether there is
9+
a desired arrangement of the strings.
10+
11+
I can construct the graph in `O(N<sup>2</sup>)` time where `N` is the number of
12+
elements in the inputArray, and also the number of vertices in my graph. I check
13+
every possible pair of strings to see whether they differ by exactly one place,
14+
and add an edge in the graph between the two if so.
15+
16+
I can then run DFS from each node in `N * O(N) = O(N<sup>2</sup>)` time (this
17+
analysis doesn't seem right to me actually) to complete the algorithm.
18+
19+
Overall, it comes out to a runtime of `O(n<sup>2</sup>)`. The space complexity
20+
scales in proportion to the number of vertices and edges I need to keep track
21+
of in my graph. Vertices increase with every additional element in the inputArray.
22+
Edges increase when there are higher frequencies of words in the inputArray
23+
that are closer to each other in edit distance.
24+
25+
## Code
26+
27+
[Python](./stringsRearrangementBacktracking.py) (unfinished)
28+
29+
## Follow up
30+

0 commit comments

Comments
 (0)