mirror of
https://github.com/mfocko/blog.git
synced 2024-11-14 18:17:31 +01:00
1 line
7.9 KiB
JavaScript
1 line
7.9 KiB
JavaScript
|
"use strict";(self.webpackChunkfi=self.webpackChunkfi||[]).push([[5934],{1910:(e,n,t)=>{t.r(n),t.d(n,{assets:()=>l,contentTitle:()=>o,default:()=>h,frontMatter:()=>s,metadata:()=>a,toc:()=>d});var i=t(85893),r=t(11151);const s={id:"greedy",slug:"/recursion/pyramid-slide-down/greedy",title:"Greedy solution",description:"Greedy solution of the Pyramid Slide Down.\n",tags:["java","greedy"],last_update:{date:new Date("2023-08-17T00:00:00.000Z")}},o=void 0,a={id:"recursion/2023-08-17-pyramid-slide-down/greedy",title:"Greedy solution",description:"Greedy solution of the Pyramid Slide Down.\n",source:"@site/algorithms/04-recursion/2023-08-17-pyramid-slide-down/02-greedy.md",sourceDirName:"04-recursion/2023-08-17-pyramid-slide-down",slug:"/recursion/pyramid-slide-down/greedy",permalink:"/algorithms/recursion/pyramid-slide-down/greedy",draft:!1,unlisted:!1,editUrl:"https://github.com/mfocko/blog/tree/main/algorithms/04-recursion/2023-08-17-pyramid-slide-down/02-greedy.md",tags:[{label:"java",permalink:"/algorithms/tags/java"},{label:"greedy",permalink:"/algorithms/tags/greedy"}],version:"current",lastUpdatedAt:1692230400,formattedLastUpdatedAt:"Aug 17, 2023",sidebarPosition:2,frontMatter:{id:"greedy",slug:"/recursion/pyramid-slide-down/greedy",title:"Greedy solution",description:"Greedy solution of the Pyramid Slide Down.\n",tags:["java","greedy"],last_update:{date:"2023-08-17T00:00:00.000Z"}},sidebar:"autogeneratedBar",previous:{title:"Na\xefve solution",permalink:"/algorithms/recursion/pyramid-slide-down/naive"},next:{title:"Top-down DP solution",permalink:"/algorithms/recursion/pyramid-slide-down/top-down-dp"}},l={},d=[{value:"Time complexity",id:"time-complexity",level:2},{value:"Running the tests",id:"running-the-tests",level:2}];function c(e){const n={admonition:"admonition",annotation:"annotation",code:"code",em:"em",h2:"h2",li:"li",math:"math",mi:"mi",mo:"mo",mrow:"mrow",ol:"ol",p:"p",pre:"pre",semantics:"semantics",span:"span",strong:"strong",...(0,r.a)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsxs)(n.p,{children:["We will try to optimize it a bit. Let's start with a relatively simple ",(0,i.jsx)(n.em,{children:"greedy"}),"\napproach."]}),"\n",(0,i.jsx)(n.admonition,{title:"Greedy algorithms",type:"info",children:(0,i.jsxs)(n.p,{children:[(0,i.jsx)(n.em,{children:"Greedy algorithms"})," can be described as algorithms that decide the action on the\noptimal option at the moment."]})}),"\n",(0,i.jsx)(n.p,{children:"We can try to adjust the na\xefve solution. The most problematic part are the\nrecursive calls. Let's apply the greedy approach there:"}),"\n",(0,i.jsx)(n.pre,{children:(0,i.jsx)(n.code,{className:"language-java",children:"public static int longestSlideDown(int[][] pyramid, int row, int col) {\n if (row == pyramid.length - 1) {\n // BASE: We're at the bottom\n return pyramid[row][col];\n }\n\n if (col + 1 >= pyramid[row + 1].length\n || pyramid[row + 1][col] > pyramid[row + 1][col + 1]) {\n // If we cannot go right or it's not feasible, we continue to the left.\n return pyramid[row][col] + longestSlideDown(pyramid, row + 1, col);\n }\n\n // Otherwise we just move to the right.\n return pyramid[row][col] + longestSlideDown(pyramid, row + 1, col + 1);\n}\n"})}),"\n",(0,i.jsxs)(n.p,{children:["OK, if we cannot go right ",(0,i.jsx)(n.strong,{children:"or"})," the right path adds smaller value to the sum,\nwe simply go left."]}),"\n",(0,i.jsx)(n.h2,{id:"time-complexity",children:"Time complexity"}),"\n",(0,i.jsxs)(n.p,{children:["We have switched from ",(0,i.jsx)(n.em,{children:"adding the maximum"})," to ",(0,i.jsx)(n.em,{children:"following the \u201cbigger\u201d path"}),", so\nwe improved the time complexity tremendously. We just go down the pyramid all\nthe way to the bottom. Therefore we are getting:"]}),"\n",(0,i.jsx)(n.span,{className:"katex-display",children:(0,i.jsxs)(n.span,{className:"katex",children:[(0,i.jsx)(n.span,{className:"katex-mathml",children:(0,i.jsx)(n.math,{xmlns:"http://www.w3.org/1998/Math/MathML",display:"block",
|