feat: two pointers post

This commit is contained in:
Barrett Ruth 2024-06-17 12:16:41 -05:00
parent cb481c4fe3
commit 6c97fa2890
13 changed files with 268 additions and 10 deletions

100
posts/two-pointers.html Normal file
View file

@ -0,0 +1,100 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="../styles/common.css" />
<link rel="stylesheet" href="../styles/post.css" />
<link rel="icon" type="image/webp" href="../public/logo.webp" />
<link href="../public/prism/prism.css" rel="stylesheet" />
<link href="../public/prism/prism-theme.css" rel="stylesheet" />
<script defer src="../public/prism/prism.js"></script>
<link rel="stylesheet" href="../public/katex/katex.css" />
<script defer src="../public/katex/katex.js"></script>
<script
defer
src="../public/katex/katex-render.js"
onload="renderMathInElement(document.body);"
></script>
<title>Barrett Ruth</title>
</head>
<body class="graph">
<header>
<a
href="/"
style="text-decoration: none; color: inherit"
onclick="goHome(event)"
>
<div class="terminal-container">
<span class="terminal-prompt">barrett@ruth:~$ /algorithms</span>
<span class="terminal-cursor"></span>
</div>
</a>
</header>
<main class="main">
<div class="post-container">
<header class="post-header">
<h1 class="post-title">Two Pointers</h1>
<p class="post-meta">
<time datetime="2024-06-16">16/06/2024</time>
</p>
</header>
<article>
<h2>technique overview</h2>
<h3>
<a
target="blank"
href="https://leetcode.com/problems/container-with-most-water/"
>container with most water</a
>
</h3>
<p>Sometimes, the mathematical solution is the simplest.</p>
<p>
The area of a container bounded by the ground and its columns at
positions \((l, r)\) is: \[ \text{area} = \text{width} \cdot
\text{length} = (r - l) \cdot \min\{height[l], height[r]\} \]
</p>
<p>
At its core, this is a maximization problem: maximize the contained
area. \[ \max\{(r - l) \cdot \min\{height[l], height[r]\}\} \]
</p>
<p>
Given a new column position \(l_0 < l\) or \(r_0 < r\), the
contained area can only increase if the height of the corresponding
column increases.
</p>
<!-- TODO: add footnote -->
<p>
The following correct solution surveys all containers, initialized
with the widest columns positions, that are valid candidates for a
potentially new largest area. A running maximizum, the answer, is
maintained.
</p>
<pre><code class="language-python">
def maxArea(height: list[int]) -> int:
area = 0
l, r = 0, len(height) - 1
while l < r:
width, min_height = r - l, min(height[l], height[r])
area = max(area, width * min_height)
while l < r and height[l] <= min_height:
l += 1
while l < r and height[r] <= min_height:
r -= 1
return area
</code></pre>
<!-- <h3> -->
<!-- <a -->
<!-- target="blank" -->
<!-- href="https://leetcode.com/problems/boats-to-save-people/" -->
<!-- >boats to save people</a -->
<!-- > -->
<!-- </h3> -->
</article>
</div>
</main>
<script src="../scripts/common.js"></script>
<script src="../scripts/post.js"></script>
</body>
</html>

View file

@ -0,0 +1 @@
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("katex")):"function"==typeof define&&define.amd?define(["katex"],t):"object"==typeof exports?exports.renderMathInElement=t(require("katex")):e.renderMathInElement=t(e.katex)}("undefined"!=typeof self?self:this,(function(e){return function(){"use strict";var t={771:function(t){t.exports=e}},n={};function r(e){var o=n[e];if(void 0!==o)return o.exports;var i=n[e]={exports:{}};return t[e](i,i.exports,r),i.exports}r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,{a:t}),t},r.d=function(e,t){for(var n in t)r.o(t,n)&&!r.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)};var o={};return function(){r.d(o,{default:function(){return d}});var e=r(771),t=r.n(e);const n=function(e,t,n){let r=n,o=0;const i=e.length;for(;r<t.length;){const n=t[r];if(o<=0&&t.slice(r,r+i)===e)return r;"\\"===n?r++:"{"===n?o++:"}"===n&&o--,r++}return-1},i=/^\\begin{/;var a=function(e,t){let r;const o=[],a=new RegExp("("+t.map((e=>e.left.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&"))).join("|")+")");for(;r=e.search(a),-1!==r;){r>0&&(o.push({type:"text",data:e.slice(0,r)}),e=e.slice(r));const a=t.findIndex((t=>e.startsWith(t.left)));if(r=n(t[a].right,e,t[a].left.length),-1===r)break;const l=e.slice(0,r+t[a].right.length),s=i.test(l)?l:e.slice(t[a].left.length,r);o.push({type:"math",data:s,rawData:l,display:t[a].display}),e=e.slice(r+t[a].right.length)}return""!==e&&o.push({type:"text",data:e}),o};const l=function(e,n){const r=a(e,n.delimiters);if(1===r.length&&"text"===r[0].type)return null;const o=document.createDocumentFragment();for(let e=0;e<r.length;e++)if("text"===r[e].type)o.appendChild(document.createTextNode(r[e].data));else{const i=document.createElement("span");let a=r[e].data;n.displayMode=r[e].display;try{n.preProcess&&(a=n.preProcess(a)),t().render(a,i,n)}catch(i){if(!(i instanceof t().ParseError))throw i;n.errorCallback("KaTeX auto-render: Failed to parse `"+r[e].data+"` with ",i),o.appendChild(document.createTextNode(r[e].rawData));continue}o.appendChild(i)}return o},s=function(e,t){for(let n=0;n<e.childNodes.length;n++){const r=e.childNodes[n];if(3===r.nodeType){let o=r.textContent,i=r.nextSibling,a=0;for(;i&&i.nodeType===Node.TEXT_NODE;)o+=i.textContent,i=i.nextSibling,a++;const s=l(o,t);if(s){for(let e=0;e<a;e++)r.nextSibling.remove();n+=s.childNodes.length-1,e.replaceChild(s,r)}else n+=a}else if(1===r.nodeType){const e=" "+r.className+" ";-1===t.ignoredTags.indexOf(r.nodeName.toLowerCase())&&t.ignoredClasses.every((t=>-1===e.indexOf(" "+t+" ")))&&s(r,t)}}};var d=function(e,t){if(!e)throw new Error("No element provided to render");const n={};for(const e in t)t.hasOwnProperty(e)&&(n[e]=t[e]);n.delimiters=n.delimiters||[{left:"$$",right:"$$",display:!0},{left:"\\(",right:"\\)",display:!1},{left:"\\begin{equation}",right:"\\end{equation}",display:!0},{left:"\\begin{align}",right:"\\end{align}",display:!0},{left:"\\begin{alignat}",right:"\\end{alignat}",display:!0},{left:"\\begin{gather}",right:"\\end{gather}",display:!0},{left:"\\begin{CD}",right:"\\end{CD}",display:!0},{left:"\\[",right:"\\]",display:!0}],n.ignoredTags=n.ignoredTags||["script","noscript","style","textarea","pre","code","option"],n.ignoredClasses=n.ignoredClasses||[],n.errorCallback=n.errorCallback||console.error,n.macros=n.macros||{},s(e,n)}}(),o=o.default}()}));

1
public/katex/katex.css Normal file

File diff suppressed because one or more lines are too long

1
public/katex/katex.js Normal file

File diff suppressed because one or more lines are too long

View file

Before

Width:  |  Height:  |  Size: 15 KiB

After

Width:  |  Height:  |  Size: 15 KiB

Before After
Before After

View file

@ -0,0 +1,132 @@
/**
* Github Light theme for Prism.js
* Based on Github: https://github.com
* @author Katorly
*/
/* General */
pre[class*="language-"],
code[class*="language-"] {
color: #24292f;
font-size: 13px;
text-shadow: none;
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
direction: ltr;
text-align: left;
white-space: pre;
word-spacing: normal;
word-break: normal;
line-height: 1.5;
-moz-tab-size: 4;
-o-tab-size: 4;
tab-size: 4;
-webkit-hyphens: none;
-moz-hyphens: none;
-ms-hyphens: none;
hyphens: none;
}
pre[class*="language-"]::selection,
code[class*="language-"]::selection,
pre[class*="language-"]::mozselection,
code[class*="language-"]::mozselection {
text-shadow: none;
background: #9fc6e9;
}
@media print {
pre[class*="language-"],
code[class*="language-"] {
text-shadow: none;
}
}
pre[class*="language-"] {
padding: 1em;
margin: .5em 0;
overflow: auto;
background: #f6f8fa;
}
:not(pre) > code[class*="language-"] {
padding: .1em .3em;
border-radius: .3em;
color: #24292f;
background: #eff1f3;
}
/* Line highlighting */
pre[data-line] {
position: relative;
}
pre[class*="language-"] > code[class*="language-"] {
position: relative;
z-index: 1;
}
.line-highlight {
position: absolute;
left: 0;
right: 0;
padding: inherit 0;
margin-top: 1em;
background: #fff8c5;
box-shadow: inset 5px 0 0 #eed888;
z-index: 0;
pointer-events: none;
line-height: inherit;
white-space: pre;
}
/* Tokens */
.namespace {
opacity: .7;
}
.token.comment,
.token.prolog,
.token.doctype,
.token.cdata {
color: #6e7781;
}
.token.punctuation {
color: #24292f;
}
.token.property,
.token.tag,
.token.boolean,
.token.number,
.token.constant,
.token.symbol,
.token.deleted {
color: #0550ae;
}
.token.selector,
.token.attr-name,
.token.string,
.token.char,
.token.builtin,
.token.inserted {
color: #0a3069;
}
.token.operator,
.token.entity,
.token.url,
.language-css .token.string,
.style .token.string {
color: #0550ae;
}
.token.atrule,
.token.attr-value,
.token.keyword {
color: #cf222e;
}
.token.function {
color: #8250df;
}
.token.regex,
.token.important,
.token.variable {
color: #0a3069;
}
.token.important,
.token.bold {
font-weight: bold;
}
.token.italic {
font-style: italic;
}
.token.entity {
cursor: help;
}

3
public/prism/prism.css Normal file
View file

@ -0,0 +1,3 @@
/* PrismJS 1.29.0
https://prismjs.com/download.html#themes=prism-tomorrow&languages=python */
code[class*=language-],pre[class*=language-]{color:#ccc;background:0 0;font-family:Consolas,Monaco,'Andale Mono','Ubuntu Mono',monospace;font-size:1em;text-align:left;white-space:pre;word-spacing:normal;word-break:normal;word-wrap:normal;line-height:1.5;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-hyphens:none;-moz-hyphens:none;-ms-hyphens:none;hyphens:none}pre[class*=language-]{padding:1em;margin:.5em 0;overflow:auto}:not(pre)>code[class*=language-],pre[class*=language-]{background:#2d2d2d}:not(pre)>code[class*=language-]{padding:.1em;border-radius:.3em;white-space:normal}.token.block-comment,.token.cdata,.token.comment,.token.doctype,.token.prolog{color:#999}.token.punctuation{color:#ccc}.token.attr-name,.token.deleted,.token.namespace,.token.tag{color:#e2777a}.token.function-name{color:#6196cc}.token.boolean,.token.function,.token.number{color:#f08d49}.token.class-name,.token.constant,.token.property,.token.symbol{color:#f8c555}.token.atrule,.token.builtin,.token.important,.token.keyword,.token.selector{color:#cc99cd}.token.attr-value,.token.char,.token.regex,.token.string,.token.variable{color:#7ec699}.token.entity,.token.operator,.token.url{color:#67cdcc}.token.bold,.token.important{font-weight:700}.token.italic{font-style:italic}.token.entity{cursor:help}.token.inserted{color:green}

4
public/prism/prism.js Normal file

File diff suppressed because one or more lines are too long

View file

@ -16,7 +16,10 @@ const postMapping = new Map([
],
],
["Trading", [{ name: "InteractiveBrokers TWS" }, { name: "valuation" }]],
["Algorithms", [{ name: "two pointers" }, { name: "convex hull" }]],
[
"Algorithms",
[{ name: "two pointers", link: "two-pointers" }, { name: "convex hull" }],
],
]);
function refresh(e) {

View file

@ -1,8 +1,13 @@
const h2s = document.querySelectorAll("article h2");
h2s.forEach((h2) => {
document.querySelectorAll("article h2").forEach((h2) => {
const mdHeading = document.createElement("span");
mdHeading.textContent = "# ";
mdHeading.style.color = "#0073e6";
h2.prepend(mdHeading);
});
document.querySelectorAll("article h3").forEach((h3) => {
const mdHeading = document.createElement("span");
mdHeading.textContent = "## ";
mdHeading.style.color = "#0073e6";
h3.prepend(mdHeading);
});

View file

@ -40,6 +40,11 @@ footer {
padding: 20px;
}
a {
color: inherit;
text-decoration: none;
}
.graph {
background-image: linear-gradient(
to right,

View file

@ -34,11 +34,6 @@ ul {
padding: 0;
}
a {
color: inherit;
text-decoration: none;
}
.content {
display: flex;
justify-content: space-between;

View file

@ -24,7 +24,7 @@ header {
}
.post-container {
max-width: 80%;
width: 80%;
}
.post-meta {
@ -72,3 +72,11 @@ article h2 {
font-weight: normal;
font-style: italic;
}
article h3 {
font-weight: normal;
}
article a {
text-decoration: underline;
}