You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
2. For each already-reachable $i$, add all $i+1..i+a\[i]$.
68
+
2. For each already-reachable $i$, add all $i+1..i+a[i]$.
69
69
3. Stop when nothing new appears.
70
70
71
71
*Walkthrough:*
@@ -88,7 +88,7 @@ Carry one number while scanning left→right: the furthest frontier $F$ seen so
88
88
Rules:
89
89
90
90
* If you are at $i$ with $i>F$, you hit a gap → stuck forever.
91
-
* Otherwise, extend $F \leftarrow \max(F, i+a\[i])$ and continue.
91
+
* Otherwise, extend $F \leftarrow \max(F, i+a[i])$ and continue.
92
92
93
93
At the end:
94
94
@@ -134,7 +134,7 @@ indices: 0 1 2 3 4 5
134
134
F=3
135
135
```
136
136
137
-
Final: $F=3$. Since $F\<n-1=5$, last is unreachable; furthest reachable square is $3$.
137
+
Final: $F=3$. Since $F<n-1=5$, last is unreachable; furthest reachable square is $3$.
138
138
139
139
Complexity: time $O(n)$, space $O(1)$.
140
140
@@ -353,7 +353,7 @@ Time $O((|V|+|E|)\log|V|)$; space $O(|V|)$.
353
353
354
354
*Walkthrough*
355
355
356
-
Legend: “S” = settled, “π\[x]” = parent of $x$. Ties break arbitrarily.
356
+
Legend: “S” = settled, “π[x]” = parent of $x$. Ties break arbitrarily.
357
357
358
358
Round 0 (init)
359
359
@@ -729,7 +729,7 @@ A valid optimal answer will be a prefix code with expected length as small as po
729
729
730
730
**Baseline**
731
731
732
-
One conceptual baseline is to enumerate all full binary trees with five labeled leaves and pick the one minimizing $\sum f\_i,L\_i$. That is correct but explodes combinatorially as the number of symbols grows. A simpler but usually suboptimal baseline is to give every symbol the same length $\lceil \log\_2 5\rceil=3$. That fixed-length code has $\mathbb{E}\[L]=3$.
732
+
One conceptual baseline is to enumerate all full binary trees with five labeled leaves and pick the one minimizing $\sum f\_i,L\_i$. That is correct but explodes combinatorially as the number of symbols grows. A simpler but usually suboptimal baseline is to give every symbol the same length $\lceil \log\_2 5\rceil=3$. That fixed-length code has $\mathbb{E}[L]=3$.
**Why is the greedy choice optimal?** In an optimal tree the two deepest leaves must be siblings; if not, pairing them to be siblings never increases any other depth and strictly reduces cost whenever a heavier symbol is deeper than a lighter one (an **exchange argument**: swapping depths changes the cost by $f\_{\text{heavy}}-f\_{\text{light}}>0$ in our favor). Collapsing those siblings into a single pseudo-symbol reduces the problem size without changing optimality, so induction finishes the proof. (Ties can be broken arbitrarily; all tie-breaks achieve the same minimum $\mathbb{E}\[L]$.)
742
+
**Why is the greedy choice optimal?** In an optimal tree the two deepest leaves must be siblings; if not, pairing them to be siblings never increases any other depth and strictly reduces cost whenever a heavier symbol is deeper than a lighter one (an **exchange argument**: swapping depths changes the cost by $f\_{\text{heavy}}-f\_{\text{light}}>0$ in our favor). Collapsing those siblings into a single pseudo-symbol reduces the problem size without changing optimality, so induction finishes the proof. (Ties can be broken arbitrarily; all tie-breaks achieve the same minimum $\mathbb{E}[L]$.)
743
743
744
744
Start with the multiset ${0.40, 0.20, 0.20, 0.10, 0.10}$. At each line, merge the two smallest weights and add their sum to the running cost.
745
745
@@ -757,7 +757,7 @@ Start with the multiset ${0.40, 0.20, 0.20, 0.10, 0.10}$. At each line, merge th
757
757
multiset becomes {1.00} (done)
758
758
```
759
759
760
-
So the optimal expected length is $\boxed{\mathbb{E}\[L]=2.20}$ bits per symbol. This already beats the naive fixed-length baseline $3$. It also matches the information-theoretic bound $H(f)\le \mathbb{E}\[L]\<H(f)+1$, since the entropy here is $H\approx 2.1219$.
760
+
So the optimal expected length is $\boxed{\mathbb{E}[L]=2.20}$ bits per symbol. This already beats the naive fixed-length baseline $3$. It also matches the information-theoretic bound $H(f)\le \mathbb{E}[L]<H(f)+1$, since the entropy here is $H\approx 2.1219$.
761
761
762
762
Now assign actual lengths. Record who merged with whom:
763
763
@@ -796,7 +796,7 @@ One concrete codebook arises by reading left edges as 0 and right edges as 1 (th
796
796
* $D \mapsto 010$
797
797
* $E \mapsto 011$
798
798
799
-
You can verify the prefix property immediately and recompute $\mathbb{E}\[L]$ from these lengths to get $2.20$ again. (From these lengths you can also construct the **canonical Huffman code**, which orders codewords lexicographically—useful for compactly storing the codebook.)
799
+
You can verify the prefix property immediately and recompute $\mathbb{E}[L]$ from these lengths to get $2.20$ again. (From these lengths you can also construct the **canonical Huffman code**, which orders codewords lexicographically—useful for compactly storing the codebook.)
0 commit comments