blog/assets/js/1cd58e77.1abe79ba.js

1 line
13 KiB
JavaScript
Raw Normal View History

"use strict";(self.webpackChunkfi=self.webpackChunkfi||[]).push([[1547],{32090:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>l,contentTitle:()=>a,default:()=>h,frontMatter:()=>o,metadata:()=>r,toc:()=>d});var s=n(85893),i=n(11151);const o={id:"bottom-up-dp",slug:"/recursion/pyramid-slide-down/bottom-up-dp",title:"Bottom-up DP solution",description:"Bottom-up DP solution of the Pyramid Slide Down.\n",tags:["java","dynamic-programming","bottom-up-dp"],last_updated:{date:new Date("2023-08-17T00:00:00.000Z")}},a="Bottom-up dynamic programming",r={id:"recursion/2023-08-17-pyramid-slide-down/bottom-up-dp",title:"Bottom-up DP solution",description:"Bottom-up DP solution of the Pyramid Slide Down.\n",source:"@site/algorithms/04-recursion/2023-08-17-pyramid-slide-down/04-bottom-up-dp.md",sourceDirName:"04-recursion/2023-08-17-pyramid-slide-down",slug:"/recursion/pyramid-slide-down/bottom-up-dp",permalink:"/algorithms/recursion/pyramid-slide-down/bottom-up-dp",draft:!1,unlisted:!1,editUrl:"https://github.com/mfocko/blog/tree/main/algorithms/04-recursion/2023-08-17-pyramid-slide-down/04-bottom-up-dp.md",tags:[{label:"java",permalink:"/algorithms/tags/java"},{label:"dynamic-programming",permalink:"/algorithms/tags/dynamic-programming"},{label:"bottom-up-dp",permalink:"/algorithms/tags/bottom-up-dp"}],version:"current",lastUpdatedAt:1703786024,formattedLastUpdatedAt:"Dec 28, 2023",sidebarPosition:4,frontMatter:{id:"bottom-up-dp",slug:"/recursion/pyramid-slide-down/bottom-up-dp",title:"Bottom-up DP solution",description:"Bottom-up DP solution of the Pyramid Slide Down.\n",tags:["java","dynamic-programming","bottom-up-dp"],last_updated:{date:"2023-08-17T00:00:00.000Z"}},sidebar:"autogeneratedBar",previous:{title:"Top-down DP solution",permalink:"/algorithms/recursion/pyramid-slide-down/top-down-dp"},next:{title:"Red-Black Trees",permalink:"/algorithms/category/red-black-trees"}},l={},d=[{value:"Time complexity",id:"time-complexity",level:2},{value:"Memory complexity",id:"memory-complexity",level:2}];function c(e){const t={a:"a",admonition:"admonition",annotation:"annotation",code:"code",em:"em",h1:"h1",h2:"h2",hr:"hr",li:"li",math:"math",mdxAdmonitionTitle:"mdxAdmonitionTitle",mi:"mi",mn:"mn",mo:"mo",mrow:"mrow",ol:"ol",p:"p",pre:"pre",section:"section",semantics:"semantics",span:"span",strong:"strong",sup:"sup",...(0,i.a)(),...e.components};return(0,s.jsxs)(s.Fragment,{children:[(0,s.jsx)(t.h1,{id:"bottom-up-dynamic-programming",children:"Bottom-up dynamic programming"}),"\n",(0,s.jsxs)(t.p,{children:["If you try to think in depth about the top-down DP solution, you might notice\nthat the ",(0,s.jsx)(t.em,{children:"core"})," of it stands on caching the calculations that have been already\ndone on the lower \u201clevels\u201d of the pyramid. Our bottom-up implementation will be\nusing this fact!"]}),"\n",(0,s.jsxs)(t.admonition,{type:"tip",children:[(0,s.jsxs)(t.p,{children:["As I have said in the ",(0,s.jsx)(t.em,{children:"top-down DP"})," section, it is the easiest way to implement\nDP (unless the cached function has complicated parameters, in that case it might\nget messy)."]}),(0,s.jsx)(t.p,{children:"Bottom-up dynamic programming can be more effective, but may be more complicated\nto implement right from the beginning."})]}),"\n",(0,s.jsx)(t.p,{children:"Let's see how we can implement it:"}),"\n",(0,s.jsx)(t.pre,{children:(0,s.jsx)(t.code,{className:"language-java",children:"public static int longestSlideDown(int[][] pyramid) {\n // In the beginning we declare new array. At this point it is easier to just\n // work with the one dimension, i.e. just allocating the space for the rows.\n int[][] slideDowns = new int[pyramid.length][];\n\n // Bottom row gets just copied, there's nothing else to do\u2026 It's the base\n // case.\n slideDowns[pyramid.length - 1] = Arrays.copyOf(pyramid[pyramid.length - 1],\n pyramid[pyramid.length - 1].length);\n\n // Then we need to propagate the found slide downs for each of the levels\n // above.\n for (int y = pyramid.length - 2; y >= 0; --y) {\n // We start by c