mirror of
https://github.com/mfocko/blog.git
synced 2024-11-14 01:59:42 +01:00
1 line
53 KiB
JavaScript
1 line
53 KiB
JavaScript
|
"use strict";(self.webpackChunkfi=self.webpackChunkfi||[]).push([[7728],{73212:(s,e,a)=>{a.r(e),a.d(e,{assets:()=>c,contentTitle:()=>t,default:()=>d,frontMatter:()=>i,metadata:()=>m,toc:()=>r});var n=a(85893),l=a(11151);const i={id:"top-down-dp",slug:"/recursion/pyramid-slide-down/top-down-dp",title:"Top-down DP solution",description:"Top-down DP solution of the Pyramid Slide Down.\n",tags:["java","dynamic-programming","top-down-dp"],last_update:{date:new Date("2023-08-17T00:00:00.000Z")}},t="Top-down dynamic programming",m={id:"recursion/2023-08-17-pyramid-slide-down/top-down-dp",title:"Top-down DP solution",description:"Top-down DP solution of the Pyramid Slide Down.\n",source:"@site/algorithms/04-recursion/2023-08-17-pyramid-slide-down/03-top-down-dp.md",sourceDirName:"04-recursion/2023-08-17-pyramid-slide-down",slug:"/recursion/pyramid-slide-down/top-down-dp",permalink:"/algorithms/recursion/pyramid-slide-down/top-down-dp",draft:!1,unlisted:!1,editUrl:"https://github.com/mfocko/blog/tree/main/algorithms/04-recursion/2023-08-17-pyramid-slide-down/03-top-down-dp.md",tags:[{label:"java",permalink:"/algorithms/tags/java"},{label:"dynamic-programming",permalink:"/algorithms/tags/dynamic-programming"},{label:"top-down-dp",permalink:"/algorithms/tags/top-down-dp"}],version:"current",lastUpdatedAt:1692230400,formattedLastUpdatedAt:"Aug 17, 2023",sidebarPosition:3,frontMatter:{id:"top-down-dp",slug:"/recursion/pyramid-slide-down/top-down-dp",title:"Top-down DP solution",description:"Top-down DP solution of the Pyramid Slide Down.\n",tags:["java","dynamic-programming","top-down-dp"],last_update:{date:"2023-08-17T00:00:00.000Z"}},sidebar:"autogeneratedBar",previous:{title:"Greedy solution",permalink:"/algorithms/recursion/pyramid-slide-down/greedy"},next:{title:"Bottom-up DP solution",permalink:"/algorithms/recursion/pyramid-slide-down/bottom-up-dp"}},c={},r=[{value:"Time complexity",id:"time-complexity",level:2},{value:"Memory complexity",id:"memory-complexity",level:2}];function h(s){const e={a:"a",admonition:"admonition",annotation:"annotation",code:"code",em:"em",h1:"h1",h2:"h2",li:"li",math:"math",mi:"mi",mn:"mn",mo:"mo",mrow:"mrow",mspace:"mspace",mstyle:"mstyle",msub:"msub",mtable:"mtable",mtd:"mtd",mtr:"mtr",munderover:"munderover",ol:"ol",p:"p",pre:"pre",section:"section",semantics:"semantics",span:"span",strong:"strong",sup:"sup",...(0,l.a)(),...s.components};return(0,n.jsxs)(n.Fragment,{children:[(0,n.jsx)(e.h1,{id:"top-down-dynamic-programming",children:"Top-down dynamic programming"}),"\n",(0,n.jsxs)(e.p,{children:[(0,n.jsx)(e.em,{children:"Top-down dynamic programming"})," is probably the most common approach, since (at\nleast looks like) is the easiest to implement. The whole point is avoiding the\nunnecessary computations that we have already done."]}),"\n",(0,n.jsxs)(e.p,{children:["In our case, we can use our na\xefve solution and put a ",(0,n.jsx)(e.em,{children:"cache"})," on top of it that\nwill make sure, we don't do unnecessary calculations."]}),"\n",(0,n.jsx)(e.pre,{children:(0,n.jsx)(e.code,{className:"language-java",children:"// This \u201cstructure\u201d is required, since I have decided to use \u2039TreeMap\u203a which\n// requires the ordering on the keys. It represents one position in the pyramid.\nrecord Position(int row, int col) implements Comparable<Position> {\n public int compareTo(Position r) {\n if (row != r.row) {\n return Integer.valueOf(row).compareTo(r.row);\n }\n\n if (col != r.col) {\n return Integer.valueOf(col).compareTo(r.col);\n }\n\n return 0;\n }\n}\n\npublic static int longestSlideDown(\n int[][] pyramid,\n TreeMap<Position, Integer> cache,\n Position position) {\n int row = position.row;\n int col = position.col;\n\n if (row >= pyramid.length || col < 0 || col >= pyramid[row].length) {\n // BASE: out of bounds\n return Integer.MIN_VALUE;\n }\n\n if (row == pyramid.length - 1) {\n // BASE: bottom of the pyramid\n return pyramid[position.row][position.c
|