immutable structure with implicitly defined <code>.equals()</code>, <code>.hashCode()</code>,
<code>.toString()</code> and getters for the attributes.</p></div></div>
<p>Because of the choice of <code>TreeMap</code>, we had to additionally define the ordering
on it.</p>
<p>In the <code>longestSlideDown</code> you can notice that the computation which used to be
at the end of the naïve version above, is now wrapped in an <code>if</code> statement that
checks for the presence of the position in the cache and computes the slide down
just when it's needed.</p>
<h2class="anchor anchorWithStickyNavbar_LWe7"id="time-complexity">Time complexity<ahref="#time-complexity"class="hash-link"aria-label="Direct link to Time complexity"title="Direct link to Time complexity"></a></h2>
<p>If you think that evaluating time complexity for this approach is a bit more
tricky, you are right. Keeping the cache in mind, it is not the easiest thing
to do. However there are some observations that might help us figure this out:</p>
<ol>
<li>Slide down from each position is calculated only once.</li>
<li>Once calculated, we use the result from the cache.</li>
</ol>
<p>Knowing this, we still cannot, at least easily, describe the time complexity of
finding the best slide down from a specific position, <strong>but</strong> we can bound it
from above for the <strong>whole</strong> run from the top. Now the question is how we can do
that!</p>
<p>Overall we are doing the same things for almost<sup><ahref="#user-content-fn-1"id="user-content-fnref-1"data-footnote-ref="true"aria-describedby="footnote-label">1</a></sup> all of the positions within
the pyramid:</p>
<ol>
<li>
<p>We calculate and store it (using the partial results stored in cache). This
is done only once.</p>
<p>For each calculation we take 2 values from the cache and insert one value.
Because we have chosen <code>TreeMap</code>, these 3 operations have logarithmic time
complexity and therefore this step is equivalent to <spanclass="katex"><spanclass="katex-mathml"><mathxmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mn>3</mn><mo>⋅</mo><msub><mrow><mi>log</mi><mo></mo></mrow><mn>2</mn></msub><mi>n</mi></mrow><annotationencoding="application/x-tex">3 \cdot \log_2{n}</annotation></semantics></math></span><spanclass="katex-html"aria-hidden="true"><spanclass="base"><spanclass="strut"style="height:0.6444em"></span><spanclass="mord">3</span><spanclass="mspace"style="margin-right:0.2222em"></span><spanclass="mbin">