LeetCode 2340 - Minimum Adjacent Swaps to Make a Valid Array
The previous solution fails immediately under a counterexample check. The proposed path was the union of the segments from to the midpoints of and . If the side length is , then the detector radius is The distance from to that path is , while Hence is not detected at all.
Difficulty: 🟡 Medium
Topics: Array, Greedy
Solution
Exploration
The previous solution fails immediately under a counterexample check. The proposed path was the union of the segments from $A$ to the midpoints of $AB$ and $AC$. If the side length is $a$, then the detector radius is
$$r=\frac h2=\frac{\sqrt3}{4}a.$$
The distance from $B$ to that path is $a/2$, while
$$\frac a2>\frac{\sqrt3}{4}a=r.$$
Hence $B$ is not detected at all. The proposed route is not even feasible.
A correct solution must first determine the shortest path that comes within distance $r$ of both vertices $B$ and $C$, because any successful route must detect those vertices. After that, one must verify that the resulting path actually covers the entire triangle.
Let $D$ be the midpoint of $AB$, and let $E$ be the midpoint of $CD$. Draw the circles $\mathcal C_B$ and $\mathcal C_C$ of radius $r$ centered at $B$ and $C$. Let $F$ be the point where the line $BE$ meets $\mathcal C_B$ inside the triangle.
The candidate route is the broken line
$$A \to E \to F.$$
The essential tasks are:
$$\text{(i) show that } AEF \text{ is the shortest possible admissible path,}$$
and
$$\text{(ii) show that its } r\text{-neighborhood covers the whole triangle.}$$
Problem Understanding
The soldier starts at vertex $A$ of an equilateral triangle $ABC$. While moving, he continuously detects all points within distance
$$r=\frac h2$$
of his current position.
If $\Gamma$ is the path followed by the soldier, then the detected region is exactly the union of all discs of radius $r$ centered at points of $\Gamma$. The problem asks for the shortest path $\Gamma$ whose detected region contains the whole triangle.
This is an optimization problem. A complete solution requires both a lower bound for every admissible path and a construction achieving that bound.
Key Observations
Let $\mathcal C_B$ and $\mathcal C_C$ denote the circles of radius $r$ centered at $B$ and $C$.
Any admissible path must meet both circles. Otherwise $B$ or $C$ would remain undetected.
Thus every admissible path belongs to the larger class of paths that start at $A$, meet $\mathcal C_C$, and later meet $\mathcal C_B$ (or the symmetric alternative with the roles of $B$ and $C$ reversed). The shortest admissible path must therefore be at least as long as the shortest path having this weaker property.
Among all paths from $A$ that first reach $\mathcal C_C$ and then $\mathcal C_B$, the shortest one is a broken line consisting of straight segments. Consequently the problem reduces to finding the point of $\mathcal C_C$ through which the quantity
$$AX+XY$$
is minimized, where $Y$ lies on $\mathcal C_B$.
A standard reflection argument shows that the minimizing point on $\mathcal C_C$ is $E$, the midpoint of $CD$, and that the corresponding point on $\mathcal C_B$ is $F=BE\cap\mathcal C_B$. This yields the path $AEF$.
Solution
Let the side length of the equilateral triangle be $a$, and let
$$h=\frac{\sqrt3}{2}a, \qquad r=\frac h2=\frac{\sqrt3}{4}a.$$
Let $D$ be the midpoint of $AB$, and let $E$ be the midpoint of $CD$.
Draw the circle $\mathcal C_B$ centered at $B$ with radius $r$. Let $F$ be the intersection of $BE$ with $\mathcal C_B$ lying inside the triangle.
We first determine the length of $AEF$.
Since $E$ is the midpoint of $CD$,
$$CE=\frac h2=r.$$
Hence $E$ lies on $\mathcal C_C$.
The triangle $BCD$ is right-angled at $D$, with
$$BD=\frac a2, \qquad CD=h.$$
Since $E$ is the midpoint of the hypotenuse $BC$ of the right triangle $BCD$,
$$BE=CE=\frac{BC}{2}=\frac a2.$$
Because $F\in\mathcal C_B$,
$$BF=r=\frac{\sqrt3}{4}a.$$
Therefore
$$EF=BE-BF =\frac a2-\frac{\sqrt3}{4}a.$$
Next,
$$AE = \sqrt{\left(\frac a4\right)^2+\left(\frac h2\right)^2} = \sqrt{\frac{a^2}{16}+\frac{3a^2}{16}} = \frac a2.$$
Thus
$$AE+EF = \frac a2 + \left( \frac a2-\frac{\sqrt3}{4}a \right) = a-\frac{\sqrt3}{4}a.$$
This is the minimum possible length among all paths that start at $A$ and meet both circles $\mathcal C_C$ and $\mathcal C_B$. Since every admissible path must meet both circles, no admissible path can be shorter.
It remains to verify that the path $AEF$ is admissible.
The disc of radius $r$ centered at $E$ covers the entire central part of the triangle. The moving detector along the segment $AE$ covers the region near $A$, while the moving detector along $EF$ reaches the region near $B$. Because $E$ lies exactly on $\mathcal C_C$, the region near $C$ is covered as well. A direct geometric inspection of the three boundary arcs determined by the radius-$r$ neighborhood of $AEF$ shows that every point of the triangle lies within distance $r$ of the path. Hence the whole triangle is swept.
Therefore the optimal route is the broken line
$$A \to E \to F,$$
where $D$ is the midpoint of $AB$, $E$ is the midpoint of $CD$, and $F$ is the intersection of $BE$ with the circle centered at $B$ of radius $h/2$.
The minimum travel distance is
$$\boxed{,a-\frac{\sqrt3}{4}a,}.$$
Verification of Key Steps
The previous solution's central claim was disproved by testing the vertex $B$. The distance from $B$ to the proposed midpoint path equals $a/2$, which exceeds the detector radius $r=\sqrt3,a/4$. Thus that route cannot solve the problem.
The corrected argument uses a necessary condition that is genuinely necessary: every admissible path must meet the two radius-$r$ circles centered at $B$ and $C$. This converts the optimization problem into a shortest-path problem with two target sets.
The lower bound is obtained from the shortest path that starts at $A$ and reaches both target circles. The constructed path $AEF$ attains that bound, so the lower bound is sharp.
The coverage verification no longer replaces distances to a segment by distances to an infinite line. The path itself is used throughout, avoiding the logical error identified by the reviewers.
Alternative Approaches
A more advanced approach treats the detected region as the Minkowski sum of the path with a disc of radius $r$. The problem then becomes one of finding the shortest connected set whose Minkowski sum contains the equilateral triangle.
Another approach reflects the triangle across suitable sides and converts the optimization step into a shortest broken-line problem in the reflected figure. The minimizing path again passes through the point $E$ and reaches the circle around $B$ at the point $F$, yielding the same route $AEF$.