diff --git a/apps/docs/src/generated/Ask.astro b/apps/docs/src/generated/Ask.astro
new file mode 100644
index 0000000..30142ae
--- /dev/null
+++ b/apps/docs/src/generated/Ask.astro
@@ -0,0 +1,15 @@
+---
+// Generated by Blume. Do not edit.
+import Assistant from "blume/components/islands/Assistant.astro";
+import data from "blume:data";
+
+const { strings } = Astro.props;
+---
+
+
diff --git a/apps/docs/src/generated/app.css b/apps/docs/src/generated/app.css
new file mode 100644
index 0000000..777ab40
--- /dev/null
+++ b/apps/docs/src/generated/app.css
@@ -0,0 +1,1443 @@
+/* Generated by Blume. Do not edit. */
+@import "tailwindcss";
+@plugin "@tailwindcss/typography";
+
+/* Scan Blume's components and the user's project for utility classes. */
+@source "../../../../../../../../../../tmp/bunx-1000-blume@latest/node_modules/blume/src/**/*.{astro,ts,tsx}";
+@source "../../**/*.{astro,jsx,mdx,ts,tsx}";
+
+/* Dark mode is driven by data-theme on the element. */
+@custom-variant dark (&:where([data-theme="dark"], [data-theme="dark"] *));
+
+:root {
+ color-scheme: light;
+}
+
+:root[data-theme="dark"] {
+ color-scheme: dark;
+}
+
+:root {
+ --blume-background: oklch(1 0 0);
+ --blume-background-image: none;
+ --blume-background-image-repeat: no-repeat;
+ --blume-background-image-size: cover;
+ --blume-foreground: oklch(0.145 0 0);
+ --blume-muted: oklch(0.965 0 0);
+ /* 5.28:1 on the page background. The headroom over 4.5:1 is the point: muted
+ text is 14px body copy routinely set on a tinted surface (callouts, badges,
+ panels), and each tint costs a few tenths. */
+ --blume-muted-foreground: oklch(0.53 0 0);
+ --blume-border: oklch(0.88 0.006 260 / 0.72);
+ --blume-accent: oklch(0.145 0 0);
+ --blume-accent-foreground: oklch(1 0 0);
+ --blume-action: var(--blume-accent);
+ --blume-action-foreground: var(--blume-accent-foreground);
+ --blume-code-background: oklch(0.99 0 0);
+ /* Shiki notation transformers: line/word highlight, diff add/remove, and
+ error/warning lines. */
+ --blume-code-highlight: oklch(0.55 0.16 255 / 0.1);
+ --blume-code-highlight-border: oklch(0.55 0.16 255 / 0.55);
+ --blume-code-add: oklch(0.72 0.16 150 / 0.16);
+ --blume-code-add-border: oklch(0.52 0.15 150 / 0.7);
+ --blume-code-remove: oklch(0.66 0.21 22 / 0.16);
+ --blume-code-remove-border: oklch(0.55 0.2 22 / 0.7);
+ --blume-code-error: oklch(0.66 0.21 22 / 0.16);
+ --blume-code-error-border: oklch(0.55 0.2 22 / 0.7);
+ --blume-code-warning: oklch(0.8 0.16 80 / 0.2);
+ --blume-code-warning-border: oklch(0.66 0.15 70 / 0.75);
+ --blume-code-word: oklch(0.55 0.16 255 / 0.16);
+ --blume-code-word-border: oklch(0.55 0.16 255 / 0.5);
+ --blume-radius: 0.75rem;
+
+ /* Max width of the prose content column (article, breadcrumb, TOC, feedback,
+ pagination, last-updated). Override to re-measure the whole column at once. */
+ --blume-content-width: 42rem;
+
+ /*
+ * Font tokens resolve through optional src variables that Astro's Fonts API
+ * populates (via theme.fonts). When unset they fall back to the system
+ * stacks, so the default look is unchanged. The display font defaults to the
+ * body font.
+ */
+ --blume-font-body: var(
+ --blume-font-body-src,
+ ui-sans-serif,
+ system-ui,
+ -apple-system,
+ "Segoe UI",
+ Roboto,
+ Helvetica,
+ Arial,
+ sans-serif
+ );
+ --blume-font-mono: var(
+ --blume-font-mono-src,
+ ui-monospace,
+ "SF Mono",
+ "Cascadia Code",
+ "Source Code Pro",
+ Menlo,
+ Consolas,
+ monospace
+ );
+ --blume-font-display: var(--blume-font-display-src, var(--blume-font-body));
+}
+
+:root[data-theme="dark"] {
+ --blume-background: oklch(0.085 0 0);
+ --blume-background-image: none;
+ --blume-background-image-repeat: no-repeat;
+ --blume-background-image-size: cover;
+ --blume-foreground: oklch(0.96 0 0);
+ --blume-muted: oklch(0.16 0 0);
+ --blume-muted-foreground: oklch(0.68 0 0);
+ --blume-border: oklch(0.24 0 0 / 0.8);
+ --blume-accent: oklch(0.96 0 0);
+ --blume-accent-foreground: oklch(0.085 0 0);
+ --blume-action: var(--blume-accent);
+ --blume-action-foreground: var(--blume-accent-foreground);
+ --blume-code-background: oklch(0.12 0 0);
+ /* Brighter tints read better over the dark code surface. */
+ --blume-code-highlight: oklch(0.7 0.14 255 / 0.16);
+ --blume-code-highlight-border: oklch(0.7 0.14 255 / 0.6);
+ --blume-code-add: oklch(0.78 0.17 150 / 0.2);
+ --blume-code-add-border: oklch(0.72 0.16 150 / 0.7);
+ --blume-code-remove: oklch(0.72 0.21 22 / 0.22);
+ --blume-code-remove-border: oklch(0.7 0.2 22 / 0.7);
+ --blume-code-error: oklch(0.72 0.21 22 / 0.22);
+ --blume-code-error-border: oklch(0.7 0.2 22 / 0.7);
+ --blume-code-warning: oklch(0.82 0.16 80 / 0.18);
+ --blume-code-warning-border: oklch(0.82 0.16 80 / 0.7);
+ --blume-code-word: oklch(0.7 0.14 255 / 0.22);
+ --blume-code-word-border: oklch(0.7 0.14 255 / 0.55);
+}
+
+@theme inline {
+ --color-background: var(--blume-background);
+ --color-foreground: var(--blume-foreground);
+ --color-muted: var(--blume-muted);
+ --color-muted-foreground: var(--blume-muted-foreground);
+ --color-border: var(--blume-border);
+ --color-accent: var(--blume-accent);
+ --color-accent-foreground: var(--blume-accent-foreground);
+ --color-action: var(--blume-action);
+ --color-action-foreground: var(--blume-action-foreground);
+ --color-code: var(--blume-code-background);
+ --radius-blume: var(--blume-radius);
+ --container-content: var(--blume-content-width);
+ --font-sans: var(--blume-font-body);
+ --font-mono: var(--blume-font-mono);
+ --font-display: var(--blume-font-display);
+}
+
+@layer base {
+ /* Nothing refuses to shrink below its intrinsic content width. This global
+ min-width reset defuses the classic flex/grid overflow — a long or
+ truncating child forcing its container (and the page) past the viewport
+ edge — so components don't need per-element min-w-0 overrides. */
+ * {
+ min-width: 0;
+ }
+ /* Interactive controls get a pointer cursor unless disabled. */
+ button:not(:disabled),
+ [role="button"]:not(:disabled) {
+ cursor: pointer;
+ }
+ html {
+ scroll-behavior: smooth;
+ scroll-padding-top: 4.5rem;
+ text-rendering: optimizeLegibility;
+ }
+ /* Modal surfaces coordinate through independent root attributes. The lock
+ remains until every owner releases its attribute, while !important lets it
+ temporarily override (and therefore preserve) an authored inline value.
+ Classic (non-overlay) scrollbars would otherwise vanish while locked and
+ shift the page and the centered dialog sideways, so keep their gutter. */
+ html:where([data-blume-nav-open], [data-blume-search-dialog-open]) {
+ overflow: hidden !important;
+ scrollbar-gutter: stable;
+ }
+ /* Headings use the display font (defaults to the body font when unset).
+ The tightened tracking is part of the theme, not the font: display-tuned
+ families bake it into their metrics, but a text family promoted to
+ headings (including the Inter default) reads loose without it. -0.05em
+ was matched visually against Inter Tight, the previous display default. */
+ h1,
+ h2,
+ h3,
+ h4,
+ h5,
+ h6 {
+ font-family: var(--font-display);
+ letter-spacing: -0.05em;
+ }
+ :focus-visible {
+ outline: 2px solid var(--blume-accent);
+ outline-offset: 2px;
+ border-radius: 2px;
+ }
+ body {
+ background-attachment: fixed;
+ background-image: var(--blume-background-image);
+ background-position: center top;
+ background-repeat: var(--blume-background-image-repeat);
+ background-size: var(--blume-background-image-size);
+ }
+ @media (prefers-reduced-motion: reduce) {
+ html {
+ scroll-behavior: auto;
+ }
+ }
+}
+
+/* Same-origin navigations ride Astro's client router (the layouts render
+ ClientRouter), which owns the page-transition styling — including its own
+ prefers-reduced-motion guard — so the theme declares nothing about it. The
+ cross-document view-transition opt-in that briefly lived here is
+ deliberately gone: mixing it with the client router double-animates the
+ full-load navigations the router hands back to the browser. */
+
+/* Code reads left-to-right regardless of page direction; only the surrounding
+ chrome mirrors for RTL. Inline code is isolated so LTR identifiers don't
+ disturb the bidi flow of right-to-left prose. */
+[dir="rtl"] pre,
+[dir="rtl"] .prose pre {
+ direction: ltr;
+ text-align: left;
+}
+[dir="rtl"] :not(pre) > code {
+ unicode-bidi: isolate;
+}
+
+/* The sidebar's rows. A docs site renders every sidebar row on every page,
+ so a large site's HTML is mostly this markup — one utility per row kind
+ instead of the dozen classes each expands to keeps every page (and the
+ build's HTML output) small. Kept in sync with NavTree.astro: a page row,
+ a page-mode group's drill-in row (or, when the group has a page, the
+ link and chevron button it splits into), a collapsible group's summary
+ and the link inside it, and a flat group's heading and the link inside
+ it. */
+@utility blume-nav-link {
+ @apply block rounded-[0.65rem] px-2.5 py-1.5 text-muted-foreground text-sm transition-colors hover:bg-muted hover:text-foreground aria-[current=page]:bg-muted aria-[current=page]:font-medium aria-[current=page]:text-foreground;
+}
+@utility blume-nav-drill {
+ @apply flex w-full items-center gap-2 rounded-[0.65rem] px-2.5 py-1.5 text-start font-medium text-foreground text-sm transition-colors hover:bg-muted aria-[current]:bg-muted;
+}
+/* A routed drill row's label and chevron are separate pills, like a panel's
+ back button and title, so each shows which one the pointer is on. The
+ chevron's padding matches the drill row's, so chevrons line up. */
+@utility blume-nav-drill-link {
+ @apply flex min-w-0 flex-1 items-center gap-2 rounded-[0.65rem] px-2.5 py-1.5 font-medium text-foreground text-sm transition-colors hover:bg-muted aria-[current]:bg-muted;
+}
+@utility blume-nav-drill-toggle {
+ @apply flex shrink-0 items-center justify-center self-stretch rounded-[0.65rem] px-2.5 text-muted-foreground transition-colors hover:bg-muted hover:text-foreground;
+}
+@utility blume-nav-summary {
+ @apply flex cursor-pointer list-none items-center gap-1.5 rounded-[0.65rem] px-2.5 py-1.5 text-muted-foreground text-sm transition-colors hover:bg-muted hover:text-foreground [&::-webkit-details-marker]:hidden;
+}
+@utility blume-nav-summary-link {
+ @apply -my-1 flex flex-1 items-center gap-1.5 rounded py-1 transition-colors aria-[current=page]:text-foreground aria-[current=page]:font-semibold;
+}
+@utility blume-nav-heading {
+ @apply mb-1 flex items-center gap-1.5 px-2.5 py-1.5 font-medium text-foreground text-sm;
+}
+/* The linked heading fills the heading's own box with negative margins, so
+ its hover/active pill is the page row's exactly (same padding and radius)
+ and an unlinked heading beside it keeps the same text position. */
+@utility blume-nav-heading-link {
+ @apply -mx-2.5 -my-1.5 flex flex-1 items-center gap-1.5 rounded-[0.65rem] px-2.5 py-1.5 text-foreground transition-colors hover:bg-muted aria-[current=page]:bg-muted;
+}
+
+/* Theme Tailwind Typography (prose) with Blume tokens. */
+.prose {
+ --tw-prose-body: var(--blume-foreground);
+ --tw-prose-headings: var(--blume-foreground);
+ --tw-prose-lead: var(--blume-muted-foreground);
+ --tw-prose-links: var(--blume-foreground);
+ --tw-prose-bold: var(--blume-foreground);
+ --tw-prose-counters: var(--blume-muted-foreground);
+ --tw-prose-bullets: var(--blume-muted-foreground);
+ --tw-prose-hr: var(--blume-border);
+ --tw-prose-quotes: var(--blume-muted-foreground);
+ --tw-prose-quote-borders: var(--blume-border);
+ --tw-prose-captions: var(--blume-muted-foreground);
+ --tw-prose-code: var(--blume-foreground);
+ --tw-prose-pre-code: var(--blume-foreground);
+ --tw-prose-pre-bg: var(--blume-code-background);
+ --tw-prose-th-borders: var(--blume-border);
+ --tw-prose-td-borders: var(--blume-border);
+ /* Neutralized: the plugin defaults are near-black text and shadow with no
+ dark counterpart (Blume never applies prose-invert), and the kbd rule
+ below draws the badge with Blume tokens instead. */
+ --tw-prose-kbd: inherit;
+ --tw-prose-kbd-shadows: transparent;
+ color: var(--blume-muted-foreground);
+ font-size: 0.875rem;
+ line-height: 1.7;
+ /* An unbreakable run — an API permission, a broker list, a bare URL used as
+ its own link text, an OpenAPI operation title of the form
+ `METHOD /very/long/{path}`, a module-qualified name in a generated
+ reference — paints past the content column, and nothing between the
+ paragraph and the viewport clips it, so it lands in the document's scroll
+ width and drags the page sideways on a phone. Inherited, so headings and
+ inline code are covered too; the only re-declarations below are the
+ opt-in code-wrap mode and Twoslash, which want different values.
+ `break-word` and not `anywhere`: only `anywhere` reduces min-content,
+ which is what sizes `table-layout: auto` columns, so it would resize
+ every table on the site. The flip side is that a token in a table cell
+ still never breaks — the column grows to fit it — so wide tables rely on
+ the .blume-table-scroll wrapper, not on this. */
+ overflow-wrap: break-word;
+}
+
+/* No letter-spacing here: prose headings inherit the base h1-h6 rule's
+ display tracking, same as headings outside the prose column. */
+.prose :where(h1, h2, h3, h4) {
+ font-weight: 500;
+}
+
+.prose :where(h1) {
+ font-size: 3rem;
+ line-height: 1.1;
+ margin: 0 0 1rem;
+}
+
+.prose :where(h2) {
+ font-size: 1.875rem;
+ line-height: 1.2;
+ margin-top: 3rem;
+}
+
+.prose :where(h3) {
+ font-size: 1.25rem;
+ line-height: 1.35;
+}
+
+.prose :where(h4) {
+ color: var(--blume-muted-foreground);
+ font-size: 0.875rem;
+ line-height: 1.4;
+}
+
+.prose :where(h1, h2, h3, h4, h5, h6) a {
+ color: inherit;
+ font-weight: inherit;
+ text-decoration: none;
+}
+
+/* Auto-generated heading permalinks (markdown.headingAnchors): the whole
+ heading links to its own id, with a muted “#” revealed on hover or keyboard
+ focus so the link is discoverable without cluttering the heading at rest. */
+.prose :where(h2, h3, h4, h5, h6) a.blume-heading-anchor::after {
+ content: "#";
+ color: var(--blume-muted-foreground);
+ margin-inline-start: 0.35em;
+ opacity: 0;
+ transition: opacity 0.15s ease;
+}
+.prose :where(h2, h3, h4, h5, h6) a.blume-heading-anchor:hover::after,
+.prose :where(h2, h3, h4, h5, h6) a.blume-heading-anchor:focus-visible::after {
+ opacity: 1;
+}
+
+/* A [toc]-marked heading exists only for the table of contents: it stays in
+ the flow as a zero-height, invisible anchor target so its TOC entry (and any
+ deep link) still has somewhere to scroll to, without rendering on the page. */
+.prose :where(.blume-toc-only) {
+ border: 0;
+ height: 0;
+ margin: 0;
+ overflow: hidden;
+ padding: 0;
+ visibility: hidden;
+}
+
+.prose :where(h2:first-child) {
+ border-top: 0;
+ padding-top: 0;
+}
+
+.prose :where(p, ul, ol) {
+ margin-top: 1rem;
+ margin-bottom: 1rem;
+}
+
+.prose :where(strong) {
+ font-weight: 600;
+}
+
+.prose :where(a) {
+ color: var(--blume-foreground);
+ font-weight: 500;
+ text-decoration-line: underline;
+ text-decoration-style: dotted;
+ text-decoration-thickness: 1px;
+ text-underline-offset: 0.2em;
+}
+
+.prose :where(a[data-blume-card]) {
+ color: inherit;
+ font-weight: inherit;
+ text-decoration: none;
+}
+
+/* External Markdown links (markdown.externalLinks) open in a new tab and trail
+ the same Lucide arrow-up-right the featured sidebar links use, as a mask so it
+ takes the link's color. A link wrapping only an image (a badge, a logo)
+ skips the arrow. */
+.prose :where(a[data-blume-external]:not(:has(img, svg)))::after {
+ background-color: currentColor;
+ content: "";
+ display: inline-block;
+ height: 0.75em;
+ margin-inline-start: 0.125em;
+ -webkit-mask: url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20stroke%3D%22%23000%22%20stroke-linecap%3D%22round%22%20stroke-linejoin%3D%22round%22%20stroke-width%3D%222%22%3E%3Cpath%20d%3D%22M7%207h10v10M7%2017L17%207%22%2F%3E%3C%2Fsvg%3E") center / contain no-repeat;
+ mask: url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20stroke%3D%22%23000%22%20stroke-linecap%3D%22round%22%20stroke-linejoin%3D%22round%22%20stroke-width%3D%222%22%3E%3Cpath%20d%3D%22M7%207h10v10M7%2017L17%207%22%2F%3E%3C%2Fsvg%3E") center / contain no-repeat;
+ opacity: 0.6;
+ width: 0.75em;
+}
+
+.prose :where(hr) {
+ margin: 2.5rem 0;
+}
+
+.prose :where(pre) {
+ background: transparent;
+ border: 1px solid var(--blume-border);
+ border-radius: var(--blume-radius);
+ color: var(--blume-foreground);
+ font-family: var(--font-mono);
+ font-size: 0.8125rem;
+ line-height: 1.55;
+ margin: 1.5rem 0;
+ overflow-x: auto;
+ padding: 1rem 0;
+ position: relative;
+}
+
+.prose :where(pre.astro-code) {
+ background: transparent !important;
+ color: var(--shiki-light, var(--blume-foreground)) !important;
+}
+
+:root[data-theme="dark"] .prose :where(pre.astro-code) {
+ color: var(--shiki-dark, var(--blume-foreground)) !important;
+}
+
+.prose :where(pre.astro-code span) {
+ background: transparent !important;
+ color: var(--shiki-light, inherit);
+ font-style: var(--shiki-light-font-style, inherit);
+ font-weight: var(--shiki-light-font-weight, inherit);
+ text-decoration: var(--shiki-light-text-decoration, inherit);
+}
+
+:root[data-theme="dark"] .prose :where(pre.astro-code span) {
+ color: var(--shiki-dark, inherit);
+ font-style: var(--shiki-dark-font-style, inherit);
+ font-weight: var(--shiki-dark-font-weight, inherit);
+ text-decoration: var(--shiki-dark-text-decoration, inherit);
+}
+
+/* The Diff component (@pierre/diffs) renders into a declarative shadow root and
+ themes itself with light-dark(). Drive that from Blume's data-theme instead
+ of the OS, scoped to the diff host: the custom property pierces the shadow
+ boundary, where the appended host color-scheme rule consumes it. */
+blume-diff {
+ --blume-diff-color-scheme: light;
+ display: block;
+}
+
+:root[data-theme="dark"] blume-diff {
+ --blume-diff-color-scheme: dark;
+}
+
+.prose :where(pre[data-language]) {
+ padding-top: 3.75rem;
+}
+
+.prose :where(pre[data-language])::before {
+ align-items: center;
+ border-bottom: 1px solid var(--blume-border);
+ color: var(--blume-muted-foreground);
+ content: attr(data-language);
+ display: flex;
+ font-family: var(--font-sans);
+ font-size: 0.75rem;
+ font-weight: 500;
+ height: 2.75rem;
+ left: 0;
+ padding: 0 3.25rem 0 1rem;
+ position: absolute;
+ right: 0;
+ top: 0;
+}
+
+.prose :where(pre[data-title])::before {
+ content: attr(data-title);
+}
+
+/* Language icon (simple-icons) sits at the header's left edge; the label shifts
+ right to make room. Injected at build time by the language-icon transformer.
+ The icon is gated on data-language (the header bar), not just data-icon: the
+ transformer runs whenever icons are on, but a header-less standalone
+ CodeBlock (no title) never gets data-language, so without this gate the
+ absolutely-positioned icon would overlap the first code line. Fenced code
+ and titled blocks always have a header, so the icon shows there. */
+.prose > :where(pre[data-language][data-icon])::after {
+ content: "";
+ height: 0.875rem;
+ left: 1rem;
+ -webkit-mask-position: center;
+ -webkit-mask-repeat: no-repeat;
+ -webkit-mask-size: contain;
+ mask-position: center;
+ mask-repeat: no-repeat;
+ mask-size: contain;
+ pointer-events: none;
+ position: absolute;
+ top: 0.875rem;
+ width: 0.875rem;
+}
+
+.prose > :where(pre[data-language][data-icon])::before {
+ padding-left: 2.5rem;
+}
+
+/* One mask rule per language the site's Markdown uses (see
+ markdown/language-icon.ts); the ::after above only gets a color and a mask
+ from these, so a block whose language has no rule paints nothing. */
+.prose > :where(pre[data-language][data-icon="javascript"])::after {
+ background-color: var(--blume-muted-foreground);
+ -webkit-mask-image: url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2024%2024%22%3E%3Cpath%20d%3D%22M0%200h24v24H0V0zm22.034%2018.276c-.175-1.095-.888-2.015-3.003-2.873-.736-.345-1.554-.585-1.797-1.14-.091-.33-.105-.51-.046-.705.15-.646.915-.84%201.515-.66.39.12.75.42.976.9%201.034-.676%201.034-.676%201.755-1.125-.27-.42-.404-.601-.586-.78-.63-.705-1.469-1.065-2.834-1.034l-.705.089c-.676.165-1.32.525-1.71%201.005-1.14%201.291-.811%203.541.569%204.471%201.365%201.02%203.361%201.244%203.616%202.205.24%201.17-.87%201.545-1.966%201.41-.811-.18-1.26-.586-1.755-1.336l-1.83%201.051c.21.48.45.689.81%201.109%201.74%201.756%206.09%201.666%206.871-1.004.029-.09.24-.705.074-1.65l.046.067zm-8.983-7.245h-2.248c0%201.938-.009%203.864-.009%205.805%200%201.232.063%202.363-.138%202.711-.33.689-1.18.601-1.566.48-.396-.196-.597-.466-.83-.855-.063-.105-.11-.196-.127-.196l-1.825%201.125c.305.63.75%201.172%201.324%201.517.855.51%202.004.675%203.207.405.783-.226%201.458-.691%201.811-1.411.51-.93.402-2.07.397-3.346.012-2.054%200-4.109%200-6.179l.004-.056z%22%2F%3E%3C%2Fsvg%3E");
+ mask-image: url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2024%2024%22%3E%3Cpath%20d%3D%22M0%200h24v24H0V0zm22.034%2018.276c-.175-1.095-.888-2.015-3.003-2.873-.736-.345-1.554-.585-1.797-1.14-.091-.33-.105-.51-.046-.705.15-.646.915-.84%201.515-.66.39.12.75.42.976.9%201.034-.676%201.034-.676%201.755-1.125-.27-.42-.404-.601-.586-.78-.63-.705-1.469-1.065-2.834-1.034l-.705.089c-.676.165-1.32.525-1.71%201.005-1.14%201.291-.811%203.541.569%204.471%201.365%201.02%203.361%201.244%203.616%202.205.24%201.17-.87%201.545-1.966%201.41-.811-.18-1.26-.586-1.755-1.336l-1.83%201.051c.21.48.45.689.81%201.109%201.74%201.756%206.09%201.666%206.871-1.004.029-.09.24-.705.074-1.65l.046.067zm-8.983-7.245h-2.248c0%201.938-.009%203.864-.009%205.805%200%201.232.063%202.363-.138%202.711-.33.689-1.18.601-1.566.48-.396-.196-.597-.466-.83-.855-.063-.105-.11-.196-.127-.196l-1.825%201.125c.305.63.75%201.172%201.324%201.517.855.51%202.004.675%203.207.405.783-.226%201.458-.691%201.811-1.411.51-.93.402-2.07.397-3.346.012-2.054%200-4.109%200-6.179l.004-.056z%22%2F%3E%3C%2Fsvg%3E");
+}
+.prose > :where(pre[data-language][data-icon="python"])::after {
+ background-color: var(--blume-muted-foreground);
+ -webkit-mask-image: url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2024%2024%22%3E%3Cpath%20d%3D%22M14.25.18l.9.2.73.26.59.3.45.32.34.34.25.34.16.33.1.3.04.26.02.2-.01.13V8.5l-.05.63-.13.55-.21.46-.26.38-.3.31-.33.25-.35.19-.35.14-.33.1-.3.07-.26.04-.21.02H8.77l-.69.05-.59.14-.5.22-.41.27-.33.32-.27.35-.2.36-.15.37-.1.35-.07.32-.04.27-.02.21v3.06H3.17l-.21-.03-.28-.07-.32-.12-.35-.18-.36-.26-.36-.36-.35-.46-.32-.59-.28-.73-.21-.88-.14-1.05-.05-1.23.06-1.22.16-1.04.24-.87.32-.71.36-.57.4-.44.42-.33.42-.24.4-.16.36-.1.32-.05.24-.01h.16l.06.01h8.16v-.83H6.18l-.01-2.75-.02-.37.05-.34.11-.31.17-.28.25-.26.31-.23.38-.2.44-.18.51-.15.58-.12.64-.1.71-.06.77-.04.84-.02%201.27.05zm-6.3%201.98l-.23.33-.08.41.08.41.23.34.33.22.41.09.41-.09.33-.22.23-.34.08-.41-.08-.41-.23-.33-.33-.22-.41-.09-.41.09zm13.09%203.95l.28.06.32.12.35.18.36.27.36.35.35.47.32.59.28.73.21.88.14%201.04.05%201.23-.06%201.23-.16%201.04-.24.86-.32.71-.36.57-.4.45-.42.33-.42.24-.4.16-.36.09-.32.05-.24.02-.16-.01h-8.22v.82h5.84l.01%202.76.02.36-.05.34-.11.31-.17.29-.25.25-.31.24-.38.2-.44.17-.51.15-.58.13-.64.09-.71.07-.77.04-.84.01-1.27-.04-1.07-.14-.9-.2-.73-.25-.59-.3-.45-.33-.34-.34-.25-.34-.16-.33-.1-.3-.04-.25-.02-.2.01-.13v-5.34l.05-.64.13-.54.21-.46.26-.38.3-.32.33-.24.35-.2.35-.14.33-.1.3-.06.26-.04.21-.02.13-.01h5.84l.69-.05.59-.14.5-.21.41-.28.33-.32.27-.35.2-.36.15-.36.1-.35.07-.32.04-.28.02-.21V6.07h2.09l.14.01zm-6.47%2014.25l-.23.33-.08.41.08.41.23.33.33.23.41.08.41-.08.33-.23.23-.33.08-.41-.08-.41-.23-.33-.33-.23-.41-.08-.41.08z%22%2F%3E%3C%2Fsvg%3E");
+ mask-image: url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%2024%2024%22%3E%3Cpath%20d%3D%22M14.25.18l.9.2.73.26.59.3.45.32.34.34.25.34.16.33.1.3.04.26.02.2-.01.13V8.5l-.05.63-.13.55-.21.46-.26.38-.3.31-.33.25-.35.19-.35.14-.33.1-.3.07-.26.04-.21.02H8.77l-.69.05-.59.14-.5.22-.41.27-.33.32-.27.35-.2.36-.15.37-.1.35-.07.32-.04.27-.02.21v3.06H3.17l-.21-.03-.28-.07-.32-.12-.35-.18-.36-.26-.36-.36-.35-.46-.32-.59-.28-.73-.21-.88-.14-1.05-.05-1.23.06-1.22.16-1.04.24-.87.32-.71.36-.57.4-.44.42-.33.42-.24.4-.16.36-.1.32-.05.24-.01h.16l.06.01h8.16v-.83H6.18l-.01-2.75-.02-.37.05-.34.11-.31.17-.28.25-.26.31-.23.38-.2.44-.18.51-.15.58-.12.64-.1.71-.06.77-.04.84-.02%201.27.05zm-6.3%201.98l-.23.33-.08.41.08.41.23.34.33.22.41.09.41-.09.33-.22.23-.34.08-.41-.08-.41-.23-.33-.33-.22-.41-.09-.41.09zm13.09%203.95l.28.06.32.12.35.18.36.27.36.35.35.47.32.59.28.73.21.88.14%201.04.05%201.23-.06%201.23-.16%201.04-.24.86-.32.71-.36.57-.4.45-.42.33-.42.24-.4.16-.36.09-.32.05-.24.02-.16-.01h-8.22v.82h5.84l.01%202.76.02.36-.05.34-.11.31-.17.29-.25.25-.31.24-.38.2-.44.17-.51.15-.58.13-.64.09-.71.07-.77.04-.84.01-1.27-.04-1.07-.14-.9-.2-.73-.25-.59-.3-.45-.33-.34-.34-.25-.34-.16-.33-.1-.3-.04-.25-.02-.2.01-.13v-5.34l.05-.64.13-.54.21-.46.26-.38.3-.32.33-.24.35-.2.35-.14.33-.1.3-.06.26-.04.21-.02.13-.01h5.84l.69-.05.59-.14.5-.21.41-.28.33-.32.27-.35.2-.36.15-.36.1-.35.07-.32.04-.28.02-.21V6.07h2.09l.14.01zm-6.47%2014.25l-.23.33-.08.41.08.41.23.33.33.23.41.08.41-.08.33-.23.23-.33.08-.41-.08-.41-.23-.33-.33-.23-.41-.08-.41.08z%22%2F%3E%3C%2Fsvg%3E");
+}
+
+.prose :where(pre code) {
+ background: transparent;
+ border-radius: 0;
+ color: inherit;
+ font-size: inherit;
+ font-weight: 400;
+ padding: 0;
+}
+
+/* Long lines scroll inside the code element, not the pre: the pre stays static
+ so its absolute header bar (::before) and copy button don't drift with the
+ scroll. The pre's horizontal padding lives here so content still scrolls
+ edge-to-edge past it. Two contexts opt out: twoslash blocks (popups must
+ escape any scroll container — see theme/twoslash.ts), and the API request
+ panel, which owns its code layout and keeps the copy control in the panel
+ header. Every other component that hosts a code block — Tabs, CodeGroup,
+ Steps, Callout, Card, Accordion — is real prose content and keeps the inset,
+ even though its chrome wrapper is not-prose. */
+.prose :where(pre:not(.twoslash, .twoslash pre, blume-panel-tabs *) > code) {
+ display: block;
+ /* Tall blocks scroll vertically in place instead of taking the page. Both
+ axes live on the code element for the same reason: the pre stays static
+ so the header bar and copy button never drift with the scroll. */
+ max-height: 24rem;
+ overflow: auto;
+ /* The small bottom inset keeps the horizontal thumb off the last line's
+ descenders now that scrollbars are visible. The pre gives up the same
+ amount below (next rule), so the inset adds no height to the block. */
+ padding: 0 1.25rem 0.375rem;
+ /* Thin theme-colored scrollbars, matching the sidebar treatment, so a
+ height-capped block reads as scrollable instead of simply ending.
+ Safari before 18.2 supports neither property and falls back to the
+ platform-default scrollbar — acceptable, since macOS overlays it. */
+ scrollbar-color: var(--blume-border) transparent;
+ scrollbar-width: thin;
+}
+
+/* The scroller's bottom inset comes out of the pre's own block padding: the
+ text still sits one full inset above the frame's bottom edge, the thumb sits
+ in the gap, and a one-line command is no taller than it was before the
+ scroller existed. */
+.prose :where(pre:not(.twoslash, .twoslash pre, blume-panel-tabs *):has(> code)) {
+ padding-bottom: calc(1rem - 0.375rem);
+}
+
+/* The dark border token is too close to the page background to read as a
+ scrollbar thumb; derive a brighter one from the muted foreground instead. */
+:root[data-theme="dark"]
+ .prose
+ :where(pre:not(.twoslash, .twoslash pre, blume-panel-tabs *) > code) {
+ scrollbar-color: color-mix(in oklab, var(--blume-muted-foreground) 55%, transparent)
+ transparent;
+}
+
+/* Word wrap: long lines wrap instead of scrolling, on every block
+ (markdown.code.wrap, an attribute on
) or on one (```ts wrap, which
+ Shiki's fence-meta reader sets as data-wrap). Default code keeps
+ `white-space: pre`. */
+[data-blume-code-wrap] pre,
+[data-blume-code-wrap] pre code,
+pre[data-wrap],
+pre[data-wrap] code {
+ overflow-wrap: break-word;
+ white-space: pre-wrap;
+}
+
+/* An expandable block (```ts expandable) starts collapsed to its first lines,
+ faded out toward the cut, with a toggle under it (added by the layout
+ script). Opened, it shows in full: no height cap and no inner scroll. The
+ collapse needs that script to undo it, so a page without scripting shows
+ the block the ordinary way. */
+@media (scripting: enabled) {
+ .prose pre[data-expandable]:not([data-expanded]) > code {
+ max-height: calc(10lh + 0.375rem);
+ mask-image: linear-gradient(to bottom, #000 calc(100% - 3lh), transparent);
+ overflow-y: hidden;
+ }
+}
+
+.prose pre[data-expandable][data-expanded] > code {
+ max-height: none;
+}
+
+[data-blume-code-expand] {
+ align-items: center;
+ color: var(--blume-muted-foreground);
+ display: flex;
+ font-family: var(--font-sans);
+ font-size: 0.75rem;
+ font-weight: 500;
+ gap: 0.25rem;
+ justify-content: center;
+ padding-top: 0.5rem;
+ width: 100%;
+}
+
+[data-blume-code-expand]:hover {
+ color: var(--blume-foreground);
+}
+
+[data-blume-code-expand] svg {
+ transition: transform 150ms;
+}
+
+[data-blume-code-expand][aria-expanded="true"] svg {
+ transform: rotate(180deg);
+}
+
+.prose :where(table) {
+ font-size: 0.8125rem;
+}
+
+/* Scroll wide tables in place; the wrapper is added by the blume:table-wrap hast
+ plugin. The border+radius frames the scroll viewport so a clipped wide table
+ reads as scrollable rather than cut off, and carries the table's vertical
+ rhythm (the inner table's own margin is zeroed so it fills the frame). */
+.blume-table-scroll {
+ overflow-x: auto;
+ -webkit-overflow-scrolling: touch;
+ margin: 1.5rem 0;
+ border: 1px solid var(--blume-border);
+ border-radius: var(--blume-radius);
+}
+.blume-table-scroll > table {
+ margin: 0;
+}
+/* Restore inner padding on every cell. Typography zeroes the first/last cell's
+ inline padding so a borderless table aligns to the prose margin; inside the
+ framed wrapper that leaves edge text touching the border. The :is() selector
+ outweighs Typography's :where()-scoped rules so the outer columns get it too.
+
+ The block padding is 0.75rem against a table line-height near 1.7: at 0.5rem a
+ cell whose content wrapped put MORE space between its own two lines than
+ between itself and the next row, so a table of wrapping cells read as one
+ block rather than as rows. The inline padding stays where it was, deliberately
+ — widening it comes out of column width in a capped article, and on one corpus
+ that pushed cells fitting on two lines onto three. */
+.blume-table-scroll :is(th, td) {
+ padding: 0.75rem;
+}
+/* Keep column labels on one line so a two-word header does not wrap into a
+ ragged stack; the table just scrolls a little wider instead. Body cells keep
+ wrapping so a wide table stays a scroll away, not an endless horizontal run. */
+.blume-table-scroll th {
+ white-space: nowrap;
+}
+
+/* GFM renders cells as directly, which the descendant form alone
+ never matches (a cell is not its own descendant). */
+.prose :where(td, th) > code,
+.prose :where(td, th) :not(pre) > code {
+ white-space: normal;
+}
+
+blume-tabs pre,
+.not-prose > div > pre {
+ background: var(--blume-code-background);
+}
+
+/* The Component source pane fills its tab's fixed height (set inline to match the
+ preview tab) and scrolls the inner code element, leaving the pre static so the
+ copy button — absolute, pinned to the pre — doesn't drift while it scrolls. */
+pre.blume-source {
+ display: flex;
+ flex-direction: column;
+ height: 100%;
+ overflow: hidden;
+}
+
+pre.blume-source > code {
+ flex: 1;
+ /* The pane's measured height is authoritative (it can exceed the prose
+ 24rem cap), so undo the generic prose max-height: the code must fill
+ whatever height the tab was given. */
+ max-height: none;
+ min-height: 0;
+ overflow: auto;
+}
+
+/* Code blocks inside tabs sit flush; the tab container owns the frame. These
+ rules are unlayered like the base `pre` styles, so they win over Tailwind's
+ layered utilities (which a class on the panel cannot). The second selector
+ covers , where each code block IS the panel (a direct child of the
+ tab content wrapper) rather than a nested inside a . */
+blume-tabs [data-blume-tab-panel] > pre,
+blume-tabs [data-blume-tab-content] > pre {
+ /* Important to beat the astro-code background rule above; the tab container
+ owns the surface, so the code sits directly on it. */
+ background: transparent !important;
+ border: 0;
+ border-radius: 0;
+ margin: 0;
+}
+
+/* These contexts drop the language bar (`::before` is cleared below), so the
+ bar's padding goes with it and the block keeps the plain inset. Unscoped on
+ purpose: wraps its pre in its own `.prose` below the component
+ chrome, so without this a titled block on a page with no prose column
+ (PageLayout) would keep the 3.75rem bar padding under a cleared bar. */
+blume-tabs pre[data-language],
+.not-prose pre[data-language] {
+ padding-top: 1rem;
+}
+
+/* Where no language bar holds the copy button — a flush block in tabs or a
+ not-prose component, a bar-less block in prose — the docs layout still pins
+ one over the block's top-right corner, so a first line long enough to reach
+ it would end underneath. A code switcher (CodeGroup, ts2js) hosts the button
+ in its tab strip instead and needs nothing here; for every block that keeps
+ an overlay button, give the scrolling code element enough end padding that
+ the line's tail clears the button at the end of its scroll. Keyed on the
+ injected button itself (which the playground's client-created response pre
+ never gets), and `.prose`-scoped to outrank the base `:where(pre code)`
+ inset. Not a vertical inset — that reserved a strip above every one-line
+ command and read as a rendering bug. */
+.prose :is(blume-tabs pre, .not-prose pre, pre:not([data-language])):not(.twoslash, .twoslash pre, blume-panel-tabs *):has(> [data-blume-copy]) > code {
+ padding-inline-end: 3.5rem;
+}
+
+/* With the assistant on, an Ask button sits beside the copy button, so the
+ line's tail clears both. */
+.prose :is(blume-tabs pre, .not-prose pre, pre:not([data-language])):not(.twoslash, .twoslash pre, blume-panel-tabs *):has(> [data-blume-ask]) > code {
+ padding-inline-end: 5.75rem;
+}
+
+blume-tabs pre[data-language]::before,
+.not-prose pre[data-language]::before {
+ content: none;
+}
+
+/* Pre-hydration layout-shift guard. The horizontal trigger row and
+ single-panel visibility are built by the custom element's JS:
+ until it upgrades, the trigger row is empty and every panel is in normal flow,
+ so the browser paints all panels stacked vertically (e.g. all four
+ package-manager commands at once) before JS hides them. While the element is
+ :not(:defined), show only the first panel and reserve the trigger row's height
+ so nothing jumps when JS takes over. The child selector covers both
+ panels and raw code blocks (which only gain
+ [data-blume-tab-panel] from JS). */
+blume-tabs:not(:defined) [data-blume-tab-content] > *:nth-child(n + 2) {
+ display: none;
+}
+
+blume-tabs:not(:defined) [data-blume-tablist] {
+ /* Matches a trigger's height: py-2.5 + text-xs line-height + border-b-2. */
+ min-height: 2.375rem;
+}
+
+blume-tabs:not(:defined)[data-dropdown="true"] [data-blume-tablist] {
+ /* Dropdown variant: p-3 padding around a select (py-2 + text-sm + border). */
+ min-height: 3.875rem;
+}
+
+/* Code blocks inside request/response example panels sit flush; the panel owns
+ the frame. Unlayered (like the tab rules) so they beat the base `pre` styles
+ that a class on the panel cannot. */
+[data-blume-code-panel] pre {
+ background: transparent !important;
+ border: 0;
+ border-radius: 0;
+ margin: 0;
+}
+
+/* Opt-in line numbers (```ts file.ts lineNumbers): a counter-driven gutter that
+ works in prose and flush (tab / API panel) code blocks alike. */
+pre[data-line-numbers] code {
+ counter-reset: line;
+}
+
+pre[data-line-numbers] .line::before {
+ color: var(--blume-muted-foreground);
+ content: counter(line);
+ counter-increment: line;
+ display: inline-block;
+ margin-right: 1.25rem;
+ text-align: right;
+ user-select: none;
+ width: 1.25rem;
+}
+
+/* Shiki notation transformers (on by default). The notation comments are
+ stripped from the output; these style the classes they leave behind. A styled
+ line becomes a full-width inline-block so its background fills the row; plain
+ lines are left untouched, so blocks without notations render as before. */
+.line.highlighted,
+.line.diff {
+ display: inline-block;
+ width: 100%;
+}
+
+/* The backgrounds need !important to beat the `pre.astro-code span` rule above,
+ which forces token spans transparent; these line/word spans are exceptions. */
+.line.highlighted {
+ background-color: var(--blume-code-highlight) !important;
+ box-shadow: inset 2px 0 0 0 var(--blume-code-highlight-border);
+}
+
+.line.diff.add {
+ background-color: var(--blume-code-add) !important;
+ box-shadow: inset 2px 0 0 0 var(--blume-code-add-border);
+}
+
+.line.diff.remove {
+ background-color: var(--blume-code-remove) !important;
+ box-shadow: inset 2px 0 0 0 var(--blume-code-remove-border);
+}
+
+/* Error and warning lines (`// [!code error]`, `// [!code warning]`) are
+ highlighted lines with a level class, tinted red and amber. */
+.line.highlighted.error {
+ background-color: var(--blume-code-error) !important;
+ box-shadow: inset 2px 0 0 0 var(--blume-code-error-border);
+}
+
+.line.highlighted.warning {
+ background-color: var(--blume-code-warning) !important;
+ box-shadow: inset 2px 0 0 0 var(--blume-code-warning-border);
+}
+
+/* Word highlight (`// [!code word:x]`) wraps matches in an inline span. The
+ element selector keeps it ahead of the transparent token-span rule. */
+span.highlighted-word {
+ background-color: var(--blume-code-word) !important;
+ border-radius: 0.25rem;
+ box-shadow: 0 0 0 1px var(--blume-code-word-border);
+ padding: 0.1em 0.2em;
+}
+
+/* Focus (`// [!code focus]`): dim and blur the rest; reveal on hover. */
+pre:has(.line.focused) .line:not(.focused) {
+ filter: blur(0.085rem);
+ opacity: 0.5;
+ transition:
+ filter 0.2s ease,
+ opacity 0.2s ease;
+}
+
+pre:has(.line.focused):hover .line:not(.focused) {
+ filter: none;
+ opacity: 1;
+}
+
+@media (prefers-reduced-motion: reduce) {
+ pre:has(.line.focused) .line:not(.focused) {
+ transition: none;
+ }
+}
+
+@media (max-width: 640px) {
+ .prose :where(h1) {
+ font-size: 2.25rem;
+ }
+
+ .prose :where(h2) {
+ font-size: 1.625rem;
+ }
+}
+
+.prose :not(pre) > code:not(:where([class~="not-prose"] *)) {
+ background: var(--blume-code-background);
+ padding: 0.15em 0.35em;
+ border-radius: 0.3rem;
+ font-family: var(--font-mono);
+ font-weight: 500;
+}
+
+.prose :not(pre) > code::before,
+.prose :not(pre) > code::after {
+ content: none;
+}
+
+/* Keyboard keys () render as the same bordered badge the search dialog
+ uses for its shortcut hints. Typography's own kbd rule is scoped with
+ :not(.not-prose *), so it never reaches a key inside , ,
+ , or any other not-prose component; this rule is deliberately
+ unscoped from that exclusion so a key looks the same everywhere in the
+ content column. Unlayered, so it outranks the plugin's layered rule. */
+.prose kbd {
+ background: var(--blume-muted);
+ border: 1px solid var(--blume-border);
+ border-radius: 0.25rem;
+ box-shadow: none;
+ color: inherit;
+ font-family: var(--font-mono);
+ font-size: 0.85em;
+ font-weight: 500;
+ padding: 0.125rem 0.25rem;
+}
+
+/* Inline code highlighting: Shiki colors the tokens of a `code`{:lang} snippet
+ via the same dual-theme CSS variables as fenced blocks, keeping the inline
+ pill background. Always on — it only fires on the trailing {:lang} marker. */
+.prose code.blume-inline-code span {
+ color: var(--shiki-light);
+}
+
+:root[data-theme="dark"] .prose code.blume-inline-code span {
+ color: var(--shiki-dark);
+}
+
+/* Print / "Save as PDF" (the page-actions Export → PDF runs window.print()).
+ Strip the surrounding chrome so the printout is just the article. Scoped to
+ the chrome itself, never bare `header`/`aside`: content renders those too
+ (every Callout and Panel is an , every changelog Update has a
+ ), and they belong in the printout. `body > header` and
+ `body > footer` keep `layout.Header` and `layout.Footer` overrides out
+ as well. Everything after the article (directory and related-page cards,
+ last updated, feedback, prev/next) is page chrome too, and so are the
+ copy, ask, and expand buttons on code blocks. */
+@media print {
+ [data-blume-banner],
+ [data-blume-header],
+ body > header,
+ [data-blume-nav-drawer],
+ [data-blume-toc],
+ [data-blume-assistant-panel],
+ [data-blume-page-actions],
+ #blume-content > nav,
+ #blume-content > details,
+ [data-blume-page-end],
+ [data-blume-footer],
+ body > footer,
+ [data-blume-copy],
+ [data-blume-ask],
+ [data-blume-code-expand] {
+ display: none !important;
+ }
+
+ /* Paper can't scroll: uncap the code scroller so long blocks print in
+ full, same as tab panels are force-expanded below. */
+ .prose :where(pre:not(.twoslash, .twoslash pre, blume-panel-tabs *) > code) {
+ max-height: none;
+ overflow: visible;
+ }
+
+ /* A collapsed `expandable` block prints open: its toggle is hidden above,
+ so the collapse would cut the block off at its first lines. Same
+ selector as the collapse, so this later rule wins. */
+ .prose pre[data-expandable]:not([data-expanded]) > code {
+ mask-image: none;
+ max-height: none;
+ overflow-y: visible;
+ }
+
+ /* The theme script switches a dark page to light for printing
+ (`beforeprint`); a color transition would print mid-fade. */
+ *,
+ *::before,
+ *::after {
+ transition: none !important;
+ }
+
+ #blume-content {
+ padding: 0 !important;
+ }
+
+ .prose {
+ margin: 0 !important;
+ max-width: none !important;
+ }
+
+ /* Reveal collapsed tab panels so their content isn't dropped from the print. */
+ blume-tabs [data-blume-tab-panel] {
+ display: block !important;
+ }
+
+ /* Keep code/callout backgrounds (subject to the dialog's "Background graphics"
+ toggle). */
+ pre,
+ .prose :not(pre) > code {
+ -webkit-print-color-adjust: exact;
+ print-color-adjust: exact;
+ }
+}
+
+/* Twoslash rich-renderer styles (used by fences with the `twoslash` meta). */
+/* ===== Basic ===== */
+:root {
+ --twoslash-border-color: #8888;
+ --twoslash-underline-color: currentColor;
+ --twoslash-highlighted-border: #c37d0d50;
+ --twoslash-highlighted-bg: #c37d0d20;
+ --twoslash-popup-bg: #f8f8f8;
+ --twoslash-popup-color: inherit;
+ --twoslash-popup-shadow: rgba(0, 0, 0, 0.08) 0px 1px 4px;
+ --twoslash-docs-color: #888;
+ --twoslash-docs-font: sans-serif;
+ --twoslash-code-font: inherit;
+ --twoslash-code-font-size: 1em;
+ --twoslash-matched-color: inherit;
+ --twoslash-unmatched-color: #888;
+ --twoslash-cursor-color: #8888;
+ --twoslash-error-color: #d45656;
+ --twoslash-error-bg: #d4565620;
+ --twoslash-warn-color: #c37d0d;
+ --twoslash-warn-bg: #c37d0d20;
+ --twoslash-tag-color: #3772cf;
+ --twoslash-tag-bg: #3772cf20;
+ --twoslash-tag-warn-color: var(--twoslash-warn-color);
+ --twoslash-tag-warn-bg: var(--twoslash-warn-bg);
+ --twoslash-tag-annotate-color: #1ba673;
+ --twoslash-tag-annotate-bg: #1ba67320;
+}
+
+/* Respect people's wishes to not have animations */
+@media (prefers-reduced-motion: reduce) {
+ .twoslash * {
+ transition: none !important;
+ }
+}
+
+/* ===== Hover Info ===== */
+.twoslash:hover .twoslash-hover {
+ border-color: var(--twoslash-underline-color);
+}
+
+.twoslash .twoslash-hover {
+ border-bottom: 1px dotted transparent;
+ transition-timing-function: ease;
+ transition: border-color 0.3s;
+ position: relative;
+}
+
+.twoslash .twoslash-popup-container {
+ position: absolute;
+ opacity: 0;
+ display: inline-flex;
+ flex-direction: column;
+ transform: translateY(1.1em);
+ background: var(--twoslash-popup-bg);
+ color: var(--twoslash-popup-color);
+ border: 1px solid var(--twoslash-border-color);
+ transition: opacity 0.3s;
+ border-radius: 4px;
+ pointer-events: none;
+ z-index: 10;
+ user-select: none;
+ text-align: left;
+ box-shadow: var(--twoslash-popup-shadow);
+}
+
+.twoslash .twoslash-query-persisted .twoslash-popup-container {
+ z-index: 9;
+ transform: translateY(1.5em);
+}
+
+.twoslash .twoslash-hover:hover .twoslash-popup-container,
+.twoslash .twoslash-error-hover:hover .twoslash-popup-container,
+.twoslash .twoslash-query-persisted .twoslash-popup-container,
+.twoslash .twoslash-query-line .twoslash-popup-container {
+ opacity: 1;
+ pointer-events: auto;
+}
+
+.twoslash .twoslash-popup-container:hover {
+ user-select: auto;
+}
+
+.twoslash .twoslash-popup-arrow {
+ position: absolute;
+ top: -4px;
+ left: 1em;
+ border-top: 1px solid var(--twoslash-border-color);
+ border-right: 1px solid var(--twoslash-border-color);
+ background: var(--twoslash-popup-bg);
+ transform: rotate(-45deg);
+ width: 6px;
+ height: 6px;
+ pointer-events: none;
+}
+
+.twoslash .twoslash-popup-code,
+.twoslash .twoslash-popup-error,
+.twoslash .twoslash-popup-docs {
+ padding: 6px 8px !important;
+}
+
+.twoslash .twoslash-popup-code {
+ font-family: var(--twoslash-code-font);
+ font-size: var(--twoslash-code-font-size);
+}
+
+.twoslash .twoslash-popup-docs {
+ color: var(--twoslash-docs-color);
+ font-family: var(--twoslash-docs-font);
+ font-size: 0.8em;
+ border-top: 1px solid var(--twoslash-border-color);
+}
+
+.twoslash .twoslash-popup-error {
+ color: var(--twoslash-error-color);
+ background-color: var(--twoslash-error-bg);
+ font-family: var(--twoslash-docs-font);
+ font-size: 0.8em;
+}
+
+.twoslash .twoslash-popup-docs-tags {
+ display: flex;
+ flex-direction: column;
+ font-family: var(--twoslash-docs-font);
+}
+
+.twoslash .twoslash-popup-docs-tags,
+.twoslash .twoslash-popup-docs-tag-name {
+ margin-right: 0.5em;
+}
+
+.twoslash .twoslash-popup-docs-tag-name {
+ font-family: var(--twoslash-code-font);
+}
+
+/* ===== Query Line ===== */
+.twoslash .twoslash-query-line .twoslash-popup-container {
+ position: relative;
+ margin-bottom: 1.4em;
+ transform: translateY(0.6em);
+}
+
+/* ===== Error Line ===== */
+.twoslash .twoslash-error-line {
+ position: relative;
+ background-color: var(--twoslash-error-bg);
+ border-left: 3px solid var(--twoslash-error-color);
+ color: var(--twoslash-error-color);
+ padding: 6px 12px;
+ margin: 0.2em 0;
+ min-width: 100%;
+ width: max-content;
+}
+
+.twoslash .twoslash-error-line.twoslash-error-level-warning {
+ background-color: var(--twoslash-warn-bg);
+ border-left: 3px solid var(--twoslash-warn-color);
+ color: var(--twoslash-warn-color);
+}
+
+.twoslash .twoslash-error {
+ background: url("data:image/svg+xml,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20viewBox%3D'0%200%206%203'%20enable-background%3D'new%200%200%206%203'%20height%3D'3'%20width%3D'6'%3E%3Cg%20fill%3D'%23c94824'%3E%3Cpolygon%20points%3D'5.5%2C0%202.5%2C3%201.1%2C3%204.1%2C0'%2F%3E%3Cpolygon%20points%3D'4%2C0%206%2C2%206%2C0.6%205.4%2C0'%2F%3E%3Cpolygon%20points%3D'0%2C2%201%2C3%202.4%2C3%200%2C0.6'%2F%3E%3C%2Fg%3E%3C%2Fsvg%3E")
+ repeat-x bottom left;
+ padding-bottom: 2px;
+}
+
+.twoslash .twoslash-error.twoslash-error-level-warning {
+ background: url("data:image/svg+xml,%3Csvg%20xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg'%20viewBox%3D'0%200%206%203'%20enable-background%3D'new%200%200%206%203'%20height%3D'3'%20width%3D'6'%3E%3Cg%20fill%3D'%23c37d0d'%3E%3Cpolygon%20points%3D'5.5%2C0%202.5%2C3%201.1%2C3%204.1%2C0'%2F%3E%3Cpolygon%20points%3D'4%2C0%206%2C2%206%2C0.6%205.4%2C0'%2F%3E%3Cpolygon%20points%3D'0%2C2%201%2C3%202.4%2C3%200%2C0.6'%2F%3E%3C%2Fg%3E%3C%2Fsvg%3E")
+ repeat-x bottom left;
+ padding-bottom: 2px;
+}
+
+/* ===== Completeions ===== */
+.twoslash .twoslash-completion-cursor {
+ position: relative;
+}
+
+.twoslash .twoslash-completion-cursor .twoslash-completion-list {
+ user-select: none;
+ position: absolute;
+ top: 0;
+ left: 0;
+ transform: translate(0, 1.2em);
+ margin: 3px 0 0 -1px;
+ display: inline-block;
+ z-index: 8;
+ box-shadow: var(--twoslash-popup-shadow);
+ background: var(--twoslash-popup-bg);
+ border: 1px solid var(--twoslash-border-color);
+}
+
+.twoslash-completion-list {
+ width: 240px;
+ font-size: 0.8rem;
+ padding: 4px;
+ display: flex;
+ flex-direction: column;
+ gap: 4px;
+}
+
+.twoslash-completion-list:hover {
+ user-select: auto;
+}
+
+.twoslash-completion-list::before {
+ background-color: var(--twoslash-cursor-color);
+ width: 2px;
+ position: absolute;
+ top: -1.6em;
+ height: 1.4em;
+ left: -1px;
+ content: ' ';
+}
+
+.twoslash-completion-list li {
+ overflow: hidden;
+ display: flex;
+ align-items: center;
+ gap: 0.25em;
+ line-height: 1em;
+}
+
+.twoslash-completion-list li span.twoslash-completions-unmatched {
+ color: var(--twoslash-unmatched-color);
+}
+
+.twoslash-completion-list .deprecated {
+ text-decoration: line-through;
+ opacity: 0.5;
+}
+
+.twoslash-completion-list li span.twoslash-completions-matched {
+ color: var(--twoslash-matched-color);
+}
+
+/* Highlights */
+.twoslash-highlighted {
+ background-color: var(--twoslash-highlighted-bg);
+ border: 1px solid var(--twoslash-highlighted-border);
+ padding: 1px 2px;
+ margin: -1px -3px;
+ border-radius: 4px;
+}
+
+/* Icons */
+.twoslash-completion-list .twoslash-completions-icon {
+ color: var(--twoslash-unmatched-color);
+ width: 1em;
+ flex: none;
+}
+
+/* Custom Tags */
+.twoslash .twoslash-tag-line {
+ position: relative;
+ background-color: var(--twoslash-tag-bg);
+ border-left: 3px solid var(--twoslash-tag-color);
+ color: var(--twoslash-tag-color);
+ padding: 6px 10px;
+ margin: 0.2em 0;
+ display: flex;
+ align-items: center;
+ gap: 0.3em;
+ min-width: 100%;
+ width: max-content;
+}
+
+.twoslash .twoslash-tag-line .twoslash-tag-icon {
+ width: 1.1em;
+ color: inherit;
+}
+
+.twoslash .twoslash-tag-line.twoslash-tag-error-line {
+ background-color: var(--twoslash-error-bg);
+ border-left: 3px solid var(--twoslash-error-color);
+ color: var(--twoslash-error-color);
+}
+
+.twoslash .twoslash-tag-line.twoslash-tag-warn-line {
+ background-color: var(--twoslash-tag-warn-bg);
+ border-left: 3px solid var(--twoslash-tag-warn-color);
+ color: var(--twoslash-tag-warn-color);
+}
+
+.twoslash .twoslash-tag-line.twoslash-tag-annotate-line {
+ background-color: var(--twoslash-tag-annotate-bg);
+ border-left: 3px solid var(--twoslash-tag-annotate-color);
+ color: var(--twoslash-tag-annotate-color);
+}
+
+
+/* Twoslash: theme the rich renderer with Blume tokens. */
+:root {
+ --twoslash-border-color: var(--blume-border);
+ --twoslash-popup-bg: var(--blume-code-background);
+ --twoslash-popup-color: var(--blume-foreground);
+ --twoslash-popup-shadow: 0 6px 24px oklch(0 0 0 / 0.18);
+ --twoslash-docs-color: var(--blume-muted-foreground);
+ --twoslash-docs-font: var(--font-sans);
+ --twoslash-code-font: var(--font-mono);
+ --twoslash-matched-color: var(--blume-foreground);
+ --twoslash-unmatched-color: var(--blume-muted-foreground);
+ --twoslash-cursor-color: var(--blume-muted-foreground);
+}
+
+/* Popups are absolutely positioned and must escape the pre's scroll container.
+ The base prose pre rule (from the typography layer) wins the cascade here
+ despite lower specificity, so !important is needed to force visibility.
+ Regular code blocks scroll their inner code element and carry the horizontal
+ padding there; twoslash code opts out of that scroller (popups again), so the
+ padding is restored on the pre. */
+.prose pre.twoslash {
+ overflow: visible !important;
+ padding-left: 1.25rem;
+ padding-right: 1.25rem;
+}
+
+/* Twoslash blocks keep `overflow: visible` so popups can escape, which means
+ long lines can't scroll — they'd push past the viewport on narrow screens.
+ Wrap them on mobile instead; hover popups still position against the token. */
+@media (max-width: 640px) {
+ .prose pre.twoslash code {
+ white-space: pre-wrap;
+ overflow-wrap: anywhere;
+ }
+}
+
+/* The rich renderer renders each popup's type signature as a nested Shiki
+ pre. Strip the code-block chrome (border, radius, padding, background) so it
+ sits flush inside the popup, which owns the frame. */
+.prose pre.twoslash .twoslash-popup-container pre {
+ background: transparent !important;
+ border: 0 !important;
+ border-radius: 0 !important;
+ margin: 0 !important;
+ padding: 0 !important;
+}
+
+/* Re-assert chrome backgrounds the transparent token-span rule would clear. */
+.twoslash .twoslash-popup-container,
+.twoslash .twoslash-popup-arrow,
+.twoslash .twoslash-completion-cursor .twoslash-completion-list {
+ background: var(--twoslash-popup-bg) !important;
+}
+.twoslash .twoslash-popup-error {
+ background: var(--twoslash-error-bg) !important;
+}
+.twoslash .twoslash-highlighted {
+ background: var(--twoslash-highlighted-bg) !important;
+}
+
+/* The error squiggle uses a background image the transparent rule clears, so
+ draw it as a wavy underline instead. */
+.twoslash .twoslash-error {
+ text-decoration: underline wavy var(--twoslash-error-color);
+ text-underline-offset: 0.25em;
+}
+
+.twoslash-popup-container {
+ max-width: min(90vw, 36rem);
+}
+
+
+/* Token overrides: config first, then the user's theme.css (highest priority). */
+/* Generated by Blume from theme config. */
+:root {
+ --blume-accent: oklch(0.55 0.16 250);
+ --blume-accent-foreground: oklch(1 0 0);
+ --blume-radius: 0.5rem;
+}
+:root[data-theme="dark"] {
+ --blume-accent: oklch(0.62 0.16 250);
+ --blume-accent-foreground: oklch(0.085 0 0);
+}
+
+/*
+ * shadcn/ui tokens on top of Blume's theme. Blume (Tailwind v4) already maps
+ * background/foreground/muted/border onto its --blume-* tokens; this adds the
+ * names shadcn components expect, pointed at the same tokens so the progress
+ * cards follow the site in both colour schemes. Blume inlines this file into
+ * its Tailwind entry and switches dark mode with [data-theme="dark"], not
+ * shadcn's .dark class.
+ *
+ * Deliberately NOT remapped: `accent`. In Blume it is the brand colour
+ * (bg-accent), in shadcn a subtle hover surface; the vendored components here
+ * never use it.
+ */
+
+:root {
+ /* Difficulty hues, LeetCode's: easy teal, medium amber, hard red. */
+ --chart-1: oklch(0.72 0.12 195);
+ --chart-2: oklch(0.8 0.16 80);
+ --chart-3: oklch(0.63 0.22 25);
+ /* Solved fill for the topic bubbles. */
+ --chart-4: oklch(0.72 0.17 150);
+ --chart-5: oklch(0.6 0.12 250);
+ /* Heatmap ramp — the GREENS of apps/api/src/charts.ts (README + email), with
+ level 0 on the theme's muted surface so an empty day reads in light mode. */
+ --heat-0: var(--blume-muted);
+ --heat-1: #9be9a8;
+ --heat-2: #40c463;
+ --heat-3: #30a14e;
+ --heat-4: #216e39;
+}
+
+:root[data-theme="dark"] {
+ --chart-1: oklch(0.75 0.12 195);
+ --chart-2: oklch(0.83 0.16 80);
+ --chart-3: oklch(0.68 0.21 25);
+ --chart-4: oklch(0.76 0.17 150);
+ --chart-5: oklch(0.7 0.12 250);
+ --heat-0: var(--blume-muted);
+ --heat-1: #0e4429;
+ --heat-2: #006d32;
+ --heat-3: #26a641;
+ --heat-4: #39d353;
+}
+
+/* shadcn components draw `border` with no colour and rely on a global
+ `* { border-color: var(--border) }` base rule. Blume has none (its own
+ components always name border-border), so give shadcn's parts — every one
+ carries data-slot — that default, scoped, without touching Blume's. */
+@layer base {
+ [data-slot],
+ [data-slot] * {
+ border-color: var(--blume-border);
+ }
+}
+
+@theme inline {
+ --color-card: var(--blume-background);
+ --color-card-foreground: var(--blume-foreground);
+ --color-popover: var(--blume-background);
+ --color-popover-foreground: var(--blume-foreground);
+ --color-primary: var(--blume-foreground);
+ --color-primary-foreground: var(--blume-background);
+ --color-secondary: var(--blume-muted);
+ --color-secondary-foreground: var(--blume-foreground);
+ --color-input: var(--blume-border);
+ --color-ring: var(--blume-muted-foreground);
+ --color-chart-1: var(--chart-1);
+ --color-chart-2: var(--chart-2);
+ --color-chart-3: var(--chart-3);
+ --color-chart-4: var(--chart-4);
+ --color-chart-5: var(--chart-5);
+}
+
diff --git a/apps/docs/src/generated/component-slots/mdx-DependencySpine.astro b/apps/docs/src/generated/component-slots/mdx-DependencySpine.astro
new file mode 100644
index 0000000..58f7004
--- /dev/null
+++ b/apps/docs/src/generated/component-slots/mdx-DependencySpine.astro
@@ -0,0 +1,11 @@
+---
+// Generated by Blume. Do not edit.
+import Component from "../../../islands/DependencySpine.tsx";
+type Props = typeof Component extends (
+ props: infer P extends object,
+ ...rest: never[]
+) => unknown
+ ? P
+ : Record;
+---
+
diff --git a/apps/docs/src/generated/components.ts b/apps/docs/src/generated/components.ts
new file mode 100644
index 0000000..690cf9c
--- /dev/null
+++ b/apps/docs/src/generated/components.ts
@@ -0,0 +1,9 @@
+// Generated by Blume. Do not edit.
+import type { ComponentOverride } from "blume/core/define-components.ts";
+import __blumeSlot0 from "./component-slots/mdx-DependencySpine.astro";
+import __blumeSlot1 from "../../components/Carousel.astro";
+import __blumeSlot2 from "../../components/CarouselImage.astro";
+import __blumeSlot3 from "../../components/ProblemBadges.astro";
+import __blumeSlot4 from "../../components/BacklinksList.astro";
+export const mdxComponents: Record = { "DependencySpine": __blumeSlot0, "Carousel": __blumeSlot1, "CarouselImage": __blumeSlot2, "wrapper": __blumeSlot3 };
+export const layoutOverrides: Record = { "PageFooter": __blumeSlot4 };
diff --git a/apps/docs/src/generated/consent-client.ts b/apps/docs/src/generated/consent-client.ts
new file mode 100644
index 0000000..322f580
--- /dev/null
+++ b/apps/docs/src/generated/consent-client.ts
@@ -0,0 +1,3 @@
+// Generated by Blume. Do not edit.
+// The consent adapter has no browser module, or consent is off.
+export const startConsentClient = (): void => {};
diff --git a/apps/docs/src/generated/content-assets.json b/apps/docs/src/generated/content-assets.json
new file mode 100644
index 0000000..ca2a32f
--- /dev/null
+++ b/apps/docs/src/generated/content-assets.json
@@ -0,0 +1 @@
+{"public/hash-table-operations.svg":"public/hash-table-operations.svg","public/intervals_overlap-condition.svg":"public/intervals_overlap-condition.svg","public/intervals_merge-formula.svg":"public/intervals_merge-formula.svg","public/intervals_overlap-formula.svg":"public/intervals_overlap-formula.svg","public/prefix-sum_1.png":"public/prefix-sum_1.png","public/sliding-window-fixed-size.svg":"public/sliding-window-fixed-size.svg","public/sliding-window-flexible.svg":"public/sliding-window-flexible.svg","public/sliding-window-frequency.svg":"public/sliding-window-frequency.svg","public/stack-plates-metaphor.svg":"public/stack-plates-metaphor.svg","public/stack-operations-sequence.svg":"public/stack-operations-sequence.svg","public/topo_intro_1.png":"public/topo_intro_1.png","public/topo_intro_2.png":"public/topo_intro_2.png","public/two-pointers-decision-tree.svg":"public/two-pointers-decision-tree.svg","public/same-direction-compaction.svg":"public/same-direction-compaction.svg","public/same-direction-relative-speed.svg":"public/same-direction-relative-speed.svg","public/same-direction-fixed-gap.svg":"public/same-direction-fixed-gap.svg","public/opposite-direction-monotonic-sum.svg":"public/opposite-direction-monotonic-sum.svg","public/opposite-direction-symmetry.svg":"public/opposite-direction-symmetry.svg","public/opposite-direction-bottleneck.svg":"public/opposite-direction-bottleneck.svg"}
diff --git a/apps/docs/src/generated/data.json b/apps/docs/src/generated/data.json
new file mode 100644
index 0000000..0f1db0b
--- /dev/null
+++ b/apps/docs/src/generated/data.json
@@ -0,0 +1,4383 @@
+{
+ "config": {
+ "analytics": [],
+ "api": {
+ "playground": {
+ "enabled": true,
+ "proxy": false
+ }
+ },
+ "appleIcon": null,
+ "assistant": {
+ "captcha": null,
+ "endpoint": "https://srs-api.prdlk.workers.dev/ask",
+ "suggestions": [],
+ "support": null
+ },
+ "banner": null,
+ "basePath": "",
+ "codeThemes": {
+ "dark": "github-dark",
+ "light": "github-light"
+ },
+ "codeWrap": false,
+ "consent": null,
+ "dateFormat": {
+ "dateStyle": "long"
+ },
+ "description": "Documentation for my LeetCode solutions",
+ "discovery": {
+ "agentReadability": true,
+ "aiCatalog": true,
+ "api": true,
+ "llmsTxt": true,
+ "sitemap": true
+ },
+ "favicon": {
+ "href": "/icon.png",
+ "type": "image/png"
+ },
+ "feedback": true,
+ "feedbackComments": false,
+ "footer": null,
+ "github": {
+ "api": "https://api.github.com",
+ "host": "https://github.com",
+ "owner": "prdlk",
+ "repo": "leetcode"
+ },
+ "i18n": null,
+ "identity": null,
+ "imageZoom": true,
+ "jsonLd": {},
+ "logo": {
+ "alt": "",
+ "href": "/",
+ "svg": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n "
+ },
+ "mcp": null,
+ "metatags": null,
+ "narration": null,
+ "og": {
+ "description": "Documentation for my LeetCode solutions",
+ "enabled": true,
+ "logo": "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n ",
+ "site": "lc.prd.lk"
+ },
+ "openInChat": [
+ "v0",
+ "chatgpt",
+ "claude",
+ "t3",
+ "scira",
+ "cursor"
+ ],
+ "repoUrl": "https://github.com/prdlk/leetcode",
+ "search": {
+ "analytics": {
+ "queries": true
+ },
+ "enabled": true,
+ "popular": [],
+ "provider": "orama"
+ },
+ "site": "https://lc.prd.lk",
+ "structuredData": true,
+ "theme": {
+ "accent": {
+ "dark": "blue",
+ "light": "blue"
+ },
+ "fonts": {
+ "body": {
+ "name": "Inter",
+ "variants": [
+ {
+ "src": "./node_modules/@fontsource-variable/inter/files/inter-latin-wght-normal.woff2",
+ "weight": "100..900"
+ }
+ ]
+ },
+ "display": {
+ "name": "Inter",
+ "variants": [
+ {
+ "src": "./node_modules/@fontsource-variable/inter/files/inter-latin-wght-normal.woff2",
+ "weight": "100..900"
+ }
+ ]
+ },
+ "mono": {
+ "name": "IBM Plex Mono",
+ "variants": [
+ {
+ "src": "./node_modules/@fontsource/ibm-plex-mono/files/ibm-plex-mono-latin-400-normal.woff2",
+ "weight": 400
+ },
+ {
+ "src": "./node_modules/@fontsource/ibm-plex-mono/files/ibm-plex-mono-latin-500-normal.woff2",
+ "weight": 500
+ }
+ ]
+ }
+ },
+ "mode": "system",
+ "radius": "md",
+ "toggle": true
+ },
+ "title": "prdlk",
+ "toc": {
+ "enabled": true,
+ "maxLevel": 3,
+ "minLevel": 2
+ },
+ "versions": null,
+ "webmcp": {
+ "enabled": true,
+ "llms": true
+ },
+ "x": {
+ "handle": "@prdlk"
+ }
+ },
+ "feeds": [],
+ "fontCssVars": [
+ {
+ "cssVariable": "--blume-ff-inter",
+ "preloadWeights": [
+ 400,
+ 500,
+ 600
+ ]
+ },
+ {
+ "cssVariable": "--blume-ff-ibm-plex-mono",
+ "preloadWeights": [
+ 400
+ ]
+ }
+ ],
+ "navigation": {
+ "actions": [],
+ "cta": null,
+ "featured": [],
+ "root": "/",
+ "selectors": [],
+ "sidebar": [
+ {
+ "description": "Structured dependency spine based roadmap for solving DSA Problems on leetcode.",
+ "icon": "play",
+ "kind": "page",
+ "label": "Introduction",
+ "pageId": "filesystem:index.mdx",
+ "route": "/"
+ },
+ {
+ "description": "Coding problems rarely tell you which algorithm to use, but the wording usually leaks it. \"K-th largest\" hints at a heap. \"Shortest path\" in an unweighted graph hints at BFS.",
+ "icon": "map",
+ "kind": "page",
+ "label": "Keywords",
+ "pageId": "filesystem:keywords.mdx",
+ "route": "/keywords"
+ },
+ {
+ "children": [
+ {
+ "children": [
+ {
+ "badge": "Easy",
+ "description": "You are given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Two Sum",
+ "pageId": "filesystem:(01-linear-structures)/hash-table/1_two-sum.mdx",
+ "route": "/hash-table/two-sum"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given an array of strings strs, group the anagrams together. You can return the answer in any order",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Group Anagrams",
+ "pageId": "filesystem:(01-linear-structures)/hash-table/49_group-anagrams.mdx",
+ "route": "/hash-table/group-anagrams"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Longest Consecutive Sequence",
+ "pageId": "filesystem:(01-linear-structures)/hash-table/128_longest-consecutive-sequence.mdx",
+ "route": "/hash-table/longest-consecutive-sequence"
+ },
+ {
+ "badge": "Easy",
+ "description": "Given two strings s and t, determine if they are isomorphic",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Isomorphic Strings",
+ "pageId": "filesystem:(01-linear-structures)/hash-table/205_isomorphic-strings.mdx",
+ "route": "/hash-table/isomorphic-strings"
+ },
+ {
+ "badge": "Easy",
+ "description": "Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Contains Duplicate",
+ "pageId": "filesystem:(01-linear-structures)/hash-table/217_contains-duplicate.mdx",
+ "route": "/hash-table/contains-duplicate"
+ },
+ {
+ "badge": "Easy",
+ "description": "Given two strings s and t, return true if t is an anagram of s, and false otherwise",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Valid Anagram",
+ "pageId": "filesystem:(01-linear-structures)/hash-table/242_valid-anagram.mdx",
+ "route": "/hash-table/valid-anagram"
+ },
+ {
+ "badge": "Easy",
+ "description": "Given a pattern and a string s, find if s follows the same pattern",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Word Pattern",
+ "pageId": "filesystem:(01-linear-structures)/hash-table/290_word-pattern.mdx",
+ "route": "/hash-table/word-pattern"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Top K Frequent Elements",
+ "pageId": "filesystem:(01-linear-structures)/hash-table/347_top-k-frequent-elements.mdx",
+ "route": "/hash-table/top-k-frequent-elements"
+ },
+ {
+ "badge": "Easy",
+ "description": "Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1",
+ "icon": "dot",
+ "kind": "page",
+ "label": "First Unique Character in a String",
+ "pageId": "filesystem:(01-linear-structures)/hash-table/387_first-unique-character-in-a-string.mdx",
+ "route": "/hash-table/first-unique-character-in-a-string"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Sort Characters By Frequency",
+ "pageId": "filesystem:(01-linear-structures)/hash-table/451_sort-characters-by-frequency.mdx",
+ "route": "/hash-table/sort-characters-by-frequency"
+ },
+ {
+ "badge": "Easy",
+ "description": "A pangram is a sentence where every letter of the English alphabet appears at least once",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Check if the Sentence Is Pangram",
+ "pageId": "filesystem:(01-linear-structures)/hash-table/1832_check-if-the-sentence-is-pangram.mdx",
+ "route": "/hash-table/check-if-the-sentence-is-pangram"
+ }
+ ],
+ "display": "group",
+ "icon": "hash",
+ "kind": "group",
+ "label": "Hash Table",
+ "path": "/hash-table",
+ "route": "/hash-table"
+ },
+ {
+ "children": [
+ {
+ "badge": "Medium",
+ "description": "Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Product of Array Except Self",
+ "pageId": "filesystem:(01-linear-structures)/prefix-sum/238_product-of-array-except-self.mdx",
+ "route": "/prefix-sum/product-of-array-except-self"
+ },
+ {
+ "badge": "Easy",
+ "description": "Given an integer array nums, handle multiple queries of the following type",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Range Sum Query - Immutable",
+ "pageId": "filesystem:(01-linear-structures)/prefix-sum/303_range-sum-query-immutable.mdx",
+ "route": "/prefix-sum/range-sum-query-immutable"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Contiguous Array",
+ "pageId": "filesystem:(01-linear-structures)/prefix-sum/525_contiguous-array.mdx",
+ "route": "/prefix-sum/contiguous-array"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Subarray Sum Equals K",
+ "pageId": "filesystem:(01-linear-structures)/prefix-sum/560_subarray-sum-equals-k.mdx",
+ "route": "/prefix-sum/subarray-sum-equals-k"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given an integer array nums and an integer k, return the number of non-empty subarrays that have a sum divisible by k",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Subarray Sums Divisible by K",
+ "pageId": "filesystem:(01-linear-structures)/prefix-sum/974_subarray-sums-divisible-by-k.mdx",
+ "route": "/prefix-sum/subarray-sums-divisible-by-k"
+ }
+ ],
+ "display": "group",
+ "icon": "sigma",
+ "kind": "group",
+ "label": "Prefix Sum",
+ "path": "/prefix-sum",
+ "route": "/prefix-sum"
+ },
+ {
+ "children": [
+ {
+ "badge": "Medium",
+ "description": "You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the i^th line are (i, 0) and (i, height[i])",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Container With Most Water",
+ "pageId": "filesystem:(01-linear-structures)/two-pointers/11_container-with-most-water.mdx",
+ "route": "/two-pointers/container-with-most-water"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0",
+ "icon": "dot",
+ "kind": "page",
+ "label": "3Sum",
+ "pageId": "filesystem:(01-linear-structures)/two-pointers/15_3sum.mdx",
+ "route": "/two-pointers/3sum"
+ },
+ {
+ "badge": "Hard",
+ "description": "Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Trapping Rain Water",
+ "pageId": "filesystem:(01-linear-structures)/two-pointers/42_trapping-rain-water.mdx",
+ "route": "/two-pointers/trapping-rain-water"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Remove Duplicates from Sorted Array II",
+ "pageId": "filesystem:(01-linear-structures)/two-pointers/80_remove-duplicates-from-sorted-array-ii.mdx",
+ "route": "/two-pointers/remove-duplicates-from-sorted-array-ii"
+ },
+ {
+ "badge": "Easy",
+ "description": "A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Valid Palindrome",
+ "pageId": "filesystem:(01-linear-structures)/two-pointers/125_valid-palindrome.mdx",
+ "route": "/two-pointers/valid-palindrome"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Two Sum II - Input Array Is Sorted",
+ "pageId": "filesystem:(01-linear-structures)/two-pointers/167_two-sum-ii-input-array-is-sorted.mdx",
+ "route": "/two-pointers/two-sum-ii-input-array-is-sorted"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given an integer array nums, rotate the array to the right by k steps, where k is non-negative",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Rotate Array",
+ "pageId": "filesystem:(01-linear-structures)/two-pointers/189_rotate-array.mdx",
+ "route": "/two-pointers/rotate-array"
+ },
+ {
+ "badge": "Easy",
+ "description": "Write a function that reverses a string. The input string is given as an array of characters s",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Reverse String",
+ "pageId": "filesystem:(01-linear-structures)/two-pointers/344_reverse-string.mdx",
+ "route": "/two-pointers/reverse-string"
+ },
+ {
+ "badge": "Easy",
+ "description": "Given two strings s and t, return true if s is a subsequence of t, or false otherwise",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Is Subsequence",
+ "pageId": "filesystem:(01-linear-structures)/two-pointers/392_is-subsequence.mdx",
+ "route": "/two-pointers/is-subsequence"
+ },
+ {
+ "badge": "Easy",
+ "description": "Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Squares of a Sorted Array",
+ "pageId": "filesystem:(01-linear-structures)/two-pointers/977_squares-of-a-sorted-array.mdx",
+ "route": "/two-pointers/squares-of-a-sorted-array"
+ }
+ ],
+ "display": "group",
+ "icon": "arrow-left-right",
+ "kind": "group",
+ "label": "Two Pointers",
+ "path": "/two-pointers",
+ "route": "/two-pointers"
+ },
+ {
+ "children": [
+ {
+ "badge": "Medium",
+ "description": "Given a string s, find the length of the longest substring without duplicate characters",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Longest Substring Without Repeating Characters",
+ "pageId": "filesystem:(01-linear-structures)/sliding-window/3_longest-substring-without-repeating-characters.mdx",
+ "route": "/sliding-window/longest-substring-without-repeating-characters"
+ },
+ {
+ "badge": "Hard",
+ "description": "Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string \"\"",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Minimum Window Substring",
+ "pageId": "filesystem:(01-linear-structures)/sliding-window/76_minimum-window-substring.mdx",
+ "route": "/sliding-window/minimum-window-substring"
+ },
+ {
+ "badge": "Easy",
+ "description": "You are given an array prices where prices[i] is the price of a given stock on the i^th day",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Best Time to Buy and Sell Stock",
+ "pageId": "filesystem:(01-linear-structures)/sliding-window/121_best-time-to-buy-and-sell-stock.mdx",
+ "route": "/sliding-window/best-time-to-buy-and-sell-stock"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Minimum Size Subarray Sum",
+ "pageId": "filesystem:(01-linear-structures)/sliding-window/209_minimum-size-subarray-sum.mdx",
+ "route": "/sliding-window/minimum-size-subarray-sum"
+ },
+ {
+ "badge": "Medium",
+ "description": "You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Longest Repeating Character Replacement",
+ "pageId": "filesystem:(01-linear-structures)/sliding-window/424_longest-repeating-character-replacement.mdx",
+ "route": "/sliding-window/longest-repeating-character-replacement"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Permutation in String",
+ "pageId": "filesystem:(01-linear-structures)/sliding-window/567_permutation-in-string.mdx",
+ "route": "/sliding-window/permutation-in-string"
+ },
+ {
+ "badge": "Easy",
+ "description": "You are given an integer array nums consisting of n elements, and an integer k",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Maximum Average Subarray I",
+ "pageId": "filesystem:(01-linear-structures)/sliding-window/643_maximum-average-subarray-i.mdx",
+ "route": "/sliding-window/maximum-average-subarray-i"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Max Consecutive Ones III",
+ "pageId": "filesystem:(01-linear-structures)/sliding-window/1004_max-consecutive-ones-iii.mdx",
+ "route": "/sliding-window/max-consecutive-ones-iii"
+ }
+ ],
+ "display": "group",
+ "icon": "app-window",
+ "kind": "group",
+ "label": "Sliding Window",
+ "path": "/sliding-window",
+ "route": "/sliding-window"
+ },
+ {
+ "children": [
+ {
+ "badge": "Hard",
+ "description": "Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Median of Two Sorted Arrays",
+ "pageId": "filesystem:(01-linear-structures)/binary-search/4_median-of-two-sorted-arrays.mdx",
+ "route": "/binary-search/median-of-two-sorted-arrays"
+ },
+ {
+ "badge": "Medium",
+ "description": "There is an integer array nums sorted in ascending order (with distinct values)",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Search in Rotated Sorted Array",
+ "pageId": "filesystem:(01-linear-structures)/binary-search/33_search-in-rotated-sorted-array.mdx",
+ "route": "/binary-search/search-in-rotated-sorted-array"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Find First and Last Position of Element in Sorted Array",
+ "pageId": "filesystem:(01-linear-structures)/binary-search/34_find-first-and-last-position-of-element-in-sorted-array.mdx",
+ "route": "/binary-search/find-first-and-last-position-of-element-in-sorted-array"
+ },
+ {
+ "badge": "Easy",
+ "description": "Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Search Insert Position",
+ "pageId": "filesystem:(01-linear-structures)/binary-search/35_search-insert-position.mdx",
+ "route": "/binary-search/search-insert-position"
+ },
+ {
+ "badge": "Medium",
+ "description": "You are given an m x n integer matrix matrix with the following two properties:",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Search a 2D Matrix",
+ "pageId": "filesystem:(01-linear-structures)/binary-search/74_search-a-2d-matrix.mdx",
+ "route": "/binary-search/search-a-2d-matrix"
+ },
+ {
+ "badge": "Medium",
+ "description": "Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Find Minimum in Rotated Sorted Array",
+ "pageId": "filesystem:(01-linear-structures)/binary-search/153_find-minimum-in-rotated-sorted-array.mdx",
+ "route": "/binary-search/find-minimum-in-rotated-sorted-array"
+ },
+ {
+ "badge": "Medium",
+ "description": "A peak element is an element that is strictly greater than its neighbors",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Find Peak Element",
+ "pageId": "filesystem:(01-linear-structures)/binary-search/162_find-peak-element.mdx",
+ "route": "/binary-search/find-peak-element"
+ },
+ {
+ "badge": "Easy",
+ "description": "Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Binary Search",
+ "pageId": "filesystem:(01-linear-structures)/binary-search/704_binary-search.mdx",
+ "route": "/binary-search/binary-search"
+ },
+ {
+ "badge": "Medium",
+ "description": "Koko loves to eat bananas. There are n piles of bananas, the i^th pile has piles[i] bananas. The guards have gone and will come back in h hours",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Koko Eating Bananas",
+ "pageId": "filesystem:(01-linear-structures)/binary-search/875_koko-eating-bananas.mdx",
+ "route": "/binary-search/koko-eating-bananas"
+ }
+ ],
+ "display": "group",
+ "icon": "search",
+ "kind": "group",
+ "label": "Binary Search",
+ "path": "/binary-search",
+ "route": "/binary-search"
+ },
+ {
+ "children": [
+ {
+ "badge": "Easy",
+ "description": "Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Valid Parentheses",
+ "pageId": "filesystem:(01-linear-structures)/stack/20_valid-parentheses.mdx",
+ "route": "/stack/valid-parentheses"
+ },
+ {
+ "badge": "Medium",
+ "description": "You are given an absolute path for a Unix-style file system, which always begins with a slash '/'. Your task is to transform this absolute path into its simplified canonical path",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Simplify Path",
+ "pageId": "filesystem:(01-linear-structures)/stack/71_simplify-path.mdx",
+ "route": "/stack/simplify-path"
+ },
+ {
+ "badge": "Medium",
+ "description": "You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Evaluate Reverse Polish Notation",
+ "pageId": "filesystem:(01-linear-structures)/stack/150_evaluate-reverse-polish-notation.mdx",
+ "route": "/stack/evaluate-reverse-polish-notation"
+ },
+ {
+ "badge": "Medium",
+ "description": "Design a stack that supports push, pop, top, and retrieving the minimum element in constant time",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Min Stack",
+ "pageId": "filesystem:(01-linear-structures)/stack/155_min-stack.mdx",
+ "route": "/stack/min-stack"
+ },
+ {
+ "badge": "Hard",
+ "description": "Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Basic Calculator",
+ "pageId": "filesystem:(01-linear-structures)/stack/224_basic-calculator.mdx",
+ "route": "/stack/basic-calculator"
+ }
+ ],
+ "display": "group",
+ "icon": "layers",
+ "kind": "group",
+ "label": "Stack",
+ "path": "/stack",
+ "route": "/stack"
+ },
+ {
+ "children": [
+ {
+ "badge": "Hard",
+ "description": "Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Largest Rectangle in Histogram",
+ "pageId": "filesystem:(01-linear-structures)/monotonic-stack/84_largest-rectangle-in-histogram.mdx",
+ "route": "/monotonic-stack/largest-rectangle-in-histogram"
+ },
+ {
+ "badge": "Easy",
+ "description": "The next greater element of some element x in an array is the first greater element that is to the right of x in the same array",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Next Greater Element I",
+ "pageId": "filesystem:(01-linear-structures)/monotonic-stack/496_next-greater-element-i.mdx",
+ "route": "/monotonic-stack/next-greater-element-i"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the i^th day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Daily Temperatures",
+ "pageId": "filesystem:(01-linear-structures)/monotonic-stack/739_daily-temperatures.mdx",
+ "route": "/monotonic-stack/daily-temperatures"
+ },
+ {
+ "badge": "Medium",
+ "description": "There are n cars at given miles away from the starting mile 0, traveling to reach the mile target",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Car Fleet",
+ "pageId": "filesystem:(01-linear-structures)/monotonic-stack/853_car-fleet.mdx",
+ "route": "/monotonic-stack/car-fleet"
+ },
+ {
+ "badge": "Medium",
+ "description": "Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Online Stock Span",
+ "pageId": "filesystem:(01-linear-structures)/monotonic-stack/901_online-stock-span.mdx",
+ "route": "/monotonic-stack/online-stock-span"
+ }
+ ],
+ "display": "group",
+ "icon": "trending-up",
+ "kind": "group",
+ "label": "Monotonic Stack",
+ "path": "/monotonic-stack",
+ "route": "/monotonic-stack"
+ }
+ ],
+ "display": "flat",
+ "kind": "group",
+ "label": "Linear Structures",
+ "path": "/"
+ },
+ {
+ "children": [
+ {
+ "children": [
+ {
+ "badge": "Medium",
+ "description": "Given the head of a linked list, remove the n^th node from the end of the list and return its head",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Remove Nth Node From End of List",
+ "pageId": "filesystem:(02-nodal-grid)/linked-list/19_remove-nth-node-from-end-of-list.mdx",
+ "route": "/linked-list/remove-nth-node-from-end-of-list"
+ },
+ {
+ "badge": "Easy",
+ "description": "You are given the heads of two sorted linked lists list1 and list2",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Merge Two Sorted Lists",
+ "pageId": "filesystem:(02-nodal-grid)/linked-list/21_merge-two-sorted-lists.mdx",
+ "route": "/linked-list/merge-two-sorted-lists"
+ },
+ {
+ "badge": "Medium",
+ "description": "You are given the head of a sorted linked list",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Remove Duplicates from Sorted List II",
+ "pageId": "filesystem:(02-nodal-grid)/linked-list/82_remove-duplicates-from-sorted-list-ii.mdx",
+ "route": "/linked-list/remove-duplicates-from-sorted-list-ii"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Reverse Linked List II",
+ "pageId": "filesystem:(02-nodal-grid)/linked-list/92_reverse-linked-list-ii.mdx",
+ "route": "/linked-list/reverse-linked-list-ii"
+ },
+ {
+ "badge": "Easy",
+ "description": "Given head, the head of a linked list, determine if the linked list has a cycle in it",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Linked List Cycle",
+ "pageId": "filesystem:(02-nodal-grid)/linked-list/141_linked-list-cycle.mdx",
+ "route": "/linked-list/linked-list-cycle"
+ },
+ {
+ "badge": "Medium",
+ "description": "You are given the head of a singly linked-list. The list can be represented as:",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Reorder List",
+ "pageId": "filesystem:(02-nodal-grid)/linked-list/143_reorder-list.mdx",
+ "route": "/linked-list/reorder-list"
+ },
+ {
+ "badge": "Easy",
+ "description": "Given the head of a singly linked list, reverse the list, and return the reversed list",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Reverse Linked List",
+ "pageId": "filesystem:(02-nodal-grid)/linked-list/206_reverse-linked-list.mdx",
+ "route": "/linked-list/reverse-linked-list"
+ },
+ {
+ "badge": "Easy",
+ "description": "Given the head of a singly linked list, return the middle node of the linked list",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Middle of the Linked List",
+ "pageId": "filesystem:(02-nodal-grid)/linked-list/876_middle-of-the-linked-list.mdx",
+ "route": "/linked-list/middle-of-the-linked-list"
+ }
+ ],
+ "display": "group",
+ "icon": "link",
+ "kind": "group",
+ "label": "Linked List",
+ "path": "/linked-list",
+ "route": "/linked-list"
+ },
+ {
+ "children": [
+ {
+ "badge": "Medium",
+ "description": "You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Add Two Numbers",
+ "pageId": "filesystem:(02-nodal-grid)/hybrid-structures/2_add-two-numbers.mdx",
+ "route": "/hybrid-structures/add-two-numbers"
+ },
+ {
+ "badge": "Medium",
+ "description": "Design a data structure that follows the constraints of a Least Recently Used (LRU) cache",
+ "icon": "dot",
+ "kind": "page",
+ "label": "LRU Cache",
+ "pageId": "filesystem:(02-nodal-grid)/hybrid-structures/146_lru-cache.mdx",
+ "route": "/hybrid-structures/lru-cache"
+ },
+ {
+ "badge": "Medium",
+ "description": "Implement the RandomizedSet class:",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Insert Delete GetRandom O(1)",
+ "pageId": "filesystem:(02-nodal-grid)/hybrid-structures/380_insert-delete-getrandom-o1.mdx",
+ "route": "/hybrid-structures/insert-delete-getrandom-o1"
+ }
+ ],
+ "display": "group",
+ "icon": "combine",
+ "kind": "group",
+ "label": "Hybrid Structures",
+ "path": "/hybrid-structures",
+ "route": "/hybrid-structures"
+ },
+ {
+ "children": [
+ {
+ "badge": "Medium",
+ "description": "Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Valid Sudoku",
+ "pageId": "filesystem:(02-nodal-grid)/matrix-index-math/36_valid-sudoku.mdx",
+ "route": "/matrix-index-math/valid-sudoku"
+ },
+ {
+ "badge": "Medium",
+ "description": "You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise)",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Rotate Image",
+ "pageId": "filesystem:(02-nodal-grid)/matrix-index-math/48_rotate-image.mdx",
+ "route": "/matrix-index-math/rotate-image"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given an m x n matrix, return all elements of the matrix in spiral order",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Spiral Matrix",
+ "pageId": "filesystem:(02-nodal-grid)/matrix-index-math/54_spiral-matrix.mdx",
+ "route": "/matrix-index-math/spiral-matrix"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Set Matrix Zeroes",
+ "pageId": "filesystem:(02-nodal-grid)/matrix-index-math/73_set-matrix-zeroes.mdx",
+ "route": "/matrix-index-math/set-matrix-zeroes"
+ }
+ ],
+ "display": "group",
+ "icon": "grid-3x3",
+ "kind": "group",
+ "label": "Matrix Index Math",
+ "path": "/matrix-index-math",
+ "route": "/matrix-index-math"
+ }
+ ],
+ "display": "flat",
+ "kind": "group",
+ "label": "Nodal & Grid",
+ "path": "/"
+ },
+ {
+ "children": [
+ {
+ "children": [
+ {
+ "badge": "Easy",
+ "description": "Given the root of a binary tree, return its maximum depth",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Maximum Depth of Binary Tree",
+ "pageId": "filesystem:(03-hierarchical)/tree-dfs/104_maximum-depth-of-binary-tree.mdx",
+ "route": "/tree-dfs/maximum-depth-of-binary-tree"
+ },
+ {
+ "badge": "Easy",
+ "description": "Given the root of a binary tree, invert the tree, and return its root",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Invert Binary Tree",
+ "pageId": "filesystem:(03-hierarchical)/tree-dfs/226_invert-binary-tree.mdx",
+ "route": "/tree-dfs/invert-binary-tree"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Lowest Common Ancestor of a Binary Tree",
+ "pageId": "filesystem:(03-hierarchical)/tree-dfs/236_lowest-common-ancestor-of-a-binary-tree.mdx",
+ "route": "/tree-dfs/lowest-common-ancestor-of-a-binary-tree"
+ }
+ ],
+ "display": "group",
+ "icon": "folder-tree",
+ "kind": "group",
+ "label": "Tree DFS",
+ "path": "/tree-dfs",
+ "route": "/tree-dfs"
+ },
+ {
+ "children": [
+ {
+ "badge": "Medium",
+ "description": "Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level)",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Binary Tree Level Order Traversal",
+ "pageId": "filesystem:(03-hierarchical)/tree-bfs/102_binary-tree-level-order-traversal.mdx",
+ "route": "/tree-bfs/binary-tree-level-order-traversal"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Binary Tree Right Side View",
+ "pageId": "filesystem:(03-hierarchical)/tree-bfs/199_binary-tree-right-side-view.mdx",
+ "route": "/tree-bfs/binary-tree-right-side-view"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Count Good Nodes in Binary Tree",
+ "pageId": "filesystem:(03-hierarchical)/tree-bfs/1448_count-good-nodes-in-binary-tree.mdx",
+ "route": "/tree-bfs/count-good-nodes-in-binary-tree"
+ }
+ ],
+ "display": "group",
+ "icon": "network",
+ "kind": "group",
+ "label": "Tree BFS",
+ "path": "/tree-bfs",
+ "route": "/tree-bfs"
+ },
+ {
+ "children": [
+ {
+ "badge": "Medium",
+ "description": "Given the root of a binary tree, determine if it is a valid binary search tree (BST)",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Validate Binary Search Tree",
+ "pageId": "filesystem:(03-hierarchical)/binary-search-tree/98_validate-binary-search-tree.mdx",
+ "route": "/binary-search-tree/validate-binary-search-tree"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given the root of a binary search tree, and an integer k, return the k^th smallest value (1-indexed) of all the values of the nodes in the tree",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Kth Smallest Element in a BST",
+ "pageId": "filesystem:(03-hierarchical)/binary-search-tree/230_kth-smallest-element-in-a-bst.mdx",
+ "route": "/binary-search-tree/kth-smallest-element-in-a-bst"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Lowest Common Ancestor of a Binary Search Tree",
+ "pageId": "filesystem:(03-hierarchical)/binary-search-tree/235_lowest-common-ancestor-of-a-binary-search-tree.mdx",
+ "route": "/binary-search-tree/lowest-common-ancestor-of-a-binary-search-tree"
+ }
+ ],
+ "display": "group",
+ "icon": "git-fork",
+ "kind": "group",
+ "label": "Binary Search Tree",
+ "path": "/binary-search-tree",
+ "route": "/binary-search-tree"
+ },
+ {
+ "children": [
+ {
+ "badge": "Hard",
+ "description": "You are given an array of k linked-lists lists, each linked-list is sorted in ascending order",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Merge k Sorted Lists",
+ "pageId": "filesystem:(03-hierarchical)/heap-priority-queue/23_merge-k-sorted-lists.mdx",
+ "route": "/heap-priority-queue/merge-k-sorted-lists"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given an integer array nums and an integer k, return the k^th largest element in the array",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Kth Largest Element in an Array",
+ "pageId": "filesystem:(03-hierarchical)/heap-priority-queue/215_kth-largest-element-in-an-array.mdx",
+ "route": "/heap-priority-queue/kth-largest-element-in-an-array"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0)",
+ "icon": "dot",
+ "kind": "page",
+ "label": "K Closest Points to Origin",
+ "pageId": "filesystem:(03-hierarchical)/heap-priority-queue/973_k-closest-points-to-origin.mdx",
+ "route": "/heap-priority-queue/k-closest-points-to-origin"
+ }
+ ],
+ "display": "group",
+ "icon": "triangle",
+ "kind": "group",
+ "label": "Heap / Priority Queue",
+ "path": "/heap-priority-queue",
+ "route": "/heap-priority-queue"
+ },
+ {
+ "children": [
+ {
+ "badge": "Medium",
+ "description": "A trie (pronounced as \"try\") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Implement Trie (Prefix Tree)",
+ "pageId": "filesystem:(03-hierarchical)/trie/208_implement-trie-prefix-tree.mdx",
+ "route": "/trie/implement-trie-prefix-tree"
+ },
+ {
+ "badge": "Medium",
+ "description": "Design a data structure that supports adding new words and finding if a string matches any previously added string",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Design Add and Search Words Data Structure",
+ "pageId": "filesystem:(03-hierarchical)/trie/211_design-add-and-search-words-data-structure.mdx",
+ "route": "/trie/design-add-and-search-words-data-structure"
+ }
+ ],
+ "display": "group",
+ "icon": "spell-check",
+ "kind": "group",
+ "label": "Trie",
+ "path": "/trie",
+ "route": "/trie"
+ }
+ ],
+ "display": "flat",
+ "kind": "group",
+ "label": "Hierarchical",
+ "path": "/"
+ },
+ {
+ "children": [
+ {
+ "children": [
+ {
+ "badge": "Medium",
+ "description": "Given a reference of a node in a connected undirected graph",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Clone Graph",
+ "pageId": "filesystem:(04-relational)/graph-dfs/133_clone-graph.mdx",
+ "route": "/graph-dfs/clone-graph"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Number of Islands",
+ "pageId": "filesystem:(04-relational)/graph-dfs/200_number-of-islands.mdx",
+ "route": "/graph-dfs/number-of-islands"
+ },
+ {
+ "badge": "Medium",
+ "description": "There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Pacific Atlantic Water Flow",
+ "pageId": "filesystem:(04-relational)/graph-dfs/417_pacific-atlantic-water-flow.mdx",
+ "route": "/graph-dfs/pacific-atlantic-water-flow"
+ }
+ ],
+ "display": "group",
+ "icon": "waypoints",
+ "kind": "group",
+ "label": "Graph DFS",
+ "path": "/graph-dfs",
+ "route": "/graph-dfs"
+ },
+ {
+ "children": [
+ {
+ "badge": "Hard",
+ "description": "A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Word Ladder",
+ "pageId": "filesystem:(04-relational)/graph-bfs/127_word-ladder.mdx",
+ "route": "/graph-bfs/word-ladder"
+ },
+ {
+ "badge": "Medium",
+ "description": "You are given an m x n grid where each cell can have one of three values:",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Rotting Oranges",
+ "pageId": "filesystem:(04-relational)/graph-bfs/994_rotting-oranges.mdx",
+ "route": "/graph-bfs/rotting-oranges"
+ }
+ ],
+ "display": "group",
+ "icon": "radar",
+ "kind": "group",
+ "label": "Graph BFS",
+ "path": "/graph-bfs",
+ "route": "/graph-bfs"
+ },
+ {
+ "children": [
+ {
+ "badge": "Medium",
+ "description": "There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Course Schedule",
+ "pageId": "filesystem:(04-relational)/topological-sort/207_course-schedule.mdx",
+ "route": "/topological-sort/course-schedule"
+ },
+ {
+ "badge": "Medium",
+ "description": "There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Course Schedule II",
+ "pageId": "filesystem:(04-relational)/topological-sort/210_course-schedule-ii.mdx",
+ "route": "/topological-sort/course-schedule-ii"
+ }
+ ],
+ "display": "group",
+ "icon": "workflow",
+ "kind": "group",
+ "label": "Topological Sort",
+ "path": "/topological-sort",
+ "route": "/topological-sort"
+ },
+ {
+ "children": [
+ {
+ "badge": "Medium",
+ "description": "You have a graph of n nodes. You are given an integer n and an array edges where edges[i] = [ai, bi] indicates that there is an edge between ai and bi in the graph",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Number of Connected Components in an Undirected Graph",
+ "pageId": "filesystem:(04-relational)/union-find/323_number-of-connected-components-in-an-undirected-graph.mdx",
+ "route": "/union-find/number-of-connected-components-in-an-undirected-graph"
+ },
+ {
+ "badge": "Medium",
+ "description": "In this problem, a tree is an undirected graph that is connected and has no cycles",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Redundant Connection",
+ "pageId": "filesystem:(04-relational)/union-find/684_redundant-connection.mdx",
+ "route": "/union-find/redundant-connection"
+ }
+ ],
+ "display": "group",
+ "icon": "merge",
+ "kind": "group",
+ "label": "Union-Find",
+ "path": "/union-find",
+ "route": "/union-find"
+ }
+ ],
+ "display": "flat",
+ "kind": "group",
+ "label": "Relational",
+ "path": "/"
+ },
+ {
+ "children": [
+ {
+ "children": [
+ {
+ "badge": "Medium",
+ "description": "Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Combination Sum",
+ "pageId": "filesystem:(05-decision-space)/backtracking/39_combination-sum.mdx",
+ "route": "/backtracking/combination-sum"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Permutations",
+ "pageId": "filesystem:(05-decision-space)/backtracking/46_permutations.mdx",
+ "route": "/backtracking/permutations"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given an integer array nums of unique elements, return all possible subsets (the power set)",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Subsets",
+ "pageId": "filesystem:(05-decision-space)/backtracking/78_subsets.mdx",
+ "route": "/backtracking/subsets"
+ }
+ ],
+ "display": "group",
+ "icon": "undo-2",
+ "kind": "group",
+ "label": "Backtracking",
+ "path": "/backtracking",
+ "route": "/backtracking"
+ },
+ {
+ "children": [
+ {
+ "badge": "Easy",
+ "description": "You are climbing a staircase. It takes n steps to reach the top",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Climbing Stairs",
+ "pageId": "filesystem:(05-decision-space)/1d-dynamic-programming/70_climbing-stairs.mdx",
+ "route": "/1d-dynamic-programming/climbing-stairs"
+ },
+ {
+ "badge": "Medium",
+ "description": "You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night",
+ "icon": "dot",
+ "kind": "page",
+ "label": "House Robber",
+ "pageId": "filesystem:(05-decision-space)/1d-dynamic-programming/198_house-robber.mdx",
+ "route": "/1d-dynamic-programming/house-robber"
+ },
+ {
+ "badge": "Medium",
+ "description": "You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Coin Change",
+ "pageId": "filesystem:(05-decision-space)/1d-dynamic-programming/322_coin-change.mdx",
+ "route": "/1d-dynamic-programming/coin-change"
+ }
+ ],
+ "display": "group",
+ "icon": "columns-3",
+ "kind": "group",
+ "label": "1-D Dynamic Programming",
+ "path": "/1d-dynamic-programming",
+ "route": "/1d-dynamic-programming"
+ },
+ {
+ "children": [
+ {
+ "badge": "Medium",
+ "description": "There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Unique Paths",
+ "pageId": "filesystem:(05-decision-space)/multi-d-grid-dp/62_unique-paths.mdx",
+ "route": "/multi-d-grid-dp/unique-paths"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Minimum Path Sum",
+ "pageId": "filesystem:(05-decision-space)/multi-d-grid-dp/64_minimum-path-sum.mdx",
+ "route": "/multi-d-grid-dp/minimum-path-sum"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Longest Common Subsequence",
+ "pageId": "filesystem:(05-decision-space)/multi-d-grid-dp/1143_longest-common-subsequence.mdx",
+ "route": "/multi-d-grid-dp/longest-common-subsequence"
+ }
+ ],
+ "display": "group",
+ "icon": "table",
+ "kind": "group",
+ "label": "Multi-D / Grid DP",
+ "path": "/multi-d-grid-dp",
+ "route": "/multi-d-grid-dp"
+ },
+ {
+ "children": [
+ {
+ "badge": "Medium",
+ "description": "Given an integer array nums, find the subarray with the largest sum, and return its sum",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Maximum Subarray",
+ "pageId": "filesystem:(05-decision-space)/greedy/53_maximum-subarray.mdx",
+ "route": "/greedy/maximum-subarray"
+ },
+ {
+ "badge": "Medium",
+ "description": "You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Jump Game",
+ "pageId": "filesystem:(05-decision-space)/greedy/55_jump-game.mdx",
+ "route": "/greedy/jump-game"
+ },
+ {
+ "badge": "Medium",
+ "description": "There are n gas stations along a circular route, where the amount of gas at the i^th station is gas[i]",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Gas Station",
+ "pageId": "filesystem:(05-decision-space)/greedy/134_gas-station.mdx",
+ "route": "/greedy/gas-station"
+ }
+ ],
+ "display": "group",
+ "icon": "coins",
+ "kind": "group",
+ "label": "Greedy",
+ "path": "/greedy",
+ "route": "/greedy"
+ },
+ {
+ "children": [
+ {
+ "badge": "Medium",
+ "description": "Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Merge Intervals",
+ "pageId": "filesystem:(05-decision-space)/intervals/56_merge-intervals.mdx",
+ "route": "/intervals/merge-intervals"
+ },
+ {
+ "badge": "Medium",
+ "description": "You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the i^th interval and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Insert Interval",
+ "pageId": "filesystem:(05-decision-space)/intervals/57_insert-interval.mdx",
+ "route": "/intervals/insert-interval"
+ },
+ {
+ "badge": "Medium",
+ "description": "Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping",
+ "icon": "dot",
+ "kind": "page",
+ "label": "Non-overlapping Intervals",
+ "pageId": "filesystem:(05-decision-space)/intervals/435_non-overlapping-intervals.mdx",
+ "route": "/intervals/non-overlapping-intervals"
+ }
+ ],
+ "display": "group",
+ "icon": "calendar-range",
+ "kind": "group",
+ "label": "Intervals",
+ "path": "/intervals",
+ "route": "/intervals"
+ }
+ ],
+ "display": "flat",
+ "kind": "group",
+ "label": "Decision Space",
+ "path": "/"
+ }
+ ],
+ "tabs": [
+ {
+ "label": "Changelog",
+ "path": "/changelog"
+ },
+ {
+ "label": "Progress",
+ "path": "/progress"
+ },
+ {
+ "label": "Appendix",
+ "path": "/appendix"
+ }
+ ],
+ "repoUrl": "https://github.com/prdlk/leetcode"
+ },
+ "navigationByLocale": {},
+ "navigationByVersion": {},
+ "routes": [
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Structured dependency spine based roadmap for solving DSA Problems on leetcode.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/index.mdx",
+ "entryId": "index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/",
+ "published": null,
+ "title": "Introduction",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "The result for each state is stored in a list, and each new result is computed from the results before it.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(05-decision-space)/1d-dynamic-programming/index.mdx",
+ "entryId": "(05-decision-space)/1d-dynamic-programming/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(05-decision-space)/1d-dynamic-programming/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/1d-dynamic-programming",
+ "published": null,
+ "title": "1-D Dynamic Programming",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You are climbing a staircase. It takes n steps to reach the top",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(05-decision-space)/1d-dynamic-programming/70_climbing-stairs.mdx",
+ "entryId": "(05-decision-space)/1d-dynamic-programming/70_climbing-stairs.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(05-decision-space)/1d-dynamic-programming/70_climbing-stairs.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/1d-dynamic-programming/climbing-stairs",
+ "published": null,
+ "title": "70. Climbing Stairs",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(05-decision-space)/1d-dynamic-programming/322_coin-change.mdx",
+ "entryId": "(05-decision-space)/1d-dynamic-programming/322_coin-change.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(05-decision-space)/1d-dynamic-programming/322_coin-change.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/1d-dynamic-programming/coin-change",
+ "published": null,
+ "title": "322. Coin Change",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(05-decision-space)/1d-dynamic-programming/198_house-robber.mdx",
+ "entryId": "(05-decision-space)/1d-dynamic-programming/198_house-robber.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(05-decision-space)/1d-dynamic-programming/198_house-robber.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/1d-dynamic-programming/house-robber",
+ "published": null,
+ "title": "198. House Robber",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Backtracking is a depth-first search technique that builds a solution step by step and abandons (undoes) a partial choice as soon as it can’t lead to a valid complete solution.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(05-decision-space)/backtracking/index.mdx",
+ "entryId": "(05-decision-space)/backtracking/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(05-decision-space)/backtracking/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/backtracking",
+ "published": null,
+ "title": "Backtracking",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(05-decision-space)/backtracking/39_combination-sum.mdx",
+ "entryId": "(05-decision-space)/backtracking/39_combination-sum.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(05-decision-space)/backtracking/39_combination-sum.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/backtracking/combination-sum",
+ "published": null,
+ "title": "39. Combination Sum",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(05-decision-space)/backtracking/46_permutations.mdx",
+ "entryId": "(05-decision-space)/backtracking/46_permutations.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(05-decision-space)/backtracking/46_permutations.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/backtracking/permutations",
+ "published": null,
+ "title": "46. Permutations",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an integer array nums of unique elements, return all possible subsets (the power set)",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(05-decision-space)/backtracking/78_subsets.mdx",
+ "entryId": "(05-decision-space)/backtracking/78_subsets.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(05-decision-space)/backtracking/78_subsets.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/backtracking/subsets",
+ "published": null,
+ "title": "78. Subsets",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Half of a sorted range is removed at each step until the target is found.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/binary-search/index.mdx",
+ "entryId": "(01-linear-structures)/binary-search/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/binary-search/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/binary-search",
+ "published": null,
+ "title": "Binary Search",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Each node holds a value that is larger than its left subtree and smaller than its right subtree.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(03-hierarchical)/binary-search-tree/index.mdx",
+ "entryId": "(03-hierarchical)/binary-search-tree/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(03-hierarchical)/binary-search-tree/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/binary-search-tree",
+ "published": null,
+ "title": "Binary Search Tree",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given the root of a binary search tree, and an integer k, return the k^th smallest value (1-indexed) of all the values of the nodes in the tree",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(03-hierarchical)/binary-search-tree/230_kth-smallest-element-in-a-bst.mdx",
+ "entryId": "(03-hierarchical)/binary-search-tree/230_kth-smallest-element-in-a-bst.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(03-hierarchical)/binary-search-tree/230_kth-smallest-element-in-a-bst.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/binary-search-tree/kth-smallest-element-in-a-bst",
+ "published": null,
+ "title": "230. Kth Smallest Element in a BST",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(03-hierarchical)/binary-search-tree/235_lowest-common-ancestor-of-a-binary-search-tree.mdx",
+ "entryId": "(03-hierarchical)/binary-search-tree/235_lowest-common-ancestor-of-a-binary-search-tree.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(03-hierarchical)/binary-search-tree/235_lowest-common-ancestor-of-a-binary-search-tree.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/binary-search-tree/lowest-common-ancestor-of-a-binary-search-tree",
+ "published": null,
+ "title": "235. Lowest Common Ancestor of a Binary Search Tree",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given the root of a binary tree, determine if it is a valid binary search tree (BST)",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(03-hierarchical)/binary-search-tree/98_validate-binary-search-tree.mdx",
+ "entryId": "(03-hierarchical)/binary-search-tree/98_validate-binary-search-tree.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(03-hierarchical)/binary-search-tree/98_validate-binary-search-tree.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/binary-search-tree/validate-binary-search-tree",
+ "published": null,
+ "title": "98. Validate Binary Search Tree",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/binary-search/704_binary-search.mdx",
+ "entryId": "(01-linear-structures)/binary-search/704_binary-search.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/binary-search/704_binary-search.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/binary-search/binary-search",
+ "published": null,
+ "title": "704. Binary Search",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/binary-search/34_find-first-and-last-position-of-element-in-sorted-array.mdx",
+ "entryId": "(01-linear-structures)/binary-search/34_find-first-and-last-position-of-element-in-sorted-array.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/binary-search/34_find-first-and-last-position-of-element-in-sorted-array.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/binary-search/find-first-and-last-position-of-element-in-sorted-array",
+ "published": null,
+ "title": "34. Find First and Last Position of Element in Sorted Array",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/binary-search/153_find-minimum-in-rotated-sorted-array.mdx",
+ "entryId": "(01-linear-structures)/binary-search/153_find-minimum-in-rotated-sorted-array.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/binary-search/153_find-minimum-in-rotated-sorted-array.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/binary-search/find-minimum-in-rotated-sorted-array",
+ "published": null,
+ "title": "153. Find Minimum in Rotated Sorted Array",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "A peak element is an element that is strictly greater than its neighbors",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/binary-search/162_find-peak-element.mdx",
+ "entryId": "(01-linear-structures)/binary-search/162_find-peak-element.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/binary-search/162_find-peak-element.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/binary-search/find-peak-element",
+ "published": null,
+ "title": "162. Find Peak Element",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Koko loves to eat bananas. There are n piles of bananas, the i^th pile has piles[i] bananas. The guards have gone and will come back in h hours",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/binary-search/875_koko-eating-bananas.mdx",
+ "entryId": "(01-linear-structures)/binary-search/875_koko-eating-bananas.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/binary-search/875_koko-eating-bananas.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/binary-search/koko-eating-bananas",
+ "published": null,
+ "title": "875. Koko Eating Bananas",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/binary-search/4_median-of-two-sorted-arrays.mdx",
+ "entryId": "(01-linear-structures)/binary-search/4_median-of-two-sorted-arrays.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/binary-search/4_median-of-two-sorted-arrays.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/binary-search/median-of-two-sorted-arrays",
+ "published": null,
+ "title": "4. Median of Two Sorted Arrays",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You are given an m x n integer matrix matrix with the following two properties:",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/binary-search/74_search-a-2d-matrix.mdx",
+ "entryId": "(01-linear-structures)/binary-search/74_search-a-2d-matrix.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/binary-search/74_search-a-2d-matrix.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/binary-search/search-a-2d-matrix",
+ "published": null,
+ "title": "74. Search a 2D Matrix",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "There is an integer array nums sorted in ascending order (with distinct values)",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/binary-search/33_search-in-rotated-sorted-array.mdx",
+ "entryId": "(01-linear-structures)/binary-search/33_search-in-rotated-sorted-array.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/binary-search/33_search-in-rotated-sorted-array.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/binary-search/search-in-rotated-sorted-array",
+ "published": null,
+ "title": "33. Search in Rotated Sorted Array",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/binary-search/35_search-insert-position.mdx",
+ "entryId": "(01-linear-structures)/binary-search/35_search-insert-position.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/binary-search/35_search-insert-position.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/binary-search/search-insert-position",
+ "published": null,
+ "title": "35. Search Insert Position",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Nodes are visited in order of distance from the start, which gives the shortest path in an unweighted graph.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(04-relational)/graph-bfs/index.mdx",
+ "entryId": "(04-relational)/graph-bfs/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(04-relational)/graph-bfs/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/graph-bfs",
+ "published": null,
+ "title": "Graph BFS",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You are given an m x n grid where each cell can have one of three values:",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(04-relational)/graph-bfs/994_rotting-oranges.mdx",
+ "entryId": "(04-relational)/graph-bfs/994_rotting-oranges.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(04-relational)/graph-bfs/994_rotting-oranges.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/graph-bfs/rotting-oranges",
+ "published": null,
+ "title": "994. Rotting Oranges",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(04-relational)/graph-bfs/127_word-ladder.mdx",
+ "entryId": "(04-relational)/graph-bfs/127_word-ladder.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(04-relational)/graph-bfs/127_word-ladder.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/graph-bfs/word-ladder",
+ "published": null,
+ "title": "127. Word Ladder",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Each edge is followed to its end, and a visited set prevents a repeat visit.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(04-relational)/graph-dfs/index.mdx",
+ "entryId": "(04-relational)/graph-dfs/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(04-relational)/graph-dfs/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/graph-dfs",
+ "published": null,
+ "title": "Graph DFS",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given a reference of a node in a connected undirected graph",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(04-relational)/graph-dfs/133_clone-graph.mdx",
+ "entryId": "(04-relational)/graph-dfs/133_clone-graph.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(04-relational)/graph-dfs/133_clone-graph.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/graph-dfs/clone-graph",
+ "published": null,
+ "title": "133. Clone Graph",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(04-relational)/graph-dfs/200_number-of-islands.mdx",
+ "entryId": "(04-relational)/graph-dfs/200_number-of-islands.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(04-relational)/graph-dfs/200_number-of-islands.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/graph-dfs/number-of-islands",
+ "published": null,
+ "title": "200. Number of Islands",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(04-relational)/graph-dfs/417_pacific-atlantic-water-flow.mdx",
+ "entryId": "(04-relational)/graph-dfs/417_pacific-atlantic-water-flow.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(04-relational)/graph-dfs/417_pacific-atlantic-water-flow.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/graph-dfs/pacific-atlantic-water-flow",
+ "published": null,
+ "title": "417. Pacific Atlantic Water Flow",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "The best local choice is made at each step and is not changed later.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(05-decision-space)/greedy/index.mdx",
+ "entryId": "(05-decision-space)/greedy/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(05-decision-space)/greedy/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/greedy",
+ "published": null,
+ "title": "Greedy",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "There are n gas stations along a circular route, where the amount of gas at the i^th station is gas[i]",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(05-decision-space)/greedy/134_gas-station.mdx",
+ "entryId": "(05-decision-space)/greedy/134_gas-station.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(05-decision-space)/greedy/134_gas-station.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/greedy/gas-station",
+ "published": null,
+ "title": "134. Gas Station",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(05-decision-space)/greedy/55_jump-game.mdx",
+ "entryId": "(05-decision-space)/greedy/55_jump-game.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(05-decision-space)/greedy/55_jump-game.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/greedy/jump-game",
+ "published": null,
+ "title": "55. Jump Game",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an integer array nums, find the subarray with the largest sum, and return its sum",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(05-decision-space)/greedy/53_maximum-subarray.mdx",
+ "entryId": "(05-decision-space)/greedy/53_maximum-subarray.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(05-decision-space)/greedy/53_maximum-subarray.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/greedy/maximum-subarray",
+ "published": null,
+ "title": "53. Maximum Subarray",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "A key maps to a value in constant time, which removes the need for a full scan.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/hash-table/index.mdx",
+ "entryId": "(01-linear-structures)/hash-table/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/hash-table/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T15:47:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/hash-table",
+ "published": null,
+ "title": "Hash Table",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "A pangram is a sentence where every letter of the English alphabet appears at least once",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/hash-table/1832_check-if-the-sentence-is-pangram.mdx",
+ "entryId": "(01-linear-structures)/hash-table/1832_check-if-the-sentence-is-pangram.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/hash-table/1832_check-if-the-sentence-is-pangram.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/hash-table/check-if-the-sentence-is-pangram",
+ "published": null,
+ "title": "1832. Check if the Sentence Is Pangram",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/hash-table/217_contains-duplicate.mdx",
+ "entryId": "(01-linear-structures)/hash-table/217_contains-duplicate.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/hash-table/217_contains-duplicate.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/hash-table/contains-duplicate",
+ "published": null,
+ "title": "217. Contains Duplicate",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/hash-table/387_first-unique-character-in-a-string.mdx",
+ "entryId": "(01-linear-structures)/hash-table/387_first-unique-character-in-a-string.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/hash-table/387_first-unique-character-in-a-string.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/hash-table/first-unique-character-in-a-string",
+ "published": null,
+ "title": "387. First Unique Character in a String",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an array of strings strs, group the anagrams together. You can return the answer in any order",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/hash-table/49_group-anagrams.mdx",
+ "entryId": "(01-linear-structures)/hash-table/49_group-anagrams.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/hash-table/49_group-anagrams.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/hash-table/group-anagrams",
+ "published": null,
+ "title": "49. Group Anagrams",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given two strings s and t, determine if they are isomorphic",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/hash-table/205_isomorphic-strings.mdx",
+ "entryId": "(01-linear-structures)/hash-table/205_isomorphic-strings.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/hash-table/205_isomorphic-strings.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/hash-table/isomorphic-strings",
+ "published": null,
+ "title": "205. Isomorphic Strings",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/hash-table/128_longest-consecutive-sequence.mdx",
+ "entryId": "(01-linear-structures)/hash-table/128_longest-consecutive-sequence.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/hash-table/128_longest-consecutive-sequence.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/hash-table/longest-consecutive-sequence",
+ "published": null,
+ "title": "128. Longest Consecutive Sequence",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/hash-table/451_sort-characters-by-frequency.mdx",
+ "entryId": "(01-linear-structures)/hash-table/451_sort-characters-by-frequency.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/hash-table/451_sort-characters-by-frequency.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/hash-table/sort-characters-by-frequency",
+ "published": null,
+ "title": "451. Sort Characters By Frequency",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/hash-table/347_top-k-frequent-elements.mdx",
+ "entryId": "(01-linear-structures)/hash-table/347_top-k-frequent-elements.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/hash-table/347_top-k-frequent-elements.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/hash-table/top-k-frequent-elements",
+ "published": null,
+ "title": "347. Top K Frequent Elements",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You are given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/hash-table/1_two-sum.mdx",
+ "entryId": "(01-linear-structures)/hash-table/1_two-sum.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/hash-table/1_two-sum.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/hash-table/two-sum",
+ "published": null,
+ "title": "1. Two Sum",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given two strings s and t, return true if t is an anagram of s, and false otherwise",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/hash-table/242_valid-anagram.mdx",
+ "entryId": "(01-linear-structures)/hash-table/242_valid-anagram.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/hash-table/242_valid-anagram.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/hash-table/valid-anagram",
+ "published": null,
+ "title": "242. Valid Anagram",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given a pattern and a string s, find if s follows the same pattern",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/hash-table/290_word-pattern.mdx",
+ "entryId": "(01-linear-structures)/hash-table/290_word-pattern.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/hash-table/290_word-pattern.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/hash-table/word-pattern",
+ "published": null,
+ "title": "290. Word Pattern",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "The largest or smallest item is always at the top, and it is removed in logarithmic time.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(03-hierarchical)/heap-priority-queue/index.mdx",
+ "entryId": "(03-hierarchical)/heap-priority-queue/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(03-hierarchical)/heap-priority-queue/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/heap-priority-queue",
+ "published": null,
+ "title": "Heap / Priority Queue",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0)",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(03-hierarchical)/heap-priority-queue/973_k-closest-points-to-origin.mdx",
+ "entryId": "(03-hierarchical)/heap-priority-queue/973_k-closest-points-to-origin.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(03-hierarchical)/heap-priority-queue/973_k-closest-points-to-origin.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/heap-priority-queue/k-closest-points-to-origin",
+ "published": null,
+ "title": "973. K Closest Points to Origin",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an integer array nums and an integer k, return the k^th largest element in the array",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(03-hierarchical)/heap-priority-queue/215_kth-largest-element-in-an-array.mdx",
+ "entryId": "(03-hierarchical)/heap-priority-queue/215_kth-largest-element-in-an-array.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(03-hierarchical)/heap-priority-queue/215_kth-largest-element-in-an-array.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/heap-priority-queue/kth-largest-element-in-an-array",
+ "published": null,
+ "title": "215. Kth Largest Element in an Array",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You are given an array of k linked-lists lists, each linked-list is sorted in ascending order",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(03-hierarchical)/heap-priority-queue/23_merge-k-sorted-lists.mdx",
+ "entryId": "(03-hierarchical)/heap-priority-queue/23_merge-k-sorted-lists.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(03-hierarchical)/heap-priority-queue/23_merge-k-sorted-lists.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/heap-priority-queue/merge-k-sorted-lists",
+ "published": null,
+ "title": "23. Merge k Sorted Lists",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Two structures are combined, such as a hash map and a linked list, to get the best properties of each.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(02-nodal-grid)/hybrid-structures/index.mdx",
+ "entryId": "(02-nodal-grid)/hybrid-structures/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(02-nodal-grid)/hybrid-structures/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/hybrid-structures",
+ "published": null,
+ "title": "Hybrid Structures",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(02-nodal-grid)/hybrid-structures/2_add-two-numbers.mdx",
+ "entryId": "(02-nodal-grid)/hybrid-structures/2_add-two-numbers.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(02-nodal-grid)/hybrid-structures/2_add-two-numbers.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/hybrid-structures/add-two-numbers",
+ "published": null,
+ "title": "2. Add Two Numbers",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Implement the RandomizedSet class:",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(02-nodal-grid)/hybrid-structures/380_insert-delete-getrandom-o1.mdx",
+ "entryId": "(02-nodal-grid)/hybrid-structures/380_insert-delete-getrandom-o1.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(02-nodal-grid)/hybrid-structures/380_insert-delete-getrandom-o1.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/hybrid-structures/insert-delete-getrandom-o1",
+ "published": null,
+ "title": "380. Insert Delete GetRandom O(1)",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Design a data structure that follows the constraints of a Least Recently Used (LRU) cache",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(02-nodal-grid)/hybrid-structures/146_lru-cache.mdx",
+ "entryId": "(02-nodal-grid)/hybrid-structures/146_lru-cache.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(02-nodal-grid)/hybrid-structures/146_lru-cache.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/hybrid-structures/lru-cache",
+ "published": null,
+ "title": "146. LRU Cache",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Ranges with a start and an end are sorted, and then merged, counted, or scheduled.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(05-decision-space)/intervals/index.mdx",
+ "entryId": "(05-decision-space)/intervals/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(05-decision-space)/intervals/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/intervals",
+ "published": null,
+ "title": "Intervals",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the i^th interval and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(05-decision-space)/intervals/57_insert-interval.mdx",
+ "entryId": "(05-decision-space)/intervals/57_insert-interval.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(05-decision-space)/intervals/57_insert-interval.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/intervals/insert-interval",
+ "published": null,
+ "title": "57. Insert Interval",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(05-decision-space)/intervals/56_merge-intervals.mdx",
+ "entryId": "(05-decision-space)/intervals/56_merge-intervals.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(05-decision-space)/intervals/56_merge-intervals.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/intervals/merge-intervals",
+ "published": null,
+ "title": "56. Merge Intervals",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(05-decision-space)/intervals/435_non-overlapping-intervals.mdx",
+ "entryId": "(05-decision-space)/intervals/435_non-overlapping-intervals.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(05-decision-space)/intervals/435_non-overlapping-intervals.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/intervals/non-overlapping-intervals",
+ "published": null,
+ "title": "435. Non-overlapping Intervals",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Coding problems rarely tell you which algorithm to use, but the wording usually leaks it. \"K-th largest\" hints at a heap. \"Shortest path\" in an unweighted graph hints at BFS.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/keywords.mdx",
+ "entryId": "keywords.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:keywords.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/keywords",
+ "published": null,
+ "title": "Keywords",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Each node points to the next node, and the pointers give the path through the data.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(02-nodal-grid)/linked-list/index.mdx",
+ "entryId": "(02-nodal-grid)/linked-list/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(02-nodal-grid)/linked-list/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/linked-list",
+ "published": null,
+ "title": "Linked List",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given head, the head of a linked list, determine if the linked list has a cycle in it",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(02-nodal-grid)/linked-list/141_linked-list-cycle.mdx",
+ "entryId": "(02-nodal-grid)/linked-list/141_linked-list-cycle.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(02-nodal-grid)/linked-list/141_linked-list-cycle.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/linked-list/linked-list-cycle",
+ "published": null,
+ "title": "141. Linked List Cycle",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You are given the heads of two sorted linked lists list1 and list2",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(02-nodal-grid)/linked-list/21_merge-two-sorted-lists.mdx",
+ "entryId": "(02-nodal-grid)/linked-list/21_merge-two-sorted-lists.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(02-nodal-grid)/linked-list/21_merge-two-sorted-lists.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/linked-list/merge-two-sorted-lists",
+ "published": null,
+ "title": "21. Merge Two Sorted Lists",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given the head of a singly linked list, return the middle node of the linked list",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(02-nodal-grid)/linked-list/876_middle-of-the-linked-list.mdx",
+ "entryId": "(02-nodal-grid)/linked-list/876_middle-of-the-linked-list.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(02-nodal-grid)/linked-list/876_middle-of-the-linked-list.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/linked-list/middle-of-the-linked-list",
+ "published": null,
+ "title": "876. Middle of the Linked List",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You are given the head of a sorted linked list",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(02-nodal-grid)/linked-list/82_remove-duplicates-from-sorted-list-ii.mdx",
+ "entryId": "(02-nodal-grid)/linked-list/82_remove-duplicates-from-sorted-list-ii.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(02-nodal-grid)/linked-list/82_remove-duplicates-from-sorted-list-ii.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/linked-list/remove-duplicates-from-sorted-list-ii",
+ "published": null,
+ "title": "82. Remove Duplicates from Sorted List II",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given the head of a linked list, remove the n^th node from the end of the list and return its head",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(02-nodal-grid)/linked-list/19_remove-nth-node-from-end-of-list.mdx",
+ "entryId": "(02-nodal-grid)/linked-list/19_remove-nth-node-from-end-of-list.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(02-nodal-grid)/linked-list/19_remove-nth-node-from-end-of-list.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/linked-list/remove-nth-node-from-end-of-list",
+ "published": null,
+ "title": "19. Remove Nth Node From End of List",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You are given the head of a singly linked-list. The list can be represented as:",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(02-nodal-grid)/linked-list/143_reorder-list.mdx",
+ "entryId": "(02-nodal-grid)/linked-list/143_reorder-list.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(02-nodal-grid)/linked-list/143_reorder-list.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/linked-list/reorder-list",
+ "published": null,
+ "title": "143. Reorder List",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given the head of a singly linked list, reverse the list, and return the reversed list",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(02-nodal-grid)/linked-list/206_reverse-linked-list.mdx",
+ "entryId": "(02-nodal-grid)/linked-list/206_reverse-linked-list.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(02-nodal-grid)/linked-list/206_reverse-linked-list.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/linked-list/reverse-linked-list",
+ "published": null,
+ "title": "206. Reverse Linked List",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(02-nodal-grid)/linked-list/92_reverse-linked-list-ii.mdx",
+ "entryId": "(02-nodal-grid)/linked-list/92_reverse-linked-list-ii.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(02-nodal-grid)/linked-list/92_reverse-linked-list-ii.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/linked-list/reverse-linked-list-ii",
+ "published": null,
+ "title": "92. Reverse Linked List II",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "A two-dimensional grid is read as a flat array through a formula, and the neighbors are found by arithmetic.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(02-nodal-grid)/matrix-index-math/index.mdx",
+ "entryId": "(02-nodal-grid)/matrix-index-math/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(02-nodal-grid)/matrix-index-math/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/matrix-index-math",
+ "published": null,
+ "title": "Matrix Index Math",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise)",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(02-nodal-grid)/matrix-index-math/48_rotate-image.mdx",
+ "entryId": "(02-nodal-grid)/matrix-index-math/48_rotate-image.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(02-nodal-grid)/matrix-index-math/48_rotate-image.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/matrix-index-math/rotate-image",
+ "published": null,
+ "title": "48. Rotate Image",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(02-nodal-grid)/matrix-index-math/73_set-matrix-zeroes.mdx",
+ "entryId": "(02-nodal-grid)/matrix-index-math/73_set-matrix-zeroes.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(02-nodal-grid)/matrix-index-math/73_set-matrix-zeroes.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/matrix-index-math/set-matrix-zeroes",
+ "published": null,
+ "title": "73. Set Matrix Zeroes",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an m x n matrix, return all elements of the matrix in spiral order",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(02-nodal-grid)/matrix-index-math/54_spiral-matrix.mdx",
+ "entryId": "(02-nodal-grid)/matrix-index-math/54_spiral-matrix.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(02-nodal-grid)/matrix-index-math/54_spiral-matrix.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/matrix-index-math/spiral-matrix",
+ "published": null,
+ "title": "54. Spiral Matrix",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(02-nodal-grid)/matrix-index-math/36_valid-sudoku.mdx",
+ "entryId": "(02-nodal-grid)/matrix-index-math/36_valid-sudoku.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(02-nodal-grid)/matrix-index-math/36_valid-sudoku.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/matrix-index-math/valid-sudoku",
+ "published": null,
+ "title": "36. Valid Sudoku",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "A stack keeps its items in sorted order, which finds the next larger or smaller item in one pass.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/monotonic-stack/index.mdx",
+ "entryId": "(01-linear-structures)/monotonic-stack/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/monotonic-stack/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-06T12:53:26-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/monotonic-stack",
+ "published": null,
+ "title": "Monotonic Stack",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "There are n cars at given miles away from the starting mile 0, traveling to reach the mile target",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/monotonic-stack/853_car-fleet.mdx",
+ "entryId": "(01-linear-structures)/monotonic-stack/853_car-fleet.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/monotonic-stack/853_car-fleet.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/monotonic-stack/car-fleet",
+ "published": null,
+ "title": "853. Car Fleet",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the i^th day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/monotonic-stack/739_daily-temperatures.mdx",
+ "entryId": "(01-linear-structures)/monotonic-stack/739_daily-temperatures.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/monotonic-stack/739_daily-temperatures.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/monotonic-stack/daily-temperatures",
+ "published": null,
+ "title": "739. Daily Temperatures",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/monotonic-stack/84_largest-rectangle-in-histogram.mdx",
+ "entryId": "(01-linear-structures)/monotonic-stack/84_largest-rectangle-in-histogram.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/monotonic-stack/84_largest-rectangle-in-histogram.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/monotonic-stack/largest-rectangle-in-histogram",
+ "published": null,
+ "title": "84. Largest Rectangle in Histogram",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "The next greater element of some element x in an array is the first greater element that is to the right of x in the same array",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/monotonic-stack/496_next-greater-element-i.mdx",
+ "entryId": "(01-linear-structures)/monotonic-stack/496_next-greater-element-i.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/monotonic-stack/496_next-greater-element-i.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/monotonic-stack/next-greater-element-i",
+ "published": null,
+ "title": "496. Next Greater Element I",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/monotonic-stack/901_online-stock-span.mdx",
+ "entryId": "(01-linear-structures)/monotonic-stack/901_online-stock-span.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/monotonic-stack/901_online-stock-span.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/monotonic-stack/online-stock-span",
+ "published": null,
+ "title": "901. Online Stock Span",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "The result for each state is stored in a table with two or more dimensions, and each cell is computed from its neighbors.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(05-decision-space)/multi-d-grid-dp/index.mdx",
+ "entryId": "(05-decision-space)/multi-d-grid-dp/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(05-decision-space)/multi-d-grid-dp/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/multi-d-grid-dp",
+ "published": null,
+ "title": "Multi-D / Grid DP",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(05-decision-space)/multi-d-grid-dp/1143_longest-common-subsequence.mdx",
+ "entryId": "(05-decision-space)/multi-d-grid-dp/1143_longest-common-subsequence.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(05-decision-space)/multi-d-grid-dp/1143_longest-common-subsequence.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/multi-d-grid-dp/longest-common-subsequence",
+ "published": null,
+ "title": "1143. Longest Common Subsequence",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(05-decision-space)/multi-d-grid-dp/64_minimum-path-sum.mdx",
+ "entryId": "(05-decision-space)/multi-d-grid-dp/64_minimum-path-sum.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(05-decision-space)/multi-d-grid-dp/64_minimum-path-sum.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/multi-d-grid-dp/minimum-path-sum",
+ "published": null,
+ "title": "64. Minimum Path Sum",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(05-decision-space)/multi-d-grid-dp/62_unique-paths.mdx",
+ "entryId": "(05-decision-space)/multi-d-grid-dp/62_unique-paths.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(05-decision-space)/multi-d-grid-dp/62_unique-paths.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/multi-d-grid-dp/unique-paths",
+ "published": null,
+ "title": "62. Unique Paths",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "A running total is stored at each index, so the sum of any range is one subtraction.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/prefix-sum/index.mdx",
+ "entryId": "(01-linear-structures)/prefix-sum/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/prefix-sum/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/prefix-sum",
+ "published": null,
+ "title": "Prefix Sum",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/prefix-sum/525_contiguous-array.mdx",
+ "entryId": "(01-linear-structures)/prefix-sum/525_contiguous-array.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/prefix-sum/525_contiguous-array.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/prefix-sum/contiguous-array",
+ "published": null,
+ "title": "525. Contiguous Array",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/prefix-sum/238_product-of-array-except-self.mdx",
+ "entryId": "(01-linear-structures)/prefix-sum/238_product-of-array-except-self.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/prefix-sum/238_product-of-array-except-self.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/prefix-sum/product-of-array-except-self",
+ "published": null,
+ "title": "238. Product of Array Except Self",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an integer array nums, handle multiple queries of the following type",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/prefix-sum/303_range-sum-query-immutable.mdx",
+ "entryId": "(01-linear-structures)/prefix-sum/303_range-sum-query-immutable.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/prefix-sum/303_range-sum-query-immutable.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/prefix-sum/range-sum-query-immutable",
+ "published": null,
+ "title": "303. Range Sum Query - Immutable",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/prefix-sum/560_subarray-sum-equals-k.mdx",
+ "entryId": "(01-linear-structures)/prefix-sum/560_subarray-sum-equals-k.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/prefix-sum/560_subarray-sum-equals-k.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/prefix-sum/subarray-sum-equals-k",
+ "published": null,
+ "title": "560. Subarray Sum Equals K",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an integer array nums and an integer k, return the number of non-empty subarrays that have a sum divisible by k",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/prefix-sum/974_subarray-sums-divisible-by-k.mdx",
+ "entryId": "(01-linear-structures)/prefix-sum/974_subarray-sums-divisible-by-k.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/prefix-sum/974_subarray-sums-divisible-by-k.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/prefix-sum/subarray-sums-divisible-by-k",
+ "published": null,
+ "title": "974. Subarray Sums Divisible by K",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Sliding window is a specialized two-pointer family where the pointers define a contiguous interval and you maintain information about that entire interval as it moves.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/sliding-window/index.mdx",
+ "entryId": "(01-linear-structures)/sliding-window/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/sliding-window/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/sliding-window",
+ "published": null,
+ "title": "Sliding Window",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You are given an array prices where prices[i] is the price of a given stock on the i^th day",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/sliding-window/121_best-time-to-buy-and-sell-stock.mdx",
+ "entryId": "(01-linear-structures)/sliding-window/121_best-time-to-buy-and-sell-stock.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/sliding-window/121_best-time-to-buy-and-sell-stock.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/sliding-window/best-time-to-buy-and-sell-stock",
+ "published": null,
+ "title": "121. Best Time to Buy and Sell Stock",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/sliding-window/424_longest-repeating-character-replacement.mdx",
+ "entryId": "(01-linear-structures)/sliding-window/424_longest-repeating-character-replacement.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/sliding-window/424_longest-repeating-character-replacement.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/sliding-window/longest-repeating-character-replacement",
+ "published": null,
+ "title": "424. Longest Repeating Character Replacement",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given a string s, find the length of the longest substring without duplicate characters",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/sliding-window/3_longest-substring-without-repeating-characters.mdx",
+ "entryId": "(01-linear-structures)/sliding-window/3_longest-substring-without-repeating-characters.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/sliding-window/3_longest-substring-without-repeating-characters.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/sliding-window/longest-substring-without-repeating-characters",
+ "published": null,
+ "title": "3. Longest Substring Without Repeating Characters",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/sliding-window/1004_max-consecutive-ones-iii.mdx",
+ "entryId": "(01-linear-structures)/sliding-window/1004_max-consecutive-ones-iii.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/sliding-window/1004_max-consecutive-ones-iii.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/sliding-window/max-consecutive-ones-iii",
+ "published": null,
+ "title": "1004. Max Consecutive Ones III",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You are given an integer array nums consisting of n elements, and an integer k",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/sliding-window/643_maximum-average-subarray-i.mdx",
+ "entryId": "(01-linear-structures)/sliding-window/643_maximum-average-subarray-i.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/sliding-window/643_maximum-average-subarray-i.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/sliding-window/maximum-average-subarray-i",
+ "published": null,
+ "title": "643. Maximum Average Subarray I",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/sliding-window/209_minimum-size-subarray-sum.mdx",
+ "entryId": "(01-linear-structures)/sliding-window/209_minimum-size-subarray-sum.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/sliding-window/209_minimum-size-subarray-sum.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/sliding-window/minimum-size-subarray-sum",
+ "published": null,
+ "title": "209. Minimum Size Subarray Sum",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string \"\"",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/sliding-window/76_minimum-window-substring.mdx",
+ "entryId": "(01-linear-structures)/sliding-window/76_minimum-window-substring.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/sliding-window/76_minimum-window-substring.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/sliding-window/minimum-window-substring",
+ "published": null,
+ "title": "76. Minimum Window Substring",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/sliding-window/567_permutation-in-string.mdx",
+ "entryId": "(01-linear-structures)/sliding-window/567_permutation-in-string.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/sliding-window/567_permutation-in-string.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/sliding-window/permutation-in-string",
+ "published": null,
+ "title": "567. Permutation in String",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Items are added and removed from one end, so the last item added is the first item removed.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/stack/index.mdx",
+ "entryId": "(01-linear-structures)/stack/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/stack/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-06T12:58:52-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/stack",
+ "published": null,
+ "title": "Stack",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/stack/224_basic-calculator.mdx",
+ "entryId": "(01-linear-structures)/stack/224_basic-calculator.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/stack/224_basic-calculator.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/stack/basic-calculator",
+ "published": null,
+ "title": "224. Basic Calculator",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/stack/150_evaluate-reverse-polish-notation.mdx",
+ "entryId": "(01-linear-structures)/stack/150_evaluate-reverse-polish-notation.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/stack/150_evaluate-reverse-polish-notation.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/stack/evaluate-reverse-polish-notation",
+ "published": null,
+ "title": "150. Evaluate Reverse Polish Notation",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Design a stack that supports push, pop, top, and retrieving the minimum element in constant time",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/stack/155_min-stack.mdx",
+ "entryId": "(01-linear-structures)/stack/155_min-stack.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/stack/155_min-stack.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/stack/min-stack",
+ "published": null,
+ "title": "155. Min Stack",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You are given an absolute path for a Unix-style file system, which always begins with a slash '/'. Your task is to transform this absolute path into its simplified canonical path",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/stack/71_simplify-path.mdx",
+ "entryId": "(01-linear-structures)/stack/71_simplify-path.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/stack/71_simplify-path.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/stack/simplify-path",
+ "published": null,
+ "title": "71. Simplify Path",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/stack/20_valid-parentheses.mdx",
+ "entryId": "(01-linear-structures)/stack/20_valid-parentheses.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/stack/20_valid-parentheses.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/stack/valid-parentheses",
+ "published": null,
+ "title": "20. Valid Parentheses",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Topological sort or topological ordering of a directed graph is an ordering of nodes such that every node appears in the ordering before all the nodes it points to. ",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(04-relational)/topological-sort/index.mdx",
+ "entryId": "(04-relational)/topological-sort/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(04-relational)/topological-sort/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/topological-sort",
+ "published": null,
+ "title": "Topological Sort",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(04-relational)/topological-sort/207_course-schedule.mdx",
+ "entryId": "(04-relational)/topological-sort/207_course-schedule.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(04-relational)/topological-sort/207_course-schedule.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/topological-sort/course-schedule",
+ "published": null,
+ "title": "207. Course Schedule",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(04-relational)/topological-sort/210_course-schedule-ii.mdx",
+ "entryId": "(04-relational)/topological-sort/210_course-schedule-ii.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(04-relational)/topological-sort/210_course-schedule-ii.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/topological-sort/course-schedule-ii",
+ "published": null,
+ "title": "210. Course Schedule II",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "All nodes at one level are visited before the nodes at the next level.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(03-hierarchical)/tree-bfs/index.mdx",
+ "entryId": "(03-hierarchical)/tree-bfs/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(03-hierarchical)/tree-bfs/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/tree-bfs",
+ "published": null,
+ "title": "Tree BFS",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level)",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(03-hierarchical)/tree-bfs/102_binary-tree-level-order-traversal.mdx",
+ "entryId": "(03-hierarchical)/tree-bfs/102_binary-tree-level-order-traversal.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(03-hierarchical)/tree-bfs/102_binary-tree-level-order-traversal.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/tree-bfs/binary-tree-level-order-traversal",
+ "published": null,
+ "title": "102. Binary Tree Level Order Traversal",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(03-hierarchical)/tree-bfs/199_binary-tree-right-side-view.mdx",
+ "entryId": "(03-hierarchical)/tree-bfs/199_binary-tree-right-side-view.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(03-hierarchical)/tree-bfs/199_binary-tree-right-side-view.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/tree-bfs/binary-tree-right-side-view",
+ "published": null,
+ "title": "199. Binary Tree Right Side View",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(03-hierarchical)/tree-bfs/1448_count-good-nodes-in-binary-tree.mdx",
+ "entryId": "(03-hierarchical)/tree-bfs/1448_count-good-nodes-in-binary-tree.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(03-hierarchical)/tree-bfs/1448_count-good-nodes-in-binary-tree.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/tree-bfs/count-good-nodes-in-binary-tree",
+ "published": null,
+ "title": "1448. Count Good Nodes in Binary Tree",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Each branch is followed to its end before the next branch is examined.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(03-hierarchical)/tree-dfs/index.mdx",
+ "entryId": "(03-hierarchical)/tree-dfs/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(03-hierarchical)/tree-dfs/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/tree-dfs",
+ "published": null,
+ "title": "Tree DFS",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given the root of a binary tree, invert the tree, and return its root",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(03-hierarchical)/tree-dfs/226_invert-binary-tree.mdx",
+ "entryId": "(03-hierarchical)/tree-dfs/226_invert-binary-tree.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(03-hierarchical)/tree-dfs/226_invert-binary-tree.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/tree-dfs/invert-binary-tree",
+ "published": null,
+ "title": "226. Invert Binary Tree",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(03-hierarchical)/tree-dfs/236_lowest-common-ancestor-of-a-binary-tree.mdx",
+ "entryId": "(03-hierarchical)/tree-dfs/236_lowest-common-ancestor-of-a-binary-tree.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(03-hierarchical)/tree-dfs/236_lowest-common-ancestor-of-a-binary-tree.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/tree-dfs/lowest-common-ancestor-of-a-binary-tree",
+ "published": null,
+ "title": "236. Lowest Common Ancestor of a Binary Tree",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given the root of a binary tree, return its maximum depth",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(03-hierarchical)/tree-dfs/104_maximum-depth-of-binary-tree.mdx",
+ "entryId": "(03-hierarchical)/tree-dfs/104_maximum-depth-of-binary-tree.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(03-hierarchical)/tree-dfs/104_maximum-depth-of-binary-tree.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/tree-dfs/maximum-depth-of-binary-tree",
+ "published": null,
+ "title": "104. Maximum Depth of Binary Tree",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Each node holds one character, and a path from the root spells a word.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(03-hierarchical)/trie/index.mdx",
+ "entryId": "(03-hierarchical)/trie/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(03-hierarchical)/trie/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/trie",
+ "published": null,
+ "title": "Trie",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Design a data structure that supports adding new words and finding if a string matches any previously added string",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(03-hierarchical)/trie/211_design-add-and-search-words-data-structure.mdx",
+ "entryId": "(03-hierarchical)/trie/211_design-add-and-search-words-data-structure.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(03-hierarchical)/trie/211_design-add-and-search-words-data-structure.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/trie/design-add-and-search-words-data-structure",
+ "published": null,
+ "title": "211. Design Add and Search Words Data Structure",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "A trie (pronounced as \"try\") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(03-hierarchical)/trie/208_implement-trie-prefix-tree.mdx",
+ "entryId": "(03-hierarchical)/trie/208_implement-trie-prefix-tree.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(03-hierarchical)/trie/208_implement-trie-prefix-tree.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/trie/implement-trie-prefix-tree",
+ "published": null,
+ "title": "208. Implement Trie (Prefix Tree)",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Two indices move through the data from different positions and meet at the answer.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/two-pointers/index.mdx",
+ "entryId": "(01-linear-structures)/two-pointers/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/two-pointers/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-06T13:16:45-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/two-pointers",
+ "published": null,
+ "title": "Two Pointers",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/two-pointers/15_3sum.mdx",
+ "entryId": "(01-linear-structures)/two-pointers/15_3sum.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/two-pointers/15_3sum.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/two-pointers/3sum",
+ "published": null,
+ "title": "15. 3Sum",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the i^th line are (i, 0) and (i, height[i])",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/two-pointers/11_container-with-most-water.mdx",
+ "entryId": "(01-linear-structures)/two-pointers/11_container-with-most-water.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/two-pointers/11_container-with-most-water.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/two-pointers/container-with-most-water",
+ "published": null,
+ "title": "11. Container With Most Water",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given two strings s and t, return true if s is a subsequence of t, or false otherwise",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/two-pointers/392_is-subsequence.mdx",
+ "entryId": "(01-linear-structures)/two-pointers/392_is-subsequence.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/two-pointers/392_is-subsequence.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/two-pointers/is-subsequence",
+ "published": null,
+ "title": "392. Is Subsequence",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/two-pointers/80_remove-duplicates-from-sorted-array-ii.mdx",
+ "entryId": "(01-linear-structures)/two-pointers/80_remove-duplicates-from-sorted-array-ii.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/two-pointers/80_remove-duplicates-from-sorted-array-ii.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/two-pointers/remove-duplicates-from-sorted-array-ii",
+ "published": null,
+ "title": "80. Remove Duplicates from Sorted Array II",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Write a function that reverses a string. The input string is given as an array of characters s",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/two-pointers/344_reverse-string.mdx",
+ "entryId": "(01-linear-structures)/two-pointers/344_reverse-string.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/two-pointers/344_reverse-string.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/two-pointers/reverse-string",
+ "published": null,
+ "title": "344. Reverse String",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an integer array nums, rotate the array to the right by k steps, where k is non-negative",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/two-pointers/189_rotate-array.mdx",
+ "entryId": "(01-linear-structures)/two-pointers/189_rotate-array.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/two-pointers/189_rotate-array.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/two-pointers/rotate-array",
+ "published": null,
+ "title": "189. Rotate Array",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/two-pointers/977_squares-of-a-sorted-array.mdx",
+ "entryId": "(01-linear-structures)/two-pointers/977_squares-of-a-sorted-array.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/two-pointers/977_squares-of-a-sorted-array.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/two-pointers/squares-of-a-sorted-array",
+ "published": null,
+ "title": "977. Squares of a Sorted Array",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/two-pointers/42_trapping-rain-water.mdx",
+ "entryId": "(01-linear-structures)/two-pointers/42_trapping-rain-water.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/two-pointers/42_trapping-rain-water.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/two-pointers/trapping-rain-water",
+ "published": null,
+ "title": "42. Trapping Rain Water",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/two-pointers/167_two-sum-ii-input-array-is-sorted.mdx",
+ "entryId": "(01-linear-structures)/two-pointers/167_two-sum-ii-input-array-is-sorted.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/two-pointers/167_two-sum-ii-input-array-is-sorted.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/two-pointers/two-sum-ii-input-array-is-sorted",
+ "published": null,
+ "title": "167. Two Sum II - Input Array Is Sorted",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(01-linear-structures)/two-pointers/125_valid-palindrome.mdx",
+ "entryId": "(01-linear-structures)/two-pointers/125_valid-palindrome.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(01-linear-structures)/two-pointers/125_valid-palindrome.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/two-pointers/valid-palindrome",
+ "published": null,
+ "title": "125. Valid Palindrome",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "Union-Find (Disjoint Set Union) is a data structure that maintains a collection of disjoint sets and supports efficiently finding which set an element belongs to and merging two sets.",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(04-relational)/union-find/index.mdx",
+ "entryId": "(04-relational)/union-find/index.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(04-relational)/union-find/index.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/union-find",
+ "published": null,
+ "title": "Union Find",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "You have a graph of n nodes. You are given an integer n and an array edges where edges[i] = [ai, bi] indicates that there is an edge between ai and bi in the graph",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(04-relational)/union-find/323_number-of-connected-components-in-an-undirected-graph.mdx",
+ "entryId": "(04-relational)/union-find/323_number-of-connected-components-in-an-undirected-graph.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(04-relational)/union-find/323_number-of-connected-components-in-an-undirected-graph.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-05T14:22:28-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/union-find/number-of-connected-components-in-an-undirected-graph",
+ "published": null,
+ "title": "323. Number of Connected Components in an Undirected Graph",
+ "version": "",
+ "versionAlternates": []
+ },
+ {
+ "alternates": [],
+ "collection": "docs",
+ "description": "In this problem, a tree is an undirected graph that is connected and has no cycles",
+ "draft": false,
+ "editUrl": "https://github.com/prdlk/leetcode/edit/main/content/(04-relational)/union-find/684_redundant-connection.mdx",
+ "entryId": "(04-relational)/union-find/684_redundant-connection.mdx",
+ "fallback": false,
+ "hidden": false,
+ "hreflang": [],
+ "id": "filesystem:(04-relational)/union-find/684_redundant-connection.mdx",
+ "indexable": true,
+ "lastModified": "2026-10-14T15:52:02-04:00",
+ "locale": "",
+ "monolingual": false,
+ "path": "/union-find/redundant-connection",
+ "published": null,
+ "title": "684. Redundant Connection",
+ "version": "",
+ "versionAlternates": []
+ }
+ ],
+ "ui": {
+ "actions": {
+ "addToCursor": "Add to Cursor",
+ "addToVscode": "Add to VS Code",
+ "askAboutCode": "Ask about this code",
+ "connectMcp": "Connect to MCP",
+ "copied": "Copied!",
+ "copyClaudeCode": "Copy Claude Code command",
+ "copyCode": "Copy code",
+ "copyCodex": "Copy Codex command",
+ "copyFailed": "Copy failed",
+ "copyMarkdown": "Copy as Markdown",
+ "copyServerUrl": "Copy server URL",
+ "edit": "Edit on GitHub",
+ "export": "Export",
+ "exportEpub": "Export to EPUB",
+ "exportPdf": "Export to PDF",
+ "generating": "Generating…",
+ "openIn": "Open in {name}",
+ "openInChat": "Open in chat",
+ "openInChatPrompt": "Read {url} so I can ask you questions about this page.",
+ "scrollToTop": "Scroll to top"
+ },
+ "assistant": {
+ "ai": "AI",
+ "clear": "Clear conversation",
+ "close": "Close",
+ "copy": "Copy conversation",
+ "empty": "Ask a question about the docs.",
+ "error": "Sorry, something went wrong.",
+ "explainCode": "Explain this code.",
+ "label": "Ask a question",
+ "open": "Open assistant",
+ "placeholder": "Ask a question…",
+ "rateLimited": "You've asked a lot of questions. Try again in a few minutes.",
+ "removeCode": "Remove code",
+ "send": "Send",
+ "support": "Contact support",
+ "supportSubject": "Question from the docs",
+ "tip": "Tip: You can open and close chat with",
+ "title": "Assistant",
+ "verifyFailed": "We couldn't check that you're human. Try again.",
+ "you": "You"
+ },
+ "banner": {
+ "dismiss": "Dismiss announcement"
+ },
+ "changelog": {
+ "description": "Product updates, new features, and fixes from every release.",
+ "empty": "No changelog entries yet.",
+ "title": "Changelog"
+ },
+ "consent": {
+ "accept": "Accept",
+ "decline": "Decline",
+ "label": "Cookie consent",
+ "message": "We'd like to use cookies to understand how these docs are used.",
+ "policy": "Privacy policy",
+ "settings": "Cookie settings"
+ },
+ "content": {
+ "code": "Code",
+ "copyColor": "Copy {name} color {value}",
+ "copyPrompt": "Copy prompt",
+ "default": "Default",
+ "diagramError": "Could not render this diagram.",
+ "forks": "Forks",
+ "preview": "Preview",
+ "prop": "Prop",
+ "selectTab": "Select tab",
+ "selectView": "Select view",
+ "showLess": "Show less",
+ "showMore": "Show more",
+ "stars": "Stars",
+ "tab": "Tab {n}",
+ "type": "Type",
+ "update": "Update"
+ },
+ "feedback": {
+ "comment": "Tell us more (optional)",
+ "no": "No",
+ "question": "Was this page helpful?",
+ "send": "Send",
+ "thanks": "Thanks for your feedback!",
+ "yes": "Yes"
+ },
+ "languageSwitcher": {
+ "label": "Language",
+ "untranslated": "Not translated"
+ },
+ "narration": {
+ "cueDanger": "Danger.",
+ "cueInfo": "Info.",
+ "cueNote": "Note.",
+ "cueSection": "Expandable section.",
+ "cueStep": "Step {n}.",
+ "cueSuccess": "Success.",
+ "cueTab": "{title} tab.",
+ "cueTip": "Tip.",
+ "cueWarning": "Warning.",
+ "error": "The narration couldn't load.",
+ "follow": "Follow along",
+ "label": "Listen to this page",
+ "minutes": "{n} min",
+ "next": "Next sentence",
+ "pause": "Pause",
+ "play": "Play",
+ "previous": "Previous sentence",
+ "progress": "Narration progress",
+ "speed": "Playback speed",
+ "stop": "Stop listening"
+ },
+ "nav": {
+ "back": "Back",
+ "breadcrumb": "Breadcrumb",
+ "closeNavigation": "Close navigation",
+ "deprecated": "deprecated",
+ "featured": "Featured",
+ "githubRepository": "GitHub repository",
+ "navigation": "Navigation",
+ "opensInNewTab": "Opens in a new tab",
+ "primary": "Primary",
+ "sections": "Sections",
+ "toggleNavigation": "Toggle navigation",
+ "toggleTheme": "Toggle theme"
+ },
+ "notFound": {
+ "api": "JSON API description (openapi.json)",
+ "description": "We couldn't find the page you're looking for.",
+ "home": "Back to home",
+ "llms": "Docs index for AI agents (llms.txt)",
+ "sitemap": "Sitemap",
+ "suggestions": "Where to look next",
+ "title": "Page not found"
+ },
+ "page": {
+ "lastUpdated": "Last updated on",
+ "next": "Next",
+ "pagination": "Pagination",
+ "previous": "Previous",
+ "related": "Related pages",
+ "skipToContent": "Skip to content"
+ },
+ "search": {
+ "all": "All",
+ "allLanguages": "All languages",
+ "allVersions": "All versions",
+ "assistant": "Assistant",
+ "assistantHint": "Get an instant answer from AI",
+ "button": "Search",
+ "devOnly": "Search is available in the production build.",
+ "docs": "Docs",
+ "error": "Something went wrong. Please try again.",
+ "label": "Search docs",
+ "latest": "latest",
+ "navigate": "navigate",
+ "noResults": "No results found.",
+ "open": "open",
+ "placeholder": "Search documentation…",
+ "popular": "Popular",
+ "preview": "preview",
+ "results": "Results"
+ },
+ "toc": {
+ "title": "On this page"
+ },
+ "versions": {
+ "latest": "Go to latest",
+ "notice": "You're viewing documentation for {version}. It may be out of date.",
+ "switcher": "Version"
+ }
+ },
+ "uiByLocale": {}
+}
diff --git a/apps/docs/src/generated/examples.css b/apps/docs/src/generated/examples.css
new file mode 100644
index 0000000..bbe3945
--- /dev/null
+++ b/apps/docs/src/generated/examples.css
@@ -0,0 +1,154 @@
+/* Generated by Blume. Do not edit. */
+@import "tailwindcss";
+
+/* Scan the project (and an out-of-root examples directory) for utility classes. */
+@source "../../**/*.{astro,jsx,svelte,ts,tsx,vue}";
+
+/* Dark mode is driven by data-theme on the element. */
+@custom-variant dark (&:where([data-theme="dark"], [data-theme="dark"] *));
+
+:root {
+ color-scheme: light;
+}
+
+:root[data-theme="dark"] {
+ color-scheme: dark;
+}
+
+:root {
+ --blume-background: oklch(1 0 0);
+ --blume-background-image: none;
+ --blume-background-image-repeat: no-repeat;
+ --blume-background-image-size: cover;
+ --blume-foreground: oklch(0.145 0 0);
+ --blume-muted: oklch(0.965 0 0);
+ /* 5.28:1 on the page background. The headroom over 4.5:1 is the point: muted
+ text is 14px body copy routinely set on a tinted surface (callouts, badges,
+ panels), and each tint costs a few tenths. */
+ --blume-muted-foreground: oklch(0.53 0 0);
+ --blume-border: oklch(0.88 0.006 260 / 0.72);
+ --blume-accent: oklch(0.145 0 0);
+ --blume-accent-foreground: oklch(1 0 0);
+ --blume-action: var(--blume-accent);
+ --blume-action-foreground: var(--blume-accent-foreground);
+ --blume-code-background: oklch(0.99 0 0);
+ /* Shiki notation transformers: line/word highlight, diff add/remove, and
+ error/warning lines. */
+ --blume-code-highlight: oklch(0.55 0.16 255 / 0.1);
+ --blume-code-highlight-border: oklch(0.55 0.16 255 / 0.55);
+ --blume-code-add: oklch(0.72 0.16 150 / 0.16);
+ --blume-code-add-border: oklch(0.52 0.15 150 / 0.7);
+ --blume-code-remove: oklch(0.66 0.21 22 / 0.16);
+ --blume-code-remove-border: oklch(0.55 0.2 22 / 0.7);
+ --blume-code-error: oklch(0.66 0.21 22 / 0.16);
+ --blume-code-error-border: oklch(0.55 0.2 22 / 0.7);
+ --blume-code-warning: oklch(0.8 0.16 80 / 0.2);
+ --blume-code-warning-border: oklch(0.66 0.15 70 / 0.75);
+ --blume-code-word: oklch(0.55 0.16 255 / 0.16);
+ --blume-code-word-border: oklch(0.55 0.16 255 / 0.5);
+ --blume-radius: 0.75rem;
+
+ /* Max width of the prose content column (article, breadcrumb, TOC, feedback,
+ pagination, last-updated). Override to re-measure the whole column at once. */
+ --blume-content-width: 42rem;
+
+ /*
+ * Font tokens resolve through optional src variables that Astro's Fonts API
+ * populates (via theme.fonts). When unset they fall back to the system
+ * stacks, so the default look is unchanged. The display font defaults to the
+ * body font.
+ */
+ --blume-font-body: var(
+ --blume-font-body-src,
+ ui-sans-serif,
+ system-ui,
+ -apple-system,
+ "Segoe UI",
+ Roboto,
+ Helvetica,
+ Arial,
+ sans-serif
+ );
+ --blume-font-mono: var(
+ --blume-font-mono-src,
+ ui-monospace,
+ "SF Mono",
+ "Cascadia Code",
+ "Source Code Pro",
+ Menlo,
+ Consolas,
+ monospace
+ );
+ --blume-font-display: var(--blume-font-display-src, var(--blume-font-body));
+}
+
+:root[data-theme="dark"] {
+ --blume-background: oklch(0.085 0 0);
+ --blume-background-image: none;
+ --blume-background-image-repeat: no-repeat;
+ --blume-background-image-size: cover;
+ --blume-foreground: oklch(0.96 0 0);
+ --blume-muted: oklch(0.16 0 0);
+ --blume-muted-foreground: oklch(0.68 0 0);
+ --blume-border: oklch(0.24 0 0 / 0.8);
+ --blume-accent: oklch(0.96 0 0);
+ --blume-accent-foreground: oklch(0.085 0 0);
+ --blume-action: var(--blume-accent);
+ --blume-action-foreground: var(--blume-accent-foreground);
+ --blume-code-background: oklch(0.12 0 0);
+ /* Brighter tints read better over the dark code surface. */
+ --blume-code-highlight: oklch(0.7 0.14 255 / 0.16);
+ --blume-code-highlight-border: oklch(0.7 0.14 255 / 0.6);
+ --blume-code-add: oklch(0.78 0.17 150 / 0.2);
+ --blume-code-add-border: oklch(0.72 0.16 150 / 0.7);
+ --blume-code-remove: oklch(0.72 0.21 22 / 0.22);
+ --blume-code-remove-border: oklch(0.7 0.2 22 / 0.7);
+ --blume-code-error: oklch(0.72 0.21 22 / 0.22);
+ --blume-code-error-border: oklch(0.7 0.2 22 / 0.7);
+ --blume-code-warning: oklch(0.82 0.16 80 / 0.18);
+ --blume-code-warning-border: oklch(0.82 0.16 80 / 0.7);
+ --blume-code-word: oklch(0.7 0.14 255 / 0.22);
+ --blume-code-word-border: oklch(0.7 0.14 255 / 0.55);
+}
+
+@theme inline {
+ --color-background: var(--blume-background);
+ --color-foreground: var(--blume-foreground);
+ --color-muted: var(--blume-muted);
+ --color-muted-foreground: var(--blume-muted-foreground);
+ --color-border: var(--blume-border);
+ --color-accent: var(--blume-accent);
+ --color-accent-foreground: var(--blume-accent-foreground);
+ --color-action: var(--blume-action);
+ --color-action-foreground: var(--blume-action-foreground);
+ --color-code: var(--blume-code-background);
+ --radius-blume: var(--blume-radius);
+ --container-content: var(--blume-content-width);
+ --font-sans: var(--blume-font-body);
+ --font-mono: var(--blume-font-mono);
+ --font-display: var(--blume-font-display);
+}
+
+/* Frame defaults: readable text in both modes, transparent so the docs pane's
+ surface shows through. Base layer, so any user/example CSS wins. */
+@layer base {
+ body {
+ background: transparent;
+ color: var(--blume-foreground);
+ }
+}
+
+/* Token overrides: config first, then the configured examples css (highest
+ priority) — the place for shadcn variables and other component tokens. */
+/* Generated by Blume from theme config. */
+:root {
+ --blume-accent: oklch(0.55 0.16 250);
+ --blume-accent-foreground: oklch(1 0 0);
+ --blume-radius: 0.5rem;
+}
+:root[data-theme="dark"] {
+ --blume-accent: oklch(0.62 0.16 250);
+ --blume-accent-foreground: oklch(0.085 0 0);
+}
+
+
diff --git a/apps/docs/src/generated/examples.ts b/apps/docs/src/generated/examples.ts
new file mode 100644
index 0000000..df6f66b
--- /dev/null
+++ b/apps/docs/src/generated/examples.ts
@@ -0,0 +1,3 @@
+// Generated by Blume. Do not edit.
+export const examplesBase = "/blume-examples";
+export const examples = {};
diff --git a/apps/docs/src/generated/features.ts b/apps/docs/src/generated/features.ts
new file mode 100644
index 0000000..200f1d6
--- /dev/null
+++ b/apps/docs/src/generated/features.ts
@@ -0,0 +1,11 @@
+// Generated by Blume. Do not edit.
+//
+// The client features this site uses. A feature no page needs stays out of
+// the module graph entirely: its loader is null and its library never enters
+// the client bundle.
+
+/** Registers the element; null when no page has a mermaid fence. */
+export const loadMermaid: (() => Promise) | null = () => import("blume/components/content/mermaid-element.ts");
+
+/** The EPUB generator's browser bundle; null when export.epub is off. */
+export const loadEpub: (() => Promise) | null = null;
diff --git a/apps/docs/src/generated/includes.json b/apps/docs/src/generated/includes.json
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/apps/docs/src/generated/includes.json
@@ -0,0 +1 @@
+{}
diff --git a/apps/docs/src/generated/mcp-data.json b/apps/docs/src/generated/mcp-data.json
new file mode 100644
index 0000000..a1ff66b
--- /dev/null
+++ b/apps/docs/src/generated/mcp-data.json
@@ -0,0 +1 @@
+{"base":"","documents":[{"content":"1. Linear Structures Data is read in one direction, from the first item to the last. The current position is recorded at each step. All later phases use this skill. Hash Table A key maps to a value in constant time, which removes the need for a full scan. Prefix Sum A running total is stored at each index, so the sum of any range is one subtraction. Two Pointers Two indices move through the data from different positions and meet at the answer. Sliding Window A range with a start and an end moves across the data, and the contents are updated at each step. Binary Search Half of a sorted range is removed at each step until the target is found. Stack Items are added and removed from one end, so the last item added is the first item removed. Monotonic Stack A stack keeps its items in sorted order, which finds the next larger or smaller item in one pass. 2. Nodal & Grid The data is in separate parts. Each part points to the next part, or sits next to other parts on a flat surface. An index does not find the next item. The pointers or the neighbors give the path instead. Linked List Each node points to the next node, and the pointers give the path through the data. Hybrid Structures Two structures are combined, such as a hash map and a linked list, to get the best properties of each. Matrix Index Math A two-dimensional grid is read as a flat array through a formula, and the neighbors are found by arithmetic. 3. Hierarchical Each part points to one or more parts below it. Those parts do the same. The method that solves a small part also solves the large part. This is recursion. Tree DFS Each branch is followed to its end before the next branch is examined. Tree BFS All nodes at one level are visited before the nodes at the next level. Binary Search Tree Each node holds a value that is larger than its left subtree and smaller than its right subtree. Heap The largest or smallest item is always at the top, and it is removed in logarithmic time. Trie Each node holds one character, and a path from the root spells a word. 4. Relational Each part can point to any other part. Loops can occur. The method must visit each part one time only. The order of the parts, or the distance between them, is not given in the data. The method finds it. Graph DFS Each edge is followed to its end, and a visited set prevents a repeat visit. Graph BFS Nodes are visited in order of distance from the start, which gives the shortest path in an unweighted graph. Topological Sort Nodes are put in an order where each node comes after all nodes that point to it. Union-Find Two sets are merged and the set of any item is found in near-constant time. 5. Decision Space The data has no structure. The method makes one. Each choice makes a new branch. The problem sets the correct method: examine all branches, keep a record of the results already found, or make one choice and do not go back. Backtracking Each choice makes a branch, and a branch is undone when it does not give a valid result. 1-D DP The result for each state is stored in a list, and each new result is computed from the results before it. Multi-D / Grid DP The result for each state is stored in a table with two or more dimensions, and each cell is computed from its neighbors. Greedy The best local choice is made at each step and is not changed later. Intervals Ranges with a start and an end are sorted, and then merged, counted, or scheduled.","contentType":"doc","description":"Structured dependency spine based roadmap for solving DSA Problems on leetcode.","locale":"","route":"/","title":"Introduction","version":""},{"content":"","contentType":"doc","description":"The result for each state is stored in a list, and each new result is computed from the results before it.","locale":"","route":"/1d-dynamic-programming","title":"1-D Dynamic Programming","version":""},{"content":"Example 1: Input: n = 2 Output: 2 Explanation: There are two ways to climb to the top. 1. 1 step + 1 step 2. 2 steps Example 2: Input: n = 3 Output: 3 Explanation: There are three ways to climb to the top. 1. 1 step + 1 step + 1 step 2. 1 step + 2 steps 3. 2 steps + 1 step Constraints: 1 <= n <= 45 Solution","contentType":"doc","description":"You are climbing a staircase. It takes n steps to reach the top","locale":"","route":"/1d-dynamic-programming/climbing-stairs","title":"70. Climbing Stairs","version":""},{"content":"Example 1: Input: coins = [1,2,5], amount = 11 Output: 3 Explanation: 11 = 5 + 5 + 1 Example 2: Input: coins = [2], amount = 3 Output: -1 Example 3: Input: coins = [1], amount = 0 Output: 0 Constraints: 1 <= coins.length <= 12 1 <= coins[i] <= 2^31 - 1 0 <= amount <= 10^4 Solution","contentType":"doc","description":"You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money","locale":"","route":"/1d-dynamic-programming/coin-change","title":"322. Coin Change","version":""},{"content":"Example 1: Input: nums = [1,2,3,1] Output: 4 Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4. Example 2: Input: nums = [2,7,9,3,1] Output: 12 Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12. Constraints: 1 <= nums.length <= 100 0 <= nums[i] <= 400 Solution","contentType":"doc","description":"You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night","locale":"","route":"/1d-dynamic-programming/house-robber","title":"198. House Robber","version":""},{"content":"What it is Backtracking explores possible paths to a solution one step at a time. When a path reaches a dead end, you undo the last step and try a different option. Think of every problem as a decision tree. The root is an empty answer. Each level is a decision. Each branch is one option. When to use it Use backtracking when you must generate all valid solutions or evaluate all combinations. Do not use it when you need only one solution or the optimal solution. For those, use greedy or dynamic programming. Backtracking has exponential time complexity. The four components Every backtracking problem has the same parts: Base case — when to save the current path and stop. Choices — the options at each step. Constraints — the rules that reject an invalid path early. Backtrack step — undo the last choice and try the next one. The template Steps to apply the template Draw the decision tree for a small input. Name each level and each branch. Write the base case. Ask: when is the path complete? Write the choices. Ask: what can I add at this level? Write the constraints. Ask: what makes a path invalid? Reject it before recursion. Add the backtrack step. Pop what you appended. Copy the path when you save it. Never append the same list reference. Four problems, one template Subsets Choice: include or skip each number. No constraint. Base case: index equals list length. Permutations Same choices as subsets. Constraint: do not reuse a number already in the path. Base case: path length equals list length. Combinations Same as subsets. Change one line: save the path only when its length equals k. N-Queens Choice: which column to place the queen in the current row. Constraint: no shared column or diagonal. Base case: n queens placed. Backtrack: remove the last queen. Common mistakes You append the path list itself instead of a copy. The result fills with empty lists. You forget to pop. The path grows and never resets. You check constraints after recursion instead of before. You waste time on dead paths. You use backtracking when you need only one answer. Use greedy or DP instead.","contentType":"doc","description":"Backtracking is a depth-first search technique that builds a solution step by step and abandons (undoes) a partial choice as soon as it can’t lead to a valid complete solution.","locale":"","route":"/backtracking","title":"Backtracking","version":""},{"content":"Example 1: Input: candidates = [2,3,6,7], target = 7 Output: [[2,2,3],[7]] Explanation: 2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times. 7 is a candidate, and 7 = 7. These are the only two combinations. Example 2: Input: candidates = [2,3,5], target = 8 Output: [[2,2,2,2],[2,3,3],[3,5]] Example 3: Input: candidates = [2], target = 1 Output: [] Constraints: 1 <= candidates.length <= 30 2 <= candidates[i] <= 40 All elements of candidates are distinct. 1 <= target <= 40 Solution","contentType":"doc","description":"Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order","locale":"","route":"/backtracking/combination-sum","title":"39. Combination Sum","version":""},{"content":"Example 1: Input: nums = [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] Example 2: Input: nums = [0,1] Output: [[0,1],[1,0]] Example 3: Input: nums = [1] Output: [[1]] Constraints: 1 <= nums.length <= 6 -10 <= nums[i] <= 10 All the integers of nums are unique. Solution","contentType":"doc","description":"Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order","locale":"","route":"/backtracking/permutations","title":"46. Permutations","version":""},{"content":"Example 1: Input: nums = [1,2,3] Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] Example 2: Input: nums = [0] Output: [[],[0]] Constraints: 1 <= nums.length <= 10 -10 <= nums[i] <= 10 All the numbers of nums are unique. Solution","contentType":"doc","description":"Given an integer array nums of unique elements, return all possible subsets (the power set)","locale":"","route":"/backtracking/subsets","title":"78. Subsets","version":""},{"content":"Binary search is an efficient array search algorithm. It works by narrowing down the search range by half each time. If you have looked up a word in a physical dictionary, you've already used binary search in real life. Let's look at a simple example: Given a sorted array of integers and an integer called target, find the element that equals the target and return its index. If the element is not found, return -1. The key observation here is that the array is sorted. We pick a random element in the array and compare it to the target. If we happen to pick the element that equals the target (how lucky!), then bingo. We don't need to do any more work; we return its index. If the element is smaller than the target, then we know the target cannot be found in the section to the left of the current element, since everything to the left is even smaller. So, we discard the current element and everything on the left from the search range. If the element is larger than the target, then we know the target cannot be found in the section to the right of the current element, since everything to the right is even larger. So, we discard the current element and everything on the right from the search range. We repeat this process until we find the target. Instead of picking a random element, we always pick the middle element in the current search range. This way, we can discard half of the options and shrink the search range by half each time. This gives us O(log(N)) runtime. This idea can be implemented both iteratively and recursively. However, the major difference is that the iterative version of binary search uses O(1) memory while the recursive version uses O(log(N)) memory. Implementation The search range is represented by the left and right indices that start from both ends of the array and move towards each other as we search. When moving the index, we discard elements and shrink the search range. Time Complexity: O(log(n)) Calculating mid Note that when calculating mid, if the number of elements is even, there are two elements in the middle. We usually follow the convention of picking the first one, equivalent to doing integer division (left + right) / 2. In most programming languages, we calculate mid with left + floor((right-left) / 2) to avoid potential integer overflow. However, in Python, we do not need to worry about integer overflow with left + right because Python3 integers can be arbitrarily large. Deduction It's essential to understand and deduce the algorithm yourself instead of memorizing it. In an actual interview, interviewers may ask you additional questions to test your understanding, so simply memorizing the algorithm may not be enough to convince the interviewer. Key elements in writing a correct binary search: 1. When to terminate the loop Make sure the while loop includes an equality comparison; otherwise, we might skip the loop and miss the potential match for the edge case of a one-element array. 2. Whether/how to update left and right boundary in the if conditions Consider which side to discard. If arr[mid] is smaller than the target, we should discard everything on the left by making left = mid + 1. 3. Should I discard the current element? For vanilla binary search, we can discard it since it cannot be the final answer if it is not equal to the target. There might be situations where you would want to think twice before discarding the current element. We'll discuss this in the next module. When to use binary search Interestingly, binary search works beyond sorted arrays. You can use binary search whenever you make a binary decision to shrink the search range.","contentType":"doc","description":"Half of a sorted range is removed at each step until the target is found.","locale":"","route":"/binary-search","title":"Binary Search","version":""},{"content":"","contentType":"doc","description":"Each node holds a value that is larger than its left subtree and smaller than its right subtree.","locale":"","route":"/binary-search-tree","title":"Binary Search Tree","version":""},{"content":"If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize? Example 1: Input: root = [3,1,4,null,2], k = 1 Output: 1 Example 2: Input: root = [5,3,6,2,4,null,null,1], k = 3 Output: 3 Constraints: The number of nodes in the tree is n. 1 <= k <= n <= 10^4 0 <= Node.val <= 10^4 Approach Solution","contentType":"doc","description":"Given the root of a binary search tree, and an integer k, return the k^th smallest value (1-indexed) of all the values of the nodes in the tree","locale":"","route":"/binary-search-tree/kth-smallest-element-in-a-bst","title":"230. Kth Smallest Element in a BST","version":""},{"content":"Example 1: Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8 Output: 6 Explanation: The LCA of nodes 2 and 8 is 6. Example 2: Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4 Output: 2 Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition. Example 3: Input: root = [2,1], p = 2, q = 1 Output: 2 Constraints: The number of nodes in the tree is in the range [2, 10^5]. -10^9 <= Node.val <= 10^9 All Node.val are unique. p != q p and q will exist in the BST. Approach Solution","contentType":"doc","description":"Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST","locale":"","route":"/binary-search-tree/lowest-common-ancestor-of-a-binary-search-tree","title":"235. Lowest Common Ancestor of a Binary Search Tree","version":""},{"content":"Example 1: Input: root = [2,1,3] Output: true Example 2: Input: root = [5,1,4,null,null,3,6] Output: false Explanation: The root node's value is 5 but its right child's value is 4. Constraints: The number of nodes in the tree is in the range [1, 10^4]. -2^31 <= Node.val <= 2^31 - 1 Approach Solution","contentType":"doc","description":"Given the root of a binary tree, determine if it is a valid binary search tree (BST)","locale":"","route":"/binary-search-tree/validate-binary-search-tree","title":"98. Validate Binary Search Tree","version":""},{"content":"You must write an algorithm with O(log n) runtime complexity. Example 1: Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4 Explanation: 9 exists in nums and its index is 4 Example 2: Input: nums = [-1,0,3,5,9,12], target = 2 Output: -1 Explanation: 2 does not exist in nums so return -1 Constraints: 1 <= nums.length <= 10^4 -10^4 < nums[i], target < 10^4 All the integers in nums are unique. nums is sorted in ascending order. Approach Solution","contentType":"doc","description":"Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1","locale":"","route":"/binary-search/binary-search","title":"704. Binary Search","version":""},{"content":"You must write an algorithm with O(log n) runtime complexity. Example 1: Input: nums = [5,7,7,8,8,10], target = 8 Output: [3,4] Example 2: Input: nums = [5,7,7,8,8,10], target = 6 Output: [-1,-1] Example 3: Input: nums = [], target = 0 Output: [-1,-1] Constraints: 0 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9 nums is a non-decreasing array. -10^9 <= target <= 10^9 Solution","contentType":"doc","description":"Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value","locale":"","route":"/binary-search/find-first-and-last-position-of-element-in-sorted-array","title":"34. Find First and Last Position of Element in Sorted Array","version":""},{"content":"Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]. Example 1: Input: nums = [3,4,5,1,2] Output: 1 Explanation: The original array was [1,2,3,4,5] rotated 3 times. Example 2: Input: nums = [4,5,6,7,0,1,2] Output: 0 Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times. Example 3: Input: nums = [11,13,15,17] Output: 11 Explanation: The original array was [11,13,15,17] and it was rotated 4 times. Constraints: n == nums.length 1 <= n <= 5000 -5000 <= nums[i] <= 5000 All the integers of nums are unique. nums is sorted and rotated between 1 and n times. Approach Solution","contentType":"doc","description":"Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:","locale":"","route":"/binary-search/find-minimum-in-rotated-sorted-array","title":"153. Find Minimum in Rotated Sorted Array","version":""},{"content":"You must write an algorithm that runs in O(log n) time. Example 1: Input: nums = [1,2,3,1] Output: 2 Explanation: 3 is a peak element and your function should return the index number 2. Example 2: Input: nums = [1,2,1,3,5,6,4] Output: 5 Explanation: Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6. Constraints: 1 <= nums.length <= 1000 -2^31 <= nums[i] <= 2^31 - 1 nums[i] != nums[i + 1] for all valid i. Solution","contentType":"doc","description":"A peak element is an element that is strictly greater than its neighbors","locale":"","route":"/binary-search/find-peak-element","title":"162. Find Peak Element","version":""},{"content":"Example 1: Input: piles = [3,6,7,11], h = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], h = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], h = 6 Output: 23 Constraints: 1 <= piles.length <= 10^4 piles.length <= h <= 10^9 1 <= piles[i] <= 10^9 Approach Solution","contentType":"doc","description":"Koko loves to eat bananas. There are n piles of bananas, the i^th pile has piles[i] bananas. The guards have gone and will come back in h hours","locale":"","route":"/binary-search/koko-eating-bananas","title":"875. Koko Eating Bananas","version":""},{"content":"Example 1: Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2. Example 2: Input: nums1 = [1,2], nums2 = [3,4] Output: 2.50000 Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. Constraints: nums1.length == m nums2.length == n 0 <= m <= 1000 0 <= n <= 1000 1 <= m + n <= 2000 -10^6 <= nums1[i], nums2[i] <= 10^6 Solution","contentType":"doc","description":"Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays","locale":"","route":"/binary-search/median-of-two-sorted-arrays","title":"4. Median of Two Sorted Arrays","version":""},{"content":"You must write a solution in O(log(m * n)) time complexity. Example 1: Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 Output: true Example 2: Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13 Output: false Constraints: m == matrix.length n == matrix[i].length 1 <= m, n <= 100 -10^4 <= matrix[i][j], target <= 10^4 Approach Solution","contentType":"doc","description":"You are given an m x n integer matrix matrix with the following two properties:","locale":"","route":"/binary-search/search-a-2d-matrix","title":"74. Search a 2D Matrix","version":""},{"content":"You must write an algorithm with O(log n) runtime complexity. Example 1: Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4 Example 2: Input: nums = [4,5,6,7,0,1,2], target = 3 Output: -1 Example 3: Input: nums = [1], target = 0 Output: -1 Constraints: 1 <= nums.length <= 5000 -10^4 <= nums[i] <= 10^4 All values of nums are unique. nums is an ascending array that is possibly rotated. -10^4 <= target <= 10^4 Approach Solution","contentType":"doc","description":"There is an integer array nums sorted in ascending order (with distinct values)","locale":"","route":"/binary-search/search-in-rotated-sorted-array","title":"33. Search in Rotated Sorted Array","version":""},{"content":"You must write an algorithm with O(log n) runtime complexity. Example 1: Input: nums = [1,3,5,6], target = 5 Output: 2 Example 2: Input: nums = [1,3,5,6], target = 2 Output: 1 Example 3: Input: nums = [1,3,5,6], target = 7 Output: 4 Constraints: 1 <= nums.length <= 10^4 -10^4 <= nums[i] <= 10^4 nums contains distinct values sorted in ascending order. -10^4 <= target <= 10^4 Approach Solution","contentType":"doc","description":"Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order","locale":"","route":"/binary-search/search-insert-position","title":"35. Search Insert Position","version":""},{"content":"","contentType":"doc","description":"Nodes are visited in order of distance from the start, which gives the shortest path in an unweighted graph.","locale":"","route":"/graph-bfs","title":"Graph BFS","version":""},{"content":"Example 1: Input: grid = [[2,1,1],[1,1,0],[0,1,1]] Output: 4 Example 2: Input: grid = [[2,1,1],[0,1,1],[1,0,1]] Output: -1 Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally. Example 3: Input: grid = [[0,2]] Output: 0 Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0. Constraints: m == grid.length n == grid[i].length 1 <= m, n <= 10 grid[i][j] is 0, 1, or 2. Solution","contentType":"doc","description":"You are given an m x n grid where each cell can have one of three values:","locale":"","route":"/graph-bfs/rotting-oranges","title":"994. Rotting Oranges","version":""},{"content":"Example 1: Input: beginWord = \"hit\", endWord = \"cog\", wordList = [\"hot\",\"dot\",\"dog\",\"lot\",\"log\",\"cog\"] Output: 5 Explanation: One shortest transformation sequence is \"hit\" -> \"hot\" -> \"dot\" -> \"dog\" -> cog\", which is 5 words long. Example 2: Input: beginWord = \"hit\", endWord = \"cog\", wordList = [\"hot\",\"dot\",\"dog\",\"lot\",\"log\"] Output: 0 Explanation: The endWord \"cog\" is not in wordList, therefore there is no valid transformation sequence. Constraints: 1 <= beginWord.length <= 10 endWord.length == beginWord.length 1 <= wordList.length <= 5000 wordList[i].length == beginWord.length beginWord, endWord, and wordList[i] consist of lowercase English letters. beginWord != endWord All the words in wordList are unique. Solution","contentType":"doc","description":"A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:","locale":"","route":"/graph-bfs/word-ladder","title":"127. Word Ladder","version":""},{"content":"","contentType":"doc","description":"Each edge is followed to its end, and a visited set prevents a repeat visit.","locale":"","route":"/graph-dfs","title":"Graph DFS","version":""},{"content":"Example 1: Input: adjList = [[2,4],[1,3],[2,4],[1,3]] Output: [[2,4],[1,3],[2,4],[1,3]] Explanation: There are 4 nodes in the graph. 1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4). 2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3). 3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4). 4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3). Example 2: Input: adjList = [[]] Output: [[]] Explanation: Note that the input contains one empty list. The graph consists of only one node with val = 1 and it does not have any neighbors. Example 3: Input: adjList = [] Output: [] Explanation: This an empty graph, it does not have any nodes. Constraints: The number of nodes in the graph is in the range [0, 100]. 1 <= Node.val <= 100 Node.val is unique for each node. There are no repeated edges and no self-loops in the graph. The Graph is connected and all nodes can be visited starting from the given node. Solution","contentType":"doc","description":"Given a reference of a node in a connected undirected graph","locale":"","route":"/graph-dfs/clone-graph","title":"133. Clone Graph","version":""},{"content":"Example 1: Input: grid = [ [\"1\",\"1\",\"1\",\"1\",\"0\"], [\"1\",\"1\",\"0\",\"1\",\"0\"], [\"1\",\"1\",\"0\",\"0\",\"0\"], [\"0\",\"0\",\"0\",\"0\",\"0\"] ] Output: 1 Example 2: Input: grid = [ [\"1\",\"1\",\"0\",\"0\",\"0\"], [\"1\",\"1\",\"0\",\"0\",\"0\"], [\"0\",\"0\",\"1\",\"0\",\"0\"], [\"0\",\"0\",\"0\",\"1\",\"1\"] ] Output: 3 Constraints: m == grid.length n == grid[i].length 1 <= m, n <= 300 grid[i][j] is '0' or '1'. Solution","contentType":"doc","description":"Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands","locale":"","route":"/graph-dfs/number-of-islands","title":"200. Number of Islands","version":""},{"content":"Example 1: Input: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]] Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]] Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below: [0,4]: [0,4] -> Pacific Ocean [0,4] -> Atlantic Ocean [1,3]: [1,3] -> [0,3] -> Pacific Ocean [1,3] -> [1,4] -> Atlantic Ocean [1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean [1,4] -> Atlantic Ocean [2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean [2,2] -> [2,3] -> [2,4] -> Atlantic Ocean [3,0]: [3,0] -> Pacific Ocean [3,0] -> [4,0] -> Atlantic Ocean [3,1]: [3,1] -> [3,0] -> Pacific Ocean [3,1] -> [4,1] -> Atlantic Ocean [4,0]: [4,0] -> Pacific Ocean [4,0] -> Atlantic Ocean Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans. Example 2: Input: heights = [[1]] Output: [[0,0]] Explanation: The water can flow from the only cell to the Pacific and Atlantic oceans. Constraints: m == heights.length n == heights[r].length 1 <= m, n <= 200 0 <= heights[r][c] <= 10^5 Solution","contentType":"doc","description":"There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges","locale":"","route":"/graph-dfs/pacific-atlantic-water-flow","title":"417. Pacific Atlantic Water Flow","version":""},{"content":"When to use greedy algorithms? Solving optimization problems. Ask to optimize some value and calculate it. keywords (min/max, longest/shortest, largest/smallest, etc.) Optimization problems Return the minimum number of swaps required to make s1 equal to s2. Return the minimum number of steps to make s and t anagrams of each other. Given three integers a, b, and c, return the longest possible happy string, if there are multiple such strings return any of them. if there is no such string, return empty string. What are greedy algorithms? Greedy algorithms follow this format: identify what choices you have at every step find the best choice just do what looks best until done assume that the best choice is the one that will lead to the best outcome How do we know greedy works? Write out a formal proof too complicated to write out too much time better to use intuition try a bunch of ideas find counterexamples Steps to use greedy algorithms Identify the different steps and choices Choices have to be comparable must determine if a choice is better than another Check if greedy works try finding small cases where greedy is not optimal if cases pass, try to deduce why greedy is optimal Why use greedy algorithms? simple to code doesn't use complex algorithms (BFS, DFS, etc.) runtime is fast, usually 0(n) or O(nlogn) however, greedy algorithms are not always optimal The Greedy Skeleton Type 1: Walk and Carry No sort. Walk the list once. Carry one number. At each step, decide: keep it, or drop it. Type 2: Sort and Pick Used when order doesn't matter, so you pick the order. Meeting rooms, intervals, \"most tasks by a deadline.\"","contentType":"doc","description":"The best local choice is made at each step and is not changed later.","locale":"","route":"/greedy","title":"Greedy","version":""},{"content":"Example 1: Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2] Output: 3 Explanation: Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4 Travel to station 4. Your tank = 4 - 1 + 5 = 8 Travel to station 0. Your tank = 8 - 2 + 1 = 7 Travel to station 1. Your tank = 7 - 3 + 2 = 6 Travel to station 2. Your tank = 6 - 4 + 3 = 5 Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3. Therefore, return 3 as the starting index. Example 2: Input: gas = [2,3,4], cost = [3,4,3] Output: -1 Explanation: You can't start at station 0 or 1, as there is not enough gas to travel to the next station. Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4 Travel to station 0. Your tank = 4 - 3 + 2 = 3 Travel to station 1. Your tank = 3 - 3 + 3 = 3 You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3. Therefore, you can't travel around the circuit once no matter where you start. Constraints: n == gas.length == cost.length 1 <= n <= 10^5 0 <= gas[i], cost[i] <= 10^4 The input is generated such that the answer is unique. Solution","contentType":"doc","description":"There are n gas stations along a circular route, where the amount of gas at the i^th station is gas[i]","locale":"","route":"/greedy/gas-station","title":"134. Gas Station","version":""},{"content":"Example 1: Input: nums = [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: nums = [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index. Constraints: 1 <= nums.length <= 10^4 0 <= nums[i] <= 10^5 Solution","contentType":"doc","description":"You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position","locale":"","route":"/greedy/jump-game","title":"55. Jump Game","version":""},{"content":"If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle. Example 1: Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: The subarray [4,-1,2,1] has the largest sum 6. Example 2: Input: nums = [1] Output: 1 Explanation: The subarray [1] has the largest sum 1. Example 3: Input: nums = [5,4,-1,7,8] Output: 23 Explanation: The subarray [5,4,-1,7,8] has the largest sum 23. Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 Solution","contentType":"doc","description":"Given an integer array nums, find the subarray with the largest sum, and return its sum","locale":"","route":"/greedy/maximum-subarray","title":"53. Maximum Subarray","version":""},{"content":"Sometimes when programming, we need a data structure that maps an arbitrary data type to another arbitrary data type, like an array with non-integer indices. For example, a phone book that allows lookup by a person's name requires such data type. A hash table (or a hash map) is one way of representing this data structure. The idea of hash table is this: Data is stored in a fix-sized array, where each data value has an unique index. When we add a new (key, value) pair to the array, we use a hash function to generate a hash value within the range of the array with the key. We then store the data value at the index indicated by the generated hash value. Later, when we retrieve the data with the key from the hash table, we hash the key again and look up the data in the array with the corresponding index. Since the hash function always returns the same hash values with the same input, we will always find the correct item in that index. Hash Table Operations Efficiency Assuming the hash function uses constant time, the average time complexity is O(n / k) for each insertion/access operation on the table, where n is the number of entries in the hash table and k is the array size of the table. This assumes the hash function distributes keys uniformly across the k buckets, so each bucket holds about n / k entries on average. Frequency Counting Python provides a built-in collections.Counter class that does this for us.","contentType":"doc","description":"A key maps to a value in constant time, which removes the need for a full scan.","locale":"","route":"/hash-table","title":"Hash Table","version":""},{"content":"Example 1: Input: sentence = \"thequickbrownfoxjumpsoverthelazydog\" Output: true Explanation: sentence contains at least one of every letter of the English alphabet. Example 2: Input: sentence = \"leetcode\" Output: false Constraints: 1 <= sentence.length <= 1000 sentence consists of lowercase English letters. Approach Solution","contentType":"doc","description":"A pangram is a sentence where every letter of the English alphabet appears at least once","locale":"","route":"/hash-table/check-if-the-sentence-is-pangram","title":"1832. Check if the Sentence Is Pangram","version":""},{"content":"Example 1: Input: nums = [1,2,3,1] Output: true Explanation: The element 1 occurs at the indices 0 and 3. Example 2: Input: nums = [1,2,3,4] Output: false Explanation: All elements are distinct. Example 3: Input: nums = [1,1,1,3,3,4,3,2,4,2] Output: true Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9 Approach Solution","contentType":"doc","description":"Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct","locale":"","route":"/hash-table/contains-duplicate","title":"217. Contains Duplicate","version":""},{"content":"Example 1: Input: s = \"leetcode\" Output: 0 Explanation: The character 'l' at index 0 is the first character that does not occur at any other index. Example 2: Input: s = \"loveleetcode\" Output: 2 Example 3: Input: s = \"aabb\" Output: -1 Constraints: 1 <= s.length <= 10^5 s consists of only lowercase English letters. Approach Solution","contentType":"doc","description":"Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1","locale":"","route":"/hash-table/first-unique-character-in-a-string","title":"387. First Unique Character in a String","version":""},{"content":"Example 1: Input: strs = [\"eat\",\"tea\",\"tan\",\"ate\",\"nat\",\"bat\"] Output: [[\"bat\"],[\"nat\",\"tan\"],[\"ate\",\"eat\",\"tea\"]] Explanation: There is no string in strs that can be rearranged to form \"bat\". The strings \"nat\" and \"tan\" are anagrams as they can be rearranged to form each other. The strings \"ate\", \"eat\", and \"tea\" are anagrams as they can be rearranged to form each other. Example 2: Input: strs = [\"\"] Output: [[\"\"]] Example 3: Input: strs = [\"a\"] Output: [[\"a\"]] Constraints: 1 <= strs.length <= 10^4 0 <= strs[i].length <= 100 strs[i] consists of lowercase English letters. Approach Solution","contentType":"doc","description":"Given an array of strings strs, group the anagrams together. You can return the answer in any order","locale":"","route":"/hash-table/group-anagrams","title":"49. Group Anagrams","version":""},{"content":"Example 1: Input: s = \"egg\", t = \"add\" Output: true Explanation: The strings s and t can be made identical by: • Mapping 'e' to 'a'. • Mapping 'g' to 'd'. Example 2: Input: s = \"f11\", t = \"b23\" Output: false Explanation: The strings s and t can not be made identical as '1' needs to be mapped to both '2' and '3'. Example 3: Input: s = \"paper\", t = \"title\" Output: true Constraints: 1 <= s.length <= 5 * 10^4 t.length == s.length s and t consist of any valid ascii character. Solution","contentType":"doc","description":"Given two strings s and t, determine if they are isomorphic","locale":"","route":"/hash-table/isomorphic-strings","title":"205. Isomorphic Strings","version":""},{"content":"You must write an algorithm that runs in O(n) time. Example 1: Input: nums = [100,4,200,1,3,2] Output: 4 Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4. Example 2: Input: nums = [0,3,7,2,5,8,4,6,0,1] Output: 9 Example 3: Input: nums = [1,0,1,2] Output: 3 Constraints: 0 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9 Solution","contentType":"doc","description":"Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence","locale":"","route":"/hash-table/longest-consecutive-sequence","title":"128. Longest Consecutive Sequence","version":""},{"content":"Example 1: Input: s = \"tree\" Output: \"eert\" Explanation: 'e' appears twice while 'r' and 't' both appear once. So 'e' must appear before both 'r' and 't'. Therefore \"eetr\" is also a valid answer. Example 2: Input: s = \"cccaaa\" Output: \"aaaccc\" Explanation: Both 'c' and 'a' appear three times, so both \"cccaaa\" and \"aaaccc\" are valid answers. Note that \"cacaca\" is incorrect, as the same characters must be together. Example 3: Input: s = \"Aabb\" Output: \"bbAa\" Explanation: \"bbaA\" is also a valid answer, but \"Aabb\" is incorrect. Note that 'A' and 'a' are treated as two different characters. Constraints: 1 <= s.length <= 5 * 10^5 s consists of uppercase and lowercase English letters and digits. Approach Solution","contentType":"doc","description":"Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string","locale":"","route":"/hash-table/sort-characters-by-frequency","title":"451. Sort Characters By Frequency","version":""},{"content":"Your algorithm's time complexity must be better than O(n log n), where n is the array's size. Example 1: Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2] Example 2: Input: nums = [1], k = 1 Output: [1] Example 3: Input: nums = [1,2,1,2,1,2,3,1,3,2], k = 2 Output: [1,2] Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 k is in the range [1, the number of unique elements in the array]. It is guaranteed that the answer is unique. Approach Solution","contentType":"doc","description":"Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order","locale":"","route":"/hash-table/top-k-frequent-elements","title":"347. Top K Frequent Elements","version":""},{"content":"Can you come up with an algorithm that is less than O(n^2) time complexity? Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. Example 2: Input: nums = [3,2,4], target = 6 Output: [1,2] Example 3: Input: nums = [3,3], target = 6 Output: [0,1] Constraints: 2 <= nums.length <= 10^4 -10^9 <= nums[i] <= 10^9 -10^9 <= target <= 10^9 Only one valid answer exists. Approach Solution","contentType":"doc","description":"You are given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target","locale":"","route":"/hash-table/two-sum","title":"1. Two Sum","version":""},{"content":"What if the inputs contain Unicode characters? How would you adapt your solution to such a case? Example 1: Input: s = \"anagram\", t = \"nagaram\" Output: true Example 2: Input: s = \"rat\", t = \"car\" Output: false Constraints: 1 <= s.length, t.length <= 5 * 10^4 s and t consist of lowercase English letters. Approach Solution","contentType":"doc","description":"Given two strings s and t, return true if t is an anagram of s, and false otherwise","locale":"","route":"/hash-table/valid-anagram","title":"242. Valid Anagram","version":""},{"content":"Example 1: Input: pattern = \"abba\", s = \"dog cat cat dog\" Output: true Explanation: The bijection can be established as: • 'a' maps to \"dog\". • 'b' maps to \"cat\". Example 2: Input: pattern = \"abba\", s = \"dog cat cat fish\" Output: false Example 3: Input: pattern = \"aaaa\", s = \"dog cat cat dog\" Output: false Constraints: 1 <= pattern.length <= 300 pattern contains only lower-case English letters. 1 <= s.length <= 3000 s contains only lowercase English letters and spaces ' '. s does not contain any leading or trailing spaces. All the words in s are separated by a single space. Solution","contentType":"doc","description":"Given a pattern and a string s, find if s follows the same pattern","locale":"","route":"/hash-table/word-pattern","title":"290. Word Pattern","version":""},{"content":"","contentType":"doc","description":"The largest or smallest item is always at the top, and it is removed in logarithmic time.","locale":"","route":"/heap-priority-queue","title":"Heap / Priority Queue","version":""},{"content":"Example 1: Input: points = [[1,3],[-2,2]], k = 1 Output: [[-2,2]] Explanation: The distance between (1, 3) and the origin is sqrt(10). The distance between (-2, 2) and the origin is sqrt(8). Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin. We only want the closest k = 1 points from the origin, so the answer is just [[-2,2]]. Example 2: Input: points = [[3,3],[5,-1],[-2,4]], k = 2 Output: [[3,3],[-2,4]] Explanation: The answer [[-2,4],[3,3]] would also be accepted. Constraints: 1 <= k <= points.length <= 10^4 -10^4 <= xi, yi <= 10^4 Approach Solution","contentType":"doc","description":"Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0)","locale":"","route":"/heap-priority-queue/k-closest-points-to-origin","title":"973. K Closest Points to Origin","version":""},{"content":"Example 1: Input: nums = [3,2,1,5,6,4], k = 2 Output: 5 Example 2: Input: nums = [3,2,3,1,2,4,5,5,6], k = 4 Output: 4 Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 Approach Solution","contentType":"doc","description":"Given an integer array nums and an integer k, return the k^th largest element in the array","locale":"","route":"/heap-priority-queue/kth-largest-element-in-an-array","title":"215. Kth Largest Element in an Array","version":""},{"content":"Example 1: Input: lists = [[1,4,5],[1,3,4],[2,6]] Output: [1,1,2,3,4,4,5,6] Explanation: The linked-lists are: [ 1->4->5, 1->3->4, 2->6 ] merging them into one sorted linked list: 1->1->2->3->4->4->5->6 Example 2: Input: lists = [] Output: [] Example 3: Input: lists = [[]] Output: [] Constraints: k == lists.length 0 <= k <= 10^4 0 <= lists[i].length <= 500 -10^4 <= lists[i][j] <= 10^4 lists[i] is sorted in ascending order. The sum of lists[i].length will not exceed 10^4. Approach Solution","contentType":"doc","description":"You are given an array of k linked-lists lists, each linked-list is sorted in ascending order","locale":"","route":"/heap-priority-queue/merge-k-sorted-lists","title":"23. Merge k Sorted Lists","version":""},{"content":"","contentType":"doc","description":"Two structures are combined, such as a hash map and a linked list, to get the best properties of each.","locale":"","route":"/hybrid-structures","title":"Hybrid Structures","version":""},{"content":"Example 1: Input: l1 = [2,4,3], l2 = [5,6,4] Output: [7,0,8] Explanation: 342 + 465 = 807. Example 2: Input: l1 = [0], l2 = [0] Output: [0] Example 3: Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] Output: [8,9,9,9,0,0,0,1] Constraints: The number of nodes in each linked list is in the range [1, 100]. 0 <= Node.val <= 9 It is guaranteed that the list represents a number that does not have leading zeros. Approach Solution","contentType":"doc","description":"You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list","locale":"","route":"/hybrid-structures/add-two-numbers","title":"2. Add Two Numbers","version":""},{"content":"You must implement the functions of the class such that each function works in average O(1) time complexity. Example 1: Input: `` Output: `` Explanation: Input [\"RandomizedSet\", \"insert\", \"remove\", \"insert\", \"getRandom\", \"remove\", \"insert\", \"getRandom\"] [[], [1], [2], [2], [], [1], [2], []] Output [null, true, false, true, 2, true, false, 2] Explanation RandomizedSet randomizedSet = new RandomizedSet(); randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully. randomizedSet.remove(2); // Returns false as 2 does not exist in the set. randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2]. randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly. randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2]. randomizedSet.insert(2); // 2 was already in the set, so return false. randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return 2. Constraints: -2^31 <= val <= 2^31 - 1 At most 2 * 10^5 calls will be made to insert, remove, and getRandom. There will be at least one element in the data structure when getRandom is called. Solution","contentType":"doc","description":"Implement the RandomizedSet class:","locale":"","route":"/hybrid-structures/insert-delete-getrandom-o1","title":"380. Insert Delete GetRandom O(1)","version":""},{"content":"Example 1: Input: `` Output: `` Explanation: Input [\"LRUCache\", \"put\", \"put\", \"get\", \"put\", \"get\", \"put\", \"get\", \"get\", \"get\"] [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]] Output [null, null, null, 1, null, -1, null, -1, 3, 4] Explanation LRUCache lRUCache = new LRUCache(2); lRUCache.put(1, 1); // cache is {1=1} lRUCache.put(2, 2); // cache is {1=1, 2=2} lRUCache.get(1); // return 1 lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3} lRUCache.get(2); // returns -1 (not found) lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3} lRUCache.get(1); // return -1 (not found) lRUCache.get(3); // return 3 lRUCache.get(4); // return 4 Constraints: 1 <= capacity <= 3000 0 <= key <= 10^4 0 <= value <= 10^5 At most 2 * 10^5 calls will be made to get and put. Approach Solution","contentType":"doc","description":"Design a data structure that follows the constraints of a Least Recently Used (LRU) cache","locale":"","route":"/hybrid-structures/lru-cache","title":"146. LRU Cache","version":""},{"content":"An interval is a pair [start, end] marking a stretch on a line: the minutes a meeting occupies the range a task runs the span a sensor was active What interval problems ask you? Do any two collide? What does the timeline look like once you merge everything that touches? How many rooms do you need so no two meetings share one? Every one of those questions reduces to a single primitive: deciding whether two intervals overlap, and if they do, how to merge them or what part they share. Once that primitive is clear, the rest of the pattern is bookkeeping. When two intervals overlap It is easier to state when two intervals do not overlap. Interval a sits entirely before interval b when a finishes before b begins, that is a.end < b.start. By symmetry they also miss each other when b ends before a starts. If neither of those holds, the intervals overlap. Negating a.end < b.start or b.end < a.start gives the positive test directly: two intervals overlap exactly when a.start <= b.end and b.start <= a.end. That pair of comparisons is the whole overlap check. The Merged Region When two intervals overlap, you can merge them into one interval that covers both. Its start is the earlier of the two starts, and its end is the later of the two ends: [min(a.start, b.start), max(a.end, b.end)]. The merged interval begins as soon as either interval starts, and it ends only once both have stopped. The Shared Region When two intervals overlap, the part they share is itself an interval. Its start is the later of the two starts, and its end is the earlier of the two ends: [max(a.start, b.start), min(a.end, b.end)]. The overlap begins only once both intervals have started, and it ends the moment either one stops. Merge grows, overlap shrinks. Merge uses [min, max] and covers both intervals. Overlap uses [max, min] and keeps only the part both share. Sort by Start first Left unsorted, checking a list of intervals means comparing every pair, which is O(n^2). Sorting the intervals by start time removes that cost. Once the list is in start order, any interval can only overlap ones that come after it and begin before it ends, so a single left-to-right pass is enough to merge, count, or detect collisions. Sorting by start is the standard first line of almost every interval solution, and the problems below all lean on it. That gives the template every interval problem in this section builds on: sort by start, then sweep once, comparing each interval against the last one you kept. Input [[1,3], [8,10], [2,6], [4,5], [10,12]] After sort [[1,3], [2,6], [4,5], [8,10], [10,12]] Show trace table i [start, end] result[-1] start <= result[-1][1]? Action result after 0 [1,3] — — (empty) append [[1,3]] 1 [2,6] [1,3] 2 <= 3 ✅ end = max(3,6) = 6 [[1,6]] 2 [4,5] [1,6] 4 <= 6 ✅ end = max(6,5) = 6 [[1,6]] 3 [8,10] [1,6] 8 <= 6 ❌ append [[1,6], [8,10]] 4 [10,12] [8,10] 10 <= 10 ✅ end = max(10,12) = 12 [[1,6], [8,12]] :::tip[Output] [[1,6], [8,12]] Row 2: [4,5] sits inside [1,6]. Setting end = 5 without max would cut the interval short. max keeps 6. Row 4: [8,10] and [10,12] only touch at 10. The <= check merges them. With <, you would get two intervals. ::: Insert into a sorted list Sometimes the list is already sorted and has no overlaps, and you must add one new interval. You could append it, sort, and run the template. That works, but it pays O(n log n) for a sort you do not need. A sorted list with no overlaps splits into three parts around the new interval, in this order: Intervals that end before the new one starts. Keep them as they are. Intervals that overlap the new one. Merge each into it with [min, max]. Intervals that start after the new one ends. Keep them as they are. Walk the list once and handle each part in turn. No sort, so the cost drops to O(n). Intervals [[1,2], [3,5], [6,7], [8,10], [12,16]] New interval [4,8] Show trace table i interval Part Check Action [start, end] result after 0 [1,2] 1 2 < 4 ✅ keep [4,8] [[1,2]] 1 [3,5] 1 5 < 4 ❌ go to part 2 [4,8] [[1,2]] 1 [3,5] 2 3 <= 8 ✅ start = min(4,3) = 3 [3,8] [[1,2]] 2 [6,7] 2 6 <= 8 ✅ end = max(8,7) = 8 [3,8] [[1,2]] 3 [8,10] 2 8 <= 8 ✅ end = max(8,10) = 10 [3,10] [[1,2]] 4 [12,16] 2 12 <= 10 ❌ append new [3,10] [[1,2], [3,10]] 4 [12,16] 3 — extend rest — [[1,2], [3,10], [12,16]] :::tip[Output] [[1,2], [3,10], [12,16]] Row i=1: The same interval shows up twice. The part 1 check fails, so that loop stops, and part 2 reads the same interval again. i does not move. Row i=3: [8,10] only touches [3,8] at 8. The <= merges them, since one shared point counts as an overlap. Part 1 uses <, part 2 uses <=: An interval that ends exactly at start touches the new interval. It must fail part 1 so part 2 can merge it. ::: If the input is already sorted, do not sort again. Interviewers add \"sorted\" to the problem on purpose. Name the O(n log n) cost of re-sorting before they do, then offer the O(n) three-part walk. Footnote The positive overlap test comes straight from De Morgan's law. Negating (a.end < b.start) or (b.end < a.start) gives (a.end >= b.start) and (b.end >= a.start), which is the a.start <= b.end and b.start <= a.end condition rewritten.","contentType":"doc","description":"Ranges with a start and an end are sorted, and then merged, counted, or scheduled.","locale":"","route":"/intervals","title":"Intervals","version":""},{"content":"Example 1: Input: intervals = [[1,3],[6,9]], newInterval = [2,5] Output: [[1,5],[6,9]] Example 2: Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8] Output: [[1,2],[3,10],[12,16]] Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10]. Constraints: 0 <= intervals.length <= 10^4 intervals[i].length == 2 0 <= starti <= endi <= 10^5 intervals is sorted by starti in ascending order. newInterval.length == 2 0 <= start <= end <= 10^5 Solution","contentType":"doc","description":"You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the i^th interval and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval","locale":"","route":"/intervals/insert-interval","title":"57. Insert Interval","version":""},{"content":"Example 1: Input: intervals = [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6]. Example 2: Input: intervals = [[1,4],[4,5]] Output: [[1,5]] Explanation: Intervals [1,4] and [4,5] are considered overlapping. Example 3: Input: intervals = [[4,7],[1,4]] Output: [[1,7]] Explanation: Intervals [1,4] and [4,7] are considered overlapping. Constraints: 1 <= intervals.length <= 10^4 intervals[i].length == 2 0 <= starti <= endi <= 10^4 Solution","contentType":"doc","description":"Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input","locale":"","route":"/intervals/merge-intervals","title":"56. Merge Intervals","version":""},{"content":"Example 1: Input: intervals = [[1,2],[2,3],[3,4],[1,3]] Output: 1 Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping. Example 2: Input: intervals = [[1,2],[1,2],[1,2]] Output: 2 Explanation: You need to remove two [1,2] to make the rest of the intervals non-overlapping. Example 3: Input: intervals = [[1,2],[2,3]] Output: 0 Explanation: You don't need to remove any of the intervals since they're already non-overlapping. Constraints: 1 <= intervals.length <= 10^5 intervals[i].length == 2 -5 * 10^4 <= starti < endi <= 5 * 10^4 Solution","contentType":"doc","description":"Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping","locale":"","route":"/intervals/non-overlapping-intervals","title":"435. Non-overlapping Intervals","version":""},{"content":"Each entry below names the keyword, the algorithm it points to, and a one-line reason. Where a keyword has more than one possible technique, each option is listed with the sub-signal that picks it. Techniques you haven't studied yet are covered later in the course. \"Top K\" Heap — track the K-item boundary in O(n log K) instead of sorting all n. K Closest Points \"Sorted\" / \"rotated sorted\" / \"in O(log n)\" Binary Search — halve the search space at each step using order. Works on any monotonic predicate, not just literal arrays. Binary Search \"How many ways...\" Counting distinct outcomes — choose by what the input looks like. DFS, when choices form a tree of decisions. Decode Ways DP, when subproblems overlap on a grid or sequence. Robot Paths \"Substring\" Sliding Window — contiguous slices extend and shrink in O(1) at each step. Longest Substring Without Repeating Characters \"Prefix\" / \"autocomplete\" / \"dictionary lookup\" / \"starts with\" Trie — share storage for common prefixes; lookup runs in O(length of word) regardless of dictionary size. Implement Trie \"Palindrome\" Palindromes mirror around a center — choose by whether you're verifying or constructing. Two Pointers, to verify a single string. Valid Palindrome DFS, to enumerate every valid partition. Palindrome Partitioning DP, to minimize cuts or count partitions. Palindrome Partitioning II \"Tree\" One question decides everything: do you care about depth, or just need to visit every node? BFS, for level-order, shallowest depth, or \"right-side view\". Binary Tree Level-Order Traversal DFS, for sums, heights, paths — anything not level-related. Max Depth \"Parentheses\" Stack — brackets close in reverse order, last opened first closed (LIFO). Valid Parentheses \"Next greater\" / \"next smaller\" / \"warmer day\" Monotonic Stack — keep candidates in stack order so the answer for each element pops out in O(1) amortized. Next Greater Element II \"Subarray\" Contiguous slices, but the right tool depends on what you're computing. Sliding Window, for fixed size or a monotonic constraint. Subarray Sum Fixed Prefix Sum, for range-sum queries. Subarray Sum Hashmap of prefix sums, for sum equals a target. Continuous Subarray Sum \"Max subarray\" Greedy (Kadane's) — at each index, extend the run or restart; the choice is local. Kadane's Algorithm \"Two sum / K sum\" Two Pointers on a sorted array — each move strictly raises or lowers the sum. Two Sum \"Max/longest sequence\" \"Longest\" usually means each position depends on earlier ones — except when a fixed window is involved. DP / DFS with memoization, when a position depends on earlier positions. Longest Increasing Subsequence Monotonic Deque, for max within a sliding window. Sliding Window Maximum \"Minimum/Shortest\" Three very different problems wear the same word. BFS, for fewest edges in an unweighted graph. Shortest Path Dijkstra, for shortest path with non-negative edge weights. Dijkstra's Algorithm DP / DFS, for cumulative cost on a grid or sequence. Minimal Path Sum \"Partition/split\" DFS — each split point is a recursive choice; memoize if suffixes repeat. Decode Ways \"Subsequence\" DP / DFS with memoization — pick-or-skip at each index, with overlapping subproblems. Longest Increasing Subsequence \"All combinations / permutations / subsets\" / \"generate every\" Backtracking — DFS that records a partial solution, recurses, then undoes the choice on the way back up. Permutations, Subsets \"Matrix\" A grid is a graph. Connectivity questions are BFS/DFS; optimization questions are DP. BFS / DFS, for connectivity, flood-fill, or region counting. Flood Fill, Number of Islands DP, for optimization where each cell builds on neighbors. Maximal Square \"Jump\" Greedy when local-optimal is provably global; DP otherwise. Greedy, when \"always reach as far as possible\" works (typically reachability). Jump Game DP, when counting jumps or greedy fails. Jump Game II \"Game\" DP on game states — \"can the player to move force a win?\" recurses with overlap. Divisor Game \"Connected component\" Union Find — near-O(1) union and find beat re-running BFS after each edge. Use BFS or DFS instead when the graph is built once and you only traverse a few times. Number of Connected Components \"Schedule\" / \"prerequisites\" / \"build order\" / \"course\" Topological Sort (Kahn's BFS or DFS post-order) — directed dependencies must be linearized so every edge points forward. Detects cycles as a side effect. Course Schedule \"Transitive relationship\" If A relates to B and B relates to C means A relates to C, the relation is a graph in disguise. Items become nodes, relations become edges, and \"are these two related?\" becomes \"are they in the same component?\" BFS, to answer a single query by traversing on the fly. Word Ladder BFS or Union Find, for many queries on a fixed relation set. Sentence Similarity BFS or Union Find, for numbers with divisional relationships. Evaluate Division \"Interval\" Greedy after sorting — by start (merge) or by end (fit-in), then one pass. Interval Pattern","contentType":"doc","description":"Coding problems rarely tell you which algorithm to use, but the wording usually leaks it. \"K-th largest\" hints at a heap. \"Shortest path\" in an unweighted graph hints at BFS.","locale":"","route":"/keywords","title":"Keywords","version":""},{"content":"","contentType":"doc","description":"Each node points to the next node, and the pointers give the path through the data.","locale":"","route":"/linked-list","title":"Linked List","version":""},{"content":"Can you solve it using O(1) (i.e. constant) memory? Example 1: Input: head = [3,2,0,-4], pos = 1 Output: true Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed). Example 2: Input: head = [1,2], pos = 0 Output: true Explanation: There is a cycle in the linked list, where the tail connects to the 0th node. Example 3: Input: head = [1], pos = -1 Output: false Explanation: There is no cycle in the linked list. Constraints: The number of the nodes in the list is in the range [0, 10^4]. -10^5 <= Node.val <= 10^5 pos is -1 or a valid index in the linked-list. Approach Solution","contentType":"doc","description":"Given head, the head of a linked list, determine if the linked list has a cycle in it","locale":"","route":"/linked-list/linked-list-cycle","title":"141. Linked List Cycle","version":""},{"content":"Example 1: Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4] Example 2: Input: list1 = [], list2 = [] Output: [] Example 3: Input: list1 = [], list2 = [0] Output: [0] Constraints: The number of nodes in both lists is in the range [0, 50]. -100 <= Node.val <= 100 Both list1 and list2 are sorted in non-decreasing order. Approach Solution","contentType":"doc","description":"You are given the heads of two sorted linked lists list1 and list2","locale":"","route":"/linked-list/merge-two-sorted-lists","title":"21. Merge Two Sorted Lists","version":""},{"content":"Example 1: Input: head = [1,2,3,4,5] Output: [3,4,5] Explanation: The middle node of the list is node 3. Example 2: Input: head = [1,2,3,4,5,6] Output: [4,5,6] Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one. Constraints: The number of nodes in the list is in the range [1, 100]. 1 <= Node.val <= 100 Solution","contentType":"doc","description":"Given the head of a singly linked list, return the middle node of the linked list","locale":"","route":"/linked-list/middle-of-the-linked-list","title":"876. Middle of the Linked List","version":""},{"content":"Example 1: Input: head = [1,2,3,3,4,4,5] Output: [1,2,5] Example 2: Input: head = [1,1,1,2,3] Output: [2,3] Constraints: The number of nodes in the list is in the range [0, 300]. -100 <= Node.val <= 100 The list is guaranteed to be sorted in ascending order. Solution","contentType":"doc","description":"You are given the head of a sorted linked list","locale":"","route":"/linked-list/remove-duplicates-from-sorted-list-ii","title":"82. Remove Duplicates from Sorted List II","version":""},{"content":"Could you do this in one pass? Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5] Example 2: Input: head = [1], n = 1 Output: [] Example 3: Input: head = [1,2], n = 1 Output: [1] Constraints: The number of nodes in the list is sz. 1 <= sz <= 30 0 <= Node.val <= 100 1 <= n <= sz Solution","contentType":"doc","description":"Given the head of a linked list, remove the n^th node from the end of the list and return its head","locale":"","route":"/linked-list/remove-nth-node-from-end-of-list","title":"19. Remove Nth Node From End of List","version":""},{"content":"Example 1: Input: head = [1,2,3,4] Output: [1,4,2,3] Example 2: Input: head = [1,2,3,4,5] Output: [1,5,2,4,3] Constraints: The number of nodes in the list is in the range [1, 5 * 10^4]. 1 <= Node.val <= 1000 Solution","contentType":"doc","description":"You are given the head of a singly linked-list. The list can be represented as:","locale":"","route":"/linked-list/reorder-list","title":"143. Reorder List","version":""},{"content":"A linked list can be reversed either iteratively or recursively. Could you implement both? Example 1: Input: head = [1,2,3,4,5] Output: [5,4,3,2,1] Example 2: Input: head = [1,2] Output: [2,1] Example 3: Input: head = [] Output: [] Constraints: The number of nodes in the list is the range [0, 5000]. -5000 <= Node.val <= 5000 Approach Solution","contentType":"doc","description":"Given the head of a singly linked list, reverse the list, and return the reversed list","locale":"","route":"/linked-list/reverse-linked-list","title":"206. Reverse Linked List","version":""},{"content":"Could you do it in one pass? Example 1: Input: head = [1,2,3,4,5], left = 2, right = 4 Output: [1,4,3,2,5] Example 2: Input: head = [5], left = 1, right = 1 Output: [5] Constraints: The number of nodes in the list is n. 1 <= n <= 500 -500 <= Node.val <= 500 1 <= left <= right <= n Solution","contentType":"doc","description":"Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list","locale":"","route":"/linked-list/reverse-linked-list-ii","title":"92. Reverse Linked List II","version":""},{"content":"","contentType":"doc","description":"A two-dimensional grid is read as a flat array through a formula, and the neighbors are found by arithmetic.","locale":"","route":"/matrix-index-math","title":"Matrix Index Math","version":""},{"content":"Example 1: Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [[7,4,1],[8,5,2],[9,6,3]] Example 2: Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] Constraints: n == matrix.length == matrix[i].length 1 <= n <= 20 -1000 <= matrix[i][j] <= 1000 Approach Solution","contentType":"doc","description":"You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise)","locale":"","route":"/matrix-index-math/rotate-image","title":"48. Rotate Image","version":""},{"content":"You must do it in place. Example 1: Input: matrix = [[1,1,1],[1,0,1],[1,1,1]] Output: [[1,0,1],[0,0,0],[1,0,1]] Example 2: Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]] Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]] Constraints: m == matrix.length n == matrix[0].length 1 <= m, n <= 200 -2^31 <= matrix[i][j] <= 2^31 - 1 A straightforward solution using O(mn) space is probably a bad idea. A simple improvement uses O(m + n) space, but still not the best solution. Could you devise a constant space solution? Approach Solution","contentType":"doc","description":"Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's","locale":"","route":"/matrix-index-math/set-matrix-zeroes","title":"73. Set Matrix Zeroes","version":""},{"content":"Example 1: Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [1,2,3,6,9,8,7,4,5] Example 2: Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] Output: [1,2,3,4,8,12,11,10,9,5,6,7] Constraints: m == matrix.length n == matrix[i].length 1 <= m, n <= 10 -100 <= matrix[i][j] <= 100 Approach Solution","contentType":"doc","description":"Given an m x n matrix, return all elements of the matrix in spiral order","locale":"","route":"/matrix-index-math/spiral-matrix","title":"54. Spiral Matrix","version":""},{"content":"Example 1: Input: board = [[\"5\",\"3\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\"] ,[\"6\",\".\",\".\",\"1\",\"9\",\"5\",\".\",\".\",\".\"] ,[\".\",\"9\",\"8\",\".\",\".\",\".\",\".\",\"6\",\".\"] ,[\"8\",\".\",\".\",\".\",\"6\",\".\",\".\",\".\",\"3\"] ,[\"4\",\".\",\".\",\"8\",\".\",\"3\",\".\",\".\",\"1\"] ,[\"7\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\",\"6\"] ,[\".\",\"6\",\".\",\".\",\".\",\".\",\"2\",\"8\",\".\"] ,[\".\",\".\",\".\",\"4\",\"1\",\"9\",\".\",\".\",\"5\"] ,[\".\",\".\",\".\",\".\",\"8\",\".\",\".\",\"7\",\"9\"]] Output: true Example 2: Input: board = [[\"8\",\"3\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\"] ,[\"6\",\".\",\".\",\"1\",\"9\",\"5\",\".\",\".\",\".\"] ,[\".\",\"9\",\"8\",\".\",\".\",\".\",\".\",\"6\",\".\"] ,[\"8\",\".\",\".\",\".\",\"6\",\".\",\".\",\".\",\"3\"] ,[\"4\",\".\",\".\",\"8\",\".\",\"3\",\".\",\".\",\"1\"] ,[\"7\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\",\"6\"] ,[\".\",\"6\",\".\",\".\",\".\",\".\",\"2\",\"8\",\".\"] ,[\".\",\".\",\".\",\"4\",\"1\",\"9\",\".\",\".\",\"5\"] ,[\".\",\".\",\".\",\".\",\"8\",\".\",\".\",\"7\",\"9\"]] Output: false Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid. Constraints: board.length == 9 board[i].length == 9 board[i][j] is a digit 1-9 or '.'. Solution","contentType":"doc","description":"Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:","locale":"","route":"/matrix-index-math/valid-sudoku","title":"36. Valid Sudoku","version":""},{"content":"Monotonic Stack A monotonic stack keeps its values in sorted order as you push elements. The order can be increasing or decreasing, depending on the problem. A decreasing monotonic stack is the most common version. Before pushing a new value x, we pop from the top while the top is smaller than or equal to x. After those pops, pushing x keeps the stack decreasing from bottom to top. This push rule is the whole idea. A monotonic deque uses the same rule, but a deque also lets you remove old elements from the front, which is useful for sliding window problems. Example Implementation This implementation maintains a decreasing monotonic stack. In interview problems, the stack usually stores indices instead of values. You can read the value from the original array when needed, and the index gives you distance information such as \"how many steps away is the next greater element.\" For an array nums, if index j pops when you process index i, then i is the first index to the right that satisfies the stack condition for j. In a next-greater-element problem, that means nums[i] is the next greater value for nums[j].","contentType":"doc","description":"A stack keeps its items in sorted order, which finds the next larger or smaller item in one pass.","locale":"","route":"/monotonic-stack","title":"Monotonic Stack","version":""},{"content":"Example 1: Input: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3] Output: 3 Explanation: • The cars starting at 10 (speed 2) and 8 (speed 4) become a fleet, meeting each other at 12. The fleet forms at target. • The car starting at 0 (speed 1) does not catch up to any other car, so it is a fleet by itself. • The cars starting at 5 (speed 1) and 3 (speed 3) become a fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target. Example 2: Input: target = 10, position = [3], speed = [3] Output: 1 Explanation: There is only one car, hence there is only one fleet. Example 3: Input: target = 100, position = [0,2,4], speed = [4,2,1] Output: 1 Explanation: • The cars starting at 0 (speed 4) and 2 (speed 2) become a fleet, meeting each other at 4. The car starting at 4 (speed 1) travels to 5. • Then, the fleet at 4 (speed 2) and the car at position 5 (speed 1) become one fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target. Constraints: n == position.length == speed.length 1 <= n <= 10^5 0 < target <= 10^6 0 <= position[i] < target All the values of position are unique. 0 < speed[i] <= 10^6 Approach Solution","contentType":"doc","description":"There are n cars at given miles away from the starting mile 0, traveling to reach the mile target","locale":"","route":"/monotonic-stack/car-fleet","title":"853. Car Fleet","version":""},{"content":"Example 1: Input: temperatures = [73,74,75,71,69,72,76,73] Output: [1,1,4,2,1,1,0,0] Example 2: Input: temperatures = [30,40,50,60] Output: [1,1,1,0] Example 3: Input: temperatures = [30,60,90] Output: [1,1,0] Constraints: 1 <= temperatures.length <= 10^5 30 <= temperatures[i] <= 100 Approach Solution","contentType":"doc","description":"Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the i^th day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead","locale":"","route":"/monotonic-stack/daily-temperatures","title":"739. Daily Temperatures","version":""},{"content":"Example 1: Input: heights = [2,1,5,6,2,3] Output: 10 Explanation: The above is a histogram where width of each bar is 1. The largest rectangle is shown in the red area, which has an area = 10 units. Example 2: Input: heights = [2,4] Output: 4 Constraints: 1 <= heights.length <= 10^5 0 <= heights[i] <= 10^4 Solution","contentType":"doc","description":"Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram","locale":"","route":"/monotonic-stack/largest-rectangle-in-histogram","title":"84. Largest Rectangle in Histogram","version":""},{"content":"Could you find an O(nums1.length + nums2.length) solution? Example 1: Input: nums1 = [4,1,2], nums2 = [1,3,4,2] Output: [-1,3,-1] Explanation: The next greater element for each value of nums1 is as follows: 4 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1. 1 is underlined in nums2 = [1,3,4,2]. The next greater element is 3. 2 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1. Example 2: Input: nums1 = [2,4], nums2 = [1,2,3,4] Output: [3,-1] Explanation: The next greater element for each value of nums1 is as follows: 2 is underlined in nums2 = [1,2,3,4]. The next greater element is 3. 4 is underlined in nums2 = [1,2,3,4]. There is no next greater element, so the answer is -1. Constraints: 1 <= nums1.length <= nums2.length <= 1000 0 <= nums1[i], nums2[i] <= 10^4 All integers in nums1 and nums2 are unique. All the integers of nums1 also appear in nums2. Solution","contentType":"doc","description":"The next greater element of some element x in an array is the first greater element that is to the right of x in the same array","locale":"","route":"/monotonic-stack/next-greater-element-i","title":"496. Next Greater Element I","version":""},{"content":"Example 1: Input: `` Output: `` Explanation: Input [\"StockSpanner\", \"next\", \"next\", \"next\", \"next\", \"next\", \"next\", \"next\"] [[], [100], [80], [60], [70], [60], [75], [85]] Output [null, 1, 1, 1, 2, 1, 4, 6] Explanation StockSpanner stockSpanner = new StockSpanner(); stockSpanner.next(100); // return 1 stockSpanner.next(80); // return 1 stockSpanner.next(60); // return 1 stockSpanner.next(70); // return 2 stockSpanner.next(60); // return 1 stockSpanner.next(75); // return 4, because the last 4 prices (including today's price of 75) were less than or equal to today's price. stockSpanner.next(85); // return 6 Constraints: 1 <= price <= 10^5 At most 10^4 calls will be made to next. Solution","contentType":"doc","description":"Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day","locale":"","route":"/monotonic-stack/online-stock-span","title":"901. Online Stock Span","version":""},{"content":"","contentType":"doc","description":"The result for each state is stored in a table with two or more dimensions, and each cell is computed from its neighbors.","locale":"","route":"/multi-d-grid-dp","title":"Multi-D / Grid DP","version":""},{"content":"Example 1: Input: text1 = \"abcde\", text2 = \"ace\" Output: 3 Explanation: The longest common subsequence is \"ace\" and its length is 3. Example 2: Input: text1 = \"abc\", text2 = \"abc\" Output: 3 Explanation: The longest common subsequence is \"abc\" and its length is 3. Example 3: Input: text1 = \"abc\", text2 = \"def\" Output: 0 Explanation: There is no such common subsequence, so the result is 0. Constraints: 1 <= text1.length, text2.length <= 1000 text1 and text2 consist of only lowercase English characters. Solution","contentType":"doc","description":"Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0","locale":"","route":"/multi-d-grid-dp/longest-common-subsequence","title":"1143. Longest Common Subsequence","version":""},{"content":"Example 1: Input: grid = [[1,3,1],[1,5,1],[4,2,1]] Output: 7 Explanation: Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum. Example 2: Input: grid = [[1,2,3],[4,5,6]] Output: 12 Constraints: m == grid.length n == grid[i].length 1 <= m, n <= 200 0 <= grid[i][j] <= 200 Solution","contentType":"doc","description":"Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path","locale":"","route":"/multi-d-grid-dp/minimum-path-sum","title":"64. Minimum Path Sum","version":""},{"content":"Example 1: Input: m = 3, n = 7 Output: 28 Example 2: Input: m = 3, n = 2 Output: 3 Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner: 1. Right -> Down -> Down 2. Down -> Down -> Right 3. Down -> Right -> Down Constraints: 1 <= m, n <= 100 Solution","contentType":"doc","description":"There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time","locale":"","route":"/multi-d-grid-dp/unique-paths","title":"62. Unique Paths","version":""},{"content":"Suppose you are handed an array and asked for the sum of the elements between index i and index j. One query is easy: loop from i to j and add. But now imagine thousands of such queries over the same array, or a search that inspects the sum of every possible subarray. Recomputing each range from scratch costs O(n) per query, and the work piles up fast. Prefix sum removes that repetition. You spend O(n) once to precompute running totals, and afterward every range sum collapses into a single subtraction that runs in O(1), no matter how wide the range. Prefix Sum Explained Transformation A prefix sum turns an array into a new array of running totals. For an input array arr of length n, build a prefix array of length n + 1: prefix[0] = 0 prefix[k] = arr[0] + arr[1] + ... + arr[k-1] Each entry is the previous total plus one more element, so a single pass builds the whole thing: prefix[k] = prefix[k-1] + arr[k-1] The leading prefix[0] = 0 is the sum of zero elements. That extra slot is what makes the range math below land exactly on the boundaries, which is why prefix has one more cell than arr. Each bar is the running total up to that index. Every step keeps the previous total (blue) and adds one new element (amber) Any range sum is one subtraction The sum of the subarray from index i up to (but not including) j is prefix[j] - prefix[i]. prefix[j] is the total of everything before j, and prefix[i] is the total of everything before i, so subtracting throws away the front you did not ask for and leaves the middle. Watch the boundaries The one place prefix sums trip people up is the off-by-one at the edges. Keep three facts straight: prefix has length n + 1, prefix[0] is 0, and sum(arr[i..j-1]) = prefix[j] - prefix[i] uses an exclusive right index j. If a problem hands you an inclusive right index r, translate it to prefix[r + 1] - prefix[i]. When to reach for it Prefix sums fit whenever you need repeated range sums over a fixed array, or when an algorithm searches across subarray boundaries. For a single one-off sum, a plain loop is simpler; the prefix array earns its O(n) setup only once the same array is queried many times.","contentType":"doc","description":"A running total is stored at each index, so the sum of any range is one subtraction.","locale":"","route":"/prefix-sum","title":"Prefix Sum","version":""},{"content":"Example 1: Input: nums = [0,1] Output: 2 Explanation: [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. Example 2: Input: nums = [0,1,0] Output: 2 Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. Example 3: Input: nums = [0,1,1,1,1,1,0,0,0] Output: 6 Explanation: [1,1,1,0,0,0] is the longest contiguous subarray with equal number of 0 and 1. Constraints: 1 <= nums.length <= 10^5 nums[i] is either 0 or 1. Solution","contentType":"doc","description":"Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1","locale":"","route":"/prefix-sum/contiguous-array","title":"525. Contiguous Array","version":""},{"content":"You must write an algorithm that runs in O(n) time and without using the division operation. Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.) Example 1: Input: nums = [1,2,3,4] Output: [24,12,8,6] Example 2: Input: nums = [-1,1,0,-3,3] Output: [0,0,9,0,0] Constraints: 2 <= nums.length <= 10^5 -30 <= nums[i] <= 30 The input is generated such that answer[i] is guaranteed to fit in a 32-bit integer. Approach Solution","contentType":"doc","description":"Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]","locale":"","route":"/prefix-sum/product-of-array-except-self","title":"238. Product of Array Except Self","version":""},{"content":"Example 1: Input: [\"NumArray\", \"sumRange\", \"sumRange\", \"sumRange\"] [[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]] Output: [null, 1, -1, -3] Explanation: NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]); numArray.sumRange(0, 2); // return (-2) + 0 + 3 = 1 numArray.sumRange(2, 5); // return 3 + (-5) + 2 + (-1) = -1 numArray.sumRange(0, 5); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3 Constraints: 1 <= nums.length <= 10^4 -10^5 <= nums[i] <= 10^5 0 <= left <= right < nums.length At most 10^4 calls will be made to sumRange. Approach Solution","contentType":"doc","description":"Given an integer array nums, handle multiple queries of the following type","locale":"","route":"/prefix-sum/range-sum-query-immutable","title":"303. Range Sum Query - Immutable","version":""},{"content":"Example 1: Input: nums = [1,1,1], k = 2 Output: 2 Example 2: Input: nums = [1,2,3], k = 3 Output: 2 Constraints: 1 <= nums.length <= 2 * 10^4 -1000 <= nums[i] <= 1000 -10^7 <= k <= 10^7 Approach Solution","contentType":"doc","description":"Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k","locale":"","route":"/prefix-sum/subarray-sum-equals-k","title":"560. Subarray Sum Equals K","version":""},{"content":"Example 1: Input: nums = [4,5,0,-2,-3,1], k = 5 Output: 7 Explanation: There are 7 subarrays with a sum divisible by k = 5: [4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3] Example 2: Input: nums = [5], k = 9 Output: 0 Constraints: 1 <= nums.length <= 3 * 10^4 -10^4 <= nums[i] <= 10^4 2 <= k <= 10^4 Solution","contentType":"doc","description":"Given an integer array nums and an integer k, return the number of non-empty subarrays that have a sum divisible by k","locale":"","route":"/prefix-sum/subarray-sums-divisible-by-k","title":"974. Subarray Sums Divisible by K","version":""},{"content":"Sliding Window Many problems have two moving boundaries. That alone does not make them sliding window problems. A problem belongs to this family when: the answer depends on the subarray or substring between left and right you can update the window state incrementally as elements enter or leave you maintain a condition that tells you whether the window is valid The rule for all three: every element enters once and leaves once, so the whole thing runs in O(n). What is an invariant? An invariant is a statement that stays true before and after every iteration. In a sliding window, that statement usually describes the window itself, such as “the window is valid,” “it has no duplicates,” or “its sum is at most the target.” It can also describe a helper structure, such as “the frequency map matches the characters currently inside the window.” Why invariants matter? Without an invariant, shrinking the window is just a guess. You no longer know when the window is valid, when to update the answer, or why the left pointer should stop. The invariant is what makes the algorithm correct. How sliding window differs from other two-pointer families? Sliding window is not the same as every other two-pointer pattern. In Container With Most Water, the interval is only the remaining search space. In sliding window, the interval itself is the thing you care about. In Remove Duplicates, the left side is already the finished answer prefix. In sliding window, both pointers describe a live window that is still being checked. 1. Fixed-size windows The window size never changes. The main task is updating the window state efficiently when one element enters and one leaves. Sliding Window Fixed Add nums[right] to the window. If the window has k elements, record the answer. Remove the leftmost element so the window stays at size k. 2. Flexible-size windows with a validity rule Here the window expands until invalid, then shrinks until valid again. Sliding Window Flexible Longest Window Shortest Window Add nums[right] to the window. While the window is invalid, remove nums[left] and move left up. The window is now valid. Record its size. 3. Frequency-tracking windows The difficulty comes from keeping the bookkeeping correct as characters enter and leave. Frequency-tracking windows Add s[right] to the window. While the window has more than one character, remove s[left] and move left up. The window is now valid. Record its size. For anagram or \"minimum window substring\" problems, you also keep a target count. Track how many characters currently match the target with one integer (matched). Update it only when a count crosses the target value. This keeps each step O(1).","contentType":"doc","description":"Sliding window is a specialized two-pointer family where the pointers define a contiguous interval and you maintain information about that entire interval as it moves.","locale":"","route":"/sliding-window","title":"Sliding Window","version":""},{"content":"Example 1: Input: prices = [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell. Example 2: Input: prices = [7,6,4,3,1] Output: 0 Explanation: In this case, no transactions are done and the max profit = 0. Constraints: 1 <= prices.length <= 10^5 0 <= prices[i] <= 10^4 Approach The diagram traces the code below literally. That code does not run: curr and ans are never initialised, so the first curr > left test raises NameError, and left holds a price rather than an index, so prices[left] indexes by value. Solution","contentType":"doc","description":"You are given an array prices where prices[i] is the price of a given stock on the i^th day","locale":"","route":"/sliding-window/best-time-to-buy-and-sell-stock","title":"121. Best Time to Buy and Sell Stock","version":""},{"content":"Example 1: Input: s = \"ABAB\", k = 2 Output: 4 Explanation: Replace the two 'A's with two 'B's or vice versa. Example 2: Input: s = \"AABABBA\", k = 1 Output: 4 Explanation: Replace the one 'A' in the middle with 'B' and form \"AABBBBA\". The substring \"BBBB\" has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too. Constraints: 1 <= s.length <= 10^5 s consists of only uppercase English letters. 0 <= k <= s.length Approach Solution","contentType":"doc","description":"You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times","locale":"","route":"/sliding-window/longest-repeating-character-replacement","title":"424. Longest Repeating Character Replacement","version":""},{"content":"Example 1: Input: s = \"abcabcbb\" Output: 3 Explanation: The answer is \"abc\", with the length of 3. Note that \"bca\" and \"cab\" are also correct answers. Example 2: Input: s = \"bbbbb\" Output: 1 Explanation: The answer is \"b\", with the length of 1. Example 3: Input: s = \"pwwkew\" Output: 3 Explanation: The answer is \"wke\", with the length of 3. Notice that the answer must be a substring, \"pwke\" is a subsequence and not a substring. Constraints: 0 <= s.length <= 10^5 s consists of English letters, digits, symbols and spaces. Approach Solution","contentType":"doc","description":"Given a string s, find the length of the longest substring without duplicate characters","locale":"","route":"/sliding-window/longest-substring-without-repeating-characters","title":"3. Longest Substring Without Repeating Characters","version":""},{"content":"Example 1: Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Constraints: 1 <= nums.length <= 10^5 nums[i] is either 0 or 1. 0 <= k <= nums.length Solution","contentType":"doc","description":"Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's","locale":"","route":"/sliding-window/max-consecutive-ones-iii","title":"1004. Max Consecutive Ones III","version":""},{"content":"Example 1: Input: nums = [1,12,-5,-6,50,3], k = 4 Output: 12.75000 Explanation: Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75 Example 2: Input: nums = [5], k = 1 Output: 5.00000 Constraints: n == nums.length 1 <= k <= n <= 10^5 -10^4 <= nums[i] <= 10^4 Solution","contentType":"doc","description":"You are given an integer array nums consisting of n elements, and an integer k","locale":"","route":"/sliding-window/maximum-average-subarray-i","title":"643. Maximum Average Subarray I","version":""},{"content":"If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log(n)). Example 1: Input: target = 7, nums = [2,3,1,2,4,3] Output: 2 Explanation: The subarray [4,3] has the minimal length under the problem constraint. Example 2: Input: target = 4, nums = [1,4,4] Output: 1 Example 3: Input: target = 11, nums = [1,1,1,1,1,1,1,1] Output: 0 Constraints: 1 <= target <= 10^9 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^4 Solution","contentType":"doc","description":"Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead","locale":"","route":"/sliding-window/minimum-size-subarray-sum","title":"209. Minimum Size Subarray Sum","version":""},{"content":"Could you find an algorithm that runs in O(m + n) time? Example 1: Input: s = \"ADOBECODEBANC\", t = \"ABC\" Output: \"BANC\" Explanation: The minimum window substring \"BANC\" includes 'A', 'B', and 'C' from string t. Example 2: Input: s = \"a\", t = \"a\" Output: \"a\" Explanation: The entire string s is the minimum window. Example 3: Input: s = \"a\", t = \"aa\" Output: \"\" Explanation: Both 'a's from t must be included in the window. Since the largest window of s only has one 'a', return empty string. Constraints: m == s.length n == t.length 1 <= m, n <= 10^5 s and t consist of uppercase and lowercase English letters. Solution","contentType":"doc","description":"Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string \"\"","locale":"","route":"/sliding-window/minimum-window-substring","title":"76. Minimum Window Substring","version":""},{"content":"Example 1: Input: s1 = \"ab\", s2 = \"eidbaooo\" Output: true Explanation: s2 contains one permutation of s1 (\"ba\"). Example 2: Input: s1 = \"ab\", s2 = \"eidboaoo\" Output: false Constraints: 1 <= s1.length, s2.length <= 10^4 s1 and s2 consist of lowercase English letters. Approach Solution","contentType":"doc","description":"Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise","locale":"","route":"/sliding-window/permutation-in-string","title":"567. Permutation in String","version":""},{"content":"A stack works like a stack of plates in a cafeteria. You add new plates on top. When you need a plate, you take from the top. The last plate you added is the first one you remove. This is Last-In-First-Out (LIFO). The most recent item goes out first. Core Stack Operations A stack has two fundamental operations: Push: add an item to the top of the stack Pop: remove the item at the top of the stack Some stacks have additional operations: Peek: get the item at the top of the stack without removing it IsEmpty: check if the stack is empty Python lists provide append() for pushing and pop() for popping. The peek() method is not built-in, but you can implement it yourself. Last in, First out The defining characteristic of a stack is LIFO order. Whatever you put in last comes out first. This is different from a queue, which is First-In-First-Out (FIFO). Add items: A, B, C Stack: [A, B, C] (C on top) Remove items: First pop: C Second pop: B Third pop: A The order reverses. Items come out in the opposite order they went in.","contentType":"doc","description":"Items are added and removed from one end, so the last item added is the first item removed.","locale":"","route":"/stack","title":"Stack","version":""},{"content":"Example 1: Input: s = \"1 + 1\" Output: 2 Example 2: Input: s = \" 2-1 + 2 \" Output: 3 Example 3: Input: s = \"(1+(4+5+2)-3)+(6+8)\" Output: 23 Constraints: 1 <= s.length <= 3 * 10^5 s consists of digits, '+', '-', '(', ')', and ' '. s represents a valid expression. '+' is not used as a unary operation (i.e., \"+1\" and \"+(2 + 3)\" is invalid). '-' could be used as a unary operation (i.e., \"-1\" and \"-(2 + 3)\" is valid). There will be no two consecutive operators in the input. Every number and running calculation will fit in a signed 32-bit integer. Solution","contentType":"doc","description":"Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation","locale":"","route":"/stack/basic-calculator","title":"224. Basic Calculator","version":""},{"content":"Example 1: Input: tokens = [\"2\",\"1\",\"+\",\"3\",\"*\"] Output: 9 Explanation: ((2 + 1) * 3) = 9 Example 2: Input: tokens = [\"4\",\"13\",\"5\",\"/\",\"+\"] Output: 6 Explanation: (4 + (13 / 5)) = 6 Example 3: Input: tokens = [\"10\",\"6\",\"9\",\"3\",\"+\",\"-11\",\"*\",\"/\",\"*\",\"17\",\"+\",\"5\",\"+\"] Output: 22 Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5 = ((10 * (6 / (12 * -11))) + 17) + 5 = ((10 * (6 / -132)) + 17) + 5 = ((10 * 0) + 17) + 5 = (0 + 17) + 5 = 17 + 5 = 22 Constraints: 1 <= tokens.length <= 10^4 tokens[i] is either an operator: \"+\", \"-\", \"*\", or \"/\", or an integer in the range [-200, 200]. Approach Solution","contentType":"doc","description":"You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation","locale":"","route":"/stack/evaluate-reverse-polish-notation","title":"150. Evaluate Reverse Polish Notation","version":""},{"content":"You must implement a solution with O(1) time complexity for each function. Example 1: Input: `` Output: `` Explanation: Input [\"MinStack\",\"push\",\"push\",\"push\",\"getMin\",\"pop\",\"top\",\"getMin\"] [[],[-2],[0],[-3],[],[],[],[]] Output [null,null,null,null,-3,null,0,-2] Explanation MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); // return -3 minStack.pop(); minStack.top(); // return 0 minStack.getMin(); // return -2 Constraints: -2^31 <= val <= 2^31 - 1 Methods pop, top and getMin operations will always be called on non-empty stacks. At most 3 * 10^4 calls will be made to push, pop, top, and getMin. Approach Solution","contentType":"doc","description":"Design a stack that supports push, pop, top, and retrieving the minimum element in constant time","locale":"","route":"/stack/min-stack","title":"155. Min Stack","version":""},{"content":"Example 1: Input: path = \"/home/\" Output: \"/home\" Explanation: The trailing slash should be removed. Example 2: Input: path = \"/home//foo/\" Output: \"/home/foo\" Explanation: Multiple consecutive slashes are replaced by a single one. Example 3: Input: path = \"/home/user/Documents/../Pictures\" Output: \"/home/user/Pictures\" Explanation: A double period \"..\" refers to the directory up a level (the parent directory). Example 4: Input: path = \"/../\" Output: \"/\" Explanation: Going one level up from the root directory is not possible. Example 5: Input: path = \"/.../a/../b/c/../d/./\" Output: \"/.../b/d\" Explanation: \"...\" is a valid name for a directory in this problem. Constraints: 1 <= path.length <= 3000 path consists of English letters, digits, period '.', slash '/' or '_'. path is a valid absolute Unix path. Solution","contentType":"doc","description":"You are given an absolute path for a Unix-style file system, which always begins with a slash '/'. Your task is to transform this absolute path into its simplified canonical path","locale":"","route":"/stack/simplify-path","title":"71. Simplify Path","version":""},{"content":"Example 1: Input: s = \"()\" Output: true Example 2: Input: s = \"()[]{}\" Output: true Example 3: Input: s = \"(]\" Output: false Example 4: Input: s = \"([])\" Output: true Example 5: Input: s = \"([)]\" Output: false Constraints: 1 <= s.length <= 10^4 s consists of parentheses only '()[]'. Approach Solution","contentType":"doc","description":"Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid","locale":"","route":"/stack/valid-parentheses","title":"20. Valid Parentheses","version":""},{"content":"Directed Graph Before we get started on topological order, let's talk about directed graphs first. A graph is directed when its edges have directions (these edges are also called arcs). Suppose that v, w are vertices in a directed graph. Let's get familiar with a few concepts. in-edge - an edge pointing to or coming into the node out-edge - an edge pointing away or going out from the node (we will refer those nodes on the other end of the out-edges of v to v's neighbors) in-degree - the number of in-edges coming into the node out-degree - the number of out-edges going out from the node dependency - \"v depends on w\" / \"v is dependent on w\" means that there is a directed path from w to v where v, w are vertices in a directed graph. With these new terms under our belt, it will make more sense when we talk about directed graphs. What is a topological order? Topological sort is not unique. For example, for a graph like this: Both [4, 5, 2] and [5, 4, 2] are valid topological orders. Task 3 is completely independent of task 2 and 4, and it can be anywhere in the order as long as it is before task 1 which depends on it. All of the following ordering are valid topological orderings. [4, 2, 3, 1], [4, 3, 2, 1], [3, 4, 2, 1] Graphs with Cycles Do Not Have Topological Ordering It should be obvious that if a graph with a cycle does not have a topological ordering. In the above example, 2 has to come before 5 which has come before 4 which has to come before 2 which has to come before 5... which is impossible. Kahn's Algorithm To obtain a topological order, we can use Kahn's algorithm which is very similar to Breadth First Search. In order to understand why we must use this algorithm we first look at the problem it is trying to solve. Given a directed graph does there exist a way to remove the nodes such that each time we remove a node we guarantee that no other nodes point to that particular node? For this algorithm, we systematically remove one node at a time, each time removing a node such that no other nodes point to that node (in-degree is 0). If no such node exists, then there must be a cycle, and there is no way to order the nodes such that \"every node appears in the ordering before all the nodes it points to\" (no solution). After removing the current node, for each neighboring node the current node points to, we check whether any nodes point to this node. If there isn't any, we push this node into the queue. Notice it is important to keep track of the number of nodes pointing to a node (in-degree) in the question as we only push the node into the queue once all nodes the current node depended on have been removed. Here is a graphic to demonstrate the idea. Example Code Notice the topological sort algorithm is very similar to BFS. The main difference is that we only push nodes with 0 in-degree into the queue in topological sort whereas in BFS we push all the neighboring nodes into the queue.","contentType":"doc","description":"Topological sort or topological ordering of a directed graph is an ordering of nodes such that every node appears in the ordering before all the nodes it points to. ","locale":"","route":"/topological-sort","title":"Topological Sort","version":""},{"content":"Example 1: Input: numCourses = 2, prerequisites = [[1,0]] Output: true Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible. Example 2: Input: numCourses = 2, prerequisites = [[1,0],[0,1]] Output: false Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible. Constraints: 1 <= numCourses <= 2000 0 <= prerequisites.length <= 5000 prerequisites[i].length == 2 0 <= ai, bi < numCourses All the pairs prerequisites[i] are unique. Solution","contentType":"doc","description":"There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai","locale":"","route":"/topological-sort/course-schedule","title":"207. Course Schedule","version":""},{"content":"Example 1: Input: numCourses = 2, prerequisites = [[1,0]] Output: [0,1] Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1]. Example 2: Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]] Output: [0,2,1,3] Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3]. Example 3: Input: numCourses = 1, prerequisites = [] Output: [0] Constraints: 1 <= numCourses <= 2000 0 <= prerequisites.length <= numCourses * (numCourses - 1) prerequisites[i].length == 2 0 <= ai, bi < numCourses ai != bi All the pairs [ai, bi] are distinct. Solution","contentType":"doc","description":"There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai","locale":"","route":"/topological-sort/course-schedule-ii","title":"210. Course Schedule II","version":""},{"content":"","contentType":"doc","description":"All nodes at one level are visited before the nodes at the next level.","locale":"","route":"/tree-bfs","title":"Tree BFS","version":""},{"content":"Example 1: Input: root = [3,9,20,null,null,15,7] Output: [[3],[9,20],[15,7]] Example 2: Input: root = [1] Output: [[1]] Example 3: Input: root = [] Output: [] Constraints: The number of nodes in the tree is in the range [0, 2000]. -1000 <= Node.val <= 1000 Approach Solution","contentType":"doc","description":"Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level)","locale":"","route":"/tree-bfs/binary-tree-level-order-traversal","title":"102. Binary Tree Level Order Traversal","version":""},{"content":"Example 1: Input: root = [1,2,3,null,5,null,4] Output: [1,3,4] Example 2: Input: root = [1,2,3,4,null,null,null,5] Output: [1,3,4,5] Example 3: Input: root = [1,null,3] Output: [1,3] Example 4: Input: root = [] Output: [] Constraints: The number of nodes in the tree is in the range [0, 100]. -100 <= Node.val <= 100 Approach Solution","contentType":"doc","description":"Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom","locale":"","route":"/tree-bfs/binary-tree-right-side-view","title":"199. Binary Tree Right Side View","version":""},{"content":"Example 1: Input: root = [3,1,4,3,null,1,5] Output: 4 Explanation: Nodes in blue are good. Root Node (3) is always a good node. Node 4 -> (3,4) is the maximum value in the path starting from the root. Node 5 -> (3,4,5) is the maximum value in the path Node 3 -> (3,1,3) is the maximum value in the path. Example 2: Input: root = [3,3,null,4,2] Output: 3 Explanation: Node 2 -> (3, 3, 2) is not good, because \"3\" is higher than it. Example 3: Input: root = [1] Output: 1 Explanation: Root is considered as good. Constraints: The number of nodes in the binary tree is in the range [1, 10^5]. Each node's value is between [-104, 104]. Approach Solution","contentType":"doc","description":"Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X","locale":"","route":"/tree-bfs/count-good-nodes-in-binary-tree","title":"1448. Count Good Nodes in Binary Tree","version":""},{"content":"","contentType":"doc","description":"Each branch is followed to its end before the next branch is examined.","locale":"","route":"/tree-dfs","title":"Tree DFS","version":""},{"content":"Example 1: Input: root = [4,2,7,1,3,6,9] Output: [4,7,2,9,6,3,1] Example 2: Input: root = [2,1,3] Output: [2,3,1] Example 3: Input: root = [] Output: [] Constraints: The number of nodes in the tree is in the range [0, 100]. -100 <= Node.val <= 100 Approach Solution","contentType":"doc","description":"Given the root of a binary tree, invert the tree, and return its root","locale":"","route":"/tree-dfs/invert-binary-tree","title":"226. Invert Binary Tree","version":""},{"content":"Example 1: Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 Output: 3 Explanation: The LCA of nodes 5 and 1 is 3. Example 2: Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 Output: 5 Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition. Example 3: Input: root = [1,2], p = 1, q = 2 Output: 1 Constraints: The number of nodes in the tree is in the range [2, 10^5]. -10^9 <= Node.val <= 10^9 All Node.val are unique. p != q p and q will exist in the tree. Approach Solution","contentType":"doc","description":"Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree","locale":"","route":"/tree-dfs/lowest-common-ancestor-of-a-binary-tree","title":"236. Lowest Common Ancestor of a Binary Tree","version":""},{"content":"Example 1: Input: root = [3,9,20,null,null,15,7] Output: 3 Example 2: Input: root = [1,null,2] Output: 2 Constraints: The number of nodes in the tree is in the range [0, 10^4]. -100 <= Node.val <= 100 Approach Solution","contentType":"doc","description":"Given the root of a binary tree, return its maximum depth","locale":"","route":"/tree-dfs/maximum-depth-of-binary-tree","title":"104. Maximum Depth of Binary Tree","version":""},{"content":"","contentType":"doc","description":"Each node holds one character, and a path from the root spells a word.","locale":"","route":"/trie","title":"Trie","version":""},{"content":"Constraints: 1 <= word.length <= 25 word in addWord consists of lowercase English letters. word in search consist of '.' or lowercase English letters. There will be at most 2 dots in word for search queries. At most 10^4 calls will be made to addWord and search. Approach Solution","contentType":"doc","description":"Design a data structure that supports adding new words and finding if a string matches any previously added string","locale":"","route":"/trie/design-add-and-search-words-data-structure","title":"211. Design Add and Search Words Data Structure","version":""},{"content":"Example 1: Input: `` Output: `` Explanation: Input [\"Trie\", \"insert\", \"search\", \"search\", \"startsWith\", \"insert\", \"search\"] [[], [\"apple\"], [\"apple\"], [\"app\"], [\"app\"], [\"app\"], [\"app\"]] Output [null, null, true, false, true, null, true] Explanation Trie trie = new Trie(); trie.insert(\"apple\"); trie.search(\"apple\"); // return True trie.search(\"app\"); // return False trie.startsWith(\"app\"); // return True trie.insert(\"app\"); trie.search(\"app\"); // return True Constraints: 1 <= word.length, prefix.length <= 2000 word and prefix consist only of lowercase English letters. At most 3 * 10^4 calls in total will be made to insert, search, and startsWith. Approach Solution","contentType":"doc","description":"A trie (pronounced as \"try\") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker","locale":"","route":"/trie/implement-trie-prefix-tree","title":"208. Implement Trie (Prefix Tree)","version":""},{"content":"Most interview problems in this section fall into one of four families: Same direction pointers: one pointer reads, one pointer writes, or one pointer stays behind another while scanning left to right. Opposite direction pointers: one pointer starts on the left, one on the right, and each move eliminates impossible answers. Sliding window: the two pointers define a contiguous interval, and you maintain some property of that interval as the window expands and shrinks. Fast and slow pointers: both pointers move through a linked structure, but at different speeds or with a fixed gap. Same Direction Same-direction pointer problems usually scan from left to right, but the two pointers do different jobs. Usually: one pointer reads the next value from the original input one pointer marks where the next kept value should go Sometimes both pointers move forward together, but one pointer stays behind the other to preserve a gap or a relationship. When this family applies Reach for same-direction pointers when the problem asks you to: rewrite an array in-place remove or compress elements without extra memory keep only the values that satisfy some condition maintain a fixed gap between two forward-moving pointers Core Invariant The invariant in this family is usually about the prefix you have already processed. Typical invariant: \"Everything before the write pointer is already in the correct final form.\" That is what makes these problems feel different from opposite-direction scans. You are not searching from both sides. You are building the answer progressively as you sweep through the input. Subtypes in this section 1. Compaction / Overwrite The slow pointer marks the next output position. The fast pointer scans the input. When the current value should be kept, copy it forward and advance the slow pointer. 2. Relative Speed Both pointers move forward, but one moves twice as fast. The invariant is about their distance and how much of the structure each pointer has consumed. 3. Fixed Gap Both pointers move forward while preserving a gap. The invariant is not about a rewritten prefix. It is about the distance between the pointers. What changes across problems The family stays the same, but the invariant changes slightly: in array compaction, the invariant is about the prefix being correct in fast/slow midpoint problems, the invariant is about relative speed in fixed-gap problems, the invariant is about distance So the unifying idea is not \"both pointers move right.\" The unifying idea is: \"the structure to the left of the pointers already has a known meaning\" What to watch out for Common mistakes: Updating the write pointer before copying the current value. Forgetting exactly what region is already valid. Losing track of whether the invariant is about a prefix, a gap, or relative speed. As you go through the following problems, pay attention to one question: What does the region before the slow pointer mean right now? Opposite Direction Opposite-direction pointer problems place one pointer on the left and one on the right, then move inward. The hard part is never the loop itself. The hard part is justifying which pointer is safe to move. When this family applies Reach for opposite-direction pointers when the problem asks you to: the input is sorted, or can be made monotonic by sorting you are comparing a left choice and a right choice the problem has symmetry moving one side lets you eliminate a set of impossible answers Core Invariant The invariant in this family is usually: \"All candidate answers outside the current [left, right] range have already been ruled out.\" That means each move must come with a proof. If you move a pointer without knowing why the discarded cases cannot contain the optimum, the algorithm is just guessing. Subtypes in this section 1. Monotonic sum elimination Because the array is sorted: if the sum is too small, moving the left pointer right is the only useful move if the sum is too large, moving the right pointer left is the only useful move Sorted order gives you a monotonic rule. 2. Symmetry checking Here the pointers compare mirrored positions. The movement rule is mostly \"move both,\" except when you must skip punctuation or spaces. 3. Bottleneck elimination This one is important because it looks different from the other two. The key idea is that the shorter wall limits the area. If you keep that shorter wall and decrease the width, you cannot improve the answer. So it is safe to discard that wall. Why this is not a sliding window Opposite-direction scans do not maintain an interval property like \"this window is valid.\" Instead, each step does one of these: rules out sums that are too small or too large checks mirrored characters discards a bottleneck boundary The interval itself is just the remaining search space. What to watch out for Common mistakes: Using two pointers on an unsorted pair problem without first creating monotonicity. Moving the taller wall in Container With Most Water without an elimination proof. Calling the method sliding window just because two boundaries exist. As you read the problems in this family, keep asking: What exact set of candidate answers did this pointer move eliminate?","contentType":"doc","description":"Two indices move through the data from different positions and meet at the answer.","locale":"","route":"/two-pointers","title":"Two Pointers","version":""},{"content":"Notice that the solution set must not contain duplicate triplets. Example 1: Input: nums = [-1,0,1,2,-1,-4] Output: [[-1,-1,2],[-1,0,1]] Explanation: nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0. nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0. nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0. The distinct triplets are [-1,0,1] and [-1,-1,2]. Notice that the order of the output and the order of the triplets does not matter. Example 2: Input: nums = [0,1,1] Output: [] Explanation: The only possible triplet does not sum up to 0. Example 3: Input: nums = [0,0,0] Output: [[0,0,0]] Explanation: The only possible triplet sums up to 0. Constraints: 3 <= nums.length <= 3000 -10^5 <= nums[i] <= 10^5 Approach Solution","contentType":"doc","description":"Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0","locale":"","route":"/two-pointers/3sum","title":"15. 3Sum","version":""},{"content":"Notice that you may not slant the container. Example 1: Input: height = [1,8,6,2,5,4,8,3,7] Output: 49 Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49. Example 2: Input: height = [1,1] Output: 1 Constraints: n == height.length 2 <= n <= 10^5 0 <= height[i] <= 10^4 Approach Solution","contentType":"doc","description":"You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the i^th line are (i, 0) and (i, height[i])","locale":"","route":"/two-pointers/container-with-most-water","title":"11. Container With Most Water","version":""},{"content":"Suppose there are lots of incoming s, say s1, s2, ..., sk where k >= 10^9, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code? Example 1: Input: s = \"abc\", t = \"ahbgdc\" Output: true Example 2: Input: s = \"axc\", t = \"ahbgdc\" Output: false Constraints: 0 <= s.length <= 100 0 <= t.length <= 10^4 s and t consist only of lowercase English letters. Approach Solution","contentType":"doc","description":"Given two strings s and t, return true if s is a subsequence of t, or false otherwise","locale":"","route":"/two-pointers/is-subsequence","title":"392. Is Subsequence","version":""},{"content":"Example 1: Input: nums = [1,1,1,2,2,3] Output: 5, nums = [1,1,2,2,3,_] Explanation: Your function should return k = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively. It does not matter what you leave beyond the returned k (hence they are underscores). Example 2: Input: nums = [0,0,1,1,1,1,2,3,3] Output: 7, nums = [0,0,1,1,2,3,3,_,_] Explanation: Your function should return k = 7, with the first seven elements of nums being 0, 0, 1, 1, 2, 3 and 3 respectively. It does not matter what you leave beyond the returned k (hence they are underscores). Constraints: 1 <= nums.length <= 3 * 10^4 -10^4 <= nums[i] <= 10^4 nums is sorted in non-decreasing order. Solution","contentType":"doc","description":"Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same","locale":"","route":"/two-pointers/remove-duplicates-from-sorted-array-ii","title":"80. Remove Duplicates from Sorted Array II","version":""},{"content":"You must do this by modifying the input array in-place with O(1) extra memory. Example 1: Input: s = [\"h\",\"e\",\"l\",\"l\",\"o\"] Output: [\"o\",\"l\",\"l\",\"e\",\"h\"] Example 2: Input: s = [\"H\",\"a\",\"n\",\"n\",\"a\",\"h\"] Output: [\"h\",\"a\",\"n\",\"n\",\"a\",\"H\"] Constraints: 1 <= s.length <= 10^5 s[i] is a printable ascii character. Approach Solution","contentType":"doc","description":"Write a function that reverses a string. The input string is given as an array of characters s","locale":"","route":"/two-pointers/reverse-string","title":"344. Reverse String","version":""},{"content":"Example 1: Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] Example 2: Input: nums = [-1,-100,3,99], k = 2 Output: [3,99,-1,-100] Explanation: rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] Constraints: 1 <= nums.length <= 10^5 -2^31 <= nums[i] <= 2^31 - 1 0 <= k <= 10^5 Try to come up with as many solutions as you can. There are at least three different ways to solve this problem. Could you do it in-place with O(1) extra space? Solution","contentType":"doc","description":"Given an integer array nums, rotate the array to the right by k steps, where k is non-negative","locale":"","route":"/two-pointers/rotate-array","title":"189. Rotate Array","version":""},{"content":"Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach? Example 1: Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16,100] Explanation: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [0,1,9,16,100]. Example 2: Input: nums = [-7,-3,2,3,11] Output: [4,9,9,49,121] Constraints: 1 <= nums.length <= 10^4 -10^4 <= nums[i] <= 10^4 nums is sorted in non-decreasing order. Approach Solution","contentType":"doc","description":"Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order","locale":"","route":"/two-pointers/squares-of-a-sorted-array","title":"977. Squares of a Sorted Array","version":""},{"content":"Example 1: Input: height = [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Example 2: Input: height = [4,2,0,3,2,5] Output: 9 Constraints: n == height.length 1 <= n <= 2 * 10^4 0 <= height[i] <= 10^5 Solution","contentType":"doc","description":"Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining","locale":"","route":"/two-pointers/trapping-rain-water","title":"42. Trapping Rain Water","version":""},{"content":"Your solution must use only constant extra space. Example 1: Input: numbers = [2,7,11,15], target = 9 Output: [1,2] Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2]. Example 2: Input: numbers = [2,3,4], target = 6 Output: [1,3] Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3]. Example 3: Input: numbers = [-1,0], target = -1 Output: [1,2] Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2]. Constraints: 2 <= numbers.length <= 3 * 10^4 -1000 <= numbers[i] <= 1000 numbers is sorted in non-decreasing order. -1000 <= target <= 1000 The tests are generated such that there is exactly one solution. Approach Solution","contentType":"doc","description":"Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length","locale":"","route":"/two-pointers/two-sum-ii-input-array-is-sorted","title":"167. Two Sum II - Input Array Is Sorted","version":""},{"content":"Example 1: Input: s = \"A man, a plan, a canal: Panama\" Output: true Explanation: \"amanaplanacanalpanama\" is a palindrome. Example 2: Input: s = \"race a car\" Output: false Explanation: \"raceacar\" is not a palindrome. Example 3: Input: s = \" \" Output: true Explanation: s is an empty string \"\" after removing non-alphanumeric characters. Since an empty string reads the same forward and backward, it is a palindrome. Constraints: 1 <= s.length <= 2 * 10^5 s consists only of printable ASCII characters. Approach Solution","contentType":"doc","description":"A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers","locale":"","route":"/two-pointers/valid-palindrome","title":"125. Valid Palindrome","version":""},{"content":"","contentType":"doc","description":"Union-Find (Disjoint Set Union) is a data structure that maintains a collection of disjoint sets and supports efficiently finding which set an element belongs to and merging two sets.","locale":"","route":"/union-find","title":"Union Find","version":""},{"content":"Example 1: Input: n = 5, edges = [[0,1],[1,2],[3,4]] Output: 2 Example 2: Input: n = 5, edges = [[0,1],[1,2],[2,3],[3,4]] Output: 1 Constraints: 1 <= n <= 2000 1 <= edges.length <= 5000 edges[i] = [ai, bi] ai != bi There are no repeated edges. Solution","contentType":"doc","description":"You have a graph of n nodes. You are given an integer n and an array edges where edges[i] = [ai, bi] indicates that there is an edge between ai and bi in the graph","locale":"","route":"/union-find/number-of-connected-components-in-an-undirected-graph","title":"323. Number of Connected Components in an Undirected Graph","version":""},{"content":"Example 1: Input: edges = [[1,2],[1,3],[2,3]] Output: [2,3] Example 2: Input: edges = [[1,2],[2,3],[3,4],[1,4],[1,5]] Output: [1,4] Constraints: n == edges.length 3 <= n <= 1000 edges[i].length == 2 1 <= ai < bi <= edges.length ai != bi There are no repeated edges. The given graph is connected. Solution","contentType":"doc","description":"In this problem, a tree is an undirected graph that is connected and has no cycles","locale":"","route":"/union-find/redundant-connection","title":"684. Redundant Connection","version":""}],"name":"prdlk","navigation":{"actions":[],"cta":null,"featured":[],"root":"/","selectors":[],"sidebar":[{"description":"Structured dependency spine based roadmap for solving DSA Problems on leetcode.","icon":"play","kind":"page","label":"Introduction","pageId":"filesystem:index.mdx","route":"/"},{"description":"Coding problems rarely tell you which algorithm to use, but the wording usually leaks it. \"K-th largest\" hints at a heap. \"Shortest path\" in an unweighted graph hints at BFS.","icon":"map","kind":"page","label":"Keywords","pageId":"filesystem:keywords.mdx","route":"/keywords"},{"children":[{"children":[{"badge":"Easy","description":"You are given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target","icon":"dot","kind":"page","label":"Two Sum","pageId":"filesystem:(01-linear-structures)/hash-table/1_two-sum.mdx","route":"/hash-table/two-sum"},{"badge":"Medium","description":"Given an array of strings strs, group the anagrams together. You can return the answer in any order","icon":"dot","kind":"page","label":"Group Anagrams","pageId":"filesystem:(01-linear-structures)/hash-table/49_group-anagrams.mdx","route":"/hash-table/group-anagrams"},{"badge":"Medium","description":"Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence","icon":"dot","kind":"page","label":"Longest Consecutive Sequence","pageId":"filesystem:(01-linear-structures)/hash-table/128_longest-consecutive-sequence.mdx","route":"/hash-table/longest-consecutive-sequence"},{"badge":"Easy","description":"Given two strings s and t, determine if they are isomorphic","icon":"dot","kind":"page","label":"Isomorphic Strings","pageId":"filesystem:(01-linear-structures)/hash-table/205_isomorphic-strings.mdx","route":"/hash-table/isomorphic-strings"},{"badge":"Easy","description":"Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct","icon":"dot","kind":"page","label":"Contains Duplicate","pageId":"filesystem:(01-linear-structures)/hash-table/217_contains-duplicate.mdx","route":"/hash-table/contains-duplicate"},{"badge":"Easy","description":"Given two strings s and t, return true if t is an anagram of s, and false otherwise","icon":"dot","kind":"page","label":"Valid Anagram","pageId":"filesystem:(01-linear-structures)/hash-table/242_valid-anagram.mdx","route":"/hash-table/valid-anagram"},{"badge":"Easy","description":"Given a pattern and a string s, find if s follows the same pattern","icon":"dot","kind":"page","label":"Word Pattern","pageId":"filesystem:(01-linear-structures)/hash-table/290_word-pattern.mdx","route":"/hash-table/word-pattern"},{"badge":"Medium","description":"Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order","icon":"dot","kind":"page","label":"Top K Frequent Elements","pageId":"filesystem:(01-linear-structures)/hash-table/347_top-k-frequent-elements.mdx","route":"/hash-table/top-k-frequent-elements"},{"badge":"Easy","description":"Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1","icon":"dot","kind":"page","label":"First Unique Character in a String","pageId":"filesystem:(01-linear-structures)/hash-table/387_first-unique-character-in-a-string.mdx","route":"/hash-table/first-unique-character-in-a-string"},{"badge":"Medium","description":"Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string","icon":"dot","kind":"page","label":"Sort Characters By Frequency","pageId":"filesystem:(01-linear-structures)/hash-table/451_sort-characters-by-frequency.mdx","route":"/hash-table/sort-characters-by-frequency"},{"badge":"Easy","description":"A pangram is a sentence where every letter of the English alphabet appears at least once","icon":"dot","kind":"page","label":"Check if the Sentence Is Pangram","pageId":"filesystem:(01-linear-structures)/hash-table/1832_check-if-the-sentence-is-pangram.mdx","route":"/hash-table/check-if-the-sentence-is-pangram"}],"display":"group","icon":"hash","kind":"group","label":"Hash Table","path":"/hash-table","route":"/hash-table"},{"children":[{"badge":"Medium","description":"Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]","icon":"dot","kind":"page","label":"Product of Array Except Self","pageId":"filesystem:(01-linear-structures)/prefix-sum/238_product-of-array-except-self.mdx","route":"/prefix-sum/product-of-array-except-self"},{"badge":"Easy","description":"Given an integer array nums, handle multiple queries of the following type","icon":"dot","kind":"page","label":"Range Sum Query - Immutable","pageId":"filesystem:(01-linear-structures)/prefix-sum/303_range-sum-query-immutable.mdx","route":"/prefix-sum/range-sum-query-immutable"},{"badge":"Medium","description":"Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1","icon":"dot","kind":"page","label":"Contiguous Array","pageId":"filesystem:(01-linear-structures)/prefix-sum/525_contiguous-array.mdx","route":"/prefix-sum/contiguous-array"},{"badge":"Medium","description":"Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k","icon":"dot","kind":"page","label":"Subarray Sum Equals K","pageId":"filesystem:(01-linear-structures)/prefix-sum/560_subarray-sum-equals-k.mdx","route":"/prefix-sum/subarray-sum-equals-k"},{"badge":"Medium","description":"Given an integer array nums and an integer k, return the number of non-empty subarrays that have a sum divisible by k","icon":"dot","kind":"page","label":"Subarray Sums Divisible by K","pageId":"filesystem:(01-linear-structures)/prefix-sum/974_subarray-sums-divisible-by-k.mdx","route":"/prefix-sum/subarray-sums-divisible-by-k"}],"display":"group","icon":"sigma","kind":"group","label":"Prefix Sum","path":"/prefix-sum","route":"/prefix-sum"},{"children":[{"badge":"Medium","description":"You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the i^th line are (i, 0) and (i, height[i])","icon":"dot","kind":"page","label":"Container With Most Water","pageId":"filesystem:(01-linear-structures)/two-pointers/11_container-with-most-water.mdx","route":"/two-pointers/container-with-most-water"},{"badge":"Medium","description":"Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0","icon":"dot","kind":"page","label":"3Sum","pageId":"filesystem:(01-linear-structures)/two-pointers/15_3sum.mdx","route":"/two-pointers/3sum"},{"badge":"Hard","description":"Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining","icon":"dot","kind":"page","label":"Trapping Rain Water","pageId":"filesystem:(01-linear-structures)/two-pointers/42_trapping-rain-water.mdx","route":"/two-pointers/trapping-rain-water"},{"badge":"Medium","description":"Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same","icon":"dot","kind":"page","label":"Remove Duplicates from Sorted Array II","pageId":"filesystem:(01-linear-structures)/two-pointers/80_remove-duplicates-from-sorted-array-ii.mdx","route":"/two-pointers/remove-duplicates-from-sorted-array-ii"},{"badge":"Easy","description":"A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers","icon":"dot","kind":"page","label":"Valid Palindrome","pageId":"filesystem:(01-linear-structures)/two-pointers/125_valid-palindrome.mdx","route":"/two-pointers/valid-palindrome"},{"badge":"Medium","description":"Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length","icon":"dot","kind":"page","label":"Two Sum II - Input Array Is Sorted","pageId":"filesystem:(01-linear-structures)/two-pointers/167_two-sum-ii-input-array-is-sorted.mdx","route":"/two-pointers/two-sum-ii-input-array-is-sorted"},{"badge":"Medium","description":"Given an integer array nums, rotate the array to the right by k steps, where k is non-negative","icon":"dot","kind":"page","label":"Rotate Array","pageId":"filesystem:(01-linear-structures)/two-pointers/189_rotate-array.mdx","route":"/two-pointers/rotate-array"},{"badge":"Easy","description":"Write a function that reverses a string. The input string is given as an array of characters s","icon":"dot","kind":"page","label":"Reverse String","pageId":"filesystem:(01-linear-structures)/two-pointers/344_reverse-string.mdx","route":"/two-pointers/reverse-string"},{"badge":"Easy","description":"Given two strings s and t, return true if s is a subsequence of t, or false otherwise","icon":"dot","kind":"page","label":"Is Subsequence","pageId":"filesystem:(01-linear-structures)/two-pointers/392_is-subsequence.mdx","route":"/two-pointers/is-subsequence"},{"badge":"Easy","description":"Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order","icon":"dot","kind":"page","label":"Squares of a Sorted Array","pageId":"filesystem:(01-linear-structures)/two-pointers/977_squares-of-a-sorted-array.mdx","route":"/two-pointers/squares-of-a-sorted-array"}],"display":"group","icon":"arrow-left-right","kind":"group","label":"Two Pointers","path":"/two-pointers","route":"/two-pointers"},{"children":[{"badge":"Medium","description":"Given a string s, find the length of the longest substring without duplicate characters","icon":"dot","kind":"page","label":"Longest Substring Without Repeating Characters","pageId":"filesystem:(01-linear-structures)/sliding-window/3_longest-substring-without-repeating-characters.mdx","route":"/sliding-window/longest-substring-without-repeating-characters"},{"badge":"Hard","description":"Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string \"\"","icon":"dot","kind":"page","label":"Minimum Window Substring","pageId":"filesystem:(01-linear-structures)/sliding-window/76_minimum-window-substring.mdx","route":"/sliding-window/minimum-window-substring"},{"badge":"Easy","description":"You are given an array prices where prices[i] is the price of a given stock on the i^th day","icon":"dot","kind":"page","label":"Best Time to Buy and Sell Stock","pageId":"filesystem:(01-linear-structures)/sliding-window/121_best-time-to-buy-and-sell-stock.mdx","route":"/sliding-window/best-time-to-buy-and-sell-stock"},{"badge":"Medium","description":"Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead","icon":"dot","kind":"page","label":"Minimum Size Subarray Sum","pageId":"filesystem:(01-linear-structures)/sliding-window/209_minimum-size-subarray-sum.mdx","route":"/sliding-window/minimum-size-subarray-sum"},{"badge":"Medium","description":"You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times","icon":"dot","kind":"page","label":"Longest Repeating Character Replacement","pageId":"filesystem:(01-linear-structures)/sliding-window/424_longest-repeating-character-replacement.mdx","route":"/sliding-window/longest-repeating-character-replacement"},{"badge":"Medium","description":"Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise","icon":"dot","kind":"page","label":"Permutation in String","pageId":"filesystem:(01-linear-structures)/sliding-window/567_permutation-in-string.mdx","route":"/sliding-window/permutation-in-string"},{"badge":"Easy","description":"You are given an integer array nums consisting of n elements, and an integer k","icon":"dot","kind":"page","label":"Maximum Average Subarray I","pageId":"filesystem:(01-linear-structures)/sliding-window/643_maximum-average-subarray-i.mdx","route":"/sliding-window/maximum-average-subarray-i"},{"badge":"Medium","description":"Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's","icon":"dot","kind":"page","label":"Max Consecutive Ones III","pageId":"filesystem:(01-linear-structures)/sliding-window/1004_max-consecutive-ones-iii.mdx","route":"/sliding-window/max-consecutive-ones-iii"}],"display":"group","icon":"app-window","kind":"group","label":"Sliding Window","path":"/sliding-window","route":"/sliding-window"},{"children":[{"badge":"Hard","description":"Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays","icon":"dot","kind":"page","label":"Median of Two Sorted Arrays","pageId":"filesystem:(01-linear-structures)/binary-search/4_median-of-two-sorted-arrays.mdx","route":"/binary-search/median-of-two-sorted-arrays"},{"badge":"Medium","description":"There is an integer array nums sorted in ascending order (with distinct values)","icon":"dot","kind":"page","label":"Search in Rotated Sorted Array","pageId":"filesystem:(01-linear-structures)/binary-search/33_search-in-rotated-sorted-array.mdx","route":"/binary-search/search-in-rotated-sorted-array"},{"badge":"Medium","description":"Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value","icon":"dot","kind":"page","label":"Find First and Last Position of Element in Sorted Array","pageId":"filesystem:(01-linear-structures)/binary-search/34_find-first-and-last-position-of-element-in-sorted-array.mdx","route":"/binary-search/find-first-and-last-position-of-element-in-sorted-array"},{"badge":"Easy","description":"Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order","icon":"dot","kind":"page","label":"Search Insert Position","pageId":"filesystem:(01-linear-structures)/binary-search/35_search-insert-position.mdx","route":"/binary-search/search-insert-position"},{"badge":"Medium","description":"You are given an m x n integer matrix matrix with the following two properties:","icon":"dot","kind":"page","label":"Search a 2D Matrix","pageId":"filesystem:(01-linear-structures)/binary-search/74_search-a-2d-matrix.mdx","route":"/binary-search/search-a-2d-matrix"},{"badge":"Medium","description":"Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:","icon":"dot","kind":"page","label":"Find Minimum in Rotated Sorted Array","pageId":"filesystem:(01-linear-structures)/binary-search/153_find-minimum-in-rotated-sorted-array.mdx","route":"/binary-search/find-minimum-in-rotated-sorted-array"},{"badge":"Medium","description":"A peak element is an element that is strictly greater than its neighbors","icon":"dot","kind":"page","label":"Find Peak Element","pageId":"filesystem:(01-linear-structures)/binary-search/162_find-peak-element.mdx","route":"/binary-search/find-peak-element"},{"badge":"Easy","description":"Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1","icon":"dot","kind":"page","label":"Binary Search","pageId":"filesystem:(01-linear-structures)/binary-search/704_binary-search.mdx","route":"/binary-search/binary-search"},{"badge":"Medium","description":"Koko loves to eat bananas. There are n piles of bananas, the i^th pile has piles[i] bananas. The guards have gone and will come back in h hours","icon":"dot","kind":"page","label":"Koko Eating Bananas","pageId":"filesystem:(01-linear-structures)/binary-search/875_koko-eating-bananas.mdx","route":"/binary-search/koko-eating-bananas"}],"display":"group","icon":"search","kind":"group","label":"Binary Search","path":"/binary-search","route":"/binary-search"},{"children":[{"badge":"Easy","description":"Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid","icon":"dot","kind":"page","label":"Valid Parentheses","pageId":"filesystem:(01-linear-structures)/stack/20_valid-parentheses.mdx","route":"/stack/valid-parentheses"},{"badge":"Medium","description":"You are given an absolute path for a Unix-style file system, which always begins with a slash '/'. Your task is to transform this absolute path into its simplified canonical path","icon":"dot","kind":"page","label":"Simplify Path","pageId":"filesystem:(01-linear-structures)/stack/71_simplify-path.mdx","route":"/stack/simplify-path"},{"badge":"Medium","description":"You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation","icon":"dot","kind":"page","label":"Evaluate Reverse Polish Notation","pageId":"filesystem:(01-linear-structures)/stack/150_evaluate-reverse-polish-notation.mdx","route":"/stack/evaluate-reverse-polish-notation"},{"badge":"Medium","description":"Design a stack that supports push, pop, top, and retrieving the minimum element in constant time","icon":"dot","kind":"page","label":"Min Stack","pageId":"filesystem:(01-linear-structures)/stack/155_min-stack.mdx","route":"/stack/min-stack"},{"badge":"Hard","description":"Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation","icon":"dot","kind":"page","label":"Basic Calculator","pageId":"filesystem:(01-linear-structures)/stack/224_basic-calculator.mdx","route":"/stack/basic-calculator"}],"display":"group","icon":"layers","kind":"group","label":"Stack","path":"/stack","route":"/stack"},{"children":[{"badge":"Hard","description":"Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram","icon":"dot","kind":"page","label":"Largest Rectangle in Histogram","pageId":"filesystem:(01-linear-structures)/monotonic-stack/84_largest-rectangle-in-histogram.mdx","route":"/monotonic-stack/largest-rectangle-in-histogram"},{"badge":"Easy","description":"The next greater element of some element x in an array is the first greater element that is to the right of x in the same array","icon":"dot","kind":"page","label":"Next Greater Element I","pageId":"filesystem:(01-linear-structures)/monotonic-stack/496_next-greater-element-i.mdx","route":"/monotonic-stack/next-greater-element-i"},{"badge":"Medium","description":"Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the i^th day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead","icon":"dot","kind":"page","label":"Daily Temperatures","pageId":"filesystem:(01-linear-structures)/monotonic-stack/739_daily-temperatures.mdx","route":"/monotonic-stack/daily-temperatures"},{"badge":"Medium","description":"There are n cars at given miles away from the starting mile 0, traveling to reach the mile target","icon":"dot","kind":"page","label":"Car Fleet","pageId":"filesystem:(01-linear-structures)/monotonic-stack/853_car-fleet.mdx","route":"/monotonic-stack/car-fleet"},{"badge":"Medium","description":"Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day","icon":"dot","kind":"page","label":"Online Stock Span","pageId":"filesystem:(01-linear-structures)/monotonic-stack/901_online-stock-span.mdx","route":"/monotonic-stack/online-stock-span"}],"display":"group","icon":"trending-up","kind":"group","label":"Monotonic Stack","path":"/monotonic-stack","route":"/monotonic-stack"}],"display":"flat","kind":"group","label":"Linear Structures","path":"/"},{"children":[{"children":[{"badge":"Medium","description":"Given the head of a linked list, remove the n^th node from the end of the list and return its head","icon":"dot","kind":"page","label":"Remove Nth Node From End of List","pageId":"filesystem:(02-nodal-grid)/linked-list/19_remove-nth-node-from-end-of-list.mdx","route":"/linked-list/remove-nth-node-from-end-of-list"},{"badge":"Easy","description":"You are given the heads of two sorted linked lists list1 and list2","icon":"dot","kind":"page","label":"Merge Two Sorted Lists","pageId":"filesystem:(02-nodal-grid)/linked-list/21_merge-two-sorted-lists.mdx","route":"/linked-list/merge-two-sorted-lists"},{"badge":"Medium","description":"You are given the head of a sorted linked list","icon":"dot","kind":"page","label":"Remove Duplicates from Sorted List II","pageId":"filesystem:(02-nodal-grid)/linked-list/82_remove-duplicates-from-sorted-list-ii.mdx","route":"/linked-list/remove-duplicates-from-sorted-list-ii"},{"badge":"Medium","description":"Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list","icon":"dot","kind":"page","label":"Reverse Linked List II","pageId":"filesystem:(02-nodal-grid)/linked-list/92_reverse-linked-list-ii.mdx","route":"/linked-list/reverse-linked-list-ii"},{"badge":"Easy","description":"Given head, the head of a linked list, determine if the linked list has a cycle in it","icon":"dot","kind":"page","label":"Linked List Cycle","pageId":"filesystem:(02-nodal-grid)/linked-list/141_linked-list-cycle.mdx","route":"/linked-list/linked-list-cycle"},{"badge":"Medium","description":"You are given the head of a singly linked-list. The list can be represented as:","icon":"dot","kind":"page","label":"Reorder List","pageId":"filesystem:(02-nodal-grid)/linked-list/143_reorder-list.mdx","route":"/linked-list/reorder-list"},{"badge":"Easy","description":"Given the head of a singly linked list, reverse the list, and return the reversed list","icon":"dot","kind":"page","label":"Reverse Linked List","pageId":"filesystem:(02-nodal-grid)/linked-list/206_reverse-linked-list.mdx","route":"/linked-list/reverse-linked-list"},{"badge":"Easy","description":"Given the head of a singly linked list, return the middle node of the linked list","icon":"dot","kind":"page","label":"Middle of the Linked List","pageId":"filesystem:(02-nodal-grid)/linked-list/876_middle-of-the-linked-list.mdx","route":"/linked-list/middle-of-the-linked-list"}],"display":"group","icon":"link","kind":"group","label":"Linked List","path":"/linked-list","route":"/linked-list"},{"children":[{"badge":"Medium","description":"You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list","icon":"dot","kind":"page","label":"Add Two Numbers","pageId":"filesystem:(02-nodal-grid)/hybrid-structures/2_add-two-numbers.mdx","route":"/hybrid-structures/add-two-numbers"},{"badge":"Medium","description":"Design a data structure that follows the constraints of a Least Recently Used (LRU) cache","icon":"dot","kind":"page","label":"LRU Cache","pageId":"filesystem:(02-nodal-grid)/hybrid-structures/146_lru-cache.mdx","route":"/hybrid-structures/lru-cache"},{"badge":"Medium","description":"Implement the RandomizedSet class:","icon":"dot","kind":"page","label":"Insert Delete GetRandom O(1)","pageId":"filesystem:(02-nodal-grid)/hybrid-structures/380_insert-delete-getrandom-o1.mdx","route":"/hybrid-structures/insert-delete-getrandom-o1"}],"display":"group","icon":"combine","kind":"group","label":"Hybrid Structures","path":"/hybrid-structures","route":"/hybrid-structures"},{"children":[{"badge":"Medium","description":"Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:","icon":"dot","kind":"page","label":"Valid Sudoku","pageId":"filesystem:(02-nodal-grid)/matrix-index-math/36_valid-sudoku.mdx","route":"/matrix-index-math/valid-sudoku"},{"badge":"Medium","description":"You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise)","icon":"dot","kind":"page","label":"Rotate Image","pageId":"filesystem:(02-nodal-grid)/matrix-index-math/48_rotate-image.mdx","route":"/matrix-index-math/rotate-image"},{"badge":"Medium","description":"Given an m x n matrix, return all elements of the matrix in spiral order","icon":"dot","kind":"page","label":"Spiral Matrix","pageId":"filesystem:(02-nodal-grid)/matrix-index-math/54_spiral-matrix.mdx","route":"/matrix-index-math/spiral-matrix"},{"badge":"Medium","description":"Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's","icon":"dot","kind":"page","label":"Set Matrix Zeroes","pageId":"filesystem:(02-nodal-grid)/matrix-index-math/73_set-matrix-zeroes.mdx","route":"/matrix-index-math/set-matrix-zeroes"}],"display":"group","icon":"grid-3x3","kind":"group","label":"Matrix Index Math","path":"/matrix-index-math","route":"/matrix-index-math"}],"display":"flat","kind":"group","label":"Nodal & Grid","path":"/"},{"children":[{"children":[{"badge":"Easy","description":"Given the root of a binary tree, return its maximum depth","icon":"dot","kind":"page","label":"Maximum Depth of Binary Tree","pageId":"filesystem:(03-hierarchical)/tree-dfs/104_maximum-depth-of-binary-tree.mdx","route":"/tree-dfs/maximum-depth-of-binary-tree"},{"badge":"Easy","description":"Given the root of a binary tree, invert the tree, and return its root","icon":"dot","kind":"page","label":"Invert Binary Tree","pageId":"filesystem:(03-hierarchical)/tree-dfs/226_invert-binary-tree.mdx","route":"/tree-dfs/invert-binary-tree"},{"badge":"Medium","description":"Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree","icon":"dot","kind":"page","label":"Lowest Common Ancestor of a Binary Tree","pageId":"filesystem:(03-hierarchical)/tree-dfs/236_lowest-common-ancestor-of-a-binary-tree.mdx","route":"/tree-dfs/lowest-common-ancestor-of-a-binary-tree"}],"display":"group","icon":"folder-tree","kind":"group","label":"Tree DFS","path":"/tree-dfs","route":"/tree-dfs"},{"children":[{"badge":"Medium","description":"Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level)","icon":"dot","kind":"page","label":"Binary Tree Level Order Traversal","pageId":"filesystem:(03-hierarchical)/tree-bfs/102_binary-tree-level-order-traversal.mdx","route":"/tree-bfs/binary-tree-level-order-traversal"},{"badge":"Medium","description":"Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom","icon":"dot","kind":"page","label":"Binary Tree Right Side View","pageId":"filesystem:(03-hierarchical)/tree-bfs/199_binary-tree-right-side-view.mdx","route":"/tree-bfs/binary-tree-right-side-view"},{"badge":"Medium","description":"Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X","icon":"dot","kind":"page","label":"Count Good Nodes in Binary Tree","pageId":"filesystem:(03-hierarchical)/tree-bfs/1448_count-good-nodes-in-binary-tree.mdx","route":"/tree-bfs/count-good-nodes-in-binary-tree"}],"display":"group","icon":"network","kind":"group","label":"Tree BFS","path":"/tree-bfs","route":"/tree-bfs"},{"children":[{"badge":"Medium","description":"Given the root of a binary tree, determine if it is a valid binary search tree (BST)","icon":"dot","kind":"page","label":"Validate Binary Search Tree","pageId":"filesystem:(03-hierarchical)/binary-search-tree/98_validate-binary-search-tree.mdx","route":"/binary-search-tree/validate-binary-search-tree"},{"badge":"Medium","description":"Given the root of a binary search tree, and an integer k, return the k^th smallest value (1-indexed) of all the values of the nodes in the tree","icon":"dot","kind":"page","label":"Kth Smallest Element in a BST","pageId":"filesystem:(03-hierarchical)/binary-search-tree/230_kth-smallest-element-in-a-bst.mdx","route":"/binary-search-tree/kth-smallest-element-in-a-bst"},{"badge":"Medium","description":"Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST","icon":"dot","kind":"page","label":"Lowest Common Ancestor of a Binary Search Tree","pageId":"filesystem:(03-hierarchical)/binary-search-tree/235_lowest-common-ancestor-of-a-binary-search-tree.mdx","route":"/binary-search-tree/lowest-common-ancestor-of-a-binary-search-tree"}],"display":"group","icon":"git-fork","kind":"group","label":"Binary Search Tree","path":"/binary-search-tree","route":"/binary-search-tree"},{"children":[{"badge":"Hard","description":"You are given an array of k linked-lists lists, each linked-list is sorted in ascending order","icon":"dot","kind":"page","label":"Merge k Sorted Lists","pageId":"filesystem:(03-hierarchical)/heap-priority-queue/23_merge-k-sorted-lists.mdx","route":"/heap-priority-queue/merge-k-sorted-lists"},{"badge":"Medium","description":"Given an integer array nums and an integer k, return the k^th largest element in the array","icon":"dot","kind":"page","label":"Kth Largest Element in an Array","pageId":"filesystem:(03-hierarchical)/heap-priority-queue/215_kth-largest-element-in-an-array.mdx","route":"/heap-priority-queue/kth-largest-element-in-an-array"},{"badge":"Medium","description":"Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0)","icon":"dot","kind":"page","label":"K Closest Points to Origin","pageId":"filesystem:(03-hierarchical)/heap-priority-queue/973_k-closest-points-to-origin.mdx","route":"/heap-priority-queue/k-closest-points-to-origin"}],"display":"group","icon":"triangle","kind":"group","label":"Heap / Priority Queue","path":"/heap-priority-queue","route":"/heap-priority-queue"},{"children":[{"badge":"Medium","description":"A trie (pronounced as \"try\") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker","icon":"dot","kind":"page","label":"Implement Trie (Prefix Tree)","pageId":"filesystem:(03-hierarchical)/trie/208_implement-trie-prefix-tree.mdx","route":"/trie/implement-trie-prefix-tree"},{"badge":"Medium","description":"Design a data structure that supports adding new words and finding if a string matches any previously added string","icon":"dot","kind":"page","label":"Design Add and Search Words Data Structure","pageId":"filesystem:(03-hierarchical)/trie/211_design-add-and-search-words-data-structure.mdx","route":"/trie/design-add-and-search-words-data-structure"}],"display":"group","icon":"spell-check","kind":"group","label":"Trie","path":"/trie","route":"/trie"}],"display":"flat","kind":"group","label":"Hierarchical","path":"/"},{"children":[{"children":[{"badge":"Medium","description":"Given a reference of a node in a connected undirected graph","icon":"dot","kind":"page","label":"Clone Graph","pageId":"filesystem:(04-relational)/graph-dfs/133_clone-graph.mdx","route":"/graph-dfs/clone-graph"},{"badge":"Medium","description":"Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands","icon":"dot","kind":"page","label":"Number of Islands","pageId":"filesystem:(04-relational)/graph-dfs/200_number-of-islands.mdx","route":"/graph-dfs/number-of-islands"},{"badge":"Medium","description":"There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges","icon":"dot","kind":"page","label":"Pacific Atlantic Water Flow","pageId":"filesystem:(04-relational)/graph-dfs/417_pacific-atlantic-water-flow.mdx","route":"/graph-dfs/pacific-atlantic-water-flow"}],"display":"group","icon":"waypoints","kind":"group","label":"Graph DFS","path":"/graph-dfs","route":"/graph-dfs"},{"children":[{"badge":"Hard","description":"A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:","icon":"dot","kind":"page","label":"Word Ladder","pageId":"filesystem:(04-relational)/graph-bfs/127_word-ladder.mdx","route":"/graph-bfs/word-ladder"},{"badge":"Medium","description":"You are given an m x n grid where each cell can have one of three values:","icon":"dot","kind":"page","label":"Rotting Oranges","pageId":"filesystem:(04-relational)/graph-bfs/994_rotting-oranges.mdx","route":"/graph-bfs/rotting-oranges"}],"display":"group","icon":"radar","kind":"group","label":"Graph BFS","path":"/graph-bfs","route":"/graph-bfs"},{"children":[{"badge":"Medium","description":"There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai","icon":"dot","kind":"page","label":"Course Schedule","pageId":"filesystem:(04-relational)/topological-sort/207_course-schedule.mdx","route":"/topological-sort/course-schedule"},{"badge":"Medium","description":"There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai","icon":"dot","kind":"page","label":"Course Schedule II","pageId":"filesystem:(04-relational)/topological-sort/210_course-schedule-ii.mdx","route":"/topological-sort/course-schedule-ii"}],"display":"group","icon":"workflow","kind":"group","label":"Topological Sort","path":"/topological-sort","route":"/topological-sort"},{"children":[{"badge":"Medium","description":"You have a graph of n nodes. You are given an integer n and an array edges where edges[i] = [ai, bi] indicates that there is an edge between ai and bi in the graph","icon":"dot","kind":"page","label":"Number of Connected Components in an Undirected Graph","pageId":"filesystem:(04-relational)/union-find/323_number-of-connected-components-in-an-undirected-graph.mdx","route":"/union-find/number-of-connected-components-in-an-undirected-graph"},{"badge":"Medium","description":"In this problem, a tree is an undirected graph that is connected and has no cycles","icon":"dot","kind":"page","label":"Redundant Connection","pageId":"filesystem:(04-relational)/union-find/684_redundant-connection.mdx","route":"/union-find/redundant-connection"}],"display":"group","icon":"merge","kind":"group","label":"Union-Find","path":"/union-find","route":"/union-find"}],"display":"flat","kind":"group","label":"Relational","path":"/"},{"children":[{"children":[{"badge":"Medium","description":"Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order","icon":"dot","kind":"page","label":"Combination Sum","pageId":"filesystem:(05-decision-space)/backtracking/39_combination-sum.mdx","route":"/backtracking/combination-sum"},{"badge":"Medium","description":"Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order","icon":"dot","kind":"page","label":"Permutations","pageId":"filesystem:(05-decision-space)/backtracking/46_permutations.mdx","route":"/backtracking/permutations"},{"badge":"Medium","description":"Given an integer array nums of unique elements, return all possible subsets (the power set)","icon":"dot","kind":"page","label":"Subsets","pageId":"filesystem:(05-decision-space)/backtracking/78_subsets.mdx","route":"/backtracking/subsets"}],"display":"group","icon":"undo-2","kind":"group","label":"Backtracking","path":"/backtracking","route":"/backtracking"},{"children":[{"badge":"Easy","description":"You are climbing a staircase. It takes n steps to reach the top","icon":"dot","kind":"page","label":"Climbing Stairs","pageId":"filesystem:(05-decision-space)/1d-dynamic-programming/70_climbing-stairs.mdx","route":"/1d-dynamic-programming/climbing-stairs"},{"badge":"Medium","description":"You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night","icon":"dot","kind":"page","label":"House Robber","pageId":"filesystem:(05-decision-space)/1d-dynamic-programming/198_house-robber.mdx","route":"/1d-dynamic-programming/house-robber"},{"badge":"Medium","description":"You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money","icon":"dot","kind":"page","label":"Coin Change","pageId":"filesystem:(05-decision-space)/1d-dynamic-programming/322_coin-change.mdx","route":"/1d-dynamic-programming/coin-change"}],"display":"group","icon":"columns-3","kind":"group","label":"1-D Dynamic Programming","path":"/1d-dynamic-programming","route":"/1d-dynamic-programming"},{"children":[{"badge":"Medium","description":"There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time","icon":"dot","kind":"page","label":"Unique Paths","pageId":"filesystem:(05-decision-space)/multi-d-grid-dp/62_unique-paths.mdx","route":"/multi-d-grid-dp/unique-paths"},{"badge":"Medium","description":"Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path","icon":"dot","kind":"page","label":"Minimum Path Sum","pageId":"filesystem:(05-decision-space)/multi-d-grid-dp/64_minimum-path-sum.mdx","route":"/multi-d-grid-dp/minimum-path-sum"},{"badge":"Medium","description":"Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0","icon":"dot","kind":"page","label":"Longest Common Subsequence","pageId":"filesystem:(05-decision-space)/multi-d-grid-dp/1143_longest-common-subsequence.mdx","route":"/multi-d-grid-dp/longest-common-subsequence"}],"display":"group","icon":"table","kind":"group","label":"Multi-D / Grid DP","path":"/multi-d-grid-dp","route":"/multi-d-grid-dp"},{"children":[{"badge":"Medium","description":"Given an integer array nums, find the subarray with the largest sum, and return its sum","icon":"dot","kind":"page","label":"Maximum Subarray","pageId":"filesystem:(05-decision-space)/greedy/53_maximum-subarray.mdx","route":"/greedy/maximum-subarray"},{"badge":"Medium","description":"You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position","icon":"dot","kind":"page","label":"Jump Game","pageId":"filesystem:(05-decision-space)/greedy/55_jump-game.mdx","route":"/greedy/jump-game"},{"badge":"Medium","description":"There are n gas stations along a circular route, where the amount of gas at the i^th station is gas[i]","icon":"dot","kind":"page","label":"Gas Station","pageId":"filesystem:(05-decision-space)/greedy/134_gas-station.mdx","route":"/greedy/gas-station"}],"display":"group","icon":"coins","kind":"group","label":"Greedy","path":"/greedy","route":"/greedy"},{"children":[{"badge":"Medium","description":"Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input","icon":"dot","kind":"page","label":"Merge Intervals","pageId":"filesystem:(05-decision-space)/intervals/56_merge-intervals.mdx","route":"/intervals/merge-intervals"},{"badge":"Medium","description":"You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the i^th interval and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval","icon":"dot","kind":"page","label":"Insert Interval","pageId":"filesystem:(05-decision-space)/intervals/57_insert-interval.mdx","route":"/intervals/insert-interval"},{"badge":"Medium","description":"Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping","icon":"dot","kind":"page","label":"Non-overlapping Intervals","pageId":"filesystem:(05-decision-space)/intervals/435_non-overlapping-intervals.mdx","route":"/intervals/non-overlapping-intervals"}],"display":"group","icon":"calendar-range","kind":"group","label":"Intervals","path":"/intervals","route":"/intervals"}],"display":"flat","kind":"group","label":"Decision Space","path":"/"}],"tabs":[{"label":"Changelog","path":"/changelog"},{"label":"Progress","path":"/progress"},{"label":"Appendix","path":"/appendix"}]},"pages":{"/":"---\ntitle: Introduction\ndescription: Structured dependency spine based roadmap for solving DSA Problems on leetcode.\nicon: play\n---\n\n## 1. Linear Structures\n\n:::tip\nData is read in one direction, from the first item to the last. The current position is recorded at each step. All later phases use this skill.\n:::\n\n**[Hash Table](/hash-table)**\n\nA key maps to a value in constant time, which removes the need for a full scan.\n\n**[Prefix Sum](/prefix-sum)**\n\nA running total is stored at each index, so the sum of any range is one subtraction.\n\n**[Two Pointers](/two-pointers)**\n\nTwo indices move through the data from different positions and meet at the answer.\n\n**[Sliding Window](/sliding-window)**\n\nA range with a start and an end moves across the data, and the contents are updated at each step.\n\n**[Binary Search](/binary-search)**\n\nHalf of a sorted range is removed at each step until the target is found.\n\n**[Stack](/stack)**\n\nItems are added and removed from one end, so the last item added is the first item removed.\n\n**[Monotonic Stack](/monotonic-stack)**\n\nA stack keeps its items in sorted order, which finds the next larger or smaller item in one pass.\n\n## 2. Nodal & Grid\n\n:::tip\nThe data is in separate parts. Each part points to the next part, or sits next to other parts on a flat surface. An index does not find the next item. The pointers or the neighbors give the path instead.\n:::\n\n**[Linked List](/linked-list)**\n\nEach node points to the next node, and the pointers give the path through the data.\n\n**[Hybrid Structures](/hybrid-structures)**\n\nTwo structures are combined, such as a hash map and a linked list, to get the best properties of each.\n\n**[Matrix Index Math](/matrix-index-math)**\n\nA two-dimensional grid is read as a flat array through a formula, and the neighbors are found by arithmetic.\n\n## 3. Hierarchical\n\n:::tip\nEach part points to one or more parts below it. Those parts do the same. The method that solves a small part also solves the large part. This is recursion.\n:::\n\n**[Tree DFS](/tree-dfs)**\n\nEach branch is followed to its end before the next branch is examined.\n\n**[Tree BFS](/tree-bfs)**\n\nAll nodes at one level are visited before the nodes at the next level.\n\n**[Binary Search Tree](/binary-search-tree)**\n\nEach node holds a value that is larger than its left subtree and smaller than its right subtree.\n\n**[Heap](/heap-priority-queue)**\n\nThe largest or smallest item is always at the top, and it is removed in logarithmic time.\n\n**[Trie](/trie)**\n\nEach node holds one character, and a path from the root spells a word.\n\n## 4. Relational\n\n:::tip\nEach part can point to any other part. Loops can occur. The method must visit each part one time only. The order of the parts, or the distance between them, is not given in the data. The method finds it.\n:::\n\n**[Graph DFS](/graph-dfs)**\n\nEach edge is followed to its end, and a visited set prevents a repeat visit.\n\n**[Graph BFS](/graph-bfs)**\n\nNodes are visited in order of distance from the start, which gives the shortest path in an unweighted graph.\n\n**[Topological Sort](/topological-sort)**\n\nNodes are put in an order where each node comes after all nodes that point to it.\n\n**[Union-Find](/union-find)**\n\nTwo sets are merged and the set of any item is found in near-constant time.\n\n## 5. Decision Space\n\n:::tip\nThe data has no structure. The method makes one. Each choice makes a new branch. The problem sets the correct method: examine all branches, keep a record of the results already found, or make one choice and do not go back.\n:::\n\n**[Backtracking](/backtracking)**\n\nEach choice makes a branch, and a branch is undone when it does not give a valid result.\n\n**[1-D DP](/1d-dynamic-programming)**\n\nThe result for each state is stored in a list, and each new result is computed from the results before it.\n\n**[Multi-D / Grid DP](/multi-d-grid-dp)**\n\nThe result for each state is stored in a table with two or more dimensions, and each cell is computed from its neighbors.\n\n**[Greedy](/greedy)**\n\nThe best local choice is made at each step and is not changed later.\n\n**[Intervals](/intervals)**\n\nRanges with a start and an end are sorted, and then merged, counted, or scheduled.\n","/1d-dynamic-programming":"---\ntitle: 1-D Dynamic Programming\ndescription: 'The result for each state is stored in a list, and each new result is computed from the results before it.'\nhidden: true\n---\n","/1d-dynamic-programming/climbing-stairs":"---\ntitle: '70. Climbing Stairs'\ndescription: You are climbing a staircase. It takes n steps to reach the top\nicon: dot\ntopics:\n - { name: \"Math\", slug: \"math\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\n - { name: \"Memoization\", slug: \"memoization\" }\nissue: \"https://github.com/prdlk/leetcode/issues/157\"\ncompanies:\n - { name: \"Infosys\", period: \"Thirty Days\", frequency: 80.2 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Intuit\", period: \"Thirty Days\", frequency: 66.1 }\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 65.6 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 36.7 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 48.3 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"Deloitte\", period: \"Six Months\", frequency: 81 }\nsidebar:\n label: 'Climbing Stairs'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `n = 2`\n- Output: `2`\n- Explanation: There are two ways to climb to the top. `1`. `1` step + `1` step `2`. `2` steps\n\n### Example 2:\n- Input: `n = 3`\n- Output: `3`\n- Explanation: There are three ways to climb to the top. `1`. `1` step + `1` step + `1` step `2`. `1` step + `2` steps `3`. `2` steps + `1` step\n\n### Constraints:\n\n- `1 <= n <= 45`\n\n## Solution\n\n```py\nclass Solution:\n def climbStairs(self, n: int) -> int:\n if n == 1:\n return 1\n if n == 2:\n return 2\n\n first, second = 1, 2\n\n for _ in range(3, n + 1):\n third = first + second\n first, second = second, third\n\n return second\n```\n","/1d-dynamic-programming/coin-change":"---\ntitle: '322. Coin Change'\ndescription: You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Knapsack Problem\", slug: \"knapsack-problem\" }\n - { name: \"Complete Knapsack\", slug: \"complete-knapsack\" }\nissue: \"https://github.com/prdlk/leetcode/issues/159\"\ncompanies:\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 59.7 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 48.7 }\n - { name: \"Intuit\", period: \"Six Months\", frequency: 71.5 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 67.9 }\n - { name: \"Pinterest\", period: \"Six Months\", frequency: 64.4 }\nsidebar:\n label: 'Coin Change'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `coins = [1,2,5], amount = 11`\n- Output: `3`\n- Explanation: `11 = 5 + 5 + 1`\n\n### Example 2:\n- Input: `coins = [2], amount = 3`\n- Output: `-1`\n\n### Example 3:\n- Input: `coins = [1], amount = 0`\n- Output: `0`\n\n### Constraints:\n\n- `1 <= coins.length <= 12`\n- `1 <= coins[i] <= 2^31 - 1`\n- `0 <= amount <= 10^4`\n\n## Solution\n\n```py\nfrom math import inf\n\n\nclass Solution:\n def coinChange(self, coins: list[int], amount: int) -> int:\n n = len(coins)\n memo = [[-1] * (amount + 1) for _ in range(n + 1)]\n\n def min_coins(coins, i, amount, memo):\n if amount == 0:\n return 0\n if i == 0 or amount < 0:\n return inf\n if memo[i][amount] != -1:\n return memo[i][amount]\n\n skip = min_coins(coins, i - 1, amount, memo)\n use = min_coins(coins, i, amount - coins[i - 1], memo)\n if use != inf:\n use += 1\n\n memo[i][amount] = min(skip, use)\n return memo[i][amount]\n\n result = min_coins(coins, n, amount, memo)\n return result if result != inf else -1 # pyright: ignore[reportReturnType]\n```\n","/1d-dynamic-programming/house-robber":"---\ntitle: '198. House Robber'\ndescription: You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\nissue: \"https://github.com/prdlk/leetcode/issues/158\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 83.1 }\n - { name: \"Infosys\", period: \"Thirty Days\", frequency: 68.6 }\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 65.6 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 50.2 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 53.2 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Freecharge\", period: \"Six Months\", frequency: 100 }\n - { name: \"Agoda\", period: \"Six Months\", frequency: 65 }\nsidebar:\n label: 'House Robber'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [1,2,3,1]`\n- Output: `4`\n- Explanation: Rob house `1` (money = `1`) and then rob house `3` (money = `3`). Total amount you can rob = `1 + 3 = 4`.\n\n### Example 2:\n- Input: `nums = [2,7,9,3,1]`\n- Output: `12`\n- Explanation: Rob house `1` (money = `2`), rob house `3` (money = `9`) and rob house `5` (money = `1`). Total amount you can rob = `2 + 9 + 1 = 12`.\n\n### Constraints:\n\n- `1 <= nums.length <= 100`\n- `0 <= nums[i] <= 400`\n\n## Solution\n\n```py\nclass Solution:\n def rob(self, nums: list[int]) -> int:\n n = len(nums)\n if n == 0:\n return 0\n if n <= 2:\n return max(nums)\n\n dp = [0] * n\n dp[0] = nums[0]\n dp[1] = max(nums[0], nums[1])\n for i in range(2, n):\n dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])\n return dp[-1]\n```\n","/backtracking":"---\ntitle: Backtracking\ndescription: 'Backtracking is a depth-first search technique that builds a solution step by step and abandons (undoes) a partial choice as soon as it can’t lead to a valid complete solution.'\nhidden: true\n---\n\n## What it is\n\nBacktracking explores possible paths to a solution one step at a time. When a path reaches a dead end, you undo the last step and try a different option. Think of every problem as a decision tree. The root is an empty answer. Each level is a decision. Each branch is one option.\n\n## When to use it\n\nUse backtracking when you must generate **all** valid solutions or evaluate **all** combinations. Do not use it when you need only one solution or the optimal solution. For those, use greedy or dynamic programming. Backtracking has exponential time complexity.\n\n## The four components\n\nEvery backtracking problem has the same parts:\n\n1. **Base case** — when to save the current path and stop.\n2. **Choices** — the options at each step.\n3. **Constraints** — the rules that reject an invalid path early.\n4. **Backtrack step** — undo the last choice and try the next one.\n\n## The template\n\n```python\ndef solve(data):\n result = []\n path = []\n\n def backtrack(index):\n # 1. Base case\n if is_complete(index, path):\n result.append(path[:]) # copy — lists are passed by reference\n return\n\n # 2. Choices\n for choice in get_choices(index, data):\n # 3. Constraints\n if not is_valid(choice, path):\n continue\n\n path.append(choice) # make the choice\n backtrack(index + 1) # explore\n path.pop() # 4. Backtrack\n\n backtrack(0)\n return result\n```\n\n## Steps to apply the template\n\n1. Draw the decision tree for a small input. Name each level and each branch.\n2. Write the base case. Ask: when is the path complete?\n3. Write the choices. Ask: what can I add at this level?\n4. Write the constraints. Ask: what makes a path invalid? Reject it before recursion.\n5. Add the backtrack step. Pop what you appended.\n6. Copy the path when you save it. Never append the same list reference.\n\n## Four problems, one template\n\n### Subsets\n\nChoice: include or skip each number. No constraint. Base case: index equals list length.\n\n```python\ndef subsets(nums):\n result, path = [], []\n\n def backtrack(i):\n if i == len(nums):\n result.append(path[:])\n return\n path.append(nums[i]) # include\n backtrack(i + 1)\n path.pop()\n backtrack(i + 1) # skip\n\n backtrack(0)\n return result\n```\n\n### Permutations\n\nSame choices as subsets. Constraint: do not reuse a number already in the path. Base case: path length equals list length.\n\n```python\ndef permute(nums):\n result, path = [], []\n used = [False] * len(nums)\n\n def backtrack():\n if len(path) == len(nums):\n result.append(path[:])\n return\n for i in range(len(nums)):\n if used[i]:\n continue\n used[i] = True\n path.append(nums[i])\n backtrack()\n path.pop()\n used[i] = False\n\n backtrack()\n return result\n```\n\n### Combinations\n\nSame as subsets. Change one line: save the path only when its length equals `k`.\n\n```python\ndef combine(n, k):\n result, path = [], []\n\n def backtrack(start):\n if len(path) == k:\n result.append(path[:])\n return\n for i in range(start, n + 1):\n path.append(i)\n backtrack(i + 1)\n path.pop()\n\n backtrack(1)\n return result\n```\n\n### N-Queens\n\nChoice: which column to place the queen in the current row. Constraint: no shared column or diagonal. Base case: `n` queens placed. Backtrack: remove the last queen.\n\n```python\ndef solve_n_queens(n):\n result, cols = [], []\n used_cols, diag1, diag2 = set(), set(), set()\n\n def backtrack(row):\n if row == n:\n result.append(cols[:])\n return\n for col in range(n):\n if col in used_cols or row - col in diag1 or row + col in diag2:\n continue\n cols.append(col)\n used_cols.add(col); diag1.add(row - col); diag2.add(row + col)\n backtrack(row + 1)\n cols.pop()\n used_cols.remove(col); diag1.remove(row - col); diag2.remove(row + col)\n\n backtrack(0)\n return result\n```\n\n## Common mistakes\n\n- You append the `path` list itself instead of a copy. The result fills with empty lists.\n- You forget to pop. The path grows and never resets.\n- You check constraints after recursion instead of before. You waste time on dead paths.\n- You use backtracking when you need only one answer. Use greedy or DP instead.\n\n\n","/backtracking/combination-sum":"---\ntitle: '39. Combination Sum'\ndescription: Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Backtracking\", slug: \"backtracking\" }\nissue: \"https://github.com/prdlk/leetcode/issues/150\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Google\", period: \"Three Months\", frequency: 41 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"Amazon\", period: \"Six Months\", frequency: 30.1 }\nsidebar:\n label: 'Combination Sum'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `candidates = [2,3,6,7], target = 7`\n- Output: `[[2,2,3],[7]]`\n- Explanation: `2` and `3` are `candidates`, and `2 + 2 + 3 = 7`. Note that `2` can be used multiple times. `7` is a candidate, and `7 = 7`. These are the only two combinations.\n\n### Example 2:\n- Input: `candidates = [2,3,5], target = 8`\n- Output: `[[2,2,2,2],[2,3,3],[3,5]]`\n\n### Example 3:\n- Input: `candidates = [2], target = 1`\n- Output: `[]`\n\n### Constraints:\n\n- `1 <= candidates.length <= 30`\n- `2 <= candidates[i] <= 40`\n- All elements of `candidates` are distinct.\n- `1 <= target <= 40`\n\n## Solution\n\n```py\nclass Solution:\n def combinationSum(self, candidates: list[int], target: int) -> list[list[int]]:\n candidates.sort()\n result, path = [], []\n\n def backtrack(start, remaining):\n if remaining == 0:\n result.append(path[:])\n return\n for i in range(start, len(candidates)):\n if candidates[i] > remaining:\n break\n\n path.append(candidates[i])\n backtrack(i, remaining - candidates[i])\n path.pop()\n\n backtrack(0, target)\n return result\n```\n","/backtracking/permutations":"---\ntitle: '46. Permutations'\ndescription: Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Backtracking\", slug: \"backtracking\" }\nissue: \"https://github.com/prdlk/leetcode/issues/151\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Google\", period: \"Three Months\", frequency: 32.7 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 67.9 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 54.1 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 43.4 }\nsidebar:\n label: 'Permutations'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [1,2,3]`\n- Output: `[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]`\n\n### Example 2:\n- Input: `nums = [0,1]`\n- Output: `[[0,1],[1,0]]`\n\n### Example 3:\n- Input: `nums = [1]`\n- Output: `[[1]]`\n\n### Constraints:\n\n- `1 <= nums.length <= 6`\n- `-10 <= nums[i] <= 10`\n- All the integers of `nums` are unique.\n\n## Solution\n\n```py\nclass Solution:\n def permute(self, nums: list[int]) -> list[list[int]]:\n result, path = [], []\n used = [False] * len(nums)\n\n def backtrack():\n if len(path) == len(nums):\n result.append(path[:])\n return\n for i in range(len(nums)):\n if used[i]:\n continue\n used[i] = True\n path.append(nums[i])\n backtrack()\n path.pop()\n used[i] = False\n\n backtrack()\n return result\n```\n","/backtracking/subsets":"---\ntitle: '78. Subsets'\ndescription: Given an integer array nums of unique elements, return all possible subsets (the power set)\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Backtracking\", slug: \"backtracking\" }\n - { name: \"Bit Manipulation\", slug: \"bit-manipulation\" }\nissue: \"https://github.com/prdlk/leetcode/issues/149\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 61.3 }\n - { name: \"Agoda\", period: \"Three Months\", frequency: 75.3 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 48.3 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 43.3 }\n - { name: \"Google\", period: \"Three Months\", frequency: 23.9 }\n - { name: \"TikTok\", period: \"Six Months\", frequency: 64.9 }\nsidebar:\n label: 'Subsets'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [1,2,3]`\n- Output: `[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]`\n\n### Example 2:\n- Input: `nums = [0]`\n- Output: `[[],[0]]`\n\n### Constraints:\n\n- `1 <= nums.length <= 10`\n- `-10 <= nums[i] <= 10`\n- All the numbers of `nums` are unique.\n\n## Solution\n\n```py\nclass Solution:\n def subsets(self, nums: list[int]) -> list[list[int]]:\n res = []\n\n def backtrack(i, path):\n res.append(path[:])\n for num_idx in range(i, len(nums)):\n path.append(nums[num_idx])\n backtrack(num_idx + 1, path)\n path.pop()\n\n backtrack(0, [])\n return res\n```\n","/binary-search":"---\ntitle: Binary Search\ndescription: 'Half of a sorted range is removed at each step until the target is found.'\nhidden: true\n---\n\nBinary search is an efficient array search algorithm. It works by narrowing down the search range by half each time. If you have looked up a word in a physical dictionary, you've already used binary search in real life. Let's look at a simple example:\n\n*Given a sorted array of integers and an integer called target, find the element that equals the target and return its index. If the element is not found, return -1.*\n\nThe key observation here is that the array is sorted. We pick a random element in the array and compare it to the target.\n\n- If we happen to pick the element that equals the target (how lucky!), then bingo. We don't need to do any more work; we return its index.\n- If the element is smaller than the target, then we know the target cannot be found in the section to the left of the current element, since everything to the left is even smaller. So, we discard the current element and everything on the left from the search range.\n- If the element is larger than the target, then we know the target cannot be found in the section to the right of the current element, since everything to the right is even larger. So, we discard the current element and everything on the right from the search range.\n\nWe repeat this process until we find the target. Instead of picking a random element, we always pick the **middle** element in the current search range. This way, we can discard half of the options and shrink the search range by half each time. This gives us `O(log(N))` runtime.\n\nThis idea can be implemented both iteratively and recursively. However, the major difference is that the iterative version of binary search uses `O(1)` memory while the recursive version uses `O(log(N))` memory.\n\n## Implementation\n\nThe search range is represented by the `left` and `right` indices that start from both ends of the array and move towards each other as we search. When moving the index, we discard elements and shrink the search range.\n\nTime Complexity: `O(log(n))`\n\n\n\n \n \n \n \n \n \n \n \n \n\n```python\ndef binary_search(arr: list[int], target: int) -> int:\n left, right = 0, len(arr) - 1\n # <= because left and right could point to the same element, < would miss it\n while left <= right:\n # double slash for integer division in python 3,\n # we don't have to worry about integer `left + right` overflow\n # since python integers can be arbitrarily large\n mid = (left + right) // 2\n # found target, return its index\n if arr[mid] == target:\n return mid\n # middle less than target, discard left half by making left search boundary `mid + 1`\n if arr[mid] < target:\n left = mid + 1\n # middle greater than target, discard right half by making right search boundary `mid - 1`\n else:\n right = mid - 1\n return -1 # if we get here we didn't hit above return so we didn't find target\n```\n\n### Calculating `mid`\n\nNote that when calculating `mid`, if the number of elements is even, there are two elements in the middle. We usually follow the convention of picking the first one, equivalent to doing integer division `(left + right) / 2`.\n\nIn most programming languages, we calculate `mid` with `left + floor((right-left) / 2)` to avoid potential integer overflow. However, in Python, we do not need to worry about integer overflow with `left + right` because Python3 integers can be arbitrarily large.\n\n## Deduction\n\nIt's essential to understand and deduce the algorithm yourself instead of memorizing it. In an actual interview, interviewers may ask you additional questions to test your understanding, so simply memorizing the algorithm may not be enough to convince the interviewer.\n\nKey elements in writing a correct binary search:\n\n### 1. When to terminate the loop\n\nMake sure the `while` loop includes an equality comparison; otherwise, we might skip the loop and miss the potential match for the edge case of a one-element array.\n\n### 2. Whether/how to update `left` and `right` boundary in the `if` conditions\n\nConsider which side to discard. If `arr[mid]` is smaller than the `target`, we should discard everything on the left by making `left = mid + 1`.\n\n### 3. Should I discard the current element?\n\nFor vanilla binary search, we can discard it since it cannot be the final answer if it is not equal to the target. There might be situations where you would want to think twice before discarding the current element. We'll discuss this in the next module.\n\n## When to use binary search\n\nInterestingly, binary search works beyond sorted arrays. **You can use binary search whenever you make a binary decision to shrink the search range.**\n","/binary-search-tree":"---\ntitle: Binary Search Tree\ndescription: 'Each node holds a value that is larger than its left subtree and smaller than its right subtree.'\nhidden: true\n---\n","/binary-search-tree/kth-smallest-element-in-a-bst":"---\ntitle: '230. Kth Smallest Element in a BST'\ndescription: Given the root of a binary search tree, and an integer k, return the k^th smallest value (1-indexed) of all the values of the nodes in the tree\nicon: dot\ntopics:\n - { name: \"Tree\", slug: \"tree\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Binary Search Tree\", slug: \"binary-search-tree\" }\n - { name: \"Binary Tree\", slug: \"binary-tree\" }\nvideo: \"5LUXSvjmGCw\"\nissue: \"https://github.com/prdlk/leetcode/issues/112\"\ncompanies:\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 74.3 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 28.9 }\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"Uber\", period: \"Six Months\", frequency: 71.5 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 24.5 }\nsidebar:\n label: 'Kth Smallest Element in a BST'\n badge: 'Medium'\n---\n\n::::warning\nIf the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?\n::::\n\n### Example 1:\n- Input: `root = [3,1,4,null,2], k = 1`\n- Output: `1`\n\n### Example 2:\n- Input: `root = [5,3,6,2,4,null,null,1], k = 3`\n- Output: `3`\n\n### Constraints:\n\n- The number of nodes in the tree is n.\n- `1 <= k <= n <= 10^4`\n- `0 <= Node.val <= 10^4`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"kthSmallest(root, k)\"]) --> I[\"n = 0, stack = [], cur = root\"]\n I --> W{\"cur or stack?\"}\n W -- no --> E([\"fall off the loop — unreachable, k <= n\"])\n W -- yes --> L{\"cur?\"}\n L -- yes --> P[\"stack.append(cur), cur = cur.left — push the whole left spine\"]\n P --> L\n L -- no --> O[\"cur = stack.pop(), n += 1 — nodes pop in ascending order\"]\n O --> K{\"n == k?\"}\n K -- yes --> R([\"return cur.val\"])\n K -- no --> N[\"cur = cur.right\"]\n N --> W\n```\n\n## Solution\n\n```py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:\n n = 0\n stack = []\n cur = root\n\n while cur or stack:\n while cur:\n stack.append(cur)\n cur = cur.left\n\n cur = stack.pop()\n n += 1\n if n == k:\n return cur.val\n cur = cur.right\n```\n","/binary-search-tree/lowest-common-ancestor-of-a-binary-search-tree":"---\ntitle: '235. Lowest Common Ancestor of a Binary Search Tree'\ndescription: Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST\nicon: dot\ntopics:\n - { name: \"Tree\", slug: \"tree\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Binary Search Tree\", slug: \"binary-search-tree\" }\n - { name: \"Binary Tree\", slug: \"binary-tree\" }\n - { name: \"Binary Lifting\", slug: \"binary-lifting\" }\n - { name: \"Lowest Common Ancestor\", slug: \"lowest-common-ancestor\" }\nvideo: \"gs2LMfuOR9k\"\nissue: \"https://github.com/prdlk/leetcode/issues/113\"\ncompanies:\n - { name: \"BlackRock\", period: \"Thirty Days\", frequency: 76.6 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 32 }\n - { name: \"Amazon\", period: \"Six Months\", frequency: 30.1 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\n - { name: \"Google\", period: \"Six Months\", frequency: 21.9 }\nsidebar:\n label: 'Lowest Common Ancestor of a Binary Search Tree'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8`\n- Output: `6`\n- Explanation: The LCA of nodes `2` and `8` is `6`.\n\n### Example 2:\n- Input: `root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4`\n- Output: `2`\n- Explanation: The LCA of nodes `2` and `4` is `2`, since a node can be a descendant of itself according to the LCA definition.\n\n### Example 3:\n- Input: `root = [2,1], p = 2, q = 1`\n- Output: `2`\n\n### Constraints:\n\n- The number of nodes in the tree is in the range [2, 10^5].\n- `-10^9 <= Node.val <= 10^9`\n- All Node.val are unique.\n- `p != q`\n- p and q will exist in the BST.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"lowestCommonAncestor(root, p, q)\"]) --> I[\"cur = root\"]\n I --> W{\"cur?\"}\n W -- no --> E([\"fall off the loop — unreachable, p and q exist in the BST\"])\n W -- yes --> A{\"p.val < cur.val and q.val < cur.val?\"}\n A -- yes --> L[\"cur = cur.left — both sit in the left subtree\"]\n L --> W\n A -- no --> B{\"p.val > cur.val and q.val > cur.val?\"}\n B -- yes --> R[\"cur = cur.right — both sit in the right subtree\"]\n R --> W\n B -- no --> C([\"return cur — the paths to p and q split here\"])\n```\n\n## Solution\n\n```py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.right = None\n\n\nclass Solution:\n def lowestCommonAncestor(\n self, root: \"TreeNode\", p: \"TreeNode\", q: \"TreeNode\"\n ) -> \"TreeNode\":\n cur = root\n while cur:\n if p.val < cur.val and q.val < cur.val:\n cur = cur.left\n elif p.val > cur.val and q.val > cur.val:\n cur = cur.right\n else:\n return cur\n```\n","/binary-search-tree/validate-binary-search-tree":"---\ntitle: '98. Validate Binary Search Tree'\ndescription: Given the root of a binary tree, determine if it is a valid binary search tree (BST)\nicon: dot\ntopics:\n - { name: \"Tree\", slug: \"tree\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Binary Search Tree\", slug: \"binary-search-tree\" }\n - { name: \"Binary Tree\", slug: \"binary-tree\" }\nvideo: \"s6ATEkipzow\"\nissue: \"https://github.com/prdlk/leetcode/issues/111\"\ncompanies:\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 76.7 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 46.8 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 34.6 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\nsidebar:\n label: 'Validate Binary Search Tree'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `root = [2,1,3]`\n- Output: `true`\n\n### Example 2:\n- Input: `root = [5,1,4,null,null,3,6]`\n- Output: `false`\n- Explanation: The `root` node's value is `5` but its right child's value is `4`.\n\n### Constraints:\n\n- The number of nodes in the tree is in the range [1, 10^4].\n- `-2^31 <= Node.val <= 2^31 - 1`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"isValidBST(root)\"]) --> C[\"valid(root, float('-inf'), float('inf')) — the root may hold any value\"]\n C --> E([\"return that result\"])\n subgraph valid[\"valid(node, low, high)\"]\n V1{\"not node?\"}\n V1 -- yes --> V2([\"return True\"])\n V1 -- no --> V3{\"not (low < node.val < high)?\"}\n V3 -- yes --> V4([\"return False — node breaks a bound set by an ancestor\"])\n V3 -- no --> V5[\"valid(node.left, low, node.val) and valid(node.right, node.val, high) — each child tightens one bound\"]\n V5 --> V6([\"return that result\"])\n end\n C --> V1\n```\n\n## Solution\n\n```py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def isValidBST(self, root: Optional[TreeNode]) -> bool:\n def valid(node, low, high):\n if not node:\n return True\n\n if not (low < node.val < high):\n return False\n\n return valid(node.left, low, node.val) and valid(node.right, node.val, high)\n\n return valid(root, float(\"-inf\"), float(\"inf\"))\n```\n","/binary-search/binary-search":"---\ntitle: '704. Binary Search'\ndescription: Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\nvideo: \"s4DPM8ct1pI\"\nissue: \"https://github.com/prdlk/leetcode/issues/58\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 53.2 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 49.8 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 39.1 }\nsidebar:\n label: 'Binary Search'\n badge: 'Easy'\n---\n\n::::warning\nYou must write an algorithm with O(log n) runtime complexity.\n::::\n\n### Example 1:\n- Input: `nums = [-1,0,3,5,9,12], target = 9`\n- Output: `4`\n- Explanation: `9` exists in `nums` and its index is `4`\n\n### Example 2:\n- Input: `nums = [-1,0,3,5,9,12], target = 2`\n- Output: `-1`\n- Explanation: `2` does not exist in `nums` so return `-1`\n\n### Constraints:\n\n- `1 <= nums.length <= 10^4`\n- `-10^4 < nums[i], target < 10^4`\n- All the integers in `nums` are unique.\n- `nums` is sorted in ascending order.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"search(nums, target)\"]) --> I[\"left = 0, right = n - 1\"]\n I --> W{\"left <= right?\"}\n W -- no --> E([\"return -1\"])\n W -- yes --> M[\"mid = left + floor((right - left) / 2)\"]\n M --> Q{\"nums[mid] vs target\"}\n Q -- equal --> R([\"return mid\"])\n Q -- \"nums[mid] < target\" --> L[\"left = mid + 1\"]\n L --> W\n Q -- \"nums[mid] > target\" --> H[\"right = mid - 1\"]\n H --> W\n```\n\n## Solution\n\n```js\n/**\n * @param {number[]} nums\n * @param {number} target\n * @return {number}\n */\nvar search = function(nums, target) {\n let left = 0;\n let right = nums.length - 1;\n\n while(left <= right) {\n const mid = left + Math.floor((right - left) / 2);\n if (nums[mid] === target) {\n return mid;\n } else if(nums[mid] < target){\n left = mid + 1;\n } else if (nums[mid] > target) {\n right = mid - 1;\n }\n }\n return -1;\n};\n```\n","/binary-search/find-first-and-last-position-of-element-in-sorted-array":"---\ntitle: '34. Find First and Last Position of Element in Sorted Array'\ndescription: Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\nissue: \"https://github.com/prdlk/leetcode/issues/65\"\ncompanies:\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 57.2 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 36.7 }\n - { name: \"TikTok\", period: \"Three Months\", frequency: 70.9 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 43.3 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 36.8 }\n - { name: \"Instacart\", period: \"Six Months\", frequency: 100 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 70.2 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 56.2 }\nsidebar:\n label: 'Find First and Last Position of Element in Sorted Array'\n badge: 'Medium'\n---\n\n::::warning\nYou must write an algorithm with O(log n) runtime complexity.\n::::\n\n### Example 1:\n- Input: `nums = [5,7,7,8,8,10], target = 8`\n- Output: `[3,4]`\n\n### Example 2:\n- Input: `nums = [5,7,7,8,8,10], target = 6`\n- Output: `[-1,-1]`\n\n### Example 3:\n- Input: `nums = [], target = 0`\n- Output: `[-1,-1]`\n\n### Constraints:\n\n- `0 <= nums.length <= 10^5`\n- `-10^9 <= nums[i] <= 10^9`\n- `nums` is a non-decreasing array.\n- `-10^9 <= target <= 10^9`\n\n## Solution\n\n```py\nclass Solution:\n def searchRange(self, nums: list[int], target: int) -> list[int]:\n def find(first):\n result = -1\n left, right = 0, len(nums) - 1\n while left <= right:\n mid = (left + right) // 2\n if nums[mid] == target:\n result = mid\n if first:\n right = mid - 1\n else:\n left = mid + 1\n elif nums[mid] < target:\n left = mid + 1\n else:\n right = mid - 1\n return result\n\n return [find(True), find(False)]\n```\n","/binary-search/find-minimum-in-rotated-sorted-array":"---\ntitle: '153. Find Minimum in Rotated Sorted Array'\ndescription: 'Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:'\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\nvideo: \"nIVW4P8b1VA\"\nissue: \"https://github.com/prdlk/leetcode/issues/62\"\ncompanies:\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 65.6 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"tcs\", period: \"Six Months\", frequency: 43.5 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 40.6 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 32.7 }\nsidebar:\n label: 'Find Minimum in Rotated Sorted Array'\n badge: 'Medium'\n---\n\n::::warning\nNotice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].\n::::\n\n### Example 1:\n- Input: `nums = [3,4,5,1,2]`\n- Output: `1`\n- Explanation: The original array was `[1,2,3,4,5]` rotated `3` times.\n\n### Example 2:\n- Input: `nums = [4,5,6,7,0,1,2]`\n- Output: `0`\n- Explanation: The original array was `[0,1,2,4,5,6,7]` and it was rotated `4` times.\n\n### Example 3:\n- Input: `nums = [11,13,15,17]`\n- Output: `11`\n- Explanation: The original array was `[11,13,15,17]` and it was rotated `4` times.\n\n### Constraints:\n\n- `n == nums.length`\n- `1 <= n <= 5000`\n- `-5000 <= nums[i] <= 5000`\n- All the integers of `nums` are unique.\n- `nums` is sorted and rotated between 1 and `n` times.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"findMin(nums)\"]) --> I[\"l = 0, r = n-1, lowest_index = -1\"]\n I --> W{\"l <= r?\"}\n W -- no --> E([\"return nums[lowest_index]\"])\n W -- yes --> M[\"m = (l + r) // 2\"]\n M --> C{\"nums[m] <= nums[-1]?\"}\n C -- yes --> A[\"in the right sorted run: record lowest_index = m, r = m - 1\"]\n A --> W\n C -- no --> B[\"still in the left sorted run: l = m + 1\"]\n B --> W\n```\n\n## Solution\n\n```py\nclass Solution:\n def findMin(self, nums: List[int]) -> int:\n l, r = 0, len(nums) - 1\n lowest_index = -1\n\n while l <= r:\n m = (l + r) // 2\n if nums[m] <= nums[-1]:\n lowest_index = m\n r = m - 1\n else:\n l = m + 1\n\n return nums[lowest_index]\n```\n","/binary-search/find-peak-element":"---\ntitle: '162. Find Peak Element'\ndescription: A peak element is an element that is strictly greater than its neighbors\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\nissue: \"https://github.com/prdlk/leetcode/issues/64\"\ncompanies:\n - { name: \"Infosys\", period: \"Thirty Days\", frequency: 68.6 }\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 67.6 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 54.8 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 36.8 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 34.6 }\n - { name: \"Uber\", period: \"Six Months\", frequency: 67.2 }\nsidebar:\n label: 'Find Peak Element'\n badge: 'Medium'\n---\n\n::::warning\nYou must write an algorithm that runs in O(log n) time.\n::::\n\n### Example 1:\n- Input: `nums = [1,2,3,1]`\n- Output: `2`\n- Explanation: `3` is a peak element and your function should return the index number `2`.\n\n### Example 2:\n- Input: `nums = [1,2,1,3,5,6,4]`\n- Output: `5`\n- Explanation: Your function can return either index number `1` where the peak element is `2`, or index number `5` where the peak element is `6`.\n\n### Constraints:\n\n- `1 <= nums.length <= 1000`\n- `-2^31 <= nums[i] <= 2^31 - 1`\n- `nums[i] != nums[i + 1] for all valid i.`\n\n## Solution\n\n```py\nclass Solution:\n def findPeakElement(self, nums: list[int]) -> int:\n left, right = 0, len(nums) - 1\n while left < right:\n mid = (left + right) // 2\n if nums[mid] < nums[mid + 1]:\n left = mid + 1\n else:\n right = mid\n\n return left\n```\n","/binary-search/koko-eating-bananas":"---\ntitle: '875. Koko Eating Bananas'\ndescription: Koko loves to eat bananas. There are n piles of bananas, the i^th pile has piles[i] bananas. The guards have gone and will come back in h hours\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\nvideo: \"U2SozAs9RzA\"\nissue: \"https://github.com/prdlk/leetcode/issues/59\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 69.6 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Nvidia\", period: \"Three Months\", frequency: 100 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 87.4 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 62.3 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Coupang\", period: \"Six Months\", frequency: 100 }\n - { name: \"Swiggy\", period: \"Six Months\", frequency: 100 }\n - { name: \"Accenture\", period: \"Six Months\", frequency: 75 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 56.9 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 44.8 }\nsidebar:\n label: 'Koko Eating Bananas'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `piles = [3,6,7,11], h = 8`\n- Output: `4`\n\n### Example 2:\n- Input: `piles = [30,11,23,4,20], h = 5`\n- Output: `30`\n\n### Example 3:\n- Input: `piles = [30,11,23,4,20], h = 6`\n- Output: `23`\n\n### Constraints:\n\n- `1 <= piles.length <= 10^4`\n- `piles.length <= h <= 10^9`\n- `1 <= piles[i] <= 10^9`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"minEatingSpeed(piles, h)\"]) --> I[\"l = 1, r = max(piles)\"]\n I --> W{\"l < r?\"}\n W -- no --> E([\"return l — the smallest workable speed\"])\n W -- yes --> M[\"mid = floor((l + r) / 2)\"]\n M --> K[\"kWorks(mid): hours = sum of ceil(p / mid)\"]\n K --> Q{\"hours <= h?\"}\n Q -- yes --> A[\"r = mid — keep mid, try slower\"]\n Q -- no --> B[\"l = mid + 1 — too slow, speed up\"]\n A --> W\n B --> W\n```\n\n## Solution\n\n```js\n/**\n * @param {number[]} piles\n * @param {number} h\n * @return {number}\n */\nvar minEatingSpeed = function(piles, h) {\n // Function to determine if K works for the H\n function kWorks(k){\n let hours = 0;\n for (let p of piles){\n hours += Math.ceil(p/k)\n }\n return hours <= h;\n }\n\n // Setup Binary Search\n let l = 1;\n let r = Math.max(...piles);\n\n // Find the K value which works for H and consumes all bananas\n while (l < r){\n const mid = Math.floor((l + r) / 2);\n if (kWorks(mid)){\n r = mid;\n }else{\n l = mid + 1;\n }\n }\n return l;\n};\n```\n","/binary-search/median-of-two-sorted-arrays":"---\ntitle: '4. Median of Two Sorted Arrays'\ndescription: Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\n - { name: \"Divide and Conquer\", slug: \"divide-and-conquer\" }\nissue: \"https://github.com/prdlk/leetcode/issues/66\"\ncompanies:\n - { name: \"Rippling\", period: \"Thirty Days\", frequency: 93 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 69.6 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 63.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 62 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 57.2 }\n - { name: \"eBay\", period: \"Three Months\", frequency: 82.2 }\n - { name: \"Goldman Sachs\", period: \"Three Months\", frequency: 57 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"Zoho\", period: \"Six Months\", frequency: 79.6 }\n - { name: \"DE Shaw\", period: \"Six Months\", frequency: 74.2 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 48.1 }\nsidebar:\n label: 'Median of Two Sorted Arrays'\n badge: 'Hard'\n---\n\n### Example 1:\n- Input: `nums1 = [1,3], nums2 = [2]`\n- Output: `2.00000`\n- Explanation: merged array = `[1,2,3]` and median is `2`.\n\n### Example 2:\n- Input: `nums1 = [1,2], nums2 = [3,4]`\n- Output: `2.50000`\n- Explanation: merged array = `[1,2,3,4]` and median is `(2 + 3`) / `2 = 2.5`.\n\n### Constraints:\n\n- `nums1.length == m`\n- `nums2.length == n`\n- `0 <= m <= 1000`\n- `0 <= n <= 1000`\n- `1 <= m + n <= 2000`\n- `-10^6 <= nums1[i], nums2[i] <= 10^6`\n\n## Solution\n\n```py\nclass Solution:\n def findMedianSortedArrays(self, nums1: list[int], nums2: list[int]) -> float:\n if len(nums1) > len(nums2):\n return self.findMedianSortedArrays(nums2, nums1)\n\n m, n = len(nums1), len(nums2)\n left, right = 0, m\n\n while left <= right:\n partA = (left + right) // 2\n partB = (m + n + 1) // 2 - partA\n\n maxLeftA = float(\"-inf\") if partA == 0 else nums1[partA - 1]\n minRightA = float(\"inf\") if partA == m else nums1[partA]\n maxLeftB = float(\"-inf\") if partB == 0 else nums2[partB - 1]\n minRightB = float(\"inf\") if partB == n else nums2[partB]\n\n if maxLeftA <= minRightB and maxLeftB <= minRightA:\n if (m + n) % 2 == 0:\n return (max(maxLeftA, maxLeftB) + min(minRightA, minRightB)) / 2\n else:\n return max(maxLeftA, maxLeftB)\n elif maxLeftA > minRightB:\n right = partA - 1\n else:\n left = partA + 1\n```\n","/binary-search/search-a-2d-matrix":"---\ntitle: '74. Search a 2D Matrix'\ndescription: 'You are given an m x n integer matrix matrix with the following two properties:'\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\n - { name: \"Matrix\", slug: \"matrix\" }\nvideo: \"Ber2pi2C0j0\"\nissue: \"https://github.com/prdlk/leetcode/issues/61\"\ncompanies:\n - { name: \"Goldman Sachs\", period: \"Thirty Days\", frequency: 62.9 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 57.2 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 34.6 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"SAP\", period: \"Six Months\", frequency: 100 }\n - { name: \"tcs\", period: \"Six Months\", frequency: 43.5 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\nsidebar:\n label: 'Search a 2D Matrix'\n badge: 'Medium'\n---\n\n::::warning\nYou must write a solution in O(log(m * n)) time complexity.\n::::\n\n### Example 1:\n- Input: `matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3`\n- Output: `true`\n\n### Example 2:\n- Input: `matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13`\n- Output: `false`\n\n### Constraints:\n\n- `m == matrix.length`\n- `n == matrix[i].length`\n- `1 <= m, n <= 100`\n- `-10^4 <= matrix[i][j], target <= 10^4`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"searchMatrix(matrix, target)\"]) --> I[\"top = 0, bot = ROWS - 1\"]\n I --> W1{\"top <= bot?\"}\n W1 -- yes --> R1[\"row = (top + bot) // 2\"]\n R1 --> C1{\"target vs that row's range\"}\n C1 -- \"above matrix[row][-1]\" --> U[\"top = row + 1\"]\n C1 -- \"below matrix[row][0]\" --> V[\"bot = row - 1\"]\n C1 -- inside --> B([\"break — row found\"])\n U --> W1\n V --> W1\n W1 -- no --> F([\"return False — no row can hold it\"])\n B --> P[\"row = (top + bot) // 2; l = 0, r = COLS - 1\"]\n P --> W2{\"l <= r?\"}\n W2 -- no --> G([\"return False\"])\n W2 -- yes --> M[\"m = (l + r) // 2\"]\n M --> C2{\"target vs matrix[row][m]\"}\n C2 -- greater --> X[\"l = m + 1\"]\n C2 -- smaller --> Y[\"r = m - 1\"]\n C2 -- equal --> T([\"return True\"])\n X --> W2\n Y --> W2\n```\n\n## Solution\n\n```py\nclass Solution:\n def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:\n ROWS, COLS = len(matrix), len(matrix[0])\n\n top, bot = 0, ROWS - 1\n while top <= bot:\n row = (top + bot) // 2\n if target > matrix[row][-1]:\n top = row + 1\n elif target < matrix[row][0]:\n bot = row - 1\n else:\n break\n\n if not (top <= bot):\n return False\n\n row = (top + bot) // 2\n l, r = 0, COLS - 1\n while l <= r:\n m = (l + r) // 2\n if target > matrix[row][m]:\n l = m + 1\n elif target < matrix[row][m]:\n r = m - 1\n else:\n return True\n return False\n```\n","/binary-search/search-in-rotated-sorted-array":"---\ntitle: '33. Search in Rotated Sorted Array'\ndescription: There is an integer array nums sorted in ascending order (with distinct values)\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\nvideo: \"U8XENwh8Oy8\"\nissue: \"https://github.com/prdlk/leetcode/issues/60\"\ncompanies:\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 36.7 }\n - { name: \"Oracle\", period: \"Three Months\", frequency: 100 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 68.3 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"TikTok\", period: \"Six Months\", frequency: 64.9 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 63.3 }\n - { name: \"Yandex\", period: \"Six Months\", frequency: 59.6 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 53.1 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 48.1 }\nsidebar:\n label: 'Search in Rotated Sorted Array'\n badge: 'Medium'\n---\n\n::::warning\nYou must write an algorithm with O(log n) runtime complexity.\n::::\n\n### Example 1:\n- Input: `nums = [4,5,6,7,0,1,2], target = 0`\n- Output: `4`\n\n### Example 2:\n- Input: `nums = [4,5,6,7,0,1,2], target = 3`\n- Output: `-1`\n\n### Example 3:\n- Input: `nums = [1], target = 0`\n- Output: `-1`\n\n### Constraints:\n\n- `1 <= nums.length <= 5000`\n- `-10^4 <= nums[i] <= 10^4`\n- All values of `nums` are unique.\n- `nums` is an ascending array that is possibly rotated.\n- `-10^4 <= target <= 10^4`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"search(nums, target)\"]) --> I[\"l = 0, r = n - 1\"]\n I --> W{\"l <= r?\"}\n W -- no --> E([\"return -1\"])\n W -- yes --> M[\"mid = floor((l + r) / 2)\"]\n M --> F{\"nums[mid] == target?\"}\n F -- yes --> R([\"return mid\"])\n F -- no --> H{\"nums[l] <= nums[mid]? — left half sorted\"}\n H -- yes --> A{\"target outside [nums[l], nums[mid]]?\"}\n A -- yes --> A1[\"l = mid + 1 — search the right half\"]\n A -- no --> A2[\"r = mid - 1\"]\n H -- no --> B{\"target outside [nums[mid], nums[r]]?\"}\n B -- yes --> B1[\"r = mid - 1 — search the left half\"]\n B -- no --> B2[\"l = mid + 1\"]\n A1 --> W\n A2 --> W\n B1 --> W\n B2 --> W\n```\n\n## Solution\n\n```js\n/**\n * @param {number[]} nums\n * @param {number} target\n * @return {number}\n */\nvar search = function(nums, target) {\n let l = 0, r = nums.length - 1; \n\n while (l <= r){\n let mid = Math.floor((l + r)/2);\n if (target === nums[mid]) {\n return mid\n }\n\n // Left sorted portion\n if (nums[l] <= nums[mid]){\n if (target > nums[mid] || target < nums[l]){\n l = mid + 1;\n }else{\n r = mid - 1;\n }\n }\n // Right sorted portion\n else{\n if(target < nums[mid] || target > nums[r]){\n r = mid - 1; \n }else{\n l = mid + 1;\n }\n }\n }\n return -1;\n};\n```\n","/binary-search/search-insert-position":"---\ntitle: '35. Search Insert Position'\ndescription: Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\nvideo: \"K-RYzDZkzCI\"\nissue: \"https://github.com/prdlk/leetcode/issues/63\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 50.2 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Grammarly\", period: \"Three Months\", frequency: 84.8 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 49.8 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 56.2 }\nsidebar:\n label: 'Search Insert Position'\n badge: 'Easy'\n---\n\n::::warning\nYou must write an algorithm with O(log n) runtime complexity.\n::::\n\n### Example 1:\n- Input: `nums = [1,3,5,6], target = 5`\n- Output: `2`\n\n### Example 2:\n- Input: `nums = [1,3,5,6], target = 2`\n- Output: `1`\n\n### Example 3:\n- Input: `nums = [1,3,5,6], target = 7`\n- Output: `4`\n\n### Constraints:\n\n- `1 <= nums.length <= 10^4`\n- `-10^4 <= nums[i] <= 10^4`\n- `nums` contains distinct values sorted in ascending order.\n- `-10^4 <= target <= 10^4`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"searchInsert(nums, target)\"]) --> I[\"l = 0, r = n - 1\"]\n I --> W{\"l <= r?\"}\n W -- yes --> M[\"m = (l + r) // 2\"]\n M --> Q{\"nums[m] vs target\"}\n Q -- equal --> R([\"return m\"])\n Q -- \"nums[m] < target\" --> L[\"l = m + 1\"]\n L --> W\n Q -- \"nums[m] > target\" --> H[\"r = m - 1\"]\n H --> W\n W -- no --> E([\"return (l + r) // 2 + 1 — the insert slot\"])\n```\n\n## Solution\n\n```py\nclass Solution:\n def searchInsert(self, nums: List[int], target: int) -> int:\n l, r = 0, len(nums) - 1\n\n while l <= r:\n m = (l + r) // 2\n if nums[m] == target:\n return m\n elif nums[m] < target:\n l = m + 1\n elif nums[m] > target:\n r = m - 1\n\n return (l + r) // 2 + 1\n```\n","/graph-bfs":"---\ntitle: Graph BFS\ndescription: 'Nodes are visited in order of distance from the start, which gives the shortest path in an unweighted graph.'\nhidden: true\n---\n","/graph-bfs/rotting-oranges":"---\ntitle: '994. Rotting Oranges'\ndescription: 'You are given an m x n grid where each cell can have one of three values:'\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Matrix\", slug: \"matrix\" }\nissue: \"https://github.com/prdlk/leetcode/issues/137\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 75.8 }\n - { name: \"OpenAI\", period: \"Thirty Days\", frequency: 69.7 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 57.2 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 42.3 }\n - { name: \"Anduril\", period: \"Three Months\", frequency: 94.2 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 66 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 43.3 }\n - { name: \"Cognizant\", period: \"Six Months\", frequency: 76.3 }\n - { name: \"Lyft\", period: \"Six Months\", frequency: 72.2 }\n - { name: \"Flipkart\", period: \"Six Months\", frequency: 57.1 }\n - { name: \"Pinterest\", period: \"Six Months\", frequency: 55.8 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 54.1 }\n - { name: \"Salesforce\", period: \"Six Months\", frequency: 50.2 }\n - { name: \"Uber\", period: \"Six Months\", frequency: 45.7 }\nsidebar:\n label: 'Rotting Oranges'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `grid = [[2,1,1],[1,1,0],[0,1,1]]`\n- Output: `4`\n\n### Example 2:\n- Input: `grid = [[2,1,1],[0,1,1],[1,0,1]]`\n- Output: `-1`\n- Explanation: The orange in the bottom left corner (row `2`, column `0`) is never rotten, because rotting only happens 4-directionally.\n\n### Example 3:\n- Input: `grid = [[0,2]]`\n- Output: `0`\n- Explanation: Since there are already no fresh oranges at minute `0`, the answer is just `0`.\n\n### Constraints:\n\n- `m == grid.length`\n- `n == grid[i].length`\n- `1 <= m, n <= 10`\n- grid[i][j] is 0, 1, or 2.\n\n## Solution\n\n```py\nfrom collections import deque\n\n\nclass Solution:\n def orangesRotting(self, grid: list[list[int]]) -> int:\n q = deque()\n time, fresh = 0, 0\n\n rows, cols = len(grid), len(grid[0])\n for r in range(rows):\n for c in range(cols):\n if grid[r][c] == 1:\n fresh += 1\n if grid[r][c] == 2:\n q.append([r, c])\n\n directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]\n\n while q and fresh > 0:\n for i in range(len(q)):\n r, c = q.popleft()\n for dr, dc in directions:\n row, col = dr + r, dc + c\n # check if in bounds\n if (\n row < 0\n or col < 0\n or row == len(grid)\n or col == len(grid[0])\n or grid[row][col] != 1\n ):\n continue\n grid[row][col] = 2\n q.append([row, col])\n fresh -= 1\n time += 1\n return time if fresh == 0 else -1\n```\n","/graph-bfs/word-ladder":"---\ntitle: '127. Word Ladder'\ndescription: 'A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:'\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Bidirectional Search\", slug: \"bidirectional-search\" }\nissue: \"https://github.com/prdlk/leetcode/issues/138\"\ncompanies:\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 65.6 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 55.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 69 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 46.8 }\n - { name: \"Citadel\", period: \"Six Months\", frequency: 88 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 69.8 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 32.7 }\nsidebar:\n label: 'Word Ladder'\n badge: 'Hard'\n---\n\n### Example 1:\n- Input: `beginWord = \"hit\", endWord = \"cog\", wordList = [\"hot\",\"dot\",\"dog\",\"lot\",\"log\",\"cog\"]`\n- Output: `5`\n- Explanation: One shortest transformation sequence is \"hit\" -> \"hot\" -> \"dot\" -> \"dog\" -> cog\", which is `5` words long.\n\n### Example 2:\n- Input: `beginWord = \"hit\", endWord = \"cog\", wordList = [\"hot\",\"dot\",\"dog\",\"lot\",\"log\"]`\n- Output: `0`\n- Explanation: The `endWord` \"cog\" is not in `wordList`, therefore there is no valid transformation sequence.\n\n### Constraints:\n\n- `1 <= beginWord.length <= 10`\n- `endWord.length == beginWord.length`\n- `1 <= wordList.length <= 5000`\n- `wordList[i].length == beginWord.length`\n- `beginWord`, `endWord`, and `wordList[i]` consist of lowercase English letters.\n- `beginWord != endWord`\n- All the words in `wordList` are unique.\n\n## Solution\n\n```py\nimport collections\n\n\nclass Solution:\n def ladderLength(self, beginWord: str, endWord: str, wordList: list[str]) -> int:\n if endWord not in wordList:\n return 0\n\n adj = collections.defaultdict(list)\n wordList.append(beginWord)\n\n for word in wordList:\n for j in range(len(word)):\n pattern = word[:j] + \"*\" + word[j + 1 :]\n adj[pattern].append(word)\n\n visit = set([beginWord])\n q = collections.deque([beginWord])\n res = 1\n\n while q:\n for i in range(len(q)):\n word = q.popleft()\n if word == endWord:\n return res\n for j in range(len(word)):\n pattern = word[:j] + \"*\" + word[j + 1 :]\n for adjWord in adj[pattern]:\n if adjWord not in visit:\n visit.add(adjWord)\n q.append(adjWord)\n res += 1\n return 0\n```\n","/graph-dfs":"---\ntitle: Graph DFS\ndescription: 'Each edge is followed to its end, and a visited set prevents a repeat visit.'\nhidden: true\n---\n","/graph-dfs/clone-graph":"---\ntitle: '133. Clone Graph'\ndescription: Given a reference of a node in a connected undirected graph\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Graph Theory\", slug: \"graph\" }\nissue: \"https://github.com/prdlk/leetcode/issues/131\"\ncompanies:\n - { name: \"Amazon\", period: \"Three Months\", frequency: 21.2 }\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 24.5 }\nsidebar:\n label: 'Clone Graph'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `adjList = [[2,4],[1,3],[2,4],[1,3]]`\n- Output: `[[2,4],[1,3],[2,4],[1,3]]`\n- Explanation: There are `4` nodes in the graph. 1st `node` (val = 1)'s neighbors are 2nd `node` (val = `2`) and 4th `node` (val = `4`). 2nd `node` (val = 2)'s neighbors are 1st `node` (val = `1`) and 3rd `node` (val = `3`). 3rd `node` (val = 3)'s neighbors are 2nd `node` (val = `2`) and 4th `node` (val = `4`). 4th `node` (val = 4)'s neighbors are 1st `node` (val = `1`) and 3rd `node` (val = `3`).\n\n### Example 2:\n- Input: `adjList = [[]]`\n- Output: `[[]]`\n- Explanation: Note that the input contains one empty list. The graph consists of only one `node` with val = `1` and it does not have any neighbors.\n\n### Example 3:\n- Input: `adjList = []`\n- Output: `[]`\n- Explanation: This an empty graph, it does not have any nodes.\n\n### Constraints:\n\n- The number of nodes in the graph is in the range [0, 100].\n- `1 <= Node.val <= 100`\n- Node.val is unique for each node.\n- There are no repeated edges and no self-loops in the graph.\n- The Graph is connected and all nodes can be visited starting from the given node.\n\n## Solution\n\n```py\n\"\"\"\n# Definition for a Node.\nclass Node:\n def __init__(self, val = 0, neighbors = None):\n self.val = val\n self.neighbors = neighbors if neighbors is not None else []\n\"\"\"\n\nfrom typing import Optional\n\n\nclass Solution:\n def cloneGraph(self, node: Optional[\"Node\"]) -> Optional[\"Node\"]:\n oldToNew = {}\n\n def dfs(node):\n if node in oldToNew:\n return oldToNew[node]\n\n copy = Node(node.val)\n oldToNew[node] = copy\n\n for n in node.neighbors:\n copy.neighbors.append(dfs(n))\n return copy\n\n return dfs(node) if node else None\n```\n","/graph-dfs/number-of-islands":"---\ntitle: '200. Number of Islands'\ndescription: Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Union-Find\", slug: \"union-find\" }\n - { name: \"Matrix\", slug: \"matrix\" }\nissue: \"https://github.com/prdlk/leetcode/issues/130\"\ncompanies:\n - { name: \"Anduril\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Apple\", period: \"Thirty Days\", frequency: 81.7 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 65.8 }\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 65.6 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 50.2 }\n - { name: \"Qualcomm\", period: \"Three Months\", frequency: 100 }\n - { name: \"TikTok\", period: \"Three Months\", frequency: 89.3 }\n - { name: \"Uber\", period: \"Three Months\", frequency: 72.9 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 66 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 57.2 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Autodesk\", period: \"Six Months\", frequency: 100 }\n - { name: \"ByteDance\", period: \"Six Months\", frequency: 100 }\n - { name: \"Rivian\", period: \"Six Months\", frequency: 100 }\n - { name: \"Snap\", period: \"Six Months\", frequency: 100 }\n - { name: \"Waymo\", period: \"Six Months\", frequency: 100 }\n - { name: \"Walmart Labs\", period: \"Six Months\", frequency: 95.1 }\n - { name: \"Visa\", period: \"Six Months\", frequency: 80.9 }\n - { name: \"Nvidia\", period: \"Six Months\", frequency: 78.7 }\n - { name: \"Oracle\", period: \"Six Months\", frequency: 78.7 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 69.8 }\n - { name: \"Yandex\", period: \"Six Months\", frequency: 59.6 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 59 }\nsidebar:\n label: 'Number of Islands'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `grid = [ [\"1\",\"1\",\"1\",\"1\",\"0\"], [\"1\",\"1\",\"0\",\"1\",\"0\"], [\"1\",\"1\",\"0\",\"0\",\"0\"], [\"0\",\"0\",\"0\",\"0\",\"0\"] ]`\n- Output: `1`\n\n### Example 2:\n- Input: `grid = [ [\"1\",\"1\",\"0\",\"0\",\"0\"], [\"1\",\"1\",\"0\",\"0\",\"0\"], [\"0\",\"0\",\"1\",\"0\",\"0\"], [\"0\",\"0\",\"0\",\"1\",\"1\"] ]`\n- Output: `3`\n\n### Constraints:\n\n- `m == grid.length`\n- `n == grid[i].length`\n- `1 <= m, n <= 300`\n- grid[i][j] is '0' or '1'.\n\n## Solution\n\n```py\nclass Solution:\n def numIslands(self, grid: List[List[str]]) -> int:\n rows, cols = len(grid), len(grid[0])\n\n def dfs(r, c):\n if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] != \"1\":\n return\n else:\n grid[r][c] = \"0\"\n dfs(r, c + 1) # right\n dfs(r + 1, c) # bottom\n dfs(r, c - 1) # left\n dfs(r - 1, c) # top\n\n num_islands = 0\n for r in range(rows):\n for c in range(cols):\n if grid[r][c] == \"1\":\n num_islands += 1\n dfs(r, c)\n\n return num_islands\n```\n","/graph-dfs/pacific-atlantic-water-flow":"---\ntitle: '417. Pacific Atlantic Water Flow'\ndescription: There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Matrix\", slug: \"matrix\" }\nissue: \"https://github.com/prdlk/leetcode/issues/132\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Google\", period: \"Three Months\", frequency: 23.9 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 24.1 }\nsidebar:\n label: 'Pacific Atlantic Water Flow'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]`\n- Output: `[[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]`\n- Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below: `[0,4]`: `[0,4]` -> Pacific Ocean `[0,4]` -> Atlantic Ocean `[1,3]`: `[1,3]` -> `[0,3]` -> Pacific Ocean `[1,3]` -> `[1,4]` -> Atlantic Ocean `[1,4]`: `[1,4]` -> `[1,3]` -> `[0,3]` -> Pacific Ocean `[1,4]` -> Atlantic Ocean `[2,2]`: `[2,2]` -> `[1,2]` -> `[0,2]` -> Pacific Ocean `[2,2]` -> `[2,3]` -> `[2,4]` -> Atlantic Ocean `[3,0]`: `[3,0]` -> Pacific Ocean `[3,0]` -> `[4,0]` -> Atlantic Ocean `[3,1]`: `[3,1]` -> `[3,0]` -> Pacific Ocean `[3,1]` -> `[4,1]` -> Atlantic Ocean `[4,0]`: `[4,0]` -> Pacific Ocean `[4,0]` -> Atlantic Ocean Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans.\n\n### Example 2:\n- Input: `heights = [[1]]`\n- Output: `[[0,0]]`\n- Explanation: The water can flow from the only cell to the Pacific and Atlantic oceans.\n\n### Constraints:\n\n- `m == heights.length`\n- `n == heights[r].length`\n- `1 <= m, n <= 200`\n- `0 <= heights[r][c] <= 10^5`\n\n## Solution\n\n```py\nclass Solution:\n def pacificAtlantic(self, heights: list[list[int]]) -> list[list[int]]:\n rows, cols = len(heights), len(heights[0])\n pac, atl = set(), set()\n\n def dfs(r, c, seen, prevHeight):\n if (\n (r, c) in seen\n or r < 0\n or c < 0\n or r == rows\n or c == cols\n or heights[r][c] < prevHeight\n ):\n return\n seen.add((r, c))\n dfs(r + 1, c, seen, heights[r][c])\n dfs(r - 1, c, seen, heights[r][c])\n dfs(r, c + 1, seen, heights[r][c])\n dfs(r, c - 1, seen, heights[r][c])\n\n for c in range(cols):\n dfs(0, c, pac, heights[0][c])\n dfs(rows - 1, c, atl, heights[rows - 1][c])\n\n for r in range(rows):\n dfs(r, 0, pac, heights[r][0])\n dfs(r, cols - 1, atl, heights[r][cols - 1])\n\n res = []\n for r in range(rows):\n for c in range(cols):\n if (r, c) in pac and (r, c) in atl:\n res.append([r, c])\n\n return res\n```\n","/greedy":"---\ntitle: Greedy\ndescription: 'The best local choice is made at each step and is not changed later.'\nhidden: true\n---\n\n## When to use greedy algorithms?\n\n- Solving optimization problems.\n- Ask to optimize some value and calculate it.\n- keywords (min/max, longest/shortest, largest/smallest, etc.)\n\n## Optimization problems\n\n1. Return the minimum number of swaps required to make s1 equal to s2.\n2. Return the minimum number of steps to make s and t anagrams of each other.\n3. Given three integers a, b, and c, return the longest possible happy string, if there are multiple such strings return any of them. if there is no such string, return empty string.\n\n## What are greedy algorithms?\n\n- Greedy algorithms follow this format:\n- identify what choices you have at every step\n - find the best choice\n - just do what looks best until done\n- assume that the best choice is the one that will lead to the best outcome\n\n## How do we know greedy works?\n- Write out a formal proof\n - too complicated to write out\n - too much time\n- better to use intuition\n - try a bunch of ideas\n - find counterexamples\n\n## Steps to use greedy algorithms\n\n1. **Identify the different steps and choices**\n\n - Choices have to be comparable\n - must determine if a choice is better than another\n\n2. **Check if greedy works**\n\n - try finding small cases where greedy is not optimal\n - if cases pass, try to deduce why greedy is optimal\n\n## Why use greedy algorithms?\n\n- simple to code\n- doesn't use complex algorithms (BFS, DFS, etc.)\n- runtime is fast, usually 0(n) or O(nlogn)\n- however, greedy algorithms are not always optimal\n\n## The Greedy Skeleton\n\n### Type 1: Walk and Carry\n\n:::info\nNo sort. Walk the list once. Carry one number. At each step, decide: keep it, or drop it.\n:::\n\n```python\ndef walk_and_carry(nums):\n carry = start_value\n best = start_value\n for n in nums:\n if carry_is_useless(carry):\n carry = fresh_start\n carry = update(carry, n)\n best = max(best, carry)\n return best\n```\n\n### Type 2: Sort and Pick\n\n:::info\nUsed when order doesn't matter, so you pick the order. Meeting rooms, intervals, \"most tasks by a deadline.\"\n:::\n\n```python\ndef greedy(items):\n # 1. Sort by the greedy key (deadline, end time, size, ratio, ...)\n items.sort(key=greedy_key)\n result = 0\n state = initial_state()\n for item in items:\n # 2. Take the item only if the locally best choice is valid\n if is_feasible(item, state):\n result += take(item, state)\n # 3. Correctness relies on an exchange argument, not just passing tests\n return result\n```\n","/greedy/gas-station":"---\ntitle: '134. Gas Station'\ndescription: There are n gas stations along a circular route, where the amount of gas at the i^th station is gas[i]\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Greedy\", slug: \"greedy\" }\nissue: \"https://github.com/prdlk/leetcode/issues/176\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Sprinklr\", period: \"Three Months\", frequency: 95.9 }\n - { name: \"TikTok\", period: \"Three Months\", frequency: 70.9 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 28.9 }\n - { name: \"Adobe\", period: \"Six Months\", frequency: 74.3 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 44.8 }\n - { name: \"Google\", period: \"Six Months\", frequency: 17.4 }\nsidebar:\n label: 'Gas Station'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `gas = [1,2,3,4,5], cost = [3,4,5,1,2]`\n- Output: `3`\n- Explanation: Start at station `3` (index `3`) and fill up with `4` unit of `gas`. Your tank = `0 + 4 = 4` Travel to station `4`. Your tank = `4 - 1 + 5 = 8` Travel to station `0`. Your tank = `8 - 2 + 1 = 7` Travel to station `1`. Your tank = `7 - 3 + 2 = 6` Travel to station `2`. Your tank = `6 - 4 + 3 = 5` Travel to station `3`. The `cost` is `5`. Your `gas` is just enough to travel back to station `3`. Therefore, return `3` as the starting index.\n\n### Example 2:\n- Input: `gas = [2,3,4], cost = [3,4,3]`\n- Output: `-1`\n- Explanation: You can't start at station `0` or `1`, as there is not enough `gas` to travel to the next station. Let's start at station `2` and fill up with `4` unit of `gas`. Your tank = `0 + 4 = 4` Travel to station `0`. Your tank = `4 - 3 + 2 = 3` Travel to station `1`. Your tank = `3 - 3 + 3 = 3` You cannot travel back to station `2`, as it requires `4` unit of `gas` but you only have `3`. Therefore, you can't travel around the circuit once no matter where you start.\n\n### Constraints:\n\n- `n == gas.length == cost.length`\n- `1 <= n <= 10^5`\n- `0 <= gas[i], cost[i] <= 10^4`\n- The input is generated such that the answer is unique.\n\n## Solution\n\n```py\nclass Solution:\n def canCompleteCircuit(self, gas: list[int], cost: list[int]) -> int:\n start, tank, total = 0, 0, 0\n\n for i in range(len(gas)):\n drive = gas[i] - cost[i]\n tank = tank + drive\n\n if tank < 0:\n tank = 0\n start = i + 1\n total = total + drive\n\n return start if total >= 0 else -1\n```\n","/greedy/jump-game":"---\ntitle: '55. Jump Game'\ndescription: You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\n - { name: \"Greedy\", slug: \"greedy\" }\nissue: \"https://github.com/prdlk/leetcode/issues/175\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 54.8 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Google\", period: \"Three Months\", frequency: 45 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 56.2 }\nsidebar:\n label: 'Jump Game'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [2,3,1,1,4]`\n- Output: `true`\n- Explanation: Jump `1` step from index `0` to `1`, then `3` steps to the last index.\n\n### Example 2:\n- Input: `nums = [3,2,1,0,4]`\n- Output: `false`\n- Explanation: You will always arrive at index `3` no matter what. Its maximum jump length is `0`, which makes it impossible to reach the last index.\n\n### Constraints:\n\n- `1 <= nums.length <= 10^4`\n- `0 <= nums[i] <= 10^5`\n\n## Solution\n\n```py\nclass Solution:\n def canJump(self, nums: list[int]) -> bool:\n best, last = nums[0], len(nums) - 1\n\n for i in range(last):\n if i > best:\n return False\n else:\n pos = nums[i] + i\n best = max(best, pos)\n\n return best >= last\n```\n","/greedy/maximum-subarray":"---\ntitle: '53. Maximum Subarray'\ndescription: Given an integer array nums, find the subarray with the largest sum, and return its sum\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Divide and Conquer\", slug: \"divide-and-conquer\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\nissue: \"https://github.com/prdlk/leetcode/issues/174\"\ncompanies:\n - { name: \"Upstart\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Infosys\", period: \"Thirty Days\", frequency: 80.2 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 75.6 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 69.6 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 65.3 }\n - { name: \"Google\", period: \"Three Months\", frequency: 54.6 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 85.6 }\nsidebar:\n label: 'Maximum Subarray'\n badge: 'Medium'\n---\n\n::::warning\nIf you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.\n::::\n\n### Example 1:\n- Input: `nums = [-2,1,-3,4,-1,2,1,-5,4]`\n- Output: `6`\n- Explanation: The subarray `[4,-1,2,1]` has the largest sum `6`.\n\n### Example 2:\n- Input: `nums = [1]`\n- Output: `1`\n- Explanation: The subarray `[1]` has the largest sum `1`.\n\n### Example 3:\n- Input: `nums = [5,4,-1,7,8]`\n- Output: `23`\n- Explanation: The subarray `[5,4,-1,7,8]` has the largest sum `23`.\n\n### Constraints:\n\n- `1 <= nums.length <= 10^5`\n- `-10^4 <= nums[i] <= 10^4`\n\n## Solution\n\n```py\nclass Solution:\n def maxSubArray(self, nums: list[int]) -> int:\n best, total = nums[0], nums[0]\n\n for i in range(1, len(nums)):\n if total < 0:\n total = nums[i]\n else:\n total = total + nums[i]\n best = max(best, total)\n\n return best\n```\n","/hash-table":"---\ntitle: Hash Table\ndescription: 'A key maps to a value in constant time, which removes the need for a full scan.'\nhidden: true\n---\n\nSometimes when programming, we need a data structure that maps an arbitrary data type to another arbitrary data type, like an array with non-integer indices. For example, a phone book that allows lookup by a person's name requires such data type.\n\nA **hash table** (or a hash map) is one way of representing this data structure. The idea of hash table is this:\n\nData is stored in a fix-sized array, where each data value has an unique index. When we add a new `(key, value)` pair to the array, we use a hash function to generate a hash value within the range of the array with the `key`. We then store the data `value` at the index indicated by the generated hash value. Later, when we retrieve the data with the `key` from the hash table, we hash the `key` again and look up the data in the array with the corresponding index. Since the hash function always returns the same hash values with the same input, we will always find the correct item in that index.\n\n\n\n\nHash Table Operations\n\n\n## Efficiency\n\nAssuming the hash function uses constant time, the average time complexity is `O(n / k)` for each insertion/access operation on the table, where `n` is the number of entries in the hash table and `k` is the array size of the table. This assumes the hash function distributes keys uniformly across the `k` buckets, so each bucket holds about `n / k` entries on average.\n\n### Frequency Counting\n\n```python\ndef get_counter(arr: list[int]) -> dict[int, int]:\n # initialize a hash map to store each number and its count\n counter: dict[int, int] = {}\n for num in arr:\n # check if num is a key in the hash map\n if num in counter:\n # update the count of num to increase by 1\n counter[num] += 1\n else:\n # set the count of num to 1\n counter[num] = 1\n return counter\n```\n\n:::tip\nPython provides a built-in [`collections.Counter`](https://docs.python.org/3/library/collections.html#collections.Counter) class that does this for us.\n:::\n\n```python\nfrom collections import Counter\n\nfq = Counter(arr)\n```\n","/hash-table/check-if-the-sentence-is-pangram":"---\ntitle: '1832. Check if the Sentence Is Pangram'\ndescription: A pangram is a sentence where every letter of the English alphabet appears at least once\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\ncompanies:\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 34.2 }\n - { name: \"Amazon\", period: \"Six Months\", frequency: 14.3 }\n - { name: \"Google\", period: \"Six Months\", frequency: 11.8 }\nsidebar:\n label: 'Check if the Sentence Is Pangram'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `sentence = \"thequickbrownfoxjumpsoverthelazydog\"`\n- Output: `true`\n- Explanation: `sentence` contains at least one of every letter of the English alphabet.\n\n### Example 2:\n- Input: `sentence = \"leetcode\"`\n- Output: `false`\n\n### Constraints:\n\n- `1 <= sentence.length <= 1000`\n- `sentence` consists of lowercase English letters.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"checkIfPangram(sentence)\"]) --> I[\"freq = {}\"]\n I --> L{\"more char c?\"}\n L -- yes --> C[\"freq[c] = (freq[c] or 0) + 1\"]\n C --> L\n L -- no --> Q{\"distinct keys in freq == 26?\"}\n Q -- yes --> R([\"return true\"])\n Q -- no --> F([\"return false\"])\n```\n\n## Solution\n\n```js\n/**\n * @param {string} sentence\n * @return {boolean}\n */\nvar checkIfPangram = function(sentence) {\n const freq = {};\n for (let c of sentence) freq[c] = (freq[c] || 0) + 1;\n \n if (Object.keys(freq).length === 26){\n return true;\n }\n return false;\n};\n```\n","/hash-table/contains-duplicate":"---\ntitle: '217. Contains Duplicate'\ndescription: Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Sorting\", slug: \"sorting\" }\nvideo: \"3OamzN90kPg\"\nissue: \"https://github.com/prdlk/leetcode/issues/27\"\ncompanies:\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 50.2 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 48.3 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 36.8 }\nsidebar:\n label: 'Contains Duplicate'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `nums = [1,2,3,1]`\n- Output: `true`\n- Explanation: The element `1` occurs at the indices `0` and `3`.\n\n### Example 2:\n- Input: `nums = [1,2,3,4]`\n- Output: `false`\n- Explanation: All elements are distinct.\n\n### Example 3:\n- Input: `nums = [1,1,1,3,3,4,3,2,4,2]`\n- Output: `true`\n\n### Constraints:\n\n- `1 <= nums.length <= 10^5`\n- `-10^9 <= nums[i] <= 10^9`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"containsDuplicate(nums)\"]) --> I[\"freq = {}\"]\n I --> L{\"more n in nums?\"}\n L -- no --> E([\"return false\"])\n L -- yes --> C[\"freq[n] = (freq[n] or 0) + 1\"]\n C --> Q{\"freq[n] >= 2?\"}\n Q -- yes --> R([\"return true\"])\n Q -- no --> L\n```\n\n## Solution\n\n```py\nclass Solution:\n def containsDuplicate(self, nums: List[int]) -> bool:\n return len(set(nums)) != len(nums)\n```\n","/hash-table/first-unique-character-in-a-string":"---\ntitle: '387. First Unique Character in a String'\ndescription: Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Queue\", slug: \"queue\" }\n - { name: \"Counting\", slug: \"counting\" }\nvideo: \"rBENYgWy3xU\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"Yandex\", period: \"Three Months\", frequency: 76.1 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 73.7 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 46.8 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 43.3 }\n - { name: \"Google\", period: \"Three Months\", frequency: 23.9 }\n - { name: \"EPAM Systems\", period: \"Six Months\", frequency: 76.2 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 59 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 32 }\nsidebar:\n label: 'First Unique Character in a String'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `s = \"leetcode\"`\n- Output: `0`\n- Explanation: The character `'l'` at index `0` is the first character that does not occur at any other index.\n\n### Example 2:\n- Input: `s = \"loveleetcode\"`\n- Output: `2`\n\n### Example 3:\n- Input: `s = \"aabb\"`\n- Output: `-1`\n\n### Constraints:\n\n- `1 <= s.length <= 10^5`\n- `s` consists of only lowercase English letters.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"firstUniqChar(s)\"]) --> F[\"Pass 1 — freq: count every char of s\"]\n F --> L{\"Pass 2 — more i in 0..len-1?\"}\n L -- no --> E([\"return -1\"])\n L -- yes --> Q{\"freq[s[i]] == 1?\"}\n Q -- yes --> R([\"return i — leftmost wins because the scan is in order\"])\n Q -- no --> L\n```\n\n## Solution\n\n```js\n/**\n * @param {string} s\n * @return {number}\n */\nvar firstUniqChar = function (s) {\n const freq = {};\n for (let c of s) freq[c] = (freq[c] || 0) + 1;\n\n for (let i = 0; i < s.length; i++) {\n if (freq[s[i]] === 1) {\n return i;\n }\n }\n return -1;\n};\n```\n","/hash-table/group-anagrams":"---\ntitle: '49. Group Anagrams'\ndescription: Given an array of strings strs, group the anagrams together. You can return the answer in any order\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Sorting\", slug: \"sorting\" }\nvideo: \"vzdNOK2oB2E\"\nissue: \"https://github.com/prdlk/leetcode/issues/30\"\ncompanies:\n - { name: \"Apple\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 78.4 }\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 76.7 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 58.2 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 50.2 }\n - { name: \"CrowdStrike\", period: \"Three Months\", frequency: 100 }\n - { name: \"Deloitte\", period: \"Three Months\", frequency: 100 }\n - { name: \"HashedIn\", period: \"Three Months\", frequency: 100 }\n - { name: \"Smartsheet\", period: \"Three Months\", frequency: 100 }\n - { name: \"Walmart Labs\", period: \"Three Months\", frequency: 100 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"Expedia\", period: \"Six Months\", frequency: 100 }\n - { name: \"Wipro\", period: \"Six Months\", frequency: 100 }\n - { name: \"Adobe\", period: \"Six Months\", frequency: 93.7 }\n - { name: \"Zoho\", period: \"Six Months\", frequency: 79.6 }\n - { name: \"Nvidia\", period: \"Six Months\", frequency: 78.7 }\n - { name: \"Oracle\", period: \"Six Months\", frequency: 78.7 }\n - { name: \"EPAM Systems\", period: \"Six Months\", frequency: 76.2 }\n - { name: \"Palo Alto Networks\", period: \"Six Months\", frequency: 75.9 }\n - { name: \"Visa\", period: \"Six Months\", frequency: 69.7 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 67.9 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 53.1 }\nsidebar:\n label: 'Group Anagrams'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `strs = [\"eat\",\"tea\",\"tan\",\"ate\",\"nat\",\"bat\"]`\n- Output: `[[\"bat\"],[\"nat\",\"tan\"],[\"ate\",\"eat\",\"tea\"]]`\n- Explanation: There is no string in `strs` that can be rearranged to form `\"bat\"`. The strings `\"nat\"` and `\"tan\"` are anagrams as they can be rearranged to form each other. The strings `\"ate\"`, `\"eat\"`, and `\"tea\"` are anagrams as they can be rearranged to form each other.\n\n### Example 2:\n- Input: `strs = [\"\"]`\n- Output: `[[\"\"]]`\n\n### Example 3:\n- Input: `strs = [\"a\"]`\n- Output: `[[\"a\"]]`\n\n### Constraints:\n\n- `1 <= strs.length <= 10^4`\n- `0 <= strs[i].length <= 100`\n- `strs[i]` consists of lowercase English letters.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"groupAnagrams(strs)\"]) --> K[\"sorted[i] = letters of strs[i], sorted — the anagram key\"]\n K --> I[\"anagrams = {}\"]\n I --> L{\"more i in 0..n-1?\"}\n L -- no --> E([\"return Object.values(anagrams)\"])\n L -- yes --> Q{\"anagrams has sorted[i]?\"}\n Q -- no --> N[\"anagrams[sorted[i]] = [strs[i]]\"]\n Q -- yes --> P[\"anagrams[sorted[i]].push(strs[i])\"]\n N --> L\n P --> L\n```\n\n## Solution\n\n```js\n/**\n * @param {string[]} strs\n * @return {string[][]}\n */\nvar groupAnagrams = function(strs) {\n let sorted = strs.map(str => str.split(\"\").sort().join(\"\"));\n let anagrams = {};\n\n for (let i = 0; i < strs.length; i++){\n if(!anagrams[sorted[i]]){\n anagrams[sorted[i]] = [strs[i]]\n }else{\n anagrams[sorted[i]].push(strs[i])\n }\n }\n\n return Object.values(anagrams);\n};\n```\n","/hash-table/isomorphic-strings":"---\ntitle: '205. Isomorphic Strings'\ndescription: Given two strings s and t, determine if they are isomorphic\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\nissue: \"https://github.com/prdlk/leetcode/issues/33\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 36.7 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 24.5 }\nsidebar:\n label: 'Isomorphic Strings'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `s = \"egg\", t = \"add\"`\n- Output: `true`\n- Explanation: The strings `s` and `t` can be made identical by: • Mapping 'e' to 'a'. • Mapping 'g' to 'd'.\n\n### Example 2:\n- Input: `s = \"f11\", t = \"b23\"`\n- Output: `false`\n- Explanation: The strings `s` and `t` can not be made identical as `'1`' needs to be mapped to both `'2`' and `'3`'.\n\n### Example 3:\n- Input: `s = \"paper\", t = \"title\"`\n- Output: `true`\n\n### Constraints:\n\n- `1 <= s.length <= 5 * 10^4`\n- `t.length == s.length`\n- `s` and `t` consist of any valid ascii character.\n\n## Solution\n\n```py\nclass Solution:\n def isIsomorphic(self, s: str, t: str) -> bool:\n N = len(t)\n seen = {}\n\n for i in range(N):\n c1, c2 = s[i], t[i]\n if c1 in seen and seen[c1] != c2:\n return False\n elif c1 in seen and seen[c1] == c2:\n continue\n else:\n seen[c1] = c2\n\n return len(set(s)) == len(set(t))\n```\n","/hash-table/longest-consecutive-sequence":"---\ntitle: '128. Longest Consecutive Sequence'\ndescription: Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Union-Find\", slug: \"union-find\" }\nissue: \"https://github.com/prdlk/leetcode/issues/34\"\ncompanies:\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 74.3 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 65.8 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 57.2 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 53.2 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 69 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Paytm\", period: \"Six Months\", frequency: 100 }\n - { name: \"Nvidia\", period: \"Six Months\", frequency: 91.1 }\n - { name: \"TikTok\", period: \"Six Months\", frequency: 55.2 }\n - { name: \"Salesforce\", period: \"Six Months\", frequency: 50.2 }\nsidebar:\n label: 'Longest Consecutive Sequence'\n badge: 'Medium'\n---\n\n::::warning\nYou must write an algorithm that runs in O(n) time.\n::::\n\n### Example 1:\n- Input: `nums = [100,4,200,1,3,2]`\n- Output: `4`\n- Explanation: The longest consecutive elements sequence is [1, `2`, `3`, 4]. Therefore its length is `4`.\n\n### Example 2:\n- Input: `nums = [0,3,7,2,5,8,4,6,0,1]`\n- Output: `9`\n\n### Example 3:\n- Input: `nums = [1,0,1,2]`\n- Output: `3`\n\n### Constraints:\n\n- `0 <= nums.length <= 10^5`\n- `-10^9 <= nums[i] <= 10^9`\n\n## Solution\n\n```py\nclass Solution:\n def longestConsecutive(self, nums: list[int]) -> int:\n seen = set(nums)\n best = 0\n\n for n in seen:\n if n - 1 in seen:\n continue\n\n length = 1\n while length + n in seen:\n length += 1\n\n best = max(length, best)\n\n return best\n```\n","/hash-table/sort-characters-by-frequency":"---\ntitle: '451. Sort Characters By Frequency'\ndescription: Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Sorting\", slug: \"sorting\" }\n - { name: \"Heap (Priority Queue)\", slug: \"heap-priority-queue\" }\n - { name: \"Bucket Sort\", slug: \"bucket-sort\" }\n - { name: \"Counting\", slug: \"counting\" }\nvideo: \"OXdXc9HTrIg\"\ncompanies:\n - { name: \"Meta\", period: \"Three Months\", frequency: 49.8 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 48.3 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 46.8 }\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"Amazon\", period: \"Six Months\", frequency: 20.9 }\nsidebar:\n label: 'Sort Characters By Frequency'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `s = \"tree\"`\n- Output: `\"eert\"`\n- Explanation: `'e'` appears twice while `'r'` and `'t'` both appear once. So `'e'` must appear before both `'r'` and `'t'`. Therefore `\"eetr\"` is also a valid answer.\n\n### Example 2:\n- Input: `s = \"cccaaa\"`\n- Output: `\"aaaccc\"`\n- Explanation: Both `'c'` and `'a'` appear three times, so both `\"cccaaa\"` and `\"aaaccc\"` are valid answers. Note that `\"cacaca\"` is incorrect, as the same characters must be together.\n\n### Example 3:\n- Input: `s = \"Aabb\"`\n- Output: `\"bbAa\"`\n- Explanation: `\"bbaA\"` is also a valid answer, but `\"Aabb\"` is incorrect. Note that `'A'` and `'a'` are treated as two different characters.\n\n### Constraints:\n\n- `1 <= s.length <= 5 * 10^5`\n- `s` consists of uppercase and lowercase English letters and digits.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"frequencySort(s)\"]) --> F[\"freq: count every char of s\"]\n F --> C[\"split s and sort with a two-key comparator\"]\n C --> K1[\"primary: freq[b] - freq[a] — most frequent first\"]\n C --> K2[\"tie-break: a.localeCompare(b) — alphabetical\"]\n K1 --> R([\"join and return\"])\n K2 --> R\n```\n\n## Solution\n\n```js\n/**\n * @param {string} s\n * @return {string}\n */\nvar frequencySort = function (s) {\n // count the frequency of each character\n const freq = {};\n for (let c of s) freq[c] = (freq[c] || 0) + 1;\n\n // sort the characters by frequency\n return s\n .split(\"\")\n .sort((a, b) => freq[b] - freq[a] || a.localeCompare(b))\n .join(\"\");\n};\n```\n","/hash-table/top-k-frequent-elements":"---\ntitle: '347. Top K Frequent Elements'\ndescription: Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Divide and Conquer\", slug: \"divide-and-conquer\" }\n - { name: \"Sorting\", slug: \"sorting\" }\n - { name: \"Heap (Priority Queue)\", slug: \"heap-priority-queue\" }\n - { name: \"Bucket Sort\", slug: \"bucket-sort\" }\n - { name: \"Counting\", slug: \"counting\" }\n - { name: \"Quickselect\", slug: \"quickselect\" }\nvideo: \"YPTqKIgVk-k\"\nissue: \"https://github.com/prdlk/leetcode/issues/31\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 72.9 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 42.3 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Snap\", period: \"Six Months\", frequency: 100 }\n - { name: \"SoFi\", period: \"Six Months\", frequency: 100 }\n - { name: \"PayPal\", period: \"Six Months\", frequency: 88.4 }\n - { name: \"Walmart Labs\", period: \"Six Months\", frequency: 81.4 }\n - { name: \"Nvidia\", period: \"Six Months\", frequency: 78.7 }\n - { name: \"eBay\", period: \"Six Months\", frequency: 75.2 }\n - { name: \"Visa\", period: \"Six Months\", frequency: 69.7 }\n - { name: \"Snowflake\", period: \"Six Months\", frequency: 58.7 }\n - { name: \"TikTok\", period: \"Six Months\", frequency: 55.2 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 50.6 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 48.1 }\n - { name: \"tcs\", period: \"Six Months\", frequency: 43.5 }\nsidebar:\n label: 'Top K Frequent Elements'\n badge: 'Medium'\n---\n\n::::warning\nYour algorithm's time complexity must be better than O(n log n), where n is the array's size.\n::::\n\n### Example 1:\n- Input: `nums = [1,1,1,2,2,3], k = 2`\n- Output: `[1,2]`\n\n### Example 2:\n- Input: `nums = [1], k = 1`\n- Output: `[1]`\n\n### Example 3:\n- Input: `nums = [1,2,1,2,1,2,3,1,3,2], k = 2`\n- Output: `[1,2]`\n\n### Constraints:\n\n- `1 <= nums.length <= 10^5`\n- `-10^4 <= nums[i] <= 10^4`\n- `k` is in the range `[1, the number of unique elements in the array]`.\n- It is guaranteed that the answer is unique.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"topKFrequent(nums, k)\"]) --> F[\"freq: count every value in nums\"]\n F --> K[\"keys of freq, cast to Number\"]\n K --> O[\"sort by freq[b] - freq[a] — most frequent first\"]\n O --> R([\"return slice(0, k)\"])\n```\n\n## Solution\n\n```js\n/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number[]}\n */\nvar topKFrequent = function (nums, k) {\n const freq = {};\n for (let n of nums) freq[n] = (freq[n] || 0) + 1;\n\n return Object.keys(freq)\n .map(Number)\n .sort((a, b) => freq[b] - freq[a])\n .slice(0, k);\n};\n```\n","/hash-table/two-sum":"---\ntitle: '1. Two Sum'\ndescription: You are given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\nvideo: \"KLlXCFG5TnA\"\nissue: \"https://github.com/prdlk/leetcode/issues/29\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 79.6 }\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 76.7 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Infosys\", period: \"Thirty Days\", frequency: 68.6 }\n - { name: \"Altimetrik\", period: \"Three Months\", frequency: 100 }\n - { name: \"Capgemini\", period: \"Three Months\", frequency: 100 }\n - { name: \"Microstrategy\", period: \"Three Months\", frequency: 100 }\n - { name: \"MongoDB\", period: \"Three Months\", frequency: 100 }\n - { name: \"Cognizant\", period: \"Three Months\", frequency: 88.7 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 80.4 }\n - { name: \"Barclays\", period: \"Six Months\", frequency: 100 }\n - { name: \"Cisco\", period: \"Six Months\", frequency: 100 }\n - { name: \"Deloitte\", period: \"Six Months\", frequency: 100 }\n - { name: \"Expedia\", period: \"Six Months\", frequency: 100 }\n - { name: \"NetApp\", period: \"Six Months\", frequency: 100 }\n - { name: \"Samsung\", period: \"Six Months\", frequency: 100 }\n - { name: \"Sony\", period: \"Six Months\", frequency: 100 }\n - { name: \"Oracle\", period: \"Six Months\", frequency: 91.2 }\n - { name: \"Qualcomm\", period: \"Six Months\", frequency: 80.7 }\n - { name: \"IBM\", period: \"Six Months\", frequency: 77.2 }\n - { name: \"eBay\", period: \"Six Months\", frequency: 75.2 }\n - { name: \"Walmart Labs\", period: \"Six Months\", frequency: 70.5 }\n - { name: \"Flipkart\", period: \"Six Months\", frequency: 57.1 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 44.8 }\nsidebar:\n label: 'Two Sum'\n badge: 'Easy'\n---\n\n::::warning\nCan you come up with an algorithm that is less than O(n^2) time complexity?\n::::\n\n### Example 1:\n- Input: `nums = [2,7,11,15], target = 9`\n- Output: `[0,1]`\n- Explanation: Because `nums[0] + nums[1]` == `9`, we return [0, 1].\n\n### Example 2:\n- Input: `nums = [3,2,4], target = 6`\n- Output: `[1,2]`\n\n### Example 3:\n- Input: `nums = [3,3], target = 6`\n- Output: `[0,1]`\n\n### Constraints:\n\n- `2 <= nums.length <= 10^4`\n- `-10^9 <= nums[i] <= 10^9`\n- `-10^9 <= target <= 10^9`\n- Only one valid answer exists.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"twoSum(nums, target)\"]) --> I[\"seen = {} — value to index\"]\n I --> L{\"more (i, num) in nums?\"}\n L -- no --> E([\"return []\"])\n L -- yes --> C[\"complement = target - num\"]\n C --> H{\"complement in seen?\"}\n H -- yes --> R([\"return [seen[complement], i]\"])\n H -- no --> W[\"seen[num] = i\"]\n W --> L\n```\n\n## Solution\n\n```py\nclass Solution:\n def twoSum(self, nums: List[int], target: int) -> List[int]:\n seen = {}\n for i, num in enumerate(nums):\n complement = target - num\n if complement in seen:\n return [seen[complement], i]\n seen[num] = i\n return []\n```\n","/hash-table/valid-anagram":"---\ntitle: '242. Valid Anagram'\ndescription: Given two strings s and t, return true if t is an anagram of s, and false otherwise\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Sorting\", slug: \"sorting\" }\nvideo: \"9UtInBqnCgA\"\nissue: \"https://github.com/prdlk/leetcode/issues/28\"\ncompanies:\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 58.2 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 53.2 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"IBM\", period: \"Three Months\", frequency: 100 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 66 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 64.4 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 58.9 }\n - { name: \"Deloitte\", period: \"Six Months\", frequency: 81 }\nsidebar:\n label: 'Valid Anagram'\n badge: 'Easy'\n---\n\n::::warning\nWhat if the inputs contain Unicode characters? How would you adapt your solution to such a case?\n::::\n\n### Example 1:\n- Input: `s = \"anagram\", t = \"nagaram\"`\n- Output: `true`\n\n### Example 2:\n- Input: `s = \"rat\", t = \"car\"`\n- Output: `false`\n\n### Constraints:\n\n- `1 <= s.length, t.length <= 5 * 10^4`\n- `s` and `t` consist of lowercase English letters.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"isAnagram(s, t)\"]) --> I[\"freq: count every char of s\"]\n I --> L{\"more char c in t?\"}\n L -- no --> E([\"return true\"])\n L -- yes --> Q{\"freq[c] missing or 0?\"}\n Q -- yes --> R([\"return false — t has a char s cannot cover\"])\n Q -- no --> D[\"freq[c] -= 1\"]\n D --> L\n```\n\n## Solution\n\n```js\n/**\n * @param {string} s\n * @param {string} t\n * @return {boolean}\n */\nvar isAnagram = function(s, t) {\n let freq = {};\n for (let c of s) freq[c] = (freq[c] || 0) + 1;\n \n for (let c of t) {\n if(!freq[c] || freq[c] === 0){\n return false;\n }\n freq[c] = freq[c] - 1;\n }\n return true;\n};\n```\n","/hash-table/word-pattern":"---\ntitle: '290. Word Pattern'\ndescription: Given a pattern and a string s, find if s follows the same pattern\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\nissue: \"https://github.com/prdlk/leetcode/issues/35\"\ncompanies:\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 24.1 }\n - { name: \"Amazon\", period: \"Six Months\", frequency: 20.9 }\nsidebar:\n label: 'Word Pattern'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `pattern = \"abba\", s = \"dog cat cat dog\"`\n- Output: `true`\n- Explanation: The bijection can be established as: • 'a' maps to \"dog\". • 'b' maps to \"cat\".\n\n### Example 2:\n- Input: `pattern = \"abba\", s = \"dog cat cat fish\"`\n- Output: `false`\n\n### Example 3:\n- Input: `pattern = \"aaaa\", s = \"dog cat cat dog\"`\n- Output: `false`\n\n### Constraints:\n\n- `1 <= pattern.length <= 300`\n- `pattern` contains only lower-case English letters.\n- `1 <= s.length <= 3000`\n- `s` contains only lowercase English letters and spaces ' '.\n- `s` does not contain any leading or trailing spaces.\n- All the words in `s` are separated by a single space.\n\n## Solution\n\n```py\nclass Solution:\n def wordPattern(self, pattern: str, s: str) -> bool:\n if len(pattern) != len(s.split()):\n return False\n\n words = s.split()\n map = {}\n\n for i in range(len(pattern)):\n c = pattern[i]\n w = words[i]\n\n if c in map and map[c] != w:\n return False\n else:\n map[c] = w\n\n return len(set(pattern)) == len(set(words))\n```\n","/heap-priority-queue":"---\ntitle: Heap / Priority Queue\ndescription: 'The largest or smallest item is always at the top, and it is removed in logarithmic time.'\nhidden: true\n---\n","/heap-priority-queue/k-closest-points-to-origin":"---\ntitle: '973. K Closest Points to Origin'\ndescription: Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0)\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Math\", slug: \"math\" }\n - { name: \"Divide and Conquer\", slug: \"divide-and-conquer\" }\n - { name: \"Geometry\", slug: \"geometry\" }\n - { name: \"Sorting\", slug: \"sorting\" }\n - { name: \"Heap (Priority Queue)\", slug: \"heap-priority-queue\" }\n - { name: \"Quickselect\", slug: \"quickselect\" }\n - { name: \"K-D Tree\", slug: \"k-d-tree\" }\nvideo: \"rI2EBUEMfTk\"\nissue: \"https://github.com/prdlk/leetcode/issues/119\"\ncompanies:\n - { name: \"Amazon\", period: \"Three Months\", frequency: 28.9 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 47.3 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\nsidebar:\n label: 'K Closest Points to Origin'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `points = [[1,3],[-2,2]], k = 1`\n- Output: `[[-2,2]]`\n- Explanation: The distance between `(1`, `3`) and the origin is sqrt(10). The distance between `(-2`, `2`) and the origin is sqrt(8). Since sqrt(8) < sqrt(10), `(-2`, `2`) is closer to the origin. We only want the closest `k = 1 points` from the origin, so the answer is just [[-2,2]].\n\n### Example 2:\n- Input: `points = [[3,3],[5,-1],[-2,4]], k = 2`\n- Output: `[[3,3],[-2,4]]`\n- Explanation: The answer [[-2,4],[3,3]] would also be accepted.\n\n### Constraints:\n\n- `1 <= k <= points.length <= 10^4`\n- `-10^4 <= xi, yi <= 10^4`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"kClosest(points, k)\"]) --> I[\"minHeap = []\"]\n I --> F{\"more x, y in points?\"}\n F -- yes --> D[\"dist = x**2 + y**2 — squared distance orders the same as sqrt\"]\n D --> A[\"minHeap.append([dist, x, y])\"]\n A --> F\n F -- no --> H[\"heapq.heapify(minHeap), res = [] — O(n) build, smallest dist on top\"]\n H --> W{\"k > 0?\"}\n W -- yes --> P[\"dist, x, y = heapq.heappop(minHeap)\"]\n P --> R[\"res.append([x, y]), k -= 1\"]\n R --> W\n W -- no --> E([\"return res\"])\n```\n\n## Solution\n\n```py\nimport heapq\n\n\nclass Solution:\n def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:\n minHeap = []\n for x, y in points:\n dist = (x**2) + (y**2)\n minHeap.append([dist, x, y])\n\n heapq.heapify(minHeap)\n res = []\n\n while k > 0:\n dist, x, y = heapq.heappop(minHeap)\n res.append([x, y])\n k -= 1\n\n return res\n```\n","/heap-priority-queue/kth-largest-element-in-an-array":"---\ntitle: '215. Kth Largest Element in an Array'\ndescription: Given an integer array nums and an integer k, return the k^th largest element in the array\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Divide and Conquer\", slug: \"divide-and-conquer\" }\n - { name: \"Sorting\", slug: \"sorting\" }\n - { name: \"Heap (Priority Queue)\", slug: \"heap-priority-queue\" }\n - { name: \"Quickselect\", slug: \"quickselect\" }\nvideo: \"XEmy13g1Qxc\"\nissue: \"https://github.com/prdlk/leetcode/issues/118\"\ncompanies:\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 70.8 }\n - { name: \"Spotify\", period: \"Three Months\", frequency: 90.3 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 36.8 }\n - { name: \"Google\", period: \"Three Months\", frequency: 32.7 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 28.9 }\n - { name: \"Walmart Labs\", period: \"Six Months\", frequency: 70.5 }\n - { name: \"IBM\", period: \"Six Months\", frequency: 70.1 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 63.3 }\n - { name: \"Flipkart\", period: \"Six Months\", frequency: 57.1 }\n - { name: \"TikTok\", period: \"Six Months\", frequency: 55.2 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 37.9 }\nsidebar:\n label: 'Kth Largest Element in an Array'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [3,2,1,5,6,4], k = 2`\n- Output: `5`\n\n### Example 2:\n- Input: `nums = [3,2,3,1,2,4,5,5,6], k = 4`\n- Output: `4`\n\n### Constraints:\n\n- `1 <= k <= nums.length <= 10^5`\n- `-10^4 <= nums[i] <= 10^4`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"findKthLargest(nums, k)\"]) --> F{\"more i in range(len(nums))?\"}\n F -- yes --> N[\"nums[i] = -nums[i] — negate so the min-heap acts as a max-heap\"]\n N --> F\n F -- no --> H[\"heapq.heapify(nums)\"]\n H --> W{\"more _ in range(k - 1)?\"}\n W -- yes --> P[\"heapq.heappop(nums) — discard the k-1 largest\"]\n P --> W\n W -- no --> E([\"return -heapq.heappop(nums)\"])\n```\n\n## Solution\n\n```py\nimport heapq\n\n\nclass Solution:\n def findKthLargest(self, nums: List[int], k: int) -> int:\n for i in range(len(nums)):\n nums[i] = -nums[i]\n\n heapq.heapify(nums)\n\n for _ in range(k - 1):\n heapq.heappop(nums)\n\n return -heapq.heappop(nums)\n```\n","/heap-priority-queue/merge-k-sorted-lists":"---\ntitle: '23. Merge k Sorted Lists'\ndescription: You are given an array of k linked-lists lists, each linked-list is sorted in ascending order\nicon: dot\ntopics:\n - { name: \"Linked List\", slug: \"linked-list\" }\n - { name: \"Divide and Conquer\", slug: \"divide-and-conquer\" }\n - { name: \"Heap (Priority Queue)\", slug: \"heap-priority-queue\" }\n - { name: \"Merge Sort\", slug: \"merge-sort\" }\n - { name: \"Tournament Sort\", slug: \"tournament-sort\" }\nvideo: \"q5a5OiGbT6Q\"\nissue: \"https://github.com/prdlk/leetcode/issues/120\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"FreshWorks\", period: \"Three Months\", frequency: 100 }\n - { name: \"Snap\", period: \"Three Months\", frequency: 100 }\n - { name: \"Deloitte\", period: \"Three Months\", frequency: 89.1 }\n - { name: \"Salesforce\", period: \"Three Months\", frequency: 64.7 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Google\", period: \"Three Months\", frequency: 23.9 }\n - { name: \"Nvidia\", period: \"Six Months\", frequency: 78.7 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 57.9 }\nsidebar:\n label: 'Merge k Sorted Lists'\n badge: 'Hard'\n---\n\n### Example 1:\n- Input: `lists = [[1,4,5],[1,3,4],[2,6]]`\n- Output: `[1,1,2,3,4,4,5,6]`\n- Explanation: The linked-lists are: [ 1->4->5, 1->3->4, 2->6 ] merging them into one sorted linked list: 1->1->2->3->4->4->5->6\n\n### Example 2:\n- Input: `lists = []`\n- Output: `[]`\n\n### Example 3:\n- Input: `lists = [[]]`\n- Output: `[]`\n\n### Constraints:\n\n- `k == lists.length`\n- `0 <= k <= 10^4`\n- `0 <= lists[i].length <= 500`\n- `-10^4 <= lists[i][j] <= 10^4`\n- `lists[i]` is sorted in ascending order.\n- The sum of lists[i].length will not exceed 10^4.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"mergeKLists(lists)\"]) --> I[\"heap = []\"]\n I --> F{\"more i, node in enumerate(lists)?\"}\n F -- yes --> C{\"node?\"}\n C -- yes --> P[\"heapq.heappush(heap, (node.val, i, node)) — i breaks ties so nodes are never compared\"]\n P --> F\n C -- no --> F\n F -- no --> D[\"D = ListNode(), cur = D\"]\n D --> W{\"heap?\"}\n W -- no --> E([\"return D.next\"])\n W -- yes --> O[\"val, i, node = heapq.heappop(heap) — the smallest head across all lists\"]\n O --> L[\"cur.next = node, cur = node, node = node.next\"]\n L --> N{\"node?\"}\n N -- yes --> Q[\"heapq.heappush(heap, (node.val, i, node))\"]\n Q --> W\n N -- no --> W\n```\n\n## Solution\n\n```py\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nimport heapq\n\n\nclass Solution:\n def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:\n heap = []\n\n # K log K\n for i, node in enumerate(lists):\n if node:\n heapq.heappush(heap, (node.val, i, node))\n\n D = ListNode()\n cur = D\n\n # n log k\n while heap:\n val, i, node = heapq.heappop(heap)\n cur.next = node\n cur = node\n node = node.next\n\n if node:\n heapq.heappush(heap, (node.val, i, node))\n\n # Time: O(N log k)\n # Space: O(n)\n return D.next\n```\n","/hybrid-structures":"---\ntitle: Hybrid Structures\ndescription: 'Two structures are combined, such as a hash map and a linked list, to get the best properties of each.'\nhidden: true\n---\n","/hybrid-structures/add-two-numbers":"---\ntitle: '2. Add Two Numbers'\ndescription: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list\nicon: dot\ntopics:\n - { name: \"Linked List\", slug: \"linked-list\" }\n - { name: \"Math\", slug: \"math\" }\n - { name: \"Recursion\", slug: \"recursion\" }\nvideo: \"wgFPrzTjm7s\"\nissue: \"https://github.com/prdlk/leetcode/issues/88\"\ncompanies:\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 95.8 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 83.1 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 72.8 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 63.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 61.3 }\n - { name: \"EarnIn\", period: \"Six Months\", frequency: 100 }\n - { name: \"Jane Street\", period: \"Six Months\", frequency: 100 }\n - { name: \"Palo Alto Networks\", period: \"Six Months\", frequency: 75.9 }\n - { name: \"tcs\", period: \"Six Months\", frequency: 43.5 }\nsidebar:\n label: 'Add Two Numbers'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `l1 = [2,4,3], l2 = [5,6,4]`\n- Output: `[7,0,8]`\n- Explanation: `342 + 465 = 807`.\n\n### Example 2:\n- Input: `l1 = [0], l2 = [0]`\n- Output: `[0]`\n\n### Example 3:\n- Input: `l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]`\n- Output: `[8,9,9,9,0,0,0,1]`\n\n### Constraints:\n\n- The number of nodes in each linked list is in the range [1, 100].\n- `0 <= Node.val <= 9`\n- It is guaranteed that the list represents a number that does not have leading zeros.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"addTwoNumbers(l1, l2)\"]) --> I[\"dummy = ListNode(), carry = 0, curr = dummy — dummy.next will be the head\"]\n I --> W{\"l1 or l2 or carry?\"}\n W -- no --> E([\"return dummy.next\"])\n W -- yes --> A[\"s = (l1.val if l1 else 0) + (l2.val if l2 else 0) + carry — a missing digit counts as 0\"]\n A --> D[\"carry, val = divmod(s, 10)\"]\n D --> N[\"curr.next = ListNode(val), curr = curr.next\"]\n N --> V[\"l1 = l1.next if l1 else None, l2 = l2.next if l2 else None\"]\n V --> W\n```\n\n## Solution\n\n```py\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def addTwoNumbers(\n self, l1: Optional[ListNode], l2: Optional[ListNode]\n ) -> Optional[ListNode]:\n\n dummy = ListNode()\n carry = 0\n curr = dummy\n while l1 or l2 or carry:\n s = (l1.val if l1 else 0) + (l2.val if l2 else 0) + carry\n carry, val = divmod(s, 10)\n\n curr.next = ListNode(val)\n curr = curr.next\n\n l1 = l1.next if l1 else None\n l2 = l2.next if l2 else None\n\n return dummy.next\n```\n","/hybrid-structures/insert-delete-getrandom-o1":"---\ntitle: '380. Insert Delete GetRandom O(1)'\ndescription: 'Implement the RandomizedSet class:'\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Math\", slug: \"math\" }\n - { name: \"Design\", slug: \"design\" }\n - { name: \"Randomized\", slug: \"randomized\" }\nissue: \"https://github.com/prdlk/leetcode/issues/90\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 80.4 }\n - { name: \"Google\", period: \"Three Months\", frequency: 32.7 }\n - { name: \"Axon\", period: \"Six Months\", frequency: 100 }\n - { name: \"Okta\", period: \"Six Months\", frequency: 100 }\n - { name: \"Quora\", period: \"Six Months\", frequency: 100 }\n - { name: \"SoFi\", period: \"Six Months\", frequency: 100 }\n - { name: \"Uber\", period: \"Six Months\", frequency: 61.9 }\n - { name: \"Yandex\", period: \"Six Months\", frequency: 59.6 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 54.1 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 24.1 }\nsidebar:\n label: 'Insert Delete GetRandom O(1)'\n badge: 'Medium'\n---\n\n::::warning\nYou must implement the functions of the class such that each function works in average O(1) time complexity.\n::::\n\n### Example 1:\n- Input: ``\n- Output: ``\n- Explanation: Input [\"RandomizedSet\", \"insert\", \"remove\", \"insert\", \"getRandom\", \"remove\", \"insert\", \"getRandom\"] `[[]`, `[1]`, `[2]`, `[2]`, `[]`, `[1]`, `[2]`, []] Output [null, true, false, true, `2`, true, false, 2] Explanation RandomizedSet randomizedSet = new RandomizedSet(); randomizedSet.insert(1); // Inserts `1` to the set. Returns true as `1` was inserted successfully. randomizedSet.remove(2); // Returns false as `2` does not exist in the set. randomizedSet.insert(2); // Inserts `2` to the set, returns true. Set now contains `[1,2]`. randomizedSet.getRandom(); // getRandom() should return either `1` or `2` randomly. randomizedSet.remove(1); // Removes `1` from the set, returns true. Set now contains `[2]`. randomizedSet.insert(2); // `2` was already in the set, so return false. randomizedSet.getRandom(); // Since `2` is the only number in the set, getRandom() will always return `2`.\n\n### Constraints:\n\n- `-2^31 <= val <= 2^31 - 1`\n- At most 2 * 10^5 calls will be made to insert, remove, and getRandom.\n- There will be at least one element in the data structure when getRandom is called.\n\n## Solution\n\n```py\nfrom random import choice\n\n\nclass RandomizedSet:\n def __init__(self):\n self.list = []\n self.dict = {}\n\n def insert(self, val: int) -> bool:\n if val in self.dict:\n return False\n self.dict[val] = len(self.list)\n self.list.append(val)\n return True\n\n def remove(self, val: int) -> bool:\n if val in self.dict:\n last, idx = self.list[-1], self.dict[val]\n self.list[idx], self.dict[last] = last, idx\n self.list.pop()\n del self.dict[val]\n return True\n return False\n\n def getRandom(self) -> int:\n return choice(self.list)\n\n\n# Your RandomizedSet object will be instantiated and called as such:\n# obj = RandomizedSet()\n# param_1 = obj.insert(val)\n# param_2 = obj.remove(val)\n# param_3 = obj.getRandom()\n```\n","/hybrid-structures/lru-cache":"---\ntitle: '146. LRU Cache'\ndescription: Design a data structure that follows the constraints of a Least Recently Used (LRU) cache\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Linked List\", slug: \"linked-list\" }\n - { name: \"Design\", slug: \"design\" }\n - { name: \"Doubly-Linked List\", slug: \"doubly-linked-list\" }\nvideo: \"7ABFKPK2hD4\"\nissue: \"https://github.com/prdlk/leetcode/issues/87\"\ncompanies:\n - { name: \"NetApp\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Adobe\", period: \"Thirty Days\", frequency: 83.5 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 69.6 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 36.7 }\n - { name: \"TikTok\", period: \"Three Months\", frequency: 100 }\n - { name: \"Palo Alto Networks\", period: \"Three Months\", frequency: 92.8 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 88.5 }\n - { name: \"Goldman Sachs\", period: \"Three Months\", frequency: 66.3 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 53.2 }\n - { name: \"Airwallex\", period: \"Six Months\", frequency: 100 }\n - { name: \"Docusign\", period: \"Six Months\", frequency: 100 }\n - { name: \"Moloco\", period: \"Six Months\", frequency: 100 }\n - { name: \"Nutanix\", period: \"Six Months\", frequency: 100 }\n - { name: \"Oracle\", period: \"Six Months\", frequency: 100 }\n - { name: \"Twitch\", period: \"Six Months\", frequency: 100 }\n - { name: \"Snap\", period: \"Six Months\", frequency: 92.4 }\n - { name: \"Qualcomm\", period: \"Six Months\", frequency: 92 }\n - { name: \"Nvidia\", period: \"Six Months\", frequency: 91.1 }\n - { name: \"ByteDance\", period: \"Six Months\", frequency: 88.6 }\n - { name: \"MongoDB\", period: \"Six Months\", frequency: 87.7 }\n - { name: \"Expedia\", period: \"Six Months\", frequency: 87.5 }\n - { name: \"eBay\", period: \"Six Months\", frequency: 86.1 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 74.9 }\n - { name: \"Visa\", period: \"Six Months\", frequency: 69.7 }\n - { name: \"Uber\", period: \"Six Months\", frequency: 61.9 }\n - { name: \"Salesforce\", period: \"Six Months\", frequency: 50.2 }\nsidebar:\n label: 'LRU Cache'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: ``\n- Output: ``\n- Explanation: Input [\"LRUCache\", \"put\", \"put\", \"get\", \"put\", \"get\", \"put\", \"get\", \"get\", \"get\"] `[[2]`, [1, 1], [2, 2], `[1]`, [3, 3], `[2]`, [4, 4], `[1]`, `[3]`, [4]] Output [null, null, null, `1`, null, `-1`, null, `-1`, `3`, 4] Explanation LRUCache lRUCache = new LRUCache(2); lRUCache.put(1, `1`); // cache is {1=1} lRUCache.put(2, `2`); // cache is {1=1, 2=2} lRUCache.get(1); // return `1` lRUCache.put(3, `3`); // LRU `key` was `2`, evicts `key 2`, cache is {1=1, 3=3} lRUCache.get(2); // returns `-1` (not found) lRUCache.put(4, `4`); // LRU `key` was `1`, evicts `key 1`, cache is {4=4, 3=3} lRUCache.get(1); // return `-1` (not found) lRUCache.get(3); // return `3` lRUCache.get(4); // return `4`\n\n### Constraints:\n\n- `1 <= capacity <= 3000`\n- `0 <= key <= 10^4`\n- `0 <= value <= 10^5`\n- At most 2 * 10^5 calls will be made to get and put.\n\n## Approach\n\n```mermaid\nflowchart TD\n subgraph state[\"state — dict for O(1) lookup, doubly linked list for O(1) reorder\"]\n H[\"head — least recent\"] --- N[\"... nodes ...\"] --- T[\"tail — most recent\"]\n end\n subgraph get[\"get(key)\"]\n G1{\"key in cache?\"} -- no --> G2([\"return -1\"])\n G1 -- yes --> G3[\"_remove_node then _add_node — move it beside the tail\"]\n G3 --> G4([\"return node.value\"])\n end\n subgraph put[\"put(key, value)\"]\n P1{\"key in cache?\"} -- yes --> P2[\"update value, _remove_node then _add_node\"]\n P1 -- no --> P3[\"new Node, cache[key] = node, _add_node\"]\n P3 --> P4{\"len(cache) > capacity?\"}\n P4 -- yes --> P5[\"evict head.next — the least recent — and del cache[lru.key]\"]\n P4 -- no --> P6([\"done\"])\n P2 --> P6\n P5 --> P6\n end\n state --> get\n state --> put\n```\n\n## Solution\n\n```py\nclass Node:\n def __init__(self, key=0, value=0):\n self.key = key\n self.value = value\n self.prev = None\n self.next = None\n\n\nclass LRUCache:\n def __init__(self, capacity: int):\n self.capacity = capacity\n self.cache = {}\n self.head = Node()\n self.tail = Node()\n self.head.next = self.tail # pyright: ignore[reportAttributeAccessIssue]\n self.tail.prev = self.head # pyright: ignore[reportAttributeAccessIssue]\n\n def _remove_node(self, node):\n prev_node = node.prev\n next_node = node.next\n prev_node.next = next_node\n next_node.prev = prev_node\n\n def _add_node(self, node):\n # Insert just before the tail (most recent at end)\n node.next = self.tail\n node.prev = self.tail.prev\n self.tail.prev.next = node # pyright: ignore[reportAttributeAccessIssue]\n self.tail.prev = node\n\n def get(self, key: int) -> int:\n if key not in self.cache:\n return -1\n\n node = self.cache[key]\n self._remove_node(node)\n self._add_node(node)\n return node.value\n\n def put(self, key: int, value: int) -> None:\n if key in self.cache:\n node = self.cache[key]\n node.value = value\n self._remove_node(node)\n self._add_node(node)\n else:\n new_node = Node(key, value)\n self.cache[key] = new_node\n self._add_node(new_node)\n\n if len(self.cache) > self.capacity:\n lru = self.head.next # Oldest node is next to the head\n self._remove_node(lru)\n del self.cache[lru.key] # pyright: ignore[reportAttributeAccessIssue]\n\n\n# Your LRUCache object will be instantiated and called as such:\n# obj = LRUCache(capacity)\n# param_1 = obj.get(key)\n# obj.put(key,value)\n```\n","/intervals":"---\ntitle: Intervals\ndescription: 'Ranges with a start and an end are sorted, and then merged, counted, or scheduled.'\nhidden: true\n---\n\nAn interval is a pair `[start, end]` marking a stretch on a line:\n- the minutes a meeting occupies\n- the range a task runs\n- the span a sensor was active\n\n**What interval problems ask you?**\n\n- Do any two collide?\n- What does the timeline look like once you merge everything that touches?\n- How many rooms do you need so no two meetings share one?\n\n:::tip\nEvery one of those questions reduces to a single primitive: deciding whether two intervals overlap, and if they do, how to merge them or what part they share. Once that primitive is clear, the rest of the pattern is bookkeeping.\n:::\n\n## When two intervals overlap\n\nIt is easier to state when two intervals do not overlap. Interval `a` sits entirely before interval `b` when `a` finishes before `b` begins, that is `a.end < b.start`. By symmetry they also miss each other when `b` ends before `a` starts. If neither of those holds, the intervals overlap.\n\n\n\nNegating `a.end < b.start` or `b.end < a.start` gives the positive test directly: two intervals overlap exactly when `a.start <= b.end` and `b.start <= a.end`. That pair of comparisons is the whole overlap check.\n\n## The Merged Region\n\nWhen two intervals overlap, you can merge them into one interval that covers both. Its start is the earlier of the two starts, and its end is the later of the two ends: `[min(a.start, b.start), max(a.end, b.end)]`. The merged interval begins as soon as either interval starts, and it ends only once both have stopped.\n\n\n\n## The Shared Region\n\nWhen two intervals overlap, the part they share is itself an interval. Its start is the later of the two starts, and its end is the earlier of the two ends: `[max(a.start, b.start), min(a.end, b.end)]`. The overlap begins only once both intervals have started, and it ends the moment either one stops.\n\n\n\n:::tip\nMerge grows, overlap shrinks. Merge uses `[min, max]` and covers both intervals. Overlap uses `[max, min]` and keeps only the part both share.\n:::\n\n## Sort by Start first\n\nLeft unsorted, checking a list of intervals means comparing every pair, which is `O(n^2)`. Sorting the intervals by start time removes that cost. Once the list is in start order, any interval can only overlap ones that come after it and begin before it ends, so a single left-to-right pass is enough to merge, count, or detect collisions.\n\nSorting by start is the standard first line of almost every interval solution, and the problems below all lean on it.\n\nThat gives the template every interval problem in this section builds on: sort by start, then sweep once, comparing each interval against the last one you kept.\n\n\n:::note[Input]\n`[[1,3], [8,10], [2,6], [4,5], [10,12]]`\n:::\n\n:::note[After sort]\n`[[1,3], [2,6], [4,5], [8,10], [10,12]]`\n:::\n\n```python\n# Time: O(n log n)\n# Space: O(n)\ndef interval_pattern(intervals):\n # 1. Sort by start time\n intervals.sort(key=lambda x: x[0])\n result = []\n for start, end in intervals:\n # 2. Overlap with the last kept interval? start <= last end\n if result and start <= result[-1][1]:\n # Merge: extend the end of the last interval\n result[-1][1] = max(result[-1][1], end)\n else:\n # 3. No overlap: keep this interval as-is\n result.append([start, end])\n return result\n```\n\n**Show trace table**\n\n| i | `[start, end]` | `result[-1]` | `start <= result[-1][1]`? | Action | `result` after |\n|---|---|---|---|---|---|\n| 0 | `[1,3]` | — | — (empty) | append | `[[1,3]]` |\n| 1 | `[2,6]` | `[1,3]` | `2 <= 3` ✅ | end = `max(3,6)` = 6 | `[[1,6]]` |\n| 2 | `[4,5]` | `[1,6]` | `4 <= 6` ✅ | end = `max(6,5)` = 6 | `[[1,6]]` |\n| 3 | `[8,10]` | `[1,6]` | `8 <= 6` ❌ | append | `[[1,6], [8,10]]` |\n| 4 | `[10,12]` | `[8,10]` | `10 <= 10` ✅ | end = `max(10,12)` = 12 | `[[1,6], [8,12]]` |\n\n\n:::tip[Output]\n`[[1,6], [8,12]]`\n - **Row 2:** `[4,5]` sits inside `[1,6]`. Setting `end = 5` without `max` would cut the interval short. `max` keeps 6.\n - **Row 4:** `[8,10]` and `[10,12]` only touch at 10. The `<=` check merges them. With `<`, you would get two intervals.\n:::\n\n\n## Insert into a sorted list\n\nSometimes the list is already sorted and has no overlaps, and you must add one new interval. You could append it, sort, and run the template. That works, but it pays `O(n log n)` for a sort you do not need.\n\nA sorted list with no overlaps splits into three parts around the new interval, in this order:\n\n1. Intervals that end **before** the new one starts. Keep them as they are.\n2. Intervals that **overlap** the new one. Merge each into it with `[min, max]`.\n3. Intervals that start **after** the new one ends. Keep them as they are.\n\nWalk the list once and handle each part in turn. No sort, so the cost drops to `O(n)`.\n\n:::note[Intervals]\n`[[1,2], [3,5], [6,7], [8,10], [12,16]]`\n:::\n\n:::note[New interval]\n`[4,8]`\n:::\n\n```python\n# Time: O(n)\n# Space: O(n)\ndef insert_interval(intervals, new_interval):\n result = []\n i, n = 0, len(intervals)\n start, end = new_interval\n\n # 1. Ends before new starts: keep\n while i < n and intervals[i][1] < start:\n result.append(intervals[i])\n i += 1\n\n # 2. Overlaps new: grow new to cover it\n while i < n and intervals[i][0] <= end:\n start = min(start, intervals[i][0])\n end = max(end, intervals[i][1])\n i += 1\n result.append([start, end])\n\n # 3. Starts after new ends: keep\n result.extend(intervals[i:])\n return result\n```\n\n**Show trace table**\n\n| i | interval | Part | Check | Action | `[start, end]` | `result` after |\n|---|---|---|---|---|---|---|\n| 0 | `[1,2]` | 1 | `2 < 4` ✅ | keep | `[4,8]` | `[[1,2]]` |\n| 1 | `[3,5]` | 1 | `5 < 4` ❌ | go to part 2 | `[4,8]` | `[[1,2]]` |\n| 1 | `[3,5]` | 2 | `3 <= 8` ✅ | start = `min(4,3)` = 3 | `[3,8]` | `[[1,2]]` |\n| 2 | `[6,7]` | 2 | `6 <= 8` ✅ | end = `max(8,7)` = 8 | `[3,8]` | `[[1,2]]` |\n| 3 | `[8,10]` | 2 | `8 <= 8` ✅ | end = `max(8,10)` = 10 | `[3,10]` | `[[1,2]]` |\n| 4 | `[12,16]` | 2 | `12 <= 10` ❌ | append new | `[3,10]` | `[[1,2], [3,10]]` |\n| 4 | `[12,16]` | 3 | — | extend rest | — | `[[1,2], [3,10], [12,16]]` |\n\n:::tip[Output]\n`[[1,2], [3,10], [12,16]]`\n - **Row i=1:** The same interval shows up twice. The part 1 check fails, so that loop stops, and part 2 reads the same interval again. `i` does not move.\n - **Row i=3:** `[8,10]` only touches `[3,8]` at 8. The `<=` merges them, since one shared point counts as an overlap.\n - **Part 1 uses `<`, part 2 uses `<=`:** An interval that ends exactly at `start` touches the new interval. It must fail part 1 so part 2 can merge it.\n:::\n\n:::warning\nIf the input is already sorted, do not sort again. Interviewers add \"sorted\" to the problem on purpose. Name the `O(n log n)` cost of re-sorting before they do, then offer the `O(n)` three-part walk.\n:::\n\n### Footnote\n\nThe positive overlap test comes straight from De Morgan's law. Negating `(a.end < b.start)` or `(b.end < a.start)` gives `(a.end >= b.start)` and `(b.end >= a.start)`, which is the `a.start <= b.end` and `b.start <= a.end` condition rewritten.\n","/intervals/insert-interval":"---\ntitle: '57. Insert Interval'\ndescription: You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the i^th interval and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\nissue: \"https://github.com/prdlk/leetcode/issues/181\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 39.1 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 24.1 }\nsidebar:\n label: 'Insert Interval'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `intervals = [[1,3],[6,9]], newInterval = [2,5]`\n- Output: `[[1,5],[6,9]]`\n\n### Example 2:\n- Input: `intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]`\n- Output: `[[1,2],[3,10],[12,16]]`\n- Explanation: Because the new interval `[4,8]` overlaps with [3,5],[6,7],[8,10].\n\n### Constraints:\n\n- `0 <= intervals.length <= 10^4`\n- `intervals[i].length == 2`\n- `0 <= starti <= endi <= 10^5`\n- intervals is sorted by starti in ascending order.\n- `newInterval.length == 2`\n- `0 <= start <= end <= 10^5`\n\n## Solution\n\n```py\nclass Solution:\n def insert(\n self, intervals: list[list[int]], newInterval: list[int]\n ) -> list[list[int]]:\n result = []\n i, n = 0, len(intervals)\n start, end = newInterval\n\n # Everything before\n while i < n and intervals[i][1] < start:\n result.append(intervals[i])\n i += 1\n\n # Everything merged\n while i < n and intervals[i][0] <= end:\n start = min(start, intervals[i][0])\n end = max(end, intervals[i][1])\n i += 1\n result.append([start, end])\n\n # Everything after\n result.extend(intervals[i:])\n return result\n```\n","/intervals/merge-intervals":"---\ntitle: '56. Merge Intervals'\ndescription: Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Sorting\", slug: \"sorting\" }\n - { name: \"Quicksort\", slug: \"quicksort\" }\nissue: \"https://github.com/prdlk/leetcode/issues/182\"\ncompanies:\n - { name: \"Palo Alto Networks\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 83.1 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 75.8 }\n - { name: \"Infosys\", period: \"Thirty Days\", frequency: 68.6 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 36.7 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 100 }\n - { name: \"Coupang\", period: \"Three Months\", frequency: 100 }\n - { name: \"IBM\", period: \"Three Months\", frequency: 100 }\n - { name: \"MongoDB\", period: \"Three Months\", frequency: 100 }\n - { name: \"Atlassian\", period: \"Three Months\", frequency: 89.6 }\n - { name: \"Salesforce\", period: \"Three Months\", frequency: 88 }\n - { name: \"Yandex\", period: \"Three Months\", frequency: 86.7 }\n - { name: \"Visa\", period: \"Three Months\", frequency: 80.9 }\n - { name: \"Goldman Sachs\", period: \"Three Months\", frequency: 57 }\n - { name: \"American Express\", period: \"Six Months\", frequency: 100 }\n - { name: \"Expedia\", period: \"Six Months\", frequency: 100 }\n - { name: \"General Motors\", period: \"Six Months\", frequency: 100 }\n - { name: \"Nextdoor\", period: \"Six Months\", frequency: 100 }\n - { name: \"Ozon\", period: \"Six Months\", frequency: 100 }\n - { name: \"Ripple\", period: \"Six Months\", frequency: 100 }\n - { name: \"Tesco\", period: \"Six Months\", frequency: 100 }\n - { name: \"Morgan Stanley\", period: \"Six Months\", frequency: 83.1 }\n - { name: \"Walmart Labs\", period: \"Six Months\", frequency: 81.4 }\n - { name: \"Tesla\", period: \"Six Months\", frequency: 80.9 }\n - { name: \"Zoho\", period: \"Six Months\", frequency: 79.6 }\n - { name: \"eBay\", period: \"Six Months\", frequency: 75.2 }\n - { name: \"TikTok\", period: \"Six Months\", frequency: 64.9 }\n - { name: \"Capital One\", period: \"Six Months\", frequency: 64.2 }\n - { name: \"Uber\", period: \"Six Months\", frequency: 55.2 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 54.1 }\nsidebar:\n label: 'Merge Intervals'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `intervals = [[1,3],[2,6],[8,10],[15,18]]`\n- Output: `[[1,6],[8,10],[15,18]]`\n- Explanation: Since `intervals [1,3]` and `[2,6]` overlap, merge them into `[1,6]`.\n\n### Example 2:\n- Input: `intervals = [[1,4],[4,5]]`\n- Output: `[[1,5]]`\n- Explanation: Intervals `[1,4]` and `[4,5]` are considered overlapping.\n\n### Example 3:\n- Input: `intervals = [[4,7],[1,4]]`\n- Output: `[[1,7]]`\n- Explanation: Intervals `[1,4]` and `[4,7]` are considered overlapping.\n\n### Constraints:\n\n- `1 <= intervals.length <= 10^4`\n- `intervals[i].length == 2`\n- `0 <= starti <= endi <= 10^4`\n\n## Solution\n\n```py\nclass Solution:\n def merge(self, intervals: list[list[int]]) -> list[list[int]]:\n intervals.sort(key=lambda x: x[0])\n result = []\n\n for start, end in intervals:\n if result and start <= result[-1][1]:\n result[-1][1] = max(result[-1][1], end)\n else:\n result.append([start, end])\n\n return result\n```\n","/intervals/non-overlapping-intervals":"---\ntitle: '435. Non-overlapping Intervals'\ndescription: Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\n - { name: \"Greedy\", slug: \"greedy\" }\n - { name: \"Sorting\", slug: \"sorting\" }\nissue: \"https://github.com/prdlk/leetcode/issues/183\"\ncompanies:\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 57.2 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Grammarly\", period: \"Three Months\", frequency: 100 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 28.9 }\n - { name: \"IBM\", period: \"Six Months\", frequency: 60.1 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\nsidebar:\n label: 'Non-overlapping Intervals'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `intervals = [[1,2],[2,3],[3,4],[1,3]]`\n- Output: `1`\n- Explanation: `[1,3]` can be removed and the rest of the `intervals` are non-overlapping.\n\n### Example 2:\n- Input: `intervals = [[1,2],[1,2],[1,2]]`\n- Output: `2`\n- Explanation: You need to remove two `[1,2]` to make the rest of the `intervals` non-overlapping.\n\n### Example 3:\n- Input: `intervals = [[1,2],[2,3]]`\n- Output: `0`\n- Explanation: You don't need to remove any of the `intervals` since they're already non-overlapping.\n\n### Constraints:\n\n- `1 <= intervals.length <= 10^5`\n- `intervals[i].length == 2`\n- `-5 * 10^4 <= starti < endi <= 5 * 10^4`\n\n## Solution\n\n```py\nclass Solution:\n def eraseOverlapIntervals(self, intervals: list[list[int]]) -> int:\n intervals.sort(key=lambda x: x[0])\n result = []\n\n for start, end in intervals:\n # Use '<' to avoid intervals which touch\n if result and start < result[-1][1]:\n result[-1][1] = min(result[-1][1], end)\n else:\n result.append([start, end])\n\n # Difference between original and result is to remove\n return len(intervals) - len(result)\n```\n","/keywords":"---\ntitle: Keywords\ndescription: Coding problems rarely tell you which algorithm to use, but the wording usually leaks it. \"K-th largest\" hints at a heap. \"Shortest path\" in an unweighted graph hints at BFS.\nicon: map\n---\n\nEach entry below names the keyword, the algorithm it points to, and a one-line reason. Where a keyword has more than one possible technique, each option is listed with the sub-signal that picks it. Techniques you haven't studied yet are covered later in the course.\n\n## \"Top K\"\n\nHeap — track the K-item boundary in O(n log K) instead of sorting all n. K Closest Points\n\n## \"Sorted\" / \"rotated sorted\" / \"in O(log n)\"\n\nBinary Search — halve the search space at each step using order. Works on any monotonic predicate, not just literal arrays. Binary Search\n\n## \"How many ways...\"\n\nCounting distinct outcomes — choose by what the input looks like.\n\n- DFS, when choices form a tree of decisions. Decode Ways\n- DP, when subproblems overlap on a grid or sequence. Robot Paths\n\n## \"Substring\"\n\nSliding Window — contiguous slices extend and shrink in O(1) at each step. Longest Substring Without Repeating Characters\n\n## \"Prefix\" / \"autocomplete\" / \"dictionary lookup\" / \"starts with\"\n\nTrie — share storage for common prefixes; lookup runs in O(length of word) regardless of dictionary size. Implement Trie\n\n## \"Palindrome\"\n\nPalindromes mirror around a center — choose by whether you're verifying or constructing.\n\n- Two Pointers, to verify a single string. Valid Palindrome\n- DFS, to enumerate every valid partition. Palindrome Partitioning\n- DP, to minimize cuts or count partitions. Palindrome Partitioning II\n\n## \"Tree\"\n\nOne question decides everything: do you care about depth, or just need to visit every node?\n\n- BFS, for level-order, shallowest depth, or \"right-side view\". Binary Tree Level-Order Traversal\n- DFS, for sums, heights, paths — anything not level-related. Max Depth\n\n## \"Parentheses\"\n\nStack — brackets close in reverse order, last opened first closed (LIFO). Valid Parentheses\n\n## \"Next greater\" / \"next smaller\" / \"warmer day\"\n\nMonotonic Stack — keep candidates in stack order so the answer for each element pops out in O(1) amortized. Next Greater Element II\n\n## \"Subarray\"\n\nContiguous slices, but the right tool depends on what you're computing.\n\n- Sliding Window, for fixed size or a monotonic constraint. Subarray Sum Fixed\n- Prefix Sum, for range-sum queries. Subarray Sum\n- Hashmap of prefix sums, for sum equals a target. Continuous Subarray Sum\n\n## \"Max subarray\"\n\nGreedy (Kadane's) — at each index, extend the run or restart; the choice is local. Kadane's Algorithm\n\n## \"Two sum / K sum\"\n\nTwo Pointers on a sorted array — each move strictly raises or lowers the sum. Two Sum\n\n## \"Max/longest sequence\"\n\n\"Longest\" usually means each position depends on earlier ones — except when a fixed window is involved.\n\n- DP / DFS with memoization, when a position depends on earlier positions. Longest Increasing Subsequence\n- Monotonic Deque, for max within a sliding window. Sliding Window Maximum\n\n## \"Minimum/Shortest\"\n\nThree very different problems wear the same word.\n\n- BFS, for fewest edges in an unweighted graph. Shortest Path\n- Dijkstra, for shortest path with non-negative edge weights. Dijkstra's Algorithm\n- DP / DFS, for cumulative cost on a grid or sequence. Minimal Path Sum\n\n## \"Partition/split\"\n\nDFS — each split point is a recursive choice; memoize if suffixes repeat. Decode Ways\n\n## \"Subsequence\"\n\nDP / DFS with memoization — pick-or-skip at each index, with overlapping subproblems. Longest Increasing Subsequence\n\n## \"All combinations / permutations / subsets\" / \"generate every\"\n\nBacktracking — DFS that records a partial solution, recurses, then undoes the choice on the way back up. Permutations, Subsets\n\n## \"Matrix\"\n\nA grid is a graph. Connectivity questions are BFS/DFS; optimization questions are DP.\n\n- BFS / DFS, for connectivity, flood-fill, or region counting. Flood Fill, Number of Islands\n- DP, for optimization where each cell builds on neighbors. Maximal Square\n\n## \"Jump\"\n\nGreedy when local-optimal is provably global; DP otherwise.\n\n- Greedy, when \"always reach as far as possible\" works (typically reachability). Jump Game\n- DP, when counting jumps or greedy fails. Jump Game II\n\n## \"Game\"\n\nDP on game states — \"can the player to move force a win?\" recurses with overlap. Divisor Game\n\n## \"Connected component\"\n\nUnion Find — near-O(1) union and find beat re-running BFS after each edge. Use BFS or DFS instead when the graph is built once and you only traverse a few times. Number of Connected Components\n\n## \"Schedule\" / \"prerequisites\" / \"build order\" / \"course\"\n\nTopological Sort (Kahn's BFS or DFS post-order) — directed dependencies must be linearized so every edge points forward. Detects cycles as a side effect. Course Schedule\n\n## \"Transitive relationship\"\n\nIf A relates to B and B relates to C means A relates to C, the relation is a graph in disguise. Items become nodes, relations become edges, and \"are these two related?\" becomes \"are they in the same component?\"\n\n- BFS, to answer a single query by traversing on the fly. Word Ladder\n- BFS or Union Find, for many queries on a fixed relation set. Sentence Similarity\n- BFS or Union Find, for numbers with divisional relationships. Evaluate Division\n\n## \"Interval\"\n\nGreedy after sorting — by start (merge) or by end (fit-in), then one pass. Interval Pattern\n","/linked-list":"---\ntitle: Linked List\ndescription: 'Each node points to the next node, and the pointers give the path through the data.'\nhidden: true\n---\n","/linked-list/linked-list-cycle":"---\ntitle: '141. Linked List Cycle'\ndescription: Given head, the head of a linked list, determine if the linked list has a cycle in it\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Linked List\", slug: \"linked-list\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"Floyd's Cycle Finding Algorithm\", slug: \"floyds-cycle-finding-algorithm\" }\nvideo: \"gBTe7lFR3vc\"\nissue: \"https://github.com/prdlk/leetcode/issues/79\"\ncompanies:\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Nvidia\", period: \"Three Months\", frequency: 87.7 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\nsidebar:\n label: 'Linked List Cycle'\n badge: 'Easy'\n---\n\n::::warning\nCan you solve it using O(1) (i.e. constant) memory?\n::::\n\n### Example 1:\n- Input: `head = [3,2,0,-4], pos = 1`\n- Output: `true`\n- Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).\n\n### Example 2:\n- Input: `head = [1,2], pos = 0`\n- Output: `true`\n- Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.\n\n### Example 3:\n- Input: `head = [1], pos = -1`\n- Output: `false`\n- Explanation: There is no cycle in the linked list.\n\n### Constraints:\n\n- The number of the nodes in the list is in the range [0, 10^4].\n- `-10^5 <= Node.val <= 10^5`\n- pos is -1 or a valid index in the linked-list.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"hasCycle(head)\"]) --> I[\"fast = slow = head\"]\n I --> W{\"fast and fast.next?\"}\n W -- no --> E([\"return False — ran off the end, no cycle\"])\n W -- yes --> A[\"fast = fast.next.next — two steps\"]\n A --> B[\"slow = slow.next — one step\"]\n B --> Q{\"fast is slow?\"}\n Q -- yes --> R([\"return True — the gap closed, so it loops\"])\n Q -- no --> W\n```\n\n## Solution\n\n```py\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, x):\n# self.val = x\n# self.next = None\n\n\nclass Solution:\n def hasCycle(self, head: Optional[ListNode]) -> bool:\n fast = slow = head\n\n while fast and fast.next:\n fast = fast.next.next\n slow = slow.next\n if fast is slow:\n return True\n return False\n```\n","/linked-list/merge-two-sorted-lists":"---\ntitle: '21. Merge Two Sorted Lists'\ndescription: You are given the heads of two sorted linked lists list1 and list2\nicon: dot\ntopics:\n - { name: \"Linked List\", slug: \"linked-list\" }\n - { name: \"Recursion\", slug: \"recursion\" }\nvideo: \"XIdigk956u0\"\nissue: \"https://github.com/prdlk/leetcode/issues/78\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 57.2 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Accenture\", period: \"Six Months\", frequency: 75 }\nsidebar:\n label: 'Merge Two Sorted Lists'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `list1 = [1,2,4], list2 = [1,3,4]`\n- Output: `[1,1,2,3,4,4]`\n\n### Example 2:\n- Input: `list1 = [], list2 = []`\n- Output: `[]`\n\n### Example 3:\n- Input: `list1 = [], list2 = [0]`\n- Output: `[0]`\n\n### Constraints:\n\n- The number of nodes in both lists is in the range [0, 50].\n- `-100 <= Node.val <= 100`\n- Both list1 and list2 are sorted in non-decreasing order.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"mergeTwoLists(list1, list2)\"]) --> G1{\"list1 empty?\"}\n G1 -- yes --> X1([\"return list2\"])\n G1 -- no --> G2{\"list2 empty?\"}\n G2 -- yes --> X2([\"return list1\"])\n G2 -- no --> H[\"head = the smaller first node; advance that list\"]\n H --> C[\"current = head\"]\n C --> W{\"list1 and list2 both non-empty?\"}\n W -- yes --> P[\"current.next = the smaller node; advance that list; current = current.next\"]\n P --> W\n W -- no --> T[\"current.next = list1 or list2 — append the leftover tail\"]\n T --> E([\"return head\"])\n```\n\n## Solution\n\n```py\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def mergeTwoLists(\n self, list1: Optional[ListNode], list2: Optional[ListNode]\n ) -> Optional[ListNode]:\n\n if not list1:\n return list2\n if not list2:\n return list1\n\n if list1.val < list2.val:\n head = list1\n list1 = list1.next\n else:\n head = list2\n list2 = list2.next\n\n current = head\n while list1 and list2:\n if list1.val < list2.val:\n current.next = list1\n list1 = list1.next\n else:\n current.next = list2\n list2 = list2.next\n current = current.next\n\n current.next = list1 or list2\n return head\n```\n","/linked-list/middle-of-the-linked-list":"---\ntitle: '876. Middle of the Linked List'\ndescription: Given the head of a singly linked list, return the middle node of the linked list\nicon: dot\ntopics:\n - { name: \"Linked List\", slug: \"linked-list\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\nissue: \"https://github.com/prdlk/leetcode/issues/82\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 50.2 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 49.8 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 39.1 }\n - { name: \"IBM\", period: \"Six Months\", frequency: 60.1 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 37.9 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\nsidebar:\n label: 'Middle of the Linked List'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `head = [1,2,3,4,5]`\n- Output: `[3,4,5]`\n- Explanation: The middle node of the list is node `3`.\n\n### Example 2:\n- Input: `head = [1,2,3,4,5,6]`\n- Output: `[4,5,6]`\n- Explanation: Since the list has two middle nodes with values `3` and `4`, we return the second one.\n\n### Constraints:\n\n- The number of nodes in the list is in the range [1, 100].\n- `1 <= Node.val <= 100`\n\n## Solution\n\n```py\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def middleNode(self, head: ListNode | None) -> ListNode | None:\n slow = fast = head\n while fast and fast.next:\n fast = fast.next.next\n slow = slow.next\n\n return slow\n```\n","/linked-list/remove-duplicates-from-sorted-list-ii":"---\ntitle: '82. Remove Duplicates from Sorted List II'\ndescription: You are given the head of a sorted linked list\nicon: dot\ntopics:\n - { name: \"Linked List\", slug: \"linked-list\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\nissue: \"https://github.com/prdlk/leetcode/issues/84\"\ncompanies:\n - { name: \"Amazon\", period: \"Three Months\", frequency: 34.6 }\n - { name: \"Google\", period: \"Three Months\", frequency: 23.9 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 34.2 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 24.5 }\nsidebar:\n label: 'Remove Duplicates from Sorted List II'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `head = [1,2,3,3,4,4,5]`\n- Output: `[1,2,5]`\n\n### Example 2:\n- Input: `head = [1,1,1,2,3]`\n- Output: `[2,3]`\n\n### Constraints:\n\n- The number of nodes in the list is in the range [0, 300].\n- `-100 <= Node.val <= 100`\n- The list is guaranteed to be sorted in ascending order.\n\n## Solution\n\n```py\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def deleteDuplicates(self, head: ListNode | None) -> ListNode | None:\n sentinel = ListNode(0, head)\n pred = sentinel\n\n while head:\n if head.next and head.val == head.next.val:\n while head.next and head.val == head.next.val:\n head = head.next\n\n pred.next = head.next\n else:\n pred = pred.next\n\n head = head.next\n\n return sentinel.next\n```\n","/linked-list/remove-nth-node-from-end-of-list":"---\ntitle: '19. Remove Nth Node From End of List'\ndescription: Given the head of a linked list, remove the n^th node from the end of the list and return its head\nicon: dot\ntopics:\n - { name: \"Linked List\", slug: \"linked-list\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\nissue: \"https://github.com/prdlk/leetcode/issues/80\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 51.1 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 46.8 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 43.3 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 24.1 }\nsidebar:\n label: 'Remove Nth Node From End of List'\n badge: 'Medium'\n---\n\n::::warning\nCould you do this in one pass?\n::::\n\n### Example 1:\n- Input: `head = [1,2,3,4,5], n = 2`\n- Output: `[1,2,3,5]`\n\n### Example 2:\n- Input: `head = [1], n = 1`\n- Output: `[]`\n\n### Example 3:\n- Input: `head = [1,2], n = 1`\n- Output: `[1]`\n\n### Constraints:\n\n- The number of nodes in the list is sz.\n- `1 <= sz <= 30`\n- `0 <= Node.val <= 100`\n- `1 <= n <= sz`\n\n## Solution\n\n```py\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def removeNthFromEnd(self, head: ListNode | None, n: int) -> ListNode | None:\n dummy = ListNode(0)\n dummy.next = head\n\n fast = dummy\n for _ in range(n):\n if fast.next is None:\n return head\n fast = fast.next\n\n slow = dummy\n while fast.next is not None:\n slow = slow.next\n fast = fast.next\n\n slow.next = slow.next.next\n\n return dummy.next\n```\n","/linked-list/reorder-list":"---\ntitle: '143. Reorder List'\ndescription: 'You are given the head of a singly linked-list. The list can be represented as:'\nicon: dot\ntopics:\n - { name: \"Linked List\", slug: \"linked-list\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"Stack\", slug: \"stack\" }\n - { name: \"Recursion\", slug: \"recursion\" }\nissue: \"https://github.com/prdlk/leetcode/issues/81\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 42.3 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 48.3 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 21.2 }\nsidebar:\n label: 'Reorder List'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `head = [1,2,3,4]`\n- Output: `[1,4,2,3]`\n\n### Example 2:\n- Input: `head = [1,2,3,4,5]`\n- Output: `[1,5,2,4,3]`\n\n### Constraints:\n\n- The number of nodes in the list is in the range [1, 5 * 10^4].\n- `1 <= Node.val <= 1000`\n\n## Solution\n\n```py\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def reorderList(self, head: ListNode | None) -> None:\n # Find middle\n slow, fast = head, head.next\n while fast and fast.next:\n slow = slow.next\n fast = fast.next.next\n\n # Reverse second half\n second = slow.next\n prev = slow.next = None\n while second:\n tmp = second.next\n second.next = prev\n prev = second\n second = tmp\n\n # merge two halfs\n first, second = head, prev\n while second:\n tmp1, tmp2 = first.next, second.next\n first.next = second\n second.next = tmp1\n first, second = tmp1, tmp2\n```\n","/linked-list/reverse-linked-list":"---\ntitle: '206. Reverse Linked List'\ndescription: Given the head of a singly linked list, reverse the list, and return the reversed list\nicon: dot\ntopics:\n - { name: \"Linked List\", slug: \"linked-list\" }\n - { name: \"Recursion\", slug: \"recursion\" }\nvideo: \"G0_I-ZF0S38\"\nissue: \"https://github.com/prdlk/leetcode/issues/77\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 50.2 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Nokia\", period: \"Three Months\", frequency: 100 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 66 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 57.2 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 78.4 }\nsidebar:\n label: 'Reverse Linked List'\n badge: 'Easy'\n---\n\n::::warning\nA linked list can be reversed either iteratively or recursively. Could you implement both?\n::::\n\n### Example 1:\n- Input: `head = [1,2,3,4,5]`\n- Output: `[5,4,3,2,1]`\n\n### Example 2:\n- Input: `head = [1,2]`\n- Output: `[2,1]`\n\n### Example 3:\n- Input: `head = []`\n- Output: `[]`\n\n### Constraints:\n\n- The number of nodes in the list is the range [0, 5000].\n- `-5000 <= Node.val <= 5000`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"reverseList(head)\"]) --> I[\"prev = None, curr = head\"]\n I --> W{\"curr?\"}\n W -- no --> E([\"return prev — the old tail is the new head\"])\n W -- yes --> A[\"next_ = curr.next — save it before overwriting\"]\n A --> B[\"curr.next = prev — flip the link\"]\n B --> C[\"prev = curr; curr = next_ — step forward\"]\n C --> W\n```\n\n## Solution\n\n```py\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:\n prev = None\n curr = head\n while curr:\n next_ = curr.next\n curr.next = prev\n prev = curr\n curr = next_\n return prev\n```\n","/linked-list/reverse-linked-list-ii":"---\ntitle: '92. Reverse Linked List II'\ndescription: Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list\nicon: dot\ntopics:\n - { name: \"Linked List\", slug: \"linked-list\" }\nissue: \"https://github.com/prdlk/leetcode/issues/83\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 83.1 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Google\", period: \"Three Months\", frequency: 28.8 }\nsidebar:\n label: 'Reverse Linked List II'\n badge: 'Medium'\n---\n\n::::warning\nCould you do it in one pass?\n::::\n\n### Example 1:\n- Input: `head = [1,2,3,4,5], left = 2, right = 4`\n- Output: `[1,4,3,2,5]`\n\n### Example 2:\n- Input: `head = [5], left = 1, right = 1`\n- Output: `[5]`\n\n### Constraints:\n\n- The number of nodes in the list is n.\n- `1 <= n <= 500`\n- `-500 <= Node.val <= 500`\n- `1 <= left <= right <= n`\n\n## Solution\n\n```py\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def reverseBetween(\n self, head: ListNode | None, left: int, right: int\n ) -> ListNode | None:\n cur, prev = head, None\n while left > 1:\n prev = cur\n cur = cur.next\n left, right = left - 1, right - 1\n\n tail, con = cur, prev\n while right:\n third = cur.next\n cur.next = prev\n prev = cur\n cur = third\n right -= 1\n\n if con:\n con.next = prev\n else:\n head = prev\n\n tail.next = cur\n return head\n```\n","/matrix-index-math":"---\ntitle: Matrix Index Math\ndescription: 'A two-dimensional grid is read as a flat array through a formula, and the neighbors are found by arithmetic.'\nhidden: true\n---\n","/matrix-index-math/rotate-image":"---\ntitle: '48. Rotate Image'\ndescription: You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise)\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Math\", slug: \"math\" }\n - { name: \"Matrix\", slug: \"matrix\" }\nvideo: \"fMSJSS7eO1w\"\nissue: \"https://github.com/prdlk/leetcode/issues/91\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 83.1 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Google\", period: \"Three Months\", frequency: 51.1 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 43.3 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 64.8 }\n - { name: \"Capital One\", period: \"Six Months\", frequency: 64.2 }\n - { name: \"IBM\", period: \"Six Months\", frequency: 60.1 }\nsidebar:\n label: 'Rotate Image'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `matrix = [[1,2,3],[4,5,6],[7,8,9]]`\n- Output: `[[7,4,1],[8,5,2],[9,6,3]]`\n\n### Example 2:\n- Input: `matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]`\n- Output: `[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]`\n\n### Constraints:\n\n- `n == matrix.length == matrix[i].length`\n- `1 <= n <= 20`\n- `-1000 <= matrix[i][j] <= 1000`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"rotate(matrix)\"]) --> N[\"n = len(matrix)\"]\n N --> F{\"more r in range(n)?\"}\n F -- yes --> G{\"more c in range(r + 1, n)?\"}\n G -- yes --> T[\"swap matrix[r][c] and matrix[c][r] — transpose across the diagonal\"]\n T --> G\n G -- no --> F\n F -- no --> H{\"more r in range(n)?\"}\n H -- yes --> R[\"matrix[r].reverse() — transpose then mirror is a clockwise turn\"]\n R --> H\n H -- no --> E([\"return None — rotated in place\"])\n```\n\n## Solution\n\n```py\nclass Solution:\n def rotate(self, matrix: List[List[int]]) -> None:\n n = len(matrix)\n for r in range(n):\n for c in range(r + 1, n):\n matrix[r][c], matrix[c][r] = matrix[c][r], matrix[r][c]\n\n for r in range(n):\n matrix[r].reverse()\n```\n","/matrix-index-math/set-matrix-zeroes":"---\ntitle: '73. Set Matrix Zeroes'\ndescription: Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Matrix\", slug: \"matrix\" }\nvideo: \"T41rL0L3Pnw\"\nissue: \"https://github.com/prdlk/leetcode/issues/93\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 48.7 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 43.3 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"Google\", period: \"Three Months\", frequency: 32.7 }\n - { name: \"tcs\", period: \"Six Months\", frequency: 43.5 }\nsidebar:\n label: 'Set Matrix Zeroes'\n badge: 'Medium'\n---\n\n::::warning\nYou must do it in place.\n::::\n\n### Example 1:\n- Input: `matrix = [[1,1,1],[1,0,1],[1,1,1]]`\n- Output: `[[1,0,1],[0,0,0],[1,0,1]]`\n\n### Example 2:\n- Input: `matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]`\n- Output: `[[0,0,0,0],[0,4,5,0],[0,3,1,0]]`\n\n### Constraints:\n\n- `m == matrix.length`\n- `n == matrix[0].length`\n- `1 <= m, n <= 200`\n- `-2^31 <= matrix[i][j] <= 2^31 - 1`\n- A straightforward solution using O(mn) space is probably a bad idea.\n- A simple improvement uses O(m + n) space, but still not the best solution.\n- Could you devise a constant space solution?\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"setZeroes(matrix)\"]) --> I[\"m = len(matrix), n = len(matrix[0])\"]\n I --> Z[\"row0_zero = any(matrix[0][c] == 0), col0_zero = any(matrix[r][0] == 0) — remember before they become markers\"]\n Z --> F{\"more r in range(1, m)?\"}\n F -- yes --> G{\"more c in range(1, n)?\"}\n G -- yes --> Q{\"matrix[r][c] == 0?\"}\n Q -- yes --> M[\"matrix[r][0] = 0, matrix[0][c] = 0 — row 0 and column 0 are the marker lists\"]\n M --> G\n Q -- no --> G\n G -- no --> F\n F -- no --> H{\"more r in range(1, m)?\"}\n H -- yes --> J{\"more c in range(1, n)?\"}\n J -- yes --> K{\"matrix[r][0] == 0 or matrix[0][c] == 0?\"}\n K -- yes --> W[\"matrix[r][c] = 0\"]\n W --> J\n K -- no --> J\n J -- no --> H\n H -- no --> A{\"row0_zero?\"}\n A -- yes --> B[\"matrix[0][c] = 0 for c in range(n)\"]\n B --> C{\"col0_zero?\"}\n A -- no --> C\n C -- yes --> D[\"matrix[r][0] = 0 for r in range(m)\"]\n D --> E([\"return None — zeroed in place\"])\n C -- no --> E\n```\n\n## Solution\n\n```py\nclass Solution:\n def setZeroes(self, matrix: List[List[int]]) -> None:\n \"\"\"\n Do not return anything, modify matrix in-place instead.\n \"\"\"\n m, n = len(matrix), len(matrix[0])\n\n # 1. remember if row 0 or column 0 have their own zeros\n row0_zero = any(matrix[0][c] == 0 for c in range(n))\n col0_zero = any(matrix[r][0] == 0 for r in range(m))\n\n # 2. use row 0 and column 0 as marker lists\n for r in range(1, m):\n for c in range(1, n):\n if matrix[r][c] == 0:\n matrix[r][0] = 0\n matrix[0][c] = 0\n\n # 3. zero the inner cells from the markers\n for r in range(1, m):\n for c in range(1, n):\n if matrix[r][0] == 0 or matrix[0][c] == 0:\n matrix[r][c] = 0\n\n # 4. handle row 0 and column 0 last\n if row0_zero:\n for c in range(n):\n matrix[0][c] = 0\n if col0_zero:\n for r in range(m):\n matrix[r][0] = 0\n```\n","/matrix-index-math/spiral-matrix":"---\ntitle: '54. Spiral Matrix'\ndescription: Given an m x n matrix, return all elements of the matrix in spiral order\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Matrix\", slug: \"matrix\" }\n - { name: \"Simulation\", slug: \"simulation\" }\nvideo: \"BJnMZNwUk1M\"\nissue: \"https://github.com/prdlk/leetcode/issues/92\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 57.2 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 36.7 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 46 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Capital One\", period: \"Six Months\", frequency: 73.8 }\n - { name: \"tcs\", period: \"Six Months\", frequency: 69.7 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 64.8 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 56.2 }\n - { name: \"TikTok\", period: \"Six Months\", frequency: 55.2 }\nsidebar:\n label: 'Spiral Matrix'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `matrix = [[1,2,3],[4,5,6],[7,8,9]]`\n- Output: `[1,2,3,6,9,8,7,4,5]`\n\n### Example 2:\n- Input: `matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]`\n- Output: `[1,2,3,4,8,12,11,10,9,5,6,7]`\n\n### Constraints:\n\n- `m == matrix.length`\n- `n == matrix[i].length`\n- `1 <= m, n <= 10`\n- `-100 <= matrix[i][j] <= 100`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"spiralOrder(matrix)\"]) --> I[\"res = [], top = 0, bottom = len(matrix) - 1, left = 0, right = len(matrix[0]) - 1\"]\n I --> W{\"top <= bottom and left <= right?\"}\n W -- no --> E([\"return res\"])\n W -- yes --> T[\"append matrix[top][c] for c in left..right, then top += 1 — top row, shrink from above\"]\n T --> R[\"append matrix[r][right] for r in top..bottom, then right -= 1 — right column\"]\n R --> B{\"top <= bottom?\"}\n B -- yes --> C[\"append matrix[bottom][c] for c in right down to left, then bottom -= 1 — bottom row\"]\n C --> L{\"left <= right?\"}\n B -- no --> L\n L -- yes --> D[\"append matrix[r][left] for r in bottom down to top, then left += 1 — left column\"]\n D --> W\n L -- no --> W\n```\n\n## Solution\n\n```py\nclass Solution:\n def spiralOrder(self, matrix: List[List[int]]) -> List[int]:\n res = []\n top, bottom = 0, len(matrix) - 1\n left, right = 0, len(matrix[0]) - 1\n\n while top <= bottom and left <= right:\n # 1. Top Row\n for c in range(left, right + 1):\n res.append(matrix[top][c])\n top += 1\n\n # 2. Right Column\n for r in range(top, bottom + 1):\n res.append(matrix[r][right])\n right -= 1\n\n # 3. Bottom Row\n if top <= bottom:\n for c in range(right, left - 1, -1):\n res.append(matrix[bottom][c])\n bottom -= 1\n\n # 4. Left Column\n if left <= right:\n for r in range(bottom, top - 1, -1):\n res.append(matrix[r][left])\n left += 1\n\n return res\n```\n","/matrix-index-math/valid-sudoku":"---\ntitle: '36. Valid Sudoku'\ndescription: 'Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:'\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Matrix\", slug: \"matrix\" }\nissue: \"https://github.com/prdlk/leetcode/issues/94\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 69 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 36.8 }\n - { name: \"Google\", period: \"Three Months\", frequency: 23.9 }\n - { name: \"Confluent\", period: \"Six Months\", frequency: 100 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 32.7 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 32 }\nsidebar:\n label: 'Valid Sudoku'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `board = [[\"5\",\"3\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\"] ,[\"6\",\".\",\".\",\"1\",\"9\",\"5\",\".\",\".\",\".\"] ,[\".\",\"9\",\"8\",\".\",\".\",\".\",\".\",\"6\",\".\"] ,[\"8\",\".\",\".\",\".\",\"6\",\".\",\".\",\".\",\"3\"] ,[\"4\",\".\",\".\",\"8\",\".\",\"3\",\".\",\".\",\"1\"] ,[\"7\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\",\"6\"] ,[\".\",\"6\",\".\",\".\",\".\",\".\",\"2\",\"8\",\".\"] ,[\".\",\".\",\".\",\"4\",\"1\",\"9\",\".\",\".\",\"5\"] ,[\".\",\".\",\".\",\".\",\"8\",\".\",\".\",\"7\",\"9\"]]`\n- Output: `true`\n\n### Example 2:\n- Input: `board = [[\"8\",\"3\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\"] ,[\"6\",\".\",\".\",\"1\",\"9\",\"5\",\".\",\".\",\".\"] ,[\".\",\"9\",\"8\",\".\",\".\",\".\",\".\",\"6\",\".\"] ,[\"8\",\".\",\".\",\".\",\"6\",\".\",\".\",\".\",\"3\"] ,[\"4\",\".\",\".\",\"8\",\".\",\"3\",\".\",\".\",\"1\"] ,[\"7\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\",\"6\"] ,[\".\",\"6\",\".\",\".\",\".\",\".\",\"2\",\"8\",\".\"] ,[\".\",\".\",\".\",\"4\",\"1\",\"9\",\".\",\".\",\"5\"] ,[\".\",\".\",\".\",\".\",\"8\",\".\",\".\",\"7\",\"9\"]]`\n- Output: `false`\n- Explanation: Same as Example `1`, except with the `5` in the top left corner being modified to `8`. Since there are two 8's in the top left 3x3 sub-box, it is invalid.\n\n### Constraints:\n\n- `board.length == 9`\n- `board[i].length == 9`\n- board[i][j] is a digit 1-9 or '.'.\n\n## Solution\n\n```py\nimport collections\n\n\nclass Solution:\n def isValidSudoku(self, board: list[list[str]]) -> bool:\n cols = collections.defaultdict(set)\n rows = collections.defaultdict(set)\n squares = collections.defaultdict(set) # (r/3, c/3)\n\n for r in range(9):\n for c in range(9):\n if board[r][c] == \".\":\n continue\n if (\n board[r][c] in rows[r]\n or board[r][c] in cols[c]\n or board[r][c] in squares[(r // 3, c // 3)]\n ):\n return False\n cols[c].add(board[r][c])\n rows[r].add(board[r][c])\n squares[(r // 3, c // 3)].add(board[r][c])\n\n return True\n```\n","/monotonic-stack":"---\ntitle: Monotonic Stack\ndescription: 'A stack keeps its items in sorted order, which finds the next larger or smaller item in one pass.'\nhidden: true\n---\n\n\n[Monotonic Stack](https://www.youtube.com/watch?v=Dq_ObZwTY_Q)\n\nA monotonic stack keeps its values in sorted order as you push elements. The order can be increasing or decreasing, depending on the problem.\n\nA decreasing monotonic stack is the most common version. Before pushing a new value `x`, we pop from the top while the top is smaller than or equal to `x`. After those pops, pushing `x` keeps the stack decreasing from bottom to top.\n\nThis push rule is the whole idea. A monotonic deque uses the same rule, but a deque also lets you remove old elements from the front, which is useful for sliding window problems.\n\n## Example Implementation\n\nThis implementation maintains a decreasing monotonic stack.\n\n```python\ndef mono_stack(insert_entries):\n stack = []\n for entry in insert_entries:\n # Pop smaller (or equal) values so stack stays decreasing.\n while stack and stack[-1] <= entry:\n stack.pop()\n # Do something with the popped item here\n stack.append(entry)\n```\n\n:::tip\nIn interview problems, the stack usually stores indices instead of values. You can read the value from the original array when needed, and the index gives you distance information such as **\"how many steps away is the next greater element.\"**\n:::\n\nFor an array `nums`, if index `j` pops when you process index `i`, then `i` is the first index to the right that satisfies the stack condition for `j`. In a next-greater-element problem, that means `nums[i]` is the next greater value for `nums[j]`.\n\n","/monotonic-stack/car-fleet":"---\ntitle: '853. Car Fleet'\ndescription: There are n cars at given miles away from the starting mile 0, traveling to reach the mile target\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Stack\", slug: \"stack\" }\n - { name: \"Sorting\", slug: \"sorting\" }\n - { name: \"Monotonic Stack\", slug: \"monotonic-stack\" }\nvideo: \"Pr6T-3yB9RM\"\nissue: \"https://github.com/prdlk/leetcode/issues/73\"\ncompanies:\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"Amazon\", period: \"Six Months\", frequency: 26 }\nsidebar:\n label: 'Car Fleet'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]`\n- Output: `3`\n- Explanation: • The cars starting at `10 (speed 2`) and `8 (speed 4`) become a fleet, meeting each other at `12`. The fleet forms at `target`. • The car starting at `0 (speed 1`) does not catch up to any other car, so it is a fleet by itself. • The cars starting at `5 (speed 1`) and `3 (speed 3`) become a fleet, meeting each other at `6`. The fleet moves at `speed 1` until it reaches `target`.\n\n### Example 2:\n- Input: `target = 10, position = [3], speed = [3]`\n- Output: `1`\n- Explanation: There is only one car, hence there is only one fleet.\n\n### Example 3:\n- Input: `target = 100, position = [0,2,4], speed = [4,2,1]`\n- Output: `1`\n- Explanation: • The cars starting at `0 (speed 4`) and `2 (speed 2`) become a fleet, meeting each other at `4`. The car starting at `4 (speed 1`) travels to `5`. • Then, the fleet at `4 (speed 2`) and the car at `position 5 (speed 1`) become one fleet, meeting each other at `6`. The fleet moves at `speed 1` until it reaches `target`.\n\n### Constraints:\n\n- `n == position.length == speed.length`\n- `1 <= n <= 10^5`\n- `0 < target <= 10^6`\n- `0 <= position[i] < target`\n- All the values of `position` are unique.\n- `0 < speed[i] <= 10^6`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"carFleet(target, position, speed)\"]) --> O[\"zip and sort by position, descending — nearest the target first\"]\n O --> I[\"stack = [] — arrival time of each fleet's leader\"]\n I --> L{\"more (pos, spd)?\"}\n L -- no --> E([\"return len(stack) — one entry per fleet\"])\n L -- yes --> T[\"time = (target - pos) / spd\"]\n T --> Q{\"stack empty or time > stack[-1]?\"}\n Q -- yes --> A[\"stack.append(time) — slower, so it starts a new fleet\"]\n Q -- no --> B[\"discard — it catches the car ahead and joins that fleet\"]\n A --> L\n B --> L\n```\n\n## Solution\n\n```py\nclass Solution:\n def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:\n stack = []\n for pos, spd in sorted(zip(position, speed), reverse=True):\n time = (target - pos) / spd\n if not stack or time > stack[-1]:\n stack.append(time)\n\n return len(stack)\n```\n","/monotonic-stack/daily-temperatures":"---\ntitle: '739. Daily Temperatures'\ndescription: Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the i^th day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Stack\", slug: \"stack\" }\n - { name: \"Monotonic Stack\", slug: \"monotonic-stack\" }\nvideo: \"cTBiBSnjO3c\"\nissue: \"https://github.com/prdlk/leetcode/issues/72\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Chime\", period: \"Three Months\", frequency: 100 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 59.7 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 42.8 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Anduril\", period: \"Six Months\", frequency: 92.8 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 44.8 }\n - { name: \"tcs\", period: \"Six Months\", frequency: 43.5 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 42.5 }\nsidebar:\n label: 'Daily Temperatures'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `temperatures = [73,74,75,71,69,72,76,73]`\n- Output: `[1,1,4,2,1,1,0,0]`\n\n### Example 2:\n- Input: `temperatures = [30,40,50,60]`\n- Output: `[1,1,1,0]`\n\n### Example 3:\n- Input: `temperatures = [30,60,90]`\n- Output: `[1,1,0]`\n\n### Constraints:\n\n- `1 <= temperatures.length <= 10^5`\n- `30 <= temperatures[i] <= 100`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"dailyTemperatures(temperatures)\"]) --> I[\"ans = zeros(n), stack = [] — indices of days still waiting\"]\n I --> L{\"more (i, temp)?\"}\n L -- no --> E([\"return ans\"])\n L -- yes --> W{\"stack non-empty and temperatures[stack[-1]] < temp?\"}\n W -- yes --> P[\"prev = stack.pop(); ans[prev] = i - prev — today resolves that day\"]\n P --> W\n W -- no --> A[\"stack.append(i)\"]\n A --> L\n```\n\n## Solution\n\n```py\nclass Solution:\n def dailyTemperatures(self, temperatures: List[int]) -> List[int]:\n ans = [0] * len(temperatures)\n stack = []\n for i, temp in enumerate(temperatures):\n while stack and temperatures[stack[-1]] < temp:\n prev = stack.pop()\n ans[prev] = i - prev\n stack.append(i)\n return ans\n```\n","/monotonic-stack/largest-rectangle-in-histogram":"---\ntitle: '84. Largest Rectangle in Histogram'\ndescription: Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Stack\", slug: \"stack\" }\n - { name: \"Monotonic Stack\", slug: \"monotonic-stack\" }\n - { name: \"Range Minimum/Maximum Query\", slug: \"range-minimum-maximum-query\" }\nissue: \"https://github.com/prdlk/leetcode/issues/76\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Accenture\", period: \"Three Months\", frequency: 88.6 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 73.7 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 36.8 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\nsidebar:\n label: 'Largest Rectangle in Histogram'\n badge: 'Hard'\n---\n\n### Example 1:\n- Input: `heights = [2,1,5,6,2,3]`\n- Output: `10`\n- Explanation: The above is a histogram where width of each bar is `1`. The largest rectangle is shown in the red area, which has an area = `10` units.\n\n### Example 2:\n- Input: `heights = [2,4]`\n- Output: `4`\n\n### Constraints:\n\n- `1 <= heights.length <= 10^5`\n- `0 <= heights[i] <= 10^4`\n\n## Solution\n\n```py\nclass Solution:\n def largestRectangleArea(self, heights: list[int]) -> int:\n maxArea = 0\n stack = []\n\n for i, h in enumerate(heights):\n start = i\n while stack and stack[-1][1] > h:\n index, height = stack.pop()\n width = i - index\n maxArea = max(maxArea, height * width)\n start = index\n stack.append((start, h))\n\n for i, h in stack:\n maxArea = max(maxArea, h * (len(heights) - i))\n\n return maxArea\n```\n","/monotonic-stack/next-greater-element-i":"---\ntitle: '496. Next Greater Element I'\ndescription: The next greater element of some element x in an array is the first greater element that is to the right of x in the same array\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Stack\", slug: \"stack\" }\n - { name: \"Monotonic Stack\", slug: \"monotonic-stack\" }\nissue: \"https://github.com/prdlk/leetcode/issues/74\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 46.6 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 59.7 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 43.3 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"tcs\", period: \"Six Months\", frequency: 43.5 }\nsidebar:\n label: 'Next Greater Element I'\n badge: 'Easy'\n---\n\n::::warning\nCould you find an O(nums1.length + nums2.length) solution?\n::::\n\n### Example 1:\n- Input: `nums1 = [4,1,2], nums2 = [1,3,4,2]`\n- Output: `[-1,3,-1]`\n- Explanation:\n - The next greater element for each value of `nums1` is as follows:\n - `4` is underlined in `nums2 = [1,3,4,2]`. There is no next greater element, so the answer is `-1`.\n - `1` is underlined in `nums2 = [1,3,4,2]`. The next greater element is `3`.\n - `2` is underlined in `nums2 = [1,3,4,2]`. There is no next greater element, so the answer is `-1`.\n\n### Example 2:\n- Input: `nums1 = [2,4], nums2 = [1,2,3,4]`\n- Output: `[3,-1]`\n- Explanation:\n - The next greater element for each value of `nums1` is as follows:\n - `2` is underlined in `nums2 = [1,2,3,4]`. The next greater element is `3`.\n - `4` is underlined in `nums2 = [1,2,3,4]`. There is no next greater element, so the answer is `-1`.\n\n### Constraints:\n\n- `1 <= nums1.length <= nums2.length <= 1000`\n- `0 <= nums1[i], nums2[i] <= 10^4`\n- All integers in `nums1` and `nums2` are unique.\n- All the integers of `nums1` also appear in nums2.\n\n## Solution\n\n```py\nclass Solution:\n def nextGreaterElement(self, nums1: list[int], nums2: list[int]) -> list[int]:\n mono_stack = []\n next_greater = {}\n\n for cur in nums2:\n while mono_stack and cur > mono_stack[-1]:\n val = mono_stack.pop()\n next_greater[val] = cur\n mono_stack.append(cur)\n\n return [next_greater.get(x, -1) for x in nums1]\n```\n","/monotonic-stack/online-stock-span":"---\ntitle: '901. Online Stock Span'\ndescription: Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day\nicon: dot\ntopics:\n - { name: \"Stack\", slug: \"stack\" }\n - { name: \"Design\", slug: \"design\" }\n - { name: \"Monotonic Stack\", slug: \"monotonic-stack\" }\n - { name: \"Data Stream\", slug: \"data-stream\" }\nissue: \"https://github.com/prdlk/leetcode/issues/75\"\ncompanies:\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"Amazon\", period: \"Six Months\", frequency: 26 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\nsidebar:\n label: 'Online Stock Span'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: ``\n- Output: ``\n- Explanation: Input [\"StockSpanner\", \"next\", \"next\", \"next\", \"next\", \"next\", \"next\", \"next\"] `[[]`, `[100]`, `[80]`, `[60]`, `[70]`, `[60]`, `[75]`, [85]] Output [null, `1`, `1`, `1`, `2`, `1`, `4`, 6] Explanation StockSpanner stockSpanner = new StockSpanner(); stockSpanner.next(100); // return `1` stockSpanner.next(80); // return `1` stockSpanner.next(60); // return `1` stockSpanner.next(70); // return `2` stockSpanner.next(60); // return `1` stockSpanner.next(75); // return `4`, because the last `4` prices (including today's `price` of `75`) were less than or equal to today's `price`. stockSpanner.next(85); // return `6`\n\n### Constraints:\n\n- `1 <= price <= 10^5`\n- At most 10^4 calls will be made to next.\n\n## Solution\n\n```py\nclass StockSpanner:\n def __init__(self):\n self.stack = []\n\n def next(self, price: int) -> int:\n ans = 1\n while self.stack and self.stack[-1][0] <= price:\n ans += self.stack.pop()[1]\n\n self.stack.append([price, ans])\n return ans\n\n\n# Your StockSpanner object will be instantiated and called as such:\n# obj = StockSpanner()\n# param_1 = obj.next(price)\n```\n","/multi-d-grid-dp":"---\ntitle: Multi-D / Grid DP\ndescription: 'The result for each state is stored in a table with two or more dimensions, and each cell is computed from its neighbors.'\nhidden: true\n---\n","/multi-d-grid-dp/longest-common-subsequence":"---\ntitle: '1143. Longest Common Subsequence'\ndescription: Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0\nicon: dot\ntopics:\n - { name: \"String\", slug: \"string\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\n - { name: \"Longest Common Subsequence\", slug: \"longest-common-subsequence\" }\nissue: \"https://github.com/prdlk/leetcode/issues/168\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 36.8 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 28.9 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 38.7 }\nsidebar:\n label: 'Longest Common Subsequence'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `text1 = \"abcde\", text2 = \"ace\"`\n- Output: `3`\n- Explanation: The longest common subsequence is \"ace\" and its length is `3`.\n\n### Example 2:\n- Input: `text1 = \"abc\", text2 = \"abc\"`\n- Output: `3`\n- Explanation: The longest common subsequence is \"abc\" and its length is `3`.\n\n### Example 3:\n- Input: `text1 = \"abc\", text2 = \"def\"`\n- Output: `0`\n- Explanation: There is no such common subsequence, so the result is `0`.\n\n### Constraints:\n\n- `1 <= text1.length, text2.length <= 1000`\n- `text1` and `text2` consist of only lowercase English characters.\n\n## Solution\n\n```py\nclass Solution:\n def longestCommonSubsequence(self, text1: str, text2: str) -> int:\n dp = [[0 for j in range(len(text2) + 1)] for i in range(len(text1) + 1)]\n\n for i in range(len(text1) - 1, -1, -1):\n for j in range(len(text2) - 1, -1, -1):\n if text1[i] == text2[j]:\n dp[i][j] = 1 + dp[i + 1][j + 1]\n else:\n dp[i][j] = max(dp[i][j + 1], dp[i + 1][j])\n\n return dp[0][0]\n```\n","/multi-d-grid-dp/minimum-path-sum":"---\ntitle: '64. Minimum Path Sum'\ndescription: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\n - { name: \"Matrix\", slug: \"matrix\" }\nissue: \"https://github.com/prdlk/leetcode/issues/167\"\ncompanies:\n - { name: \"Goldman Sachs\", period: \"Three Months\", frequency: 66.3 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 66 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 21.2 }\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 32 }\nsidebar:\n label: 'Minimum Path Sum'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `grid = [[1,3,1],[1,5,1],[4,2,1]]`\n- Output: `7`\n- Explanation: Because the path `1` → `3` → `1` → `1` → `1` minimizes the sum.\n\n### Example 2:\n- Input: `grid = [[1,2,3],[4,5,6]]`\n- Output: `12`\n\n### Constraints:\n\n- `m == grid.length`\n- `n == grid[i].length`\n- `1 <= m, n <= 200`\n- `0 <= grid[i][j] <= 200`\n\n## Solution\n\n```py\nclass Solution:\n def minPathSum(self, grid: list[list[int]]) -> int:\n if not grid or not grid[0]:\n return 0\n\n m = len(grid)\n n = len(grid[0])\n dp = [[0 for _ in range(n)] for _ in range(m)]\n\n for c in range(n):\n dp[0][c] = dp[0][c - 1] + grid[0][c] if c else grid[0][c]\n\n for r in range(m):\n dp[r][0] = dp[r - 1][0] + grid[r][0] if r else grid[r][0]\n\n for r in range(1, m):\n for c in range(1, n):\n dp[r][c] = min(dp[r - 1][c], dp[r][c - 1]) + grid[r][c]\n\n return dp[-1][-1]\n```\n","/multi-d-grid-dp/unique-paths":"---\ntitle: '62. Unique Paths'\ndescription: There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time\nicon: dot\ntopics:\n - { name: \"Math\", slug: \"math\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\n - { name: \"Combinatorics\", slug: \"combinatorics\" }\nissue: \"https://github.com/prdlk/leetcode/issues/166\"\ncompanies:\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 65.6 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 48.3 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 36.8 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 44.8 }\nsidebar:\n label: 'Unique Paths'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `m = 3, n = 7`\n- Output: `28`\n\n### Example 2:\n- Input: `m = 3, n = 2`\n- Output: `3`\n- Explanation: From the top-left corner, there are a total of `3` ways to reach the bottom-right corner: `1`. Right -> Down -> Down `2`. Down -> Down -> Right `3`. Down -> Right -> Down\n\n### Constraints:\n\n- `1 <= m, n <= 100`\n\n## Solution\n\n```py\nclass Solution:\n def uniquePaths(self, m: int, n: int) -> int:\n dp = [[0 for _ in range(n)] for _ in range(m)]\n\n for c in range(n):\n dp[0][c] = 1\n\n for r in range(m):\n dp[r][0] = 1\n\n for r in range(1, m):\n for c in range(1, n):\n dp[r][c] = dp[r - 1][c] + dp[r][c - 1]\n\n return dp[-1][-1]\n```\n","/prefix-sum":"---\ntitle: Prefix Sum\ndescription: 'A running total is stored at each index, so the sum of any range is one subtraction.'\nhidden: true\n---\n\nSuppose you are handed an array and asked for the sum of the elements between index `i` and index `j`. One query is easy: loop from `i` to `j` and add. But now imagine thousands of such queries over the same array, or a search that inspects the sum of every possible subarray. Recomputing each range from scratch costs `O(n)` per query, and the work piles up fast.\n\nPrefix sum removes that repetition. You spend `O(n)` once to precompute running totals, and afterward every range sum collapses into a single subtraction that runs in `O(1)`, no matter how wide the range.\n\n[Prefix Sum Explained](https://www.youtube.com/watch?v=CRVxUgmIO8M)\n\n## Transformation\n\nA prefix sum turns an array into a new array of running totals. For an input array `arr` of length `n`, build a `prefix` array of length `n + 1`:\n\n`prefix[0] = 0`\n\n`prefix[k] = arr[0] + arr[1] + ... + arr[k-1]`\n\nEach entry is the previous total plus one more element, so a single pass builds the whole thing:\n\n`prefix[k] = prefix[k-1] + arr[k-1]`\n\nThe leading `prefix[0] = 0` is the sum of zero elements. That extra slot is what makes the range math below land exactly on the boundaries, which is why `prefix` has one more cell than `arr`.\n\n\n\n\nEach bar is the running total up to that index. Every step keeps the previous total (blue) and adds one new element (amber)\n\n## Any range sum is one subtraction\n\nThe sum of the subarray from index `i` up to (but not including) `j` is `prefix[j] - prefix[i]`. `prefix[j]` is the total of everything before `j`, and `prefix[i]` is the total of everything before `i`, so subtracting throws away the front you did not ask for and leaves the middle. \n\n## Watch the boundaries\n\nThe one place prefix sums trip people up is the off-by-one at the edges. Keep three facts straight: `prefix` has length `n + 1`, `prefix[0]` is `0`, and `sum(arr[i..j-1]) = prefix[j] - prefix[i]` uses an exclusive right index `j`. If a problem hands you an inclusive right index `r`, translate it to `prefix[r + 1] - prefix[i]`.\n\n## When to reach for it\n\nPrefix sums fit whenever you need repeated range sums over a fixed array, or when an algorithm searches across subarray boundaries. For a single one-off sum, a plain loop is simpler; the prefix array earns its `O(n)` setup only once the same array is queried many times.\n","/prefix-sum/contiguous-array":"---\ntitle: '525. Contiguous Array'\ndescription: Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Prefix Sum\", slug: \"prefix-sum\" }\nissue: \"https://github.com/prdlk/leetcode/issues/39\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Cisco\", period: \"Three Months\", frequency: 100 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 49.8 }\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"Akamai\", period: \"Six Months\", frequency: 100 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 40.6 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 32 }\nsidebar:\n label: 'Contiguous Array'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [0,1]`\n- Output: `2`\n- Explanation: [0, 1] is the longest contiguous subarray with an equal number of `0` and `1`.\n\n### Example 2:\n- Input: `nums = [0,1,0]`\n- Output: `2`\n- Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of `0` and `1`.\n\n### Example 3:\n- Input: `nums = [0,1,1,1,1,1,0,0,0]`\n- Output: `6`\n- Explanation: `[1,1,1,0,0,0]` is the longest contiguous subarray with equal number of `0` and `1`.\n\n### Constraints:\n\n- `1 <= nums.length <= 10^5`\n- `nums[i]` is either 0 or 1.\n\n## Solution\n\n```py\nclass Solution:\n def findMaxLength(self, nums: List[int]) -> int:\n map = {0: -1}\n count = 0\n longest = 0\n\n for i, num in enumerate(nums):\n count += 1 if num == 1 else -1\n\n if count in map:\n longest = max(longest, i - map[count])\n else:\n map[count] = i\n\n return longest\n```\n","/prefix-sum/product-of-array-except-self":"---\ntitle: '238. Product of Array Except Self'\ndescription: Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Prefix Sum\", slug: \"prefix-sum\" }\nvideo: \"bNvIQI2wAjk\"\nissue: \"https://github.com/prdlk/leetcode/issues/37\"\ncompanies:\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 70.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 61.3 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 57.2 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 42.3 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 59.7 }\n - { name: \"Asana\", period: \"Six Months\", frequency: 100 }\n - { name: \"tcs\", period: \"Six Months\", frequency: 43.5 }\nsidebar:\n label: 'Product of Array Except Self'\n badge: 'Medium'\n---\n\n::::warning\nYou must write an algorithm that runs in O(n) time and without using the division operation.\nCan you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)\n::::\n\n### Example 1:\n- Input: `nums = [1,2,3,4]`\n- Output: `[24,12,8,6]`\n\n### Example 2:\n- Input: `nums = [-1,1,0,-3,3]`\n- Output: `[0,0,9,0,0]`\n\n### Constraints:\n\n- `2 <= nums.length <= 10^5`\n- `-30 <= nums[i] <= 30`\n- The input is generated such that `answer[i]` is guaranteed to fit in a 32-bit integer.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"productExceptSelf(nums)\"]) --> I[\"answer = ones(n), rightArr = ones(n)\"]\n I --> P1[\"Pass 1 — left to right: answer[i] = nums[i-1] * answer[i-1]\"]\n P1 --> P2[\"Pass 2 — right to left: rightArr[i] = nums[i+1] * rightArr[i+1]\"]\n P2 --> P3[\"Pass 3: answer[i] *= rightArr[i]\"]\n P3 --> E([\"return answer — no division needed\"])\n```\n\n## Solution\n\n```js\n/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar productExceptSelf = function(nums) {\n const n = nums.length;\n const answer = new Array(n).fill(1);\n const rightArr = new Array(n).fill(1);\n\n for (let i = 1; i < n; i++) {\n answer[i] = nums[i - 1] * answer[i - 1];\n }\n for (let i = n - 2; i >= 0; i--) {\n rightArr[i] = nums[i + 1] * rightArr[i + 1];\n }\n for (let i = 0; i < n; i++) {\n answer[i] *= rightArr[i];\n }\n return answer;\n};\n```\n","/prefix-sum/range-sum-query-immutable":"---\ntitle: '303. Range Sum Query - Immutable'\ndescription: Given an integer array nums, handle multiple queries of the following type\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Design\", slug: \"design\" }\n - { name: \"Prefix Sum\", slug: \"prefix-sum\" }\nvideo: \"2pndAmo_sMA\"\nissue: \"https://github.com/prdlk/leetcode/issues/36\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 42.3 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 24.5 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 24.1 }\nsidebar:\n label: 'Range Sum Query - Immutable'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `[\"NumArray\", \"sumRange\", \"sumRange\", \"sumRange\"]` `[[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]`\n- Output: `[null, 1, -1, -3]`\n- Explanation: `NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]);` `numArray.sumRange(0, 2); // return (-2) + 0 + 3 = 1` `numArray.sumRange(2, 5); // return 3 + (-5) + 2 + (-1) = -1` `numArray.sumRange(0, 5); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3`\n\n### Constraints:\n\n- `1 <= nums.length <= 10^4`\n- `-10^5 <= nums[i] <= 10^5`\n- `0 <= left <= right < nums.length`\n- At most `10^4` calls will be made to `sumRange`.\n\n## Approach\n\n```mermaid\nflowchart TD\n subgraph build[\"constructor(nums) — O(n) once\"]\n A[\"prefix = [0]\"] --> B{\"more n in nums?\"}\n B -- yes --> C[\"prefix.push(last + n)\"]\n C --> B\n end\n build --> Q\n subgraph Q[\"sumRange(left, right) — O(1) per call\"]\n D([\"return prefix[right + 1] - prefix[left]\"])\n end\n```\n\n## Solution\n\n```js\n/**\n * @param {number[]} nums\n */\nclass NumArray {\n constructor(nums) {\n this.prefix = [0];\n for (let n of nums){\n this.prefix.push(this.prefix[this.prefix.length - 1] + n);\n }\n }\n /**\n * @param {number} left\n * @param {number} right\n * @return {number}\n */\n sumRange(left, right) {\n return this.prefix[right + 1] - this.prefix[left];\n }\n}\n\n\n/** \n * Your NumArray object will be instantiated and called as such:\n * var obj = new NumArray(nums)\n * var param_1 = obj.sumRange(left,right)\n */\n```\n","/prefix-sum/subarray-sum-equals-k":"---\ntitle: '560. Subarray Sum Equals K'\ndescription: Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Prefix Sum\", slug: \"prefix-sum\" }\nvideo: \"fFVZt-6sgyo\"\nissue: \"https://github.com/prdlk/leetcode/issues/38\"\ncompanies:\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 63.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 50.2 }\n - { name: \"Visa\", period: \"Three Months\", frequency: 80.9 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 74.8 }\n - { name: \"Salesforce\", period: \"Three Months\", frequency: 64.7 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 57.1 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 48.3 }\n - { name: \"Accenture\", period: \"Six Months\", frequency: 100 }\n - { name: \"IBM\", period: \"Six Months\", frequency: 94.5 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 76.3 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 74.7 }\n - { name: \"josh technology\", period: \"Six Months\", frequency: 74 }\n - { name: \"Walmart Labs\", period: \"Six Months\", frequency: 70.5 }\n - { name: \"Yandex\", period: \"Six Months\", frequency: 69.2 }\nsidebar:\n label: 'Subarray Sum Equals K'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [1,1,1], k = 2`\n- Output: `2`\n\n### Example 2:\n- Input: `nums = [1,2,3], k = 3`\n- Output: `2`\n\n### Constraints:\n\n- `1 <= nums.length <= 2 * 10^4`\n- `-1000 <= nums[i] <= 1000`\n- `-10^7 <= k <= 10^7`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"subarraySum(nums, k)\"]) --> I[\"map = {0: 1} — the empty prefix; sum = 0, count = 0\"]\n I --> L{\"more n in nums?\"}\n L -- no --> E([\"return count\"])\n L -- yes --> A[\"sum += n\"]\n A --> Q{\"map has sum - k?\"}\n Q -- yes --> C[\"count += map.get(sum - k) — every earlier prefix that closes a window\"]\n Q -- no --> W[\"map.set(sum, count of sum + 1)\"]\n C --> W\n W --> L\n```\n\n## Solution\n\n```js\n/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar subarraySum = function(nums, k) {\n const map = new Map();\n map.set(0, 1);\n\n let sum = 0;\n let count = 0;\n\n for (let n of nums){\n sum += n;\n\n if(map.has(sum - k)){\n count += map.get(sum - k);\n }\n\n map.set(sum, (map.get(sum) || 0) + 1);\n }\n return count\n};\n```\n","/prefix-sum/subarray-sums-divisible-by-k":"---\ntitle: '974. Subarray Sums Divisible by K'\ndescription: Given an integer array nums and an integer k, return the number of non-empty subarrays that have a sum divisible by k\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Prefix Sum\", slug: \"prefix-sum\" }\nissue: \"https://github.com/prdlk/leetcode/issues/40\"\ncompanies:\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"TikTok\", period: \"Three Months\", frequency: 70.9 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 21.2 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 24.1 }\nsidebar:\n label: 'Subarray Sums Divisible by K'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [4,5,0,-2,-3,1], k = 5`\n- Output: `7`\n- Explanation: There are `7` subarrays with a sum divisible by `k = 5`: [4, `5`, `0`, `-2`, `-3`, 1], `[5]`, [5, 0], [5, `0`, `-2`, -3], `[0]`, [0, `-2`, -3], [-2, -3]\n\n### Example 2:\n- Input: `nums = [5], k = 9`\n- Output: `0`\n\n### Constraints:\n\n- `1 <= nums.length <= 3 * 10^4`\n- `-10^4 <= nums[i] <= 10^4`\n- `2 <= k <= 10^4`\n\n## Solution\n\n```py\nclass Solution:\n def subarraysDivByK(self, nums: list[int], k: int) -> int:\n remainder_counts = {0: 1}\n cur_sum = 0\n count = 0\n\n for n in nums:\n cur_sum += n\n remainder = cur_sum % k\n\n if remainder in remainder_counts:\n count += remainder_counts[remainder]\n\n remainder_counts[remainder] = remainder_counts.get(remainder, 0) + 1\n\n return count\n```\n","/sliding-window":"---\ntitle: Sliding Window\ndescription: 'Sliding window is a specialized two-pointer family where the pointers define a contiguous interval and you maintain information about that entire interval as it moves.'\nhidden: true\n---\n\n\n[Sliding Window](https://www.youtube.com/watch?v=tSTO9w3piAo)\n\n\nMany problems have two moving boundaries. That alone does not make them sliding window problems.\n\nA problem belongs to this family when:\n\n- the answer depends on the subarray or substring between left and right\n- you can update the window state incrementally as elements enter or leave\n- you maintain a condition that tells you whether the window is valid\n\n:::tip\nThe rule for all three: every element enters once and leaves once, so the whole thing runs in O(n).\n:::\n\n**What is an invariant?**\n\nAn invariant is a statement that stays true before and after every iteration. In a sliding window, that statement usually describes the window itself, such as “the window is valid,” “it has no duplicates,” or “its sum is at most the target.” It can also describe a helper structure, such as “the frequency map matches the characters currently inside the window.”\n\n**Why invariants matter?**\n\nWithout an invariant, shrinking the window is just a guess. You no longer know when the window is valid, when to update the answer, or why the left pointer should stop. The invariant is what makes the algorithm correct.\n\n**How sliding window differs from other two-pointer families?**\n\nSliding window is not the same as every other two-pointer pattern. In Container With Most Water, the interval is only the remaining search space. In sliding window, the interval itself is the thing you care about. In Remove Duplicates, the left side is already the finished answer prefix. In sliding window, both pointers describe a live window that is still being checked.\n\n\n## 1. Fixed-size windows\n\n:::tip\nThe window size never changes. The main task is updating the window state efficiently when one element enters and one leaves.\n:::\n\n\n\n\nSliding Window Fixed\n\n\n```python\ndef sliding_window_fixed(input, window_size):\n ans = window = input[0:window_size]\n for right in range(window_size, len(input)):\n left = right - window_size\n remove input[left] from window\n append input[right] to window\n ans = optimal(ans, window)\n return ans\n```\n\n1. Add `nums[right]` to the window.\n\n2. If the window has `k` elements, record the answer.\n\n3. Remove the leftmost element so the window stays at size `k`.\n\n\n## 2. Flexible-size windows with a validity rule\n\n:::tip\nHere the window expands until invalid, then shrinks until valid again.\n:::\n\n\n\nSliding Window Flexible\n\n\n**Longest Window**\n\n```python\ndef sliding_window_flexible_longest(input):\n initialize window, ans\n left = 0\n for right in range(len(input)):\n append input[right] to window\n while invalid(window): # update left until window is valid again\n remove input[left] from window\n left += 1\n ans = max(ans, window) # window is guaranteed to be valid here\n return ans\n\n```\n\n**Shortest Window**\n\n```python\ndef sliding_window_flexible_shortest(input):\n initialize window, ans\n left = 0\n for right in range(len(input)):\n append input[right] to window\n while valid(window):\n ans = min(ans, window) # window is guaranteed to be valid here\n remove input[left] from window\n left += 1\n return ans\n```\n\n1. Add `nums[right]` to the window.\n\n2. While the window is invalid, remove `nums[left]` and move `left` up.\n\n3. The window is now valid. Record its size.\n\n---\n\n## 3. Frequency-tracking windows\n\n:::tip\nThe difficulty comes from keeping the bookkeeping correct as characters enter and leave.\n:::\n\n\n\n\nFrequency-tracking windows\n\n```python\nfrom collections import defaultdict\n\ndef frequency_window(s):\n left = 0\n counts = defaultdict(int)\n best = 0\n\n for right in range(len(s)):\n counts[s[right]] += 1 # character enters\n\n while counts[s[right]] > 1: # rule: no repeats\n counts[s[left]] -= 1 # character leaves\n left += 1\n\n best = max(best, right - left + 1)\n\n return best\n```\n\n1. Add `s[right]` to the window.\n\n2. While the window has more than one character, remove `s[left]` and move `left` up.\n\n3. The window is now valid. Record its size.\n\n---\n\nFor anagram or \"minimum window substring\" problems, you also keep a target count. Track how many characters currently match the target with one integer (`matched`). Update it only when a count crosses the target value. This keeps each step `O(1)`.\n\n```python\ndef anagram_window(s, p):\n need = defaultdict(int)\n for c in p:\n need[c] += 1\n\n counts = defaultdict(int)\n matched = 0 # how many chars have counts[c] == need[c]\n result = []\n k = len(p)\n\n for right in range(len(s)):\n c = s[right]\n counts[c] += 1\n if counts[c] == need[c]:\n matched += 1\n\n if right >= k: # window too big, remove left\n d = s[right - k]\n if counts[d] == need[d]:\n matched -= 1\n counts[d] -= 1\n\n if matched == len(need):\n result.append(right - k + 1)\n\n return result\n```\n\n","/sliding-window/best-time-to-buy-and-sell-stock":"---\ntitle: '121. Best Time to Buy and Sell Stock'\ndescription: You are given an array prices where prices[i] is the price of a given stock on the i^th day\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\nvideo: \"1pkOgXD63yU\"\nissue: \"https://github.com/prdlk/leetcode/issues/50\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 83.1 }\n - { name: \"Infosys\", period: \"Thirty Days\", frequency: 80.2 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 69.6 }\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 65.6 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 65.3 }\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 58.2 }\n - { name: \"Atlassian\", period: \"Three Months\", frequency: 100 }\n - { name: \"Media.net\", period: \"Three Months\", frequency: 100 }\n - { name: \"Nvidia\", period: \"Three Months\", frequency: 87.7 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 70.4 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 69 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 60.5 }\n - { name: \"Mastercard\", period: \"Six Months\", frequency: 79 }\n - { name: \"Accenture\", period: \"Six Months\", frequency: 75 }\n - { name: \"Adobe\", period: \"Six Months\", frequency: 74.3 }\n - { name: \"Walmart Labs\", period: \"Six Months\", frequency: 70.5 }\n - { name: \"IBM\", period: \"Six Months\", frequency: 70.1 }\n - { name: \"TikTok\", period: \"Six Months\", frequency: 55.2 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 53.1 }\nsidebar:\n label: 'Best Time to Buy and Sell Stock'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `prices = [7,1,5,3,6,4]`\n- Output: `5`\n- Explanation: Buy on day `2` (price = `1`) and sell on day `5` (price = `6`), profit = `6-1 = 5`. Note that buying on day `2` and selling on day `1` is not allowed because you must buy before you sell.\n\n### Example 2:\n- Input: `prices = [7,6,4,3,1]`\n- Output: `0`\n- Explanation: In this case, no transactions are done and the max profit = `0`.\n\n### Constraints:\n\n- `1 <= prices.length <= 10^5`\n- `0 <= prices[i] <= 10^4`\n\n## Approach\n\n::::warning\nThe diagram traces the code below literally. That code does not run: `curr` and `ans` are never initialised, so the first `curr > left` test raises `NameError`, and `left` holds a price rather than an index, so `prices[left]` indexes by value.\n::::\n\n```mermaid\nflowchart TD\n S([\"maxProfit(prices)\"]) --> I[\"left = min(prices)\"]\n I --> F{\"more right in range(len(prices))?\"}\n F -- no --> E([\"return ans\"])\n F -- yes --> W{\"curr > left?\"}\n W -- yes --> P[\"curr -= prices[left]; left += 1\"]\n P --> W\n W -- no --> M[\"ans = max(ans, curr)\"]\n M --> F\n```\n\n## Solution\n\n```py\nclass Solution:\n def maxProfit(self, prices: List[int]) -> int:\n left = min(prices)\n for right in range(len(prices)):\n while curr > left:\n curr -= prices[left]\n left += 1\n ans = max(ans, curr)\n return ans\n```\n","/sliding-window/longest-repeating-character-replacement":"---\ntitle: '424. Longest Repeating Character Replacement'\ndescription: You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Sliding Window\", slug: \"sliding-window\" }\nvideo: \"gqXU1UyA8pk\"\nissue: \"https://github.com/prdlk/leetcode/issues/52\"\ncompanies:\n - { name: \"Zoho\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 46.6 }\n - { name: \"DE Shaw\", period: \"Three Months\", frequency: 91.8 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 66 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 46.8 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 34.6 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 32.7 }\nsidebar:\n label: 'Longest Repeating Character Replacement'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `s = \"ABAB\", k = 2`\n- Output: `4`\n- Explanation: Replace the two 'A's with two 'B's or vice versa.\n\n### Example 2:\n- Input: `s = \"AABABBA\", k = 1`\n- Output: `4`\n- Explanation: Replace the one 'A' in the middle with 'B' and form \"AABBBBA\". The substring \"BBBB\" has the longest repeating letters, which is `4`. There may exists other ways to achieve this answer too.\n\n### Constraints:\n\n- `1 <= s.length <= 10^5`\n- `s` consists of only uppercase English letters.\n- `0 <= k <= s.length`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"characterReplacement(s, k)\"]) --> I[\"count = {}, res = 0, l = 0, maxF = 0\"]\n I --> L{\"more (r, c) in s?\"}\n L -- no --> E([\"return res\"])\n L -- yes --> A[\"count[c] += 1; maxF = max(maxF, count[c])\"]\n A --> W{\"(r - l + 1) - maxF > k? — too many chars to replace\"}\n W -- yes --> P[\"count[s[l]] -= 1; l += 1 — shrink from the left\"]\n P --> W\n W -- no --> M[\"res = max(res, r - l + 1)\"]\n M --> L\n```\n\n## Solution\n\n```py\nclass Solution:\n def characterReplacement(self, s: str, k: int) -> int:\n count = {}\n res = 0\n\n l = 0\n maxF = 0\n for r, c in enumerate(s):\n count[c] = 1 + count.get(c, 0)\n maxF = max(maxF, count[c])\n\n while (r - l + 1) - maxF > k:\n count[s[l]] -= 1\n l += 1\n\n res = max(res, r - l + 1)\n return res\n```\n","/sliding-window/longest-substring-without-repeating-characters":"---\ntitle: '3. Longest Substring Without Repeating Characters'\ndescription: Given a string s, find the length of the longest substring without duplicate characters\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Sliding Window\", slug: \"sliding-window\" }\nvideo: \"wiGpQwVHdE0\"\nissue: \"https://github.com/prdlk/leetcode/issues/51\"\ncompanies:\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Visa\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 84.9 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 75.6 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 63.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 63.7 }\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 58.2 }\n - { name: \"Cognizant\", period: \"Three Months\", frequency: 100 }\n - { name: \"Optum\", period: \"Three Months\", frequency: 100 }\n - { name: \"Spotify\", period: \"Three Months\", frequency: 100 }\n - { name: \"Deloitte\", period: \"Three Months\", frequency: 89.1 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 87.4 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 69 }\n - { name: \"Goldman Sachs\", period: \"Three Months\", frequency: 66.3 }\n - { name: \"Media.net\", period: \"Six Months\", frequency: 100 }\n - { name: \"Wipro\", period: \"Six Months\", frequency: 100 }\n - { name: \"Wissen Technology\", period: \"Six Months\", frequency: 100 }\n - { name: \"Zoho\", period: \"Six Months\", frequency: 100 }\n - { name: \"Qualcomm\", period: \"Six Months\", frequency: 92 }\n - { name: \"Turing\", period: \"Six Months\", frequency: 89 }\n - { name: \"EPAM Systems\", period: \"Six Months\", frequency: 86.7 }\n - { name: \"Netflix\", period: \"Six Months\", frequency: 83.1 }\n - { name: \"Tesla\", period: \"Six Months\", frequency: 80.9 }\n - { name: \"Yandex\", period: \"Six Months\", frequency: 69.2 }\n - { name: \"Agoda\", period: \"Six Months\", frequency: 65 }\n - { name: \"IBM\", period: \"Six Months\", frequency: 60.1 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 54.1 }\nsidebar:\n label: 'Longest Substring Without Repeating Characters'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `s = \"abcabcbb\"`\n- Output: `3`\n- Explanation: The answer is \"abc\", with the length of `3`. Note that \"bca\" and \"cab\" are also correct answers.\n\n### Example 2:\n- Input: `s = \"bbbbb\"`\n- Output: `1`\n- Explanation: The answer is \"b\", with the length of `1`.\n\n### Example 3:\n- Input: `s = \"pwwkew\"`\n- Output: `3`\n- Explanation: The answer is \"wke\", with the length of `3`. Notice that the answer must be a substring, \"pwke\" is a subsequence and not a substring.\n\n### Constraints:\n\n- `0 <= s.length <= 10^5`\n- `s` consists of English letters, digits, symbols and spaces.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"lengthOfLongestSubstring(s)\"]) --> I[\"left = 0, ans = 0, window = set()\"]\n I --> L{\"more (right, c) in s?\"}\n L -- no --> E([\"return ans\"])\n L -- yes --> W{\"c already in window?\"}\n W -- yes --> P[\"window.remove(s[left]); left += 1 — shrink until c is free\"]\n P --> W\n W -- no --> A[\"window.add(c)\"]\n A --> M[\"ans = max(ans, right - left + 1)\"]\n M --> L\n```\n\n## Solution\n\n```py\nclass Solution:\n def lengthOfLongestSubstring(self, s: str) -> int:\n left = 0\n ans = 0\n window = set()\n for right, c in enumerate(s):\n while c in window:\n window.remove(s[left])\n left += 1\n window.add(c)\n ans = max(ans, right - left + 1)\n\n return ans\n```\n","/sliding-window/max-consecutive-ones-iii":"---\ntitle: '1004. Max Consecutive Ones III'\ndescription: Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\n - { name: \"Sliding Window\", slug: \"sliding-window\" }\n - { name: \"Prefix Sum\", slug: \"prefix-sum\" }\nissue: \"https://github.com/prdlk/leetcode/issues/56\"\ncompanies:\n - { name: \"EPAM Systems\", period: \"Thirty Days\", frequency: 89.8 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Tekion\", period: \"Three Months\", frequency: 100 }\n - { name: \"DE Shaw\", period: \"Three Months\", frequency: 80.2 }\n - { name: \"Google\", period: \"Three Months\", frequency: 38.7 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 67.9 }\n - { name: \"Yandex\", period: \"Six Months\", frequency: 59.6 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 54.1 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 42.5 }\nsidebar:\n label: 'Max Consecutive Ones III'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2`\n- Output: `6`\n- Explanation: `[1,1,1,0,0,1,1,1,1,1,1]` Bolded numbers were flipped from `0` to `1`. The longest subarray is underlined.\n\n### Example 2:\n- Input: `nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3`\n- Output: `10`\n- Explanation: `[0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]` Bolded numbers were flipped from `0` to `1`. The longest subarray is underlined.\n\n### Constraints:\n\n- `1 <= nums.length <= 10^5`\n- `nums[i]` is either 0 or 1.\n- `0 <= k <= nums.length`\n\n## Solution\n\n```py\nclass Solution:\n def longestOnes(self, nums: list[int], k: int) -> int:\n left = 0\n cur = 0\n ans = 0\n\n for right in range(len(nums)):\n if nums[right] == 0:\n cur += 1\n while cur > k:\n if nums[left] == 0:\n cur -= 1\n left += 1\n\n ans = max(ans, right - left + 1)\n\n return ans\n```\n","/sliding-window/maximum-average-subarray-i":"---\ntitle: '643. Maximum Average Subarray I'\ndescription: You are given an integer array nums consisting of n elements, and an integer k\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Sliding Window\", slug: \"sliding-window\" }\nissue: \"https://github.com/prdlk/leetcode/issues/55\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 83.1 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 55.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 36.7 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 57.2 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 32.7 }\nsidebar:\n label: 'Maximum Average Subarray I'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `nums = [1,12,-5,-6,50,3], k = 4`\n- Output: `12.75000`\n- Explanation: Maximum average is `(12 - 5 - 6 + 50`) / `4 = 51 / 4 = 12.75`\n\n### Example 2:\n- Input: `nums = [5], k = 1`\n- Output: `5.00000`\n\n### Constraints:\n\n- `n == nums.length`\n- `1 <= k <= n <= 10^5`\n- `-10^4 <= nums[i] <= 10^4`\n\n## Solution\n\n```py\nclass Solution:\n def findMaxAverage(self, nums: list[int], k: int) -> float:\n window_sum = 0\n for i in range(k):\n window_sum += nums[i]\n largest = window_sum\n\n for right in range(k, len(nums)):\n left = right - k\n window_sum -= nums[left]\n window_sum += nums[right]\n largest = max(largest, window_sum)\n\n return largest / k\n```\n","/sliding-window/minimum-size-subarray-sum":"---\ntitle: '209. Minimum Size Subarray Sum'\ndescription: Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\n - { name: \"Sliding Window\", slug: \"sliding-window\" }\n - { name: \"Prefix Sum\", slug: \"prefix-sum\" }\nissue: \"https://github.com/prdlk/leetcode/issues/54\"\ncompanies:\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 46.8 }\n - { name: \"Google\", period: \"Three Months\", frequency: 45 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 28.9 }\nsidebar:\n label: 'Minimum Size Subarray Sum'\n badge: 'Medium'\n---\n\n::::warning\nIf you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log(n)).\n::::\n\n### Example 1:\n- Input: `target = 7, nums = [2,3,1,2,4,3]`\n- Output: `2`\n- Explanation: The subarray `[4,3]` has the minimal length under the problem constraint.\n\n### Example 2:\n- Input: `target = 4, nums = [1,4,4]`\n- Output: `1`\n\n### Example 3:\n- Input: `target = 11, nums = [1,1,1,1,1,1,1,1]`\n- Output: `0`\n\n### Constraints:\n\n- `1 <= target <= 10^9`\n- `1 <= nums.length <= 10^5`\n- `1 <= nums[i] <= 10^4`\n\n## Solution\n\n```py\nclass Solution:\n def minSubArrayLen(self, target: int, nums: list[int]) -> int:\n window_sum = 0\n length = len(nums) + 1\n left = 0\n\n for right in range(len(nums)):\n window_sum += nums[right]\n while window_sum >= target:\n length = min(length, right - left + 1)\n window_sum -= nums[left]\n left += 1\n if length > len(nums):\n return 0\n return length\n```\n","/sliding-window/minimum-window-substring":"---\ntitle: '76. Minimum Window Substring'\ndescription: Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string \"\"\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Sliding Window\", slug: \"sliding-window\" }\nissue: \"https://github.com/prdlk/leetcode/issues/57\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 55.9 }\n - { name: \"Lyft\", period: \"Three Months\", frequency: 93.3 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 48.3 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Google\", period: \"Three Months\", frequency: 23.9 }\n - { name: \"Oracle\", period: \"Six Months\", frequency: 78.7 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 44.8 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\nsidebar:\n label: 'Minimum Window Substring'\n badge: 'Hard'\n---\n\n::::warning\nCould you find an algorithm that runs in O(m + n) time?\n::::\n\n### Example 1:\n- Input: `s = \"ADOBECODEBANC\", t = \"ABC\"`\n- Output: `\"BANC\"`\n- Explanation: The minimum window substring \"BANC\" includes 'A', 'B', and 'C' from string `t`.\n\n### Example 2:\n- Input: `s = \"a\", t = \"a\"`\n- Output: `\"a\"`\n- Explanation: The entire string `s` is the minimum window.\n\n### Example 3:\n- Input: `s = \"a\", t = \"aa\"`\n- Output: `\"\"`\n- Explanation: Both 'a's from `t` must be included in the window. Since the largest window of `s` only has one 'a', return empty string.\n\n### Constraints:\n\n- `m == s.length`\n- `n == t.length`\n- `1 <= m, n <= 10^5`\n- `s` and `t` consist of uppercase and lowercase English letters.\n\n## Solution\n\n```py\nclass Solution:\n def minWindow(self, s: str, t: str) -> str:\n```\n","/sliding-window/permutation-in-string":"---\ntitle: '567. Permutation in String'\ndescription: Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Sliding Window\", slug: \"sliding-window\" }\nvideo: \"UbyhOgBN834\"\nissue: \"https://github.com/prdlk/leetcode/issues/53\"\ncompanies:\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 63.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 46.8 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 48.1 }\nsidebar:\n label: 'Permutation in String'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `s1 = \"ab\", s2 = \"eidbaooo\"`\n- Output: `true`\n- Explanation: `s2` contains one permutation of `s1` (\"ba\").\n\n### Example 2:\n- Input: `s1 = \"ab\", s2 = \"eidboaoo\"`\n- Output: `false`\n\n### Constraints:\n\n- `1 <= s1.length, s2.length <= 10^4`\n- `s1` and `s2` consist of lowercase English letters.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"checkInclusion(s1, s2)\"]) --> G{\"k = len(s1) > len(s2)?\"}\n G -- yes --> X([\"return False\"])\n G -- no --> I[\"need = Counter(s1), window = Counter()\"]\n I --> L{\"more (right, c) in s2?\"}\n L -- no --> E([\"return False\"])\n L -- yes --> A[\"window[c] += 1\"]\n A --> B{\"right >= k? — window is now longer than k\"}\n B -- yes --> C[\"drop s2[right - k] from window, deleting the key at 0\"]\n B -- no --> Q\n C --> Q{\"window == need?\"}\n Q -- yes --> R([\"return True\"])\n Q -- no --> L\n```\n\n## Solution\n\n```py\nfrom collections import Counter\n\n\nclass Solution:\n def checkInclusion(self, s1: str, s2: str) -> bool:\n k = len(s1)\n if k > len(s2):\n return False\n\n need = Counter(s1)\n window = Counter()\n\n for right, c in enumerate(s2):\n window[c] += 1\n if right >= k:\n left_char = s2[right - k]\n window[left_char] -= 1\n if window[left_char] == 0:\n del window[left_char]\n if window == need:\n return True\n\n return False\n```\n","/stack":"---\ntitle: Stack\ndescription: 'Items are added and removed from one end, so the last item added is the first item removed.'\nhidden: true\n---\n\nA stack works like a stack of plates in a cafeteria. You add new plates on top. When you need a plate, you take from the top. The last plate you added is the first one you remove.\n\nThis is **Last-In-First-Out (LIFO)**. The most recent item goes out first.\n\n\n\n## Core Stack Operations\n\nA stack has two fundamental operations:\n\n- **Push**: add an item to the top of the stack\n- **Pop**: remove the item at the top of the stack\n\nSome stacks have additional operations:\n\n- **Peek**: get the item at the top of the stack without removing it\n- **IsEmpty**: check if the stack is empty\n\n\n\n```python\n# Using Python list as a stack\nstack = []\n\n# Push elements\nstack.append(10)\nstack.append(20)\nstack.append(30)\n# Stack: [10, 20, 30] (30 is on top)\n\n# Pop element\ntop = stack.pop() # Returns 30\n# Stack: [10, 20]\n\n# Peek at top\nif stack:\n top_element = stack[-1] # Returns 20 without removing\n```\n\nPython lists provide `append()` for pushing and `pop()` for popping. The `peek()` method is not built-in, but you can implement it yourself.\n\n## Last in, First out\n\nThe defining characteristic of a stack is LIFO order. Whatever you put in last comes out first. This is different from a queue, which is First-In-First-Out (FIFO).\n\nAdd items: A, B, C\n\n- Stack: [A, B, C] (C on top)\n\nRemove items:\n\n- First pop: C\n- Second pop: B\n- Third pop: A\n\nThe order reverses. Items come out in the opposite order they went in.\n","/stack/basic-calculator":"---\ntitle: '224. Basic Calculator'\ndescription: Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation\nicon: dot\ntopics:\n - { name: \"Math\", slug: \"math\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Stack\", slug: \"stack\" }\n - { name: \"Recursion\", slug: \"recursion\" }\nissue: \"https://github.com/prdlk/leetcode/issues/71\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 28.9 }\n - { name: \"Google\", period: \"Three Months\", frequency: 23.9 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 24.5 }\nsidebar:\n label: 'Basic Calculator'\n badge: 'Hard'\n---\n\n### Example 1:\n- Input: `s = \"1 + 1\"`\n- Output: `2`\n\n### Example 2:\n- Input: `s = \" 2-1 + 2 \"`\n- Output: `3`\n\n### Example 3:\n- Input: `s = \"(1+(4+5+2)-3)+(6+8)\"`\n- Output: `23`\n\n### Constraints:\n\n- `1 <= s.length <= 3 * 10^5`\n- `s` consists of digits, '+', '-', '(', ')', and ' '.\n- `s` represents a valid expression.\n- '+' is not used as a unary operation (i.e., \"+1\" and \"+(2 + 3)\" is invalid).\n- '-' could be used as a unary operation (i.e., \"-1\" and \"-(2 + 3)\" is valid).\n- There will be no two consecutive operators in the input.\n- Every number and running calculation will fit in a signed 32-bit integer.\n\n## Solution\n\n```py\nclass Solution:\n def calculate(self, s: str) -> int:\n```\n","/stack/evaluate-reverse-polish-notation":"---\ntitle: '150. Evaluate Reverse Polish Notation'\ndescription: You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Math\", slug: \"math\" }\n - { name: \"Stack\", slug: \"stack\" }\nvideo: \"iu0082c4HDE\"\nissue: \"https://github.com/prdlk/leetcode/issues/69\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"Yandex\", period: \"Six Months\", frequency: 59.6 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 38.7 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\nsidebar:\n label: 'Evaluate Reverse Polish Notation'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `tokens = [\"2\",\"1\",\"+\",\"3\",\"*\"]`\n- Output: `9`\n- Explanation: `((2 + 1`) * `3`) = `9`\n\n### Example 2:\n- Input: `tokens = [\"4\",\"13\",\"5\",\"/\",\"+\"]`\n- Output: `6`\n- Explanation: `(4 + (13 / 5`)) = `6`\n\n### Example 3:\n- Input: `tokens = [\"10\",\"6\",\"9\",\"3\",\"+\",\"-11\",\"*\",\"/\",\"*\",\"17\",\"+\",\"5\",\"+\"]`\n- Output: `22`\n- Explanation: `((10 * (6 / ((9 + 3`) * `-11`))) + `17`) + `5 = ((10 * (6 / (12 * -11`))) + `17`) + `5 = ((10 * (6 / -132`)) + `17`) + `5 = ((10 * 0`) + `17`) + `5 = (0 + 17`) + `5 = 17 + 5 = 22`\n\n### Constraints:\n\n- `1 <= tokens.length <= 10^4`\n- `tokens[i]` is either an operator: \"+\", \"-\", \"*\", or \"/\", or an integer in the range [-200, 200].\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"evalRPN(tokens)\"]) --> I[\"stack = [], operations = + - * /\"]\n I --> L{\"more token c?\"}\n L -- no --> E([\"return stack[0]\"])\n L -- yes --> Q{\"c is an operator?\"}\n Q -- no --> N[\"stack.append(int(c))\"]\n N --> L\n Q -- yes --> P[\"y = stack.pop(), x = stack.pop() — order matters\"]\n P --> C[\"stack.append(operations[c](x, y))\"]\n C --> L\n```\n\n## Solution\n\n```py\nclass Solution:\n def evalRPN(self, tokens: List[str]) -> int:\n stack = []\n operations = {\n \"+\": lambda x, y: int(x + y),\n \"-\": lambda x, y: int(x - y),\n \"*\": lambda x, y: int(x * y),\n \"/\": lambda x, y: int(x / y),\n }\n\n for c in tokens:\n if c in operations:\n y = stack.pop()\n x = stack.pop()\n calc_func = operations[c]\n result = calc_func(x, y)\n stack.append(result)\n else:\n stack.append(int(c))\n\n return stack[0]\n```\n","/stack/min-stack":"---\ntitle: '155. Min Stack'\ndescription: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time\nicon: dot\ntopics:\n - { name: \"Stack\", slug: \"stack\" }\n - { name: \"Design\", slug: \"design\" }\nvideo: \"qkLl7nAwDPo\"\nissue: \"https://github.com/prdlk/leetcode/issues/68\"\ncompanies:\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 28.9 }\n - { name: \"Google\", period: \"Three Months\", frequency: 28.8 }\n - { name: \"Odoo\", period: \"Six Months\", frequency: 100 }\n - { name: \"UiPath\", period: \"Six Months\", frequency: 100 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 67.9 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 45.6 }\nsidebar:\n label: 'Min Stack'\n badge: 'Medium'\n---\n\n::::warning\nYou must implement a solution with O(1) time complexity for each function.\n::::\n\n### Example 1:\n- Input: ``\n- Output: ``\n- Explanation: Input `[\"MinStack\",\"push\",\"push\",\"push\",\"getMin\",\"pop\",\"top\",\"getMin\"]` [[],[-2],[0],[-3],[],[],[],[]] Output `[null,null,null,null,-3,null,0,-2]` Explanation MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); // return `-3` minStack.pop(); minStack.top(); // return `0` minStack.getMin(); // return `-2`\n\n### Constraints:\n\n- `-2^31 <= val <= 2^31 - 1`\n- Methods pop, top and getMin operations will always be called on non-empty stacks.\n- At most 3 * 10^4 calls will be made to push, pop, top, and getMin.\n\n## Approach\n\n```mermaid\nflowchart TD\n subgraph state[\"two parallel stacks — same depth, always\"]\n A[\"stack — the values\"]\n B[\"minStack — the minimum as of that depth\"]\n end\n state --> P\n subgraph P[\"push(value)\"]\n P1[\"stack.append(value)\"] --> P2[\"minStack.append(min(value, minStack[-1]))\"]\n end\n state --> O\n subgraph O[\"pop()\"]\n O1[\"pop both stacks together\"]\n end\n state --> R\n subgraph R[\"top() / getMin() — O(1)\"]\n R1([\"stack[-1] / minStack[-1]\"])\n end\n```\n\n## Solution\n\n```py\nclass MinStack:\n def __init__(self):\n self.stack = []\n self.minStack = []\n\n def push(self, value: int) -> None:\n self.stack.append(value)\n value = min(value, self.minStack[-1] if self.minStack else value)\n self.minStack.append(value)\n\n def pop(self) -> None:\n self.stack.pop()\n self.minStack.pop()\n\n def top(self) -> int:\n return self.stack[-1]\n\n def getMin(self) -> int:\n return self.minStack[-1]\n\n\n# Your MinStack object will be instantiated and called as such:\n# obj = MinStack()\n# obj.push(value)\n# obj.pop()\n# param_3 = obj.top()\n# param_4 = obj.getMin()\n```\n","/stack/simplify-path":"---\ntitle: '71. Simplify Path'\ndescription: You are given an absolute path for a Unix-style file system, which always begins with a slash '/'. Your task is to transform this absolute path into its simplified canonical path\nicon: dot\ntopics:\n - { name: \"String\", slug: \"string\" }\n - { name: \"Stack\", slug: \"stack\" }\nissue: \"https://github.com/prdlk/leetcode/issues/70\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Google\", period: \"Three Months\", frequency: 28.8 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 21.2 }\n - { name: \"Snowflake\", period: \"Six Months\", frequency: 58.7 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 50.6 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 24.1 }\nsidebar:\n label: 'Simplify Path'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `path = \"/home/\"`\n- Output: `\"/home\"`\n- Explanation: The trailing slash should be removed.\n\n### Example 2:\n- Input: `path = \"/home//foo/\"`\n- Output: `\"/home/foo\"`\n- Explanation: Multiple consecutive slashes are replaced by a single one.\n\n### Example 3:\n- Input: `path = \"/home/user/Documents/../Pictures\"`\n- Output: `\"/home/user/Pictures\"`\n- Explanation: A double period \"..\" refers to the directory up a level (the parent directory).\n\n### Example 4:\n- Input: `path = \"/../\"`\n- Output: `\"/\"`\n- Explanation: Going one level up from the root directory is not possible.\n\n### Example 5:\n- Input: `path = \"/.../a/../b/c/../d/./\"`\n- Output: `\"/.../b/d\"`\n- Explanation: \"...\" is a valid name for a directory in this problem.\n\n### Constraints:\n\n- `1 <= path.length <= 3000`\n- `path` consists of English letters, digits, period '.', slash '/' or '_'.\n- `path` is a valid absolute Unix path.\n\n## Solution\n\n```py\nclass Solution:\n def simplifyPath(self, path: str) -> str:\n stack = []\n segments = [item for item in path.split(\"/\") if item]\n\n for p in segments:\n if stack and p == \"..\":\n stack.pop()\n elif not stack and p == \"..\" or p == \".\":\n continue\n else:\n stack.append(p)\n\n return \"/\" + \"/\".join(stack)\n```\n","/stack/valid-parentheses":"---\ntitle: '20. Valid Parentheses'\ndescription: Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid\nicon: dot\ntopics:\n - { name: \"String\", slug: \"string\" }\n - { name: \"Stack\", slug: \"stack\" }\n - { name: \"Bracket Sequences\", slug: \"bracket-sequences\" }\nvideo: \"WTzjTskDFMg\"\nissue: \"https://github.com/prdlk/leetcode/issues/67\"\ncompanies:\n - { name: \"BlackRock\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 61.3 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 42.3 }\n - { name: \"Accolite\", period: \"Three Months\", frequency: 100 }\n - { name: \"Intuit\", period: \"Three Months\", frequency: 73.2 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 68.3 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 65.3 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 57.2 }\n - { name: \"Autodesk\", period: \"Six Months\", frequency: 100 }\n - { name: \"Criteo\", period: \"Six Months\", frequency: 100 }\n - { name: \"Odoo\", period: \"Six Months\", frequency: 100 }\n - { name: \"Tripadvisor\", period: \"Six Months\", frequency: 100 }\n - { name: \"Virtu Financial\", period: \"Six Months\", frequency: 100 }\n - { name: \"IBM\", period: \"Six Months\", frequency: 91.1 }\n - { name: \"Epic Systems\", period: \"Six Months\", frequency: 89.6 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 85.6 }\nsidebar:\n label: 'Valid Parentheses'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `s = \"()\"`\n- Output: `true`\n\n### Example 2:\n- Input: `s = \"()[]{}\"`\n- Output: `true`\n\n### Example 3:\n- Input: `s = \"(]\"`\n- Output: `false`\n\n### Example 4:\n- Input: `s = \"([])\"`\n- Output: `true`\n\n### Example 5:\n- Input: `s = \"([)]\"`\n- Output: `false`\n\n### Constraints:\n\n- `1 <= s.length <= 10^4`\n- `s` consists of parentheses only '()[]{}'.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"isValid(s)\"]) --> I[\"stack = [], pairs = closer to opener\"]\n I --> L{\"more char c?\"}\n L -- no --> Z{\"stack empty?\"}\n Z -- yes --> T([\"return True\"])\n Z -- no --> U([\"return False — unclosed openers left\"])\n L -- yes --> Q{\"c is a closer?\"}\n Q -- no --> A[\"stack.append(c) — it is an opener\"]\n A --> L\n Q -- yes --> M{\"stack non-empty and pairs[c] == stack[-1]?\"}\n M -- yes --> P[\"stack.pop() — matched\"]\n P --> L\n M -- no --> F([\"return False — mismatch\"])\n```\n\n## Solution\n\n```py\nclass Solution:\n def isValid(self, s: str) -> bool:\n stack = []\n pairs = {\")\": \"(\", \"]\": \"[\", \"}\": \"{\"}\n\n for c in s:\n if c in pairs:\n if stack and pairs[c] == stack[-1]:\n stack.pop()\n else:\n return False\n else:\n stack.append(c)\n\n return True if not stack else False\n```\n","/topological-sort":"---\ntitle: Topological Sort\ndescription: 'Topological sort or topological ordering of a directed graph is an ordering of nodes such that every node appears in the ordering before all the nodes it points to. '\nhidden: true\n---\n\n## Directed Graph\n\nBefore we get started on topological order, let's talk about directed graphs first. A graph is directed when its edges have directions (these edges are also called arcs). Suppose that v, w are vertices in a directed graph. Let's get familiar with a few concepts.\n\n- **in-edge** - an edge pointing to or coming into the node\n- **out-edge** - an edge pointing away or going out from the node (we will refer those nodes on the other end of the out-edges of v to v's neighbors)\n- **in-degree** - the number of in-edges coming into the node\n- **out-degree** - the number of out-edges going out from the node\n- **dependency** - \"v depends on w\" / \"v is dependent on w\" means that there is a directed path from w to v where v, w are vertices in a directed graph. With these new terms under our belt, it will make more sense when we talk about directed graphs.\n\n## What is a topological order?\n\nTopological sort is not unique. For example, for a graph like this:\n\n\n\nBoth `[4, 5, 2]` and `[5, 4, 2]` are valid topological orders. \n\n\n\nTask 3 is completely independent of task 2 and 4, and it can be anywhere in the order as long as it is before task 1 which depends on it. All of the following ordering are valid topological orderings.\n\n`[4, 2, 3, 1], [4, 3, 2, 1], [3, 4, 2, 1]`\n\n\n:::warning[Graphs with Cycles Do Not Have Topological Ordering]\nIt should be obvious that if a graph with a cycle does not have a topological ordering. In the above example, 2 has to come before 5 which has come before 4 which has to come before 2 which has to come before 5... which is impossible.\n:::\n\n## Kahn's Algorithm\n\nTo obtain a topological order, we can use Kahn's algorithm which is very similar to Breadth First Search.\n\nIn order to understand why we must use this algorithm we first look at the problem it is trying to solve.\n\nGiven a directed graph does there exist a way to remove the nodes such that each time we remove a node we guarantee that no other nodes point to that particular node?\n\nFor this algorithm, we systematically remove one node at a time, each time removing a node such that no other nodes point to that node (in-degree is 0). If no such node exists, then there must be a cycle, and there is no way to order the nodes such that \"every node appears in the ordering before all the nodes it points to\" (no solution). After removing the current node, for each neighboring node the current node points to, we check whether any nodes point to this node. If there isn't any, we push this node into the queue. Notice it is important to keep track of the number of nodes pointing to a node (in-degree) in the question as we only push the node into the queue once all nodes the current node depended on have been removed. \n\nHere is a graphic to demonstrate the idea.\n\n\n \n \n \n \n \n \n \n \n \n \n\n## Example Code\n\n:::tip\nNotice the topological sort algorithm is very similar to BFS. The main difference is that we only push nodes with 0 in-degree into the queue in topological sort whereas in BFS we push all the neighboring nodes into the queue.\n:::\n\n```py\nfrom collections import deque\n\ndef find_indegree(graph):\n indegree = { node: 0 for node in graph } # dict\n for node in graph:\n for neighbor in graph[node]:\n indegree[neighbor] += 1\n return indegree\n\n\ndef topo_sort(graph):\n res = []\n q = deque()\n indegree = find_indegree(graph)\n for node in indegree:\n if indegree[node] == 0:\n q.append(node)\n while len(q) > 0:\n node = q.popleft()\n res.append(node)\n for neighbor in graph[node]:\n indegree[neighbor] -= 1\n if indegree[neighbor] == 0:\n q.append(neighbor)\n return res if len(graph) == len(res) else None\n```\n","/topological-sort/course-schedule":"---\ntitle: '207. Course Schedule'\ndescription: There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai\nicon: dot\ntopics:\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Graph Theory\", slug: \"graph\" }\n - { name: \"Topological Sort\", slug: \"topological-sort\" }\n - { name: \"Directed Acyclic Graph\", slug: \"directed-acyclic-graph\" }\nissue: \"https://github.com/prdlk/leetcode/issues/142\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 69.6 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"TikTok\", period: \"Three Months\", frequency: 70.9 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 43.3 }\n - { name: \"ByteDance\", period: \"Six Months\", frequency: 88.6 }\n - { name: \"Walmart Labs\", period: \"Six Months\", frequency: 70.5 }\n - { name: \"Anduril\", period: \"Six Months\", frequency: 65.4 }\n - { name: \"Snowflake\", period: \"Six Months\", frequency: 58.7 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 57.9 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 56.2 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 44.8 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 32 }\nsidebar:\n label: 'Course Schedule'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `numCourses = 2, prerequisites = [[1,0]]`\n- Output: `true`\n- Explanation: There are a total of `2` courses to take. To take course `1` you should have finished course `0`. So it is possible.\n\n### Example 2:\n- Input: `numCourses = 2, prerequisites = [[1,0],[0,1]]`\n- Output: `false`\n- Explanation: There are a total of `2` courses to take. To take course `1` you should have finished course `0`, and to take course `0` you should also have finished course `1`. So it is impossible.\n\n### Constraints:\n\n- `1 <= numCourses <= 2000`\n- `0 <= prerequisites.length <= 5000`\n- `prerequisites[i].length == 2`\n- `0 <= ai, bi < numCourses`\n- All the pairs `prerequisites[i]` are unique.\n\n## Solution\n\n```py\nclass Solution:\n def canFinish(self, numCourses: int, prerequisites: list[list[int]]) -> bool:\n indegree = [0] * numCourses\n adj = [[] for x in range(numCourses)]\n for prereq in prerequisites:\n adj[prereq[1]].append(prereq[0])\n indegree[prereq[0]] += 1\n\n queue = []\n\n for i in range(numCourses):\n if indegree[i] == 0:\n queue.append(i)\n\n visited = 0\n while queue:\n node = queue.pop(0)\n visited += 1\n for neighbor in adj[node]:\n indegree[neighbor] -= 1\n if indegree[neighbor] == 0:\n queue.append(neighbor)\n\n return numCourses == visited\n```\n","/topological-sort/course-schedule-ii":"---\ntitle: '210. Course Schedule II'\ndescription: There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai\nicon: dot\ntopics:\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Graph Theory\", slug: \"graph\" }\n - { name: \"Topological Sort\", slug: \"topological-sort\" }\nissue: \"https://github.com/prdlk/leetcode/issues/143\"\ncompanies:\n - { name: \"Salesforce\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Walmart Labs\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 80.4 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 39.1 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 36.8 }\n - { name: \"Google\", period: \"Three Months\", frequency: 23.9 }\n - { name: \"Audible\", period: \"Six Months\", frequency: 100 }\n - { name: \"Aurora\", period: \"Six Months\", frequency: 100 }\n - { name: \"Anduril\", period: \"Six Months\", frequency: 92.8 }\n - { name: \"Netflix\", period: \"Six Months\", frequency: 90.1 }\n - { name: \"DoorDash\", period: \"Six Months\", frequency: 89.7 }\n - { name: \"Snowflake\", period: \"Six Months\", frequency: 79.8 }\n - { name: \"Uber\", period: \"Six Months\", frequency: 55.2 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 32 }\nsidebar:\n label: 'Course Schedule II'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `numCourses = 2, prerequisites = [[1,0]]`\n- Output: `[0,1]`\n- Explanation: There are a total of `2` courses to take. To take course `1` you should have finished course `0`. So the correct course order is `[0,1]`.\n\n### Example 2:\n- Input: `numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]`\n- Output: `[0,2,1,3]`\n- Explanation: There are a total of `4` courses to take. To take course `3` you should have finished both courses `1` and `2`. Both courses `1` and `2` should be taken after you finished course `0`. So one correct course order is `[0,1,2,3]`. Another correct ordering is `[0,2,1,3]`.\n\n### Example 3:\n- Input: `numCourses = 1, prerequisites = []`\n- Output: `[0]`\n\n### Constraints:\n\n- `1 <= numCourses <= 2000`\n- `0 <= prerequisites.length <= numCourses * (numCourses - 1)`\n- `prerequisites[i].length == 2`\n- `0 <= ai, bi < numCourses`\n- `ai != bi`\n- All the pairs [ai, bi] are distinct.\n\n## Solution\n\n```py\nclass Solution:\n def findOrder(self, numCourses: int, prerequisites: list[list[int]]) -> list[int]:\n prereq = {c: [] for c in range(numCourses)}\n for crs, pre in prerequisites:\n prereq[crs].append(pre)\n\n # course has 3 possible states:\n # visited -> crs has been added to output\n # visiting -> crs not added to output, but added to cycle\n # unvisited -> crs not added to output nor cycle\n output = []\n visit, cycle = set(), set()\n\n def dfs(crs):\n if crs in cycle:\n return False\n if crs in visit:\n return True\n\n cycle.add(crs)\n for pre in prereq[crs]:\n if dfs(pre) == False:\n return False\n\n cycle.remove(crs)\n visit.add(crs)\n output.append(crs)\n return True\n\n for c in range(numCourses):\n if dfs(c) == False:\n return []\n\n return output\n```\n","/tree-bfs":"---\ntitle: Tree BFS\ndescription: 'All nodes at one level are visited before the nodes at the next level.'\nhidden: true\n---\n","/tree-bfs/binary-tree-level-order-traversal":"---\ntitle: '102. Binary Tree Level Order Traversal'\ndescription: Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level)\nicon: dot\ntopics:\n - { name: \"Tree\", slug: \"tree\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Binary Tree\", slug: \"binary-tree\" }\nvideo: \"6ZnyEApgFYg\"\nissue: \"https://github.com/prdlk/leetcode/issues/106\"\ncompanies:\n - { name: \"Google\", period: \"Three Months\", frequency: 41 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 28.9 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 32 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\nsidebar:\n label: 'Binary Tree Level Order Traversal'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `root = [3,9,20,null,null,15,7]`\n- Output: `[[3],[9,20],[15,7]]`\n\n### Example 2:\n- Input: `root = [1]`\n- Output: `[[1]]`\n\n### Example 3:\n- Input: `root = []`\n- Output: `[]`\n\n### Constraints:\n\n- The number of nodes in the tree is in the range [0, 2000].\n- `-1000 <= Node.val <= 1000`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"levelOrder(root)\"]) --> I[\"res = [], q = deque(), q.append(root)\"]\n I --> W{\"q non-empty?\"}\n W -- no --> E([\"return res\"])\n W -- yes --> L[\"qLen = len(q), level = [] — qLen freezes the current level's size\"]\n L --> F{\"more i in range(qLen)?\"}\n F -- no --> V{\"level non-empty?\"}\n V -- yes --> A[\"res.append(level)\"]\n A --> W\n V -- no --> W\n F -- yes --> P[\"node = q.popleft()\"]\n P --> N{\"node is not None?\"}\n N -- no --> F\n N -- yes --> C[\"level.append(node.val), q.append(node.left), q.append(node.right) — None children are filtered on the next level\"]\n C --> F\n```\n\n## Solution\n\n```py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nfrom collections import deque\n\n\nclass Solution:\n # T: O(n) S: O(n)\n def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:\n res = []\n\n q = deque()\n q.append(root)\n\n while q:\n qLen = len(q)\n level = []\n for i in range(qLen):\n node = q.popleft()\n if node:\n level.append(node.val)\n q.append(node.left)\n q.append(node.right)\n if level:\n res.append(level)\n\n return res\n```\n","/tree-bfs/binary-tree-right-side-view":"---\ntitle: '199. Binary Tree Right Side View'\ndescription: Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom\nicon: dot\ntopics:\n - { name: \"Tree\", slug: \"tree\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Binary Tree\", slug: \"binary-tree\" }\nvideo: \"d4zLyf32e3I\"\nissue: \"https://github.com/prdlk/leetcode/issues/107\"\ncompanies:\n - { name: \"Yandex\", period: \"Three Months\", frequency: 76.1 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 42.8 }\n - { name: \"Google\", period: \"Three Months\", frequency: 23.9 }\n - { name: \"Oracle\", period: \"Six Months\", frequency: 78.7 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 62.3 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 32 }\nsidebar:\n label: 'Binary Tree Right Side View'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `root = [1,2,3,null,5,null,4]`\n- Output: `[1,3,4]`\n\n### Example 2:\n- Input: `root = [1,2,3,4,null,null,null,5]`\n- Output: `[1,3,4,5]`\n\n### Example 3:\n- Input: `root = [1,null,3]`\n- Output: `[1,3]`\n\n### Example 4:\n- Input: `root = []`\n- Output: `[]`\n\n### Constraints:\n\n- The number of nodes in the tree is in the range [0, 100].\n- `-100 <= Node.val <= 100`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"rightSideView(root)\"]) --> I[\"res = [], q = deque([root])\"]\n I --> W{\"q non-empty?\"}\n W -- no --> E([\"return res\"])\n W -- yes --> L[\"rightSide = None, qLen = len(q) — qLen freezes the current level's size\"]\n L --> F{\"more i in range(qLen)?\"}\n F -- no --> V{\"rightSide is not None?\"}\n V -- yes --> A[\"res.append(rightSide.val) — the last real node popped on this level is the rightmost\"]\n A --> W\n V -- no --> W\n F -- yes --> P[\"node = q.popleft()\"]\n P --> N{\"node is not None?\"}\n N -- no --> F\n N -- yes --> C[\"rightSide = node, q.append(node.left), q.append(node.right)\"]\n C --> F\n```\n\n## Solution\n\n```py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nfrom collections import deque\n\n\nclass Solution:\n def rightSideView(self, root: Optional[TreeNode]) -> List[int]:\n res = []\n q = deque([root])\n\n while q:\n rightSide = None\n qLen = len(q)\n\n for i in range(qLen):\n node = q.popleft()\n if node:\n rightSide = node\n q.append(node.left)\n q.append(node.right)\n\n if rightSide:\n res.append(rightSide.val)\n\n return res\n```\n","/tree-bfs/count-good-nodes-in-binary-tree":"---\ntitle: '1448. Count Good Nodes in Binary Tree'\ndescription: Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X\nicon: dot\ntopics:\n - { name: \"Tree\", slug: \"tree\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Binary Tree\", slug: \"binary-tree\" }\nvideo: \"7cp5imvDzl4\"\nissue: \"https://github.com/prdlk/leetcode/issues/108\"\ncompanies:\n - { name: \"josh technology\", period: \"Six Months\", frequency: 74 }\n - { name: \"Google\", period: \"Six Months\", frequency: 11.8 }\nsidebar:\n label: 'Count Good Nodes in Binary Tree'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `root = [3,1,4,3,null,1,5]`\n- Output: `4`\n- Explanation: Nodes in blue are good. Root Node `(3`) is always a good node. Node `4` -> (3,4) is the maximum value in the path starting from the `root`. Node `5` -> (3,4,5) is the maximum value in the path Node `3` -> (3,1,3) is the maximum value in the path.\n\n### Example 2:\n- Input: `root = [3,3,null,4,2]`\n- Output: `3`\n- Explanation: Node `2` -> `(3`, `3`, `2`) is not good, because `\"3`\" is higher than it.\n\n### Example 3:\n- Input: `root = [1]`\n- Output: `1`\n- Explanation: Root is considered as good.\n\n### Constraints:\n\n- The number of nodes in the binary tree is in the range [1, 10^5].\n- Each node's value is between [-10^4, 10^4].\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"goodNodes(root)\"]) --> C[\"dfs(root, root.val) — the root is always good\"]\n C --> E([\"return that count\"])\n subgraph dfs[\"dfs(node, max_val)\"]\n D1{\"node is None?\"} -- yes --> D2([\"return 0\"])\n D1 -- no --> D3[\"is_good = 1 if node.val >= max_val else 0 — max_val is the largest value on the path so far\"]\n D3 --> D4[\"new_max = max(max_val, node.val)\"]\n D4 --> D5[\"l = dfs(node.left, new_max), r = dfs(node.right, new_max)\"]\n D5 --> D6([\"return is_good + l + r\"])\n end\n C --> dfs\n```\n\n## Solution\n\n```py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def goodNodes(self, root: TreeNode) -> int:\n def dfs(node, max_val):\n if not node:\n return 0\n\n is_good = 1 if node.val >= max_val else 0\n new_max = max(max_val, node.val)\n\n return is_good + dfs(node.left, new_max) + dfs(node.right, new_max)\n\n return dfs(root, root.val)\n```\n","/tree-dfs":"---\ntitle: Tree DFS\ndescription: 'Each branch is followed to its end before the next branch is examined.'\nhidden: true\n---\n","/tree-dfs/invert-binary-tree":"---\ntitle: '226. Invert Binary Tree'\ndescription: Given the root of a binary tree, invert the tree, and return its root\nicon: dot\ntopics:\n - { name: \"Tree\", slug: \"tree\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Binary Tree\", slug: \"binary-tree\" }\nvideo: \"OnSn2XEQ4MY\"\nissue: \"https://github.com/prdlk/leetcode/issues/96\"\ncompanies:\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 57.2 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 32.7 }\nsidebar:\n label: 'Invert Binary Tree'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `root = [4,2,7,1,3,6,9]`\n- Output: `[4,7,2,9,6,3,1]`\n\n### Example 2:\n- Input: `root = [2,1,3]`\n- Output: `[2,3,1]`\n\n### Example 3:\n- Input: `root = []`\n- Output: `[]`\n\n### Constraints:\n\n- The number of nodes in the tree is in the range [0, 100].\n- `-100 <= Node.val <= 100`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"invertTree(root)\"]) --> B{\"root is None?\"}\n B -- yes --> Z([\"return None\"])\n B -- no --> L[\"left = invertTree(root.left), right = invertTree(root.right) — both subtrees are already inverted\"]\n L --> X[\"root.left = right, root.right = left\"]\n X --> E([\"return root\"])\n```\n\n## Solution\n\n```py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:\n if not root:\n return None\n\n left = self.invertTree(root.left)\n right = self.invertTree(root.right)\n\n root.left = right\n root.right = left\n\n return root\n```\n","/tree-dfs/lowest-common-ancestor-of-a-binary-tree":"---\ntitle: '236. Lowest Common Ancestor of a Binary Tree'\ndescription: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree\nicon: dot\ntopics:\n - { name: \"Tree\", slug: \"tree\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Binary Tree\", slug: \"binary-tree\" }\n - { name: \"Binary Lifting\", slug: \"binary-lifting\" }\n - { name: \"Lowest Common Ancestor\", slug: \"lowest-common-ancestor\" }\nissue: \"https://github.com/prdlk/leetcode/issues/98\"\ncompanies:\n - { name: \"BlackRock\", period: \"Thirty Days\", frequency: 76.6 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 55.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 36.7 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 36.8 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 56.1 }\nsidebar:\n label: 'Lowest Common Ancestor of a Binary Tree'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1`\n- Output: `3`\n- Explanation: The LCA of nodes `5` and `1` is `3`.\n\n### Example 2:\n- Input: `root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4`\n- Output: `5`\n- Explanation: The LCA of nodes `5` and `4` is `5`, since a node can be a descendant of itself according to the LCA definition.\n\n### Example 3:\n- Input: `root = [1,2], p = 1, q = 2`\n- Output: `1`\n\n### Constraints:\n\n- The number of nodes in the tree is in the range [2, 10^5].\n- `-10^9 <= Node.val <= 10^9`\n- All Node.val are unique.\n- `p != q`\n- p and q will exist in the tree.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"lowestCommonAncestor(root, p, q)\"]) --> B{\"not root?\"}\n B -- yes --> N([\"return None\"])\n B -- no --> M{\"root == p or root == q?\"}\n M -- yes --> R([\"return root — a node is its own ancestor\"])\n M -- no --> L[\"l = self.lowestCommonAncestor(root.left, p, q)\"]\n L --> Q[\"r = self.lowestCommonAncestor(root.right, p, q)\"]\n Q --> D{\"l and r?\"}\n D -- yes --> A([\"return root — p and q were found on different sides\"])\n D -- no --> E([\"return l or r — pass up whichever side found something\"])\n```\n\n## Solution\n\n```py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.right = None\n\n\nclass Solution:\n def lowestCommonAncestor(\n self, root: \"TreeNode\", p: \"TreeNode\", q: \"TreeNode\"\n ) -> \"TreeNode\":\n\n if not root:\n return None\n\n if root == p or root == q:\n return root\n\n l = self.lowestCommonAncestor(root.left, p, q)\n r = self.lowestCommonAncestor(root.right, p, q)\n\n if l and r:\n return root\n else:\n return l or r\n```\n","/tree-dfs/maximum-depth-of-binary-tree":"---\ntitle: '104. Maximum Depth of Binary Tree'\ndescription: Given the root of a binary tree, return its maximum depth\nicon: dot\ntopics:\n - { name: \"Tree\", slug: \"tree\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Binary Tree\", slug: \"binary-tree\" }\nvideo: \"hTM3phVI6YQ\"\nissue: \"https://github.com/prdlk/leetcode/issues/97\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 83.1 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 42.3 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 21.2 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 54.1 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 32.7 }\nsidebar:\n label: 'Maximum Depth of Binary Tree'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `root = [3,9,20,null,null,15,7]`\n- Output: `3`\n\n### Example 2:\n- Input: `root = [1,null,2]`\n- Output: `2`\n\n### Constraints:\n\n- The number of nodes in the tree is in the range [0, 10^4].\n- `-100 <= Node.val <= 100`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"maxDepth(root)\"]) --> B{\"root is None?\"}\n B -- yes --> Z([\"return 0\"])\n B -- no --> L[\"l = maxDepth(root.left), r = maxDepth(root.right)\"]\n L --> E([\"return 1 + max(l, r) — this node adds one level above its deeper subtree\"])\n```\n\n## Solution\n\n```py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def maxDepth(self, root: Optional[TreeNode]) -> int:\n if not root:\n return 0\n\n return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))\n```\n","/trie":"---\ntitle: Trie\ndescription: 'Each node holds one character, and a path from the root spells a word.'\nhidden: true\n---\n","/trie/design-add-and-search-words-data-structure":"---\ntitle: '211. Design Add and Search Words Data Structure'\ndescription: Design a data structure that supports adding new words and finding if a string matches any previously added string\nicon: dot\ntopics:\n - { name: \"String\", slug: \"string\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Design\", slug: \"design\" }\n - { name: \"Trie\", slug: \"trie\" }\nvideo: \"BTf05gs_8iU\"\nissue: \"https://github.com/prdlk/leetcode/issues/128\"\ncompanies:\n - { name: \"Amazon\", period: \"Three Months\", frequency: 21.2 }\n - { name: \"Snowflake\", period: \"Six Months\", frequency: 58.7 }\nsidebar:\n label: 'Design Add and Search Words Data Structure'\n badge: 'Medium'\n---\n\n### Constraints:\n\n- `1 <= word.length <= 25`\n- `word` in addWord consists of lowercase English letters.\n- `word` in search consist of '.' or lowercase English letters.\n- There will be at most 2 dots in `word` for search queries.\n- At most 10^4 calls will be made to addWord and search.\n\n## Approach\n\n```mermaid\nflowchart TD\n subgraph state[\"state — root TrieNode; every node holds a children dict and an endOfWord flag\"]\n R[\"root\"] --- C[\"... children[c] ...\"] --- L[\"endOfWord = True — a word ends here\"]\n end\n subgraph addWord[\"addWord(word)\"]\n A1[\"cur = root\"] --> A2{\"more c in word?\"}\n A2 -- yes --> A3{\"c not in cur.children?\"}\n A3 -- yes --> A4[\"cur.children[c] = TrieNode()\"]\n A4 --> A5[\"cur = cur.children[c]\"]\n A3 -- no --> A5\n A5 --> A2\n A2 -- no --> A6[\"cur.endOfWord = True\"]\n A6 --> A7([\"done\"])\n end\n subgraph search[\"search(word)\"]\n S1[\"dfs(0, root) — match from index 0 at the root\"] --> S2[\"cur = root\"]\n S2 --> S3{\"more i in range(j, len(word))?\"}\n S3 -- no --> S4([\"return cur.endOfWord\"])\n S3 -- yes --> S5[\"c = word[i]\"]\n S5 --> S6{\"c == '.'?\"}\n S6 -- yes --> S7{\"more child in cur.children.values()?\"}\n S7 -- yes --> S8{\"dfs(i + 1, child)? — try the rest of the word from this child\"}\n S8 -- yes --> S9([\"return True\"])\n S8 -- no --> S7\n S7 -- no --> S10([\"return False — no child matched the wildcard\"])\n S6 -- no --> S11{\"c not in cur.children?\"}\n S11 -- yes --> S12([\"return False\"])\n S11 -- no --> S13[\"cur = cur.children[c]\"]\n S13 --> S3\n end\n state --> addWord\n state --> search\n```\n\n## Solution\n\n```py\nclass TrieNode:\n def __init__(self):\n self.children = {}\n self.endOfWord = False\n\n\nclass WordDictionary:\n def __init__(self):\n self.root = TrieNode()\n\n def addWord(self, word: str) -> None:\n cur = self.root\n for c in word:\n if c not in cur.children:\n cur.children[c] = TrieNode()\n cur = cur.children[c]\n cur.endOfWord = True\n\n # Leverage Recursion\n def search(self, word: str) -> bool:\n def dfs(j, root):\n cur = root\n for i in range(j, len(word)):\n c = word[i]\n\n if c == \".\":\n for child in cur.children.values():\n if dfs(i + 1, child):\n return True\n return False\n else:\n if c not in cur.children:\n return False\n cur = cur.children[c]\n return cur.endOfWord\n\n return dfs(0, self.root)\n\n\n# Your WordDictionary object will be instantiated and called as such:\n# obj = WordDictionary()\n# obj.addWord(word)\n# param_3 = obj.search(word)\n```\n","/trie/implement-trie-prefix-tree":"---\ntitle: '208. Implement Trie (Prefix Tree)'\ndescription: A trie (pronounced as \"try\") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Design\", slug: \"design\" }\n - { name: \"Trie\", slug: \"trie\" }\nvideo: \"oobqoCJlHA0\"\nissue: \"https://github.com/prdlk/leetcode/issues/127\"\ncompanies:\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 48.1 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 37.9 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 34.2 }\nsidebar:\n label: 'Implement Trie (Prefix Tree)'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: ``\n- Output: ``\n- Explanation: Input [\"Trie\", \"insert\", \"search\", \"search\", \"startsWith\", \"insert\", \"search\"] `[[]`, `[\"apple\"]`, `[\"apple\"]`, `[\"app\"]`, `[\"app\"]`, `[\"app\"]`, [\"app\"]] Output [null, null, true, false, true, null, true] Explanation Trie trie = new Trie(); trie.insert(\"apple\"); trie.search(\"apple\"); // return True trie.search(\"app\"); // return False trie.startsWith(\"app\"); // return True trie.insert(\"app\"); trie.search(\"app\"); // return True\n\n### Constraints:\n\n- `1 <= word.length, prefix.length <= 2000`\n- `word` and `prefix` consist only of lowercase English letters.\n- At most 3 * 10^4 calls in total will be made to insert, search, and startsWith.\n\n## Approach\n\n```mermaid\nflowchart TD\n subgraph state[\"state — root TrieNode; every node holds a children dict and an endOfWord flag\"]\n R[\"root\"] --- C[\"... children[c] ...\"] --- L[\"endOfWord = True — a word ends here\"]\n end\n subgraph insert[\"insert(word)\"]\n I1[\"cur = root\"] --> I2{\"more c in word?\"}\n I2 -- yes --> I3{\"c not in cur.children?\"}\n I3 -- yes --> I4[\"cur.children[c] = TrieNode()\"]\n I4 --> I5[\"cur = cur.children[c]\"]\n I3 -- no --> I5\n I5 --> I2\n I2 -- no --> I6[\"cur.endOfWord = True\"]\n I6 --> I7([\"done\"])\n end\n subgraph search[\"search(word)\"]\n S1[\"cur = root\"] --> S2{\"more c in word?\"}\n S2 -- yes --> S3{\"c not in cur.children?\"}\n S3 -- yes --> S4([\"return False\"])\n S3 -- no --> S5[\"cur = cur.children[c]\"]\n S5 --> S2\n S2 -- no --> S6([\"return cur.endOfWord — the path exists, but is it a whole word?\"])\n end\n subgraph startsWith[\"startsWith(prefix)\"]\n P1[\"cur = root\"] --> P2{\"more c in prefix?\"}\n P2 -- yes --> P3{\"c not in cur.children?\"}\n P3 -- yes --> P4([\"return False\"])\n P3 -- no --> P5[\"cur = cur.children[c]\"]\n P5 --> P2\n P2 -- no --> P6([\"return True — every prefix char had a path\"])\n end\n state --> insert\n state --> search\n state --> startsWith\n```\n\n## Solution\n\n```py\nclass TrieNode:\n def __init__(self):\n self.children = {}\n self.endOfWord = False\n\n\nclass Trie:\n def __init__(self):\n self.root = TrieNode()\n\n def insert(self, word: str) -> None:\n cur = self.root\n\n for c in word:\n if c not in cur.children:\n cur.children[c] = TrieNode()\n cur = cur.children[c]\n cur.endOfWord = True\n\n def search(self, word: str) -> bool:\n cur = self.root\n\n for c in word:\n if c not in cur.children:\n return False\n cur = cur.children[c]\n\n return cur.endOfWord\n\n def startsWith(self, prefix: str) -> bool:\n cur = self.root\n\n for c in prefix:\n if c not in cur.children:\n return False\n cur = cur.children[c]\n\n return True\n\n\n# Your Trie object will be instantiated and called as such:\n# obj = Trie()\n# obj.insert(word)\n# param_2 = obj.search(word)\n# param_3 = obj.startsWith(prefix)\n```\n","/two-pointers":"---\ntitle: Two Pointers\ndescription: 'Two indices move through the data from different positions and meet at the answer.'\nhidden: true\n---\n\nMost interview problems in this section fall into one of four families:\n\n- **Same direction pointers**: one pointer reads, one pointer writes, or one pointer stays behind another while scanning left to right.\n- **Opposite direction pointers**: one pointer starts on the left, one on the right, and each move eliminates impossible answers.\n- **Sliding window**: the two pointers define a contiguous interval, and you maintain some property of that interval as the window expands and shrinks.\n- **Fast and slow pointers**: both pointers move through a linked structure, but at different speeds or with a fixed gap.\n\n\n\n## Same Direction\n\nSame-direction pointer problems usually scan from left to right, but the two pointers do different jobs.\n\nUsually:\n\n- one pointer reads the next value from the original input\n- one pointer marks where the next kept value should go\n\nSometimes both pointers move forward together, but one pointer stays behind the other to preserve a gap or a relationship.\n\n### When this family applies\n\n\nReach for same-direction pointers when the problem asks you to:\n\n- rewrite an array in-place\n- remove or compress elements without extra memory\n- keep only the values that satisfy some condition\n- maintain a fixed gap between two forward-moving pointers\n\n### Core Invariant\n\nThe invariant in this family is usually about the prefix you have already processed.\n\nTypical invariant:\n\n> \"Everything before the write pointer is already in the correct final form.\"\n\nThat is what makes these problems feel different from opposite-direction scans. You are not searching from both sides. You are building the answer progressively as you sweep through the input.\n\n### Subtypes in this section\n\n#### 1. Compaction / Overwrite\n\nThe slow pointer marks the next output position. The fast pointer scans the input. When the current value should be kept, copy it forward and advance the slow pointer.\n\n\n\n#### 2. Relative Speed\n\nBoth pointers move forward, but one moves twice as fast. The invariant is about their distance and how much of the structure each pointer has consumed.\n\n\n\n#### 3. Fixed Gap\n\nBoth pointers move forward while preserving a gap. The invariant is not about a rewritten prefix. It is about the distance between the pointers.\n\n\n\n### What changes across problems\n\nThe family stays the same, but the invariant changes slightly:\n\n- in array compaction, the invariant is about the prefix being correct\n- in fast/slow midpoint problems, the invariant is about relative speed\n- in fixed-gap problems, the invariant is about distance\n\nSo the unifying idea is not \"both pointers move right.\" The unifying idea is:\n\n**\"the structure to the left of the pointers already has a known meaning\"**\n\n### What to watch out for\n\nCommon mistakes:\n\n- Updating the write pointer before copying the current value.\n- Forgetting exactly what region is already valid.\n- Losing track of whether the invariant is about a prefix, a gap, or relative speed.\n\nAs you go through the following problems, pay attention to one question:\n\n**What does the region before the slow pointer mean right now?**\n\n## Opposite Direction\n\nOpposite-direction pointer problems place one pointer on the left and one on the right, then move inward.\n\nThe hard part is never the loop itself. **The hard part is justifying which pointer is safe to move.**\n\n### When this family applies\n\nReach for opposite-direction pointers when the problem asks you to:\n\n- the input is sorted, or can be made monotonic by sorting\n- you are comparing a left choice and a right choice\n- the problem has symmetry\n- moving one side lets you eliminate a set of impossible answers\n\n### Core Invariant\n\nThe invariant in this family is usually:\n\n> \"All candidate answers outside the current `[left, right]` range have already been ruled out.\"\n\nThat means each move must come with a proof. If you move a pointer without knowing why the discarded cases cannot contain the optimum, the algorithm is just guessing.\n\n### Subtypes in this section\n\n#### 1. Monotonic sum elimination\n\n\nBecause the array is sorted:\n\n- if the sum is too small, moving the left pointer right is the only useful move\n- if the sum is too large, moving the right pointer left is the only useful move\n\nSorted order gives you a monotonic rule.\n\n\n\n#### 2. Symmetry checking\n\nHere the pointers compare mirrored positions. The movement rule is mostly \"move both,\" except when you must skip punctuation or spaces.\n\n\n\n#### 3. Bottleneck elimination\n\nThis one is important because it looks different from the other two. The key idea is that the shorter wall limits the area. If you keep that shorter wall and decrease the width, you cannot improve the answer. So it is safe to discard that wall.\n\n\n\n### Why this is not a sliding window\n\nOpposite-direction scans do not maintain an interval property like \"this window is valid.\"\n\nInstead, each step does one of these:\n\n- rules out sums that are too small or too large\n- checks mirrored characters\n- discards a bottleneck boundary\n\nThe interval itself is just the remaining search space.\n\n### What to watch out for\n\nCommon mistakes:\n\n- Using two pointers on an unsorted pair problem without first creating monotonicity.\n- Moving the taller wall in Container With Most Water without an elimination proof.\n- Calling the method sliding window just because two boundaries exist.\n\nAs you read the problems in this family, keep asking:\n\n**What exact set of candidate answers did this pointer move eliminate?**\n","/two-pointers/3sum":"---\ntitle: '15. 3Sum'\ndescription: Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"Sorting\", slug: \"sorting\" }\nvideo: \"jzZsG8n2R9A\"\nissue: \"https://github.com/prdlk/leetcode/issues/43\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 69.6 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 66.8 }\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 65.6 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 63.8 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 80.4 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 66 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 57.2 }\n - { name: \"Cisco\", period: \"Six Months\", frequency: 100 }\n - { name: \"Wissen Technology\", period: \"Six Months\", frequency: 100 }\n - { name: \"eBay\", period: \"Six Months\", frequency: 75.2 }\n - { name: \"Accenture\", period: \"Six Months\", frequency: 75 }\n - { name: \"Visa\", period: \"Six Months\", frequency: 69.7 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 53.1 }\nsidebar:\n label: '3Sum'\n badge: 'Medium'\n---\n\n::::warning\nNotice that the solution set must not contain duplicate triplets.\n::::\n\n### Example 1:\n- Input: `nums = [-1,0,1,2,-1,-4]`\n- Output: `[[-1,-1,2],[-1,0,1]]`\n- Explanation: `nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0`. `nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0`. `nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0`. The distinct triplets are `[-1,0,1]` and `[-1,-1,2]`. Notice that the order of the output and the order of the triplets does not matter.\n\n### Example 2:\n- Input: `nums = [0,1,1]`\n- Output: `[]`\n- Explanation: The only possible triplet does not sum up to `0`.\n\n### Example 3:\n- Input: `nums = [0,0,0]`\n- Output: `[[0,0,0]]`\n- Explanation: The only possible triplet sums up to `0`.\n\n### Constraints:\n\n- `3 <= nums.length <= 3000`\n- `-10^5 <= nums[i] <= 10^5`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"threeSum(nums)\"]) --> O[\"nums.sort() — duplicates become adjacent\"]\n O --> F{\"more i in 0..n-1?\"}\n F -- no --> E([\"return result\"])\n F -- yes --> D{\"nums[i] == nums[i-1]?\"}\n D -- yes --> F\n D -- no --> P[\"left = i+1, right = n-1, target = -nums[i]\"]\n P --> W{\"left < right?\"}\n W -- no --> F\n W -- yes --> C[\"current = nums[left] + nums[right]\"]\n C --> Q{\"current vs target\"}\n Q -- equal --> A[\"append triplet, skip equal neighbours, left += 1, right -= 1\"]\n A --> W\n Q -- \"current < target\" --> L[\"left += 1 — need a bigger sum\"]\n L --> W\n Q -- \"current > target\" --> R[\"right -= 1 — need a smaller sum\"]\n R --> W\n```\n\n## Solution\n\n```py\nclass Solution:\n def threeSum(self, nums: list[int]) -> list[list[int]]:\n nums.sort()\n result = []\n n = len(nums)\n\n for i in range(n):\n # skip all zero\n if i > 0 and nums[i] == nums[i - 1]:\n continue\n\n # two pointers\n left = i + 1\n right = n - 1\n target = -nums[i]\n\n while left < right:\n current = nums[left] + nums[right]\n\n if current == target:\n result.append([nums[i], nums[left], nums[right]])\n\n # skip duplicates\n while left < right and nums[left] == nums[left + 1]:\n left += 1\n while left < right and nums[right] == nums[right - 1]:\n right -= 1\n\n # shift pointers\n left += 1\n right -= 1\n\n # since sorted, if current < target, then move left\n elif current < target:\n left += 1\n # since sorted, if current > target, then move right\n else:\n right -= 1\n\n return result\n```\n","/two-pointers/container-with-most-water":"---\ntitle: '11. Container With Most Water'\ndescription: You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the i^th line are (i, 0) and (i, height[i])\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"Greedy\", slug: \"greedy\" }\nvideo: \"UuiTKBwPgAo\"\nissue: \"https://github.com/prdlk/leetcode/issues/44\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 83.1 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 75.8 }\n - { name: \"Infosys\", period: \"Thirty Days\", frequency: 68.6 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 67.6 }\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 65.6 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 62 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Goldman Sachs\", period: \"Three Months\", frequency: 57 }\n - { name: \"Walmart Labs\", period: \"Six Months\", frequency: 70.5 }\n - { name: \"IBM\", period: \"Six Months\", frequency: 60.1 }\nsidebar:\n label: 'Container With Most Water'\n badge: 'Medium'\n---\n\n::::warning\nNotice that you may not slant the container.\n::::\n\n### Example 1:\n- Input: `height = [1,8,6,2,5,4,8,3,7]`\n- Output: `49`\n- Explanation: The above vertical lines are represented by array `[1,8,6,2,5,4,8,3,7]`. In this case, the max area of water (blue section) the container can contain is `49`.\n\n### Example 2:\n- Input: `height = [1,1]`\n- Output: `1`\n\n### Constraints:\n\n- `n == height.length`\n- `2 <= n <= 10^5`\n- `0 <= height[i] <= 10^4`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"maxArea(height)\"]) --> I[\"res = 0, left = 0, right = len - 1\"]\n I --> W{\"left < right?\"}\n W -- no --> E([\"return res\"])\n W -- yes --> A[\"area = (right - left) * min(height[left], height[right])\"]\n A --> M[\"res = max(res, area)\"]\n M --> C{\"height[left] < height[right]?\"}\n C -- yes --> L[\"left += 1 — drop the shorter wall\"]\n C -- no --> R[\"right -= 1 — drop the shorter wall\"]\n L --> W\n R --> W\n```\n\n## Solution\n\n```py\nclass Solution:\n def maxArea(self, height: List[int]) -> int: # noqa: F821\n res = 0\n left = 0\n right = len(height) - 1\n\n while left < right:\n area = (right - left) * min(height[left], height[right])\n res = max(res, area)\n if height[left] < height[right]:\n left += 1\n else:\n right -= 1\n\n return res\n```\n","/two-pointers/is-subsequence":"---\ntitle: '392. Is Subsequence'\ndescription: Given two strings s and t, return true if s is a subsequence of t, or false otherwise\nicon: dot\ntopics:\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\nvideo: \"99RVfqklbCE\"\nissue: \"https://github.com/prdlk/leetcode/issues/45\"\ncompanies:\n - { name: \"Amazon\", period: \"Three Months\", frequency: 34.6 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\n - { name: \"Google\", period: \"Six Months\", frequency: 25.4 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 24.5 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 24.1 }\nsidebar:\n label: 'Is Subsequence'\n badge: 'Easy'\n---\n\n::::warning\nSuppose there are lots of incoming s, say s1, s2, ..., sk where k >= 10^9, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code?\n::::\n\n### Example 1:\n- Input: `s = \"abc\", t = \"ahbgdc\"`\n- Output: `true`\n\n### Example 2:\n- Input: `s = \"axc\", t = \"ahbgdc\"`\n- Output: `false`\n\n### Constraints:\n\n- `0 <= s.length <= 100`\n- `0 <= t.length <= 10^4`\n- `s` and `t` consist only of lowercase English letters.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"isSubsequence(s, t)\"]) --> G{\"len(s) > len(t)?\"}\n G -- yes --> X([\"return False\"])\n G -- no --> I[\"i = 0 into s, j = 0 into t\"]\n I --> W{\"i < len(s) and j < len(t)?\"}\n W -- no --> E([\"return i == len(s) — every char of s was matched\"])\n W -- yes --> Q{\"s[i] == t[j]?\"}\n Q -- yes --> A[\"i += 1 — consume the match\"]\n Q -- no --> B[\"j += 1 — always advance t\"]\n A --> B\n B --> W\n```\n\n## Solution\n\n```py\nclass Solution:\n def isSubsequence(self, s: str, t: str) -> bool:\n if len(s) > len(t):\n return False\n\n i, j = 0, 0\n while i < len(s) and j < len(t):\n if s[i] == t[j]:\n i += 1\n j += 1\n\n return i == len(s)\n```\n","/two-pointers/remove-duplicates-from-sorted-array-ii":"---\ntitle: '80. Remove Duplicates from Sorted Array II'\ndescription: Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\nissue: \"https://github.com/prdlk/leetcode/issues/48\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 34.6 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 32.7 }\nsidebar:\n label: 'Remove Duplicates from Sorted Array II'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [1,1,1,2,2,3]`\n- Output: `5, nums = [1,1,2,2,3,_]`\n- Explanation: Your function should return `k = 5`, with the first five elements of `nums` being `1`, `1`, `2`, `2` and `3` respectively. It does not matter what you leave beyond the returned `k` (hence they are underscores).\n\n### Example 2:\n- Input: `nums = [0,0,1,1,1,1,2,3,3]`\n- Output: `7, nums = [0,0,1,1,2,3,3,_,_]`\n- Explanation: Your function should return `k = 7`, with the first seven elements of `nums` being `0`, `0`, `1`, `1`, `2`, `3` and `3` respectively. It does not matter what you leave beyond the returned `k` (hence they are underscores).\n\n### Constraints:\n\n- `1 <= nums.length <= 3 * 10^4`\n- `-10^4 <= nums[i] <= 10^4`\n- `nums` is sorted in non-decreasing order.\n\n## Solution\n\n```py\nclass Solution:\n def removeDuplicates(self, nums: list[int]) -> int:\n slow = 0\n for fast in range(len(nums)):\n if slow < 2 or nums[fast] != nums[slow - 2]:\n nums[slow] = nums[fast]\n slow += 1\n return slow\n```\n","/two-pointers/reverse-string":"---\ntitle: '344. Reverse String'\ndescription: Write a function that reverses a string. The input string is given as an array of characters s\nicon: dot\ntopics:\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"String\", slug: \"string\" }\nvideo: \"_d0T_2Lk2qA\"\ncompanies:\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 58.2 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 50.2 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 48.3 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 46.8 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Qualcomm\", period: \"Six Months\", frequency: 80.7 }\nsidebar:\n label: 'Reverse String'\n badge: 'Easy'\n---\n\n:::warning\nYou must do this by modifying the input array in-place with O(1) extra memory.\n:::\n\n### Example 1:\n- Input: `s = [\"h\",\"e\",\"l\",\"l\",\"o\"]`\n- Output: `[\"o\",\"l\",\"l\",\"e\",\"h\"]`\n\n### Example 2:\n- Input: `s = [\"H\",\"a\",\"n\",\"n\",\"a\",\"h\"]`\n- Output: `[\"h\",\"a\",\"n\",\"n\",\"a\",\"H\"]`\n\n### Constraints:\n\n- `1 <= s.length <= 10^5`\n- `s[i]` is a printable ascii character.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"reverseString(s)\"]) --> I[\"i = 0, j = s.length - 1\"]\n I --> W{\"i < j?\"}\n W -- no --> E([\"done — s was reversed in place, nothing returned\"])\n W -- yes --> A[\"swap s[i] and s[j]\"]\n A --> B[\"i++; j--\"]\n B --> W\n```\n\n## Solution\n\n```js\n/**\n * @param {character[]} s\n * @return {void} Do not return anything, modify s in-place instead.\n */\nvar reverseString = function(s) {\n let i = 0;\n let j = s.length - 1;\n \n while (i < j) {\n [s[i], s[j]] = [s[j], s[i]];\n j--;\n i++;\n } \n};\n```\n","/two-pointers/rotate-array":"---\ntitle: '189. Rotate Array'\ndescription: Given an integer array nums, rotate the array to the right by k steps, where k is non-negative\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Math\", slug: \"math\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\nissue: \"https://github.com/prdlk/leetcode/issues/47\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 46.6 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 66 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 57.2 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 54.8 }\n - { name: \"Box\", period: \"Six Months\", frequency: 100 }\n - { name: \"TikTok\", period: \"Six Months\", frequency: 55.2 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 48.1 }\nsidebar:\n label: 'Rotate Array'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [1,2,3,4,5,6,7], k = 3`\n- Output: `[5,6,7,1,2,3,4]`\n- Explanation: rotate `1` steps to the right: `[7,1,2,3,4,5,6]` rotate `2` steps to the right: `[6,7,1,2,3,4,5]` rotate `3` steps to the right: `[5,6,7,1,2,3,4]`\n\n### Example 2:\n- Input: `nums = [-1,-100,3,99], k = 2`\n- Output: `[3,99,-1,-100]`\n- Explanation: rotate `1` steps to the right: `[99,-1,-100,3]` rotate `2` steps to the right: `[3,99,-1,-100]`\n\n### Constraints:\n\n- `1 <= nums.length <= 10^5`\n- `-2^31 <= nums[i] <= 2^31 - 1`\n- `0 <= k <= 10^5`\n- Try to come up with as many solutions as you can. There are at least three different ways to solve this problem.\n- Could you do it in-place with O(1) extra space?\n\n## Solution\n\n```py\nclass Solution:\n def rotate(self, nums: list[int], k: int) -> None:\n N = len(nums) - 1\n cut = k % len(nums)\n\n def reverse(l, r):\n while l <= r:\n nums[l], nums[r] = nums[r], nums[l]\n l += 1\n r -= 1\n\n reverse(0, N)\n reverse(0, cut - 1)\n reverse(cut, N)\n```\n","/two-pointers/squares-of-a-sorted-array":"---\ntitle: '977. Squares of a Sorted Array'\ndescription: Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"Sorting\", slug: \"sorting\" }\nvideo: \"FPCZsG_AkUg\"\nissue: \"https://github.com/prdlk/leetcode/issues/46\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 42.3 }\n - { name: \"Instacart\", period: \"Three Months\", frequency: 100 }\n - { name: \"Uber\", period: \"Three Months\", frequency: 80 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 49.8 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 46 }\n - { name: \"tcs\", period: \"Six Months\", frequency: 60 }\n - { name: \"Yandex\", period: \"Six Months\", frequency: 59.6 }\nsidebar:\n label: 'Squares of a Sorted Array'\n badge: 'Easy'\n---\n\n::::warning\nSquaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach?\n::::\n\n### Example 1:\n- Input: `nums = [-4,-1,0,3,10]`\n- Output: `[0,1,9,16,100]`\n- Explanation: After squaring, the array becomes `[16,1,0,9,100]`. After sorting, it becomes `[0,1,9,16,100]`.\n\n### Example 2:\n- Input: `nums = [-7,-3,2,3,11]`\n- Output: `[4,9,9,49,121]`\n\n### Constraints:\n\n- `1 <= nums.length <= 10^4`\n- `-10^4 <= nums[i] <= 10^4`\n- `nums` is sorted in non-decreasing order.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"sortedSquares(nums)\"]) --> I[\"ans = Array(n), left = 0, right = n - 1\"]\n I --> L{\"more i from n-1 down to 0?\"}\n L -- no --> E([\"return ans\"])\n L -- yes --> C{\"abs(nums[left]) < abs(nums[right])?\"}\n C -- yes --> R[\"square = nums[right]; right--\"]\n C -- no --> F[\"square = nums[left]; left++\"]\n R --> W[\"ans[i] = square * square — fill from the back, largest first\"]\n F --> W\n W --> L\n```\n\n## Solution\n\n```js\n/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar sortedSquares = function(nums) {\n let n = nums.length;\n let ans = new Array(nums.length);\n let left = 0, right = nums.length - 1;\n\n for (let i = n -1; i >= 0; i--){\n let square;\n if(Math.abs(nums[left]) < Math.abs(nums[right])){\n square = nums[right];\n right--;\n }else{\n square = nums[left];\n left++;\n }\n ans[i] = square*square;\n }\n return ans;\n};\n```\n","/two-pointers/trapping-rain-water":"---\ntitle: '42. Trapping Rain Water'\ndescription: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\n - { name: \"Stack\", slug: \"stack\" }\n - { name: \"Monotonic Stack\", slug: \"monotonic-stack\" }\nissue: \"https://github.com/prdlk/leetcode/issues/211\"\ncompanies:\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 90.7 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 80.7 }\n - { name: \"Goldman Sachs\", period: \"Thirty Days\", frequency: 71.7 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Infosys\", period: \"Thirty Days\", frequency: 68.6 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 63.7 }\n - { name: \"Snowflake\", period: \"Three Months\", frequency: 90.3 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 58.9 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 57.2 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"Docusign\", period: \"Six Months\", frequency: 100 }\n - { name: \"Qualcomm\", period: \"Six Months\", frequency: 80.7 }\n - { name: \"Nvidia\", period: \"Six Months\", frequency: 78.7 }\nsidebar:\n label: 'Trapping Rain Water'\n badge: 'Hard'\n---\n\n### Example 1:\n- Input: `height = [0,1,0,2,1,0,1,3,2,1,2,1]`\n- Output: `6`\n- Explanation: The above elevation map (black section) is represented by array `[0,1,0,2,1,0,1,3,2,1,2,1]`. In this case, `6` units of rain water (blue section) are being trapped.\n\n### Example 2:\n- Input: `height = [4,2,0,3,2,5]`\n- Output: `9`\n\n### Constraints:\n\n- `n == height.length`\n- `1 <= n <= 2 * 10^4`\n- `0 <= height[i] <= 10^5`\n\n## Solution\n\n```py\nclass Solution:\n def trap(self, height: list[int]) -> int:\n left, right = 0, len(height) - 1\n leftMax, rightMax = 0, 0\n total = 0\n\n while left < right:\n leftMax = max(leftMax, height[left])\n rightMax = max(rightMax, height[right])\n\n if height[left] < height[right]:\n total += leftMax - height[left]\n left += 1\n else:\n total += rightMax - height[right]\n right -= 1\n\n return total\n```\n","/two-pointers/two-sum-ii-input-array-is-sorted":"---\ntitle: '167. Two Sum II - Input Array Is Sorted'\ndescription: Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\nvideo: \"cQ1Oz4ckceM\"\nissue: \"https://github.com/prdlk/leetcode/issues/42\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 65.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 46.6 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 57.2 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 54.8 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"Oracle\", period: \"Six Months\", frequency: 78.7 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 40.6 }\nsidebar:\n label: 'Two Sum II - Input Array Is Sorted'\n badge: 'Medium'\n---\n\n::::warning\nYour solution must use only constant extra space.\n::::\n\n### Example 1:\n- Input: `numbers = [2,7,11,15], target = 9`\n- Output: `[1,2]`\n- Explanation: The sum of `2` and `7` is `9`. Therefore, `index1 = 1`, `index2 = 2`. We return `[1, 2]`.\n\n### Example 2:\n- Input: `numbers = [2,3,4], target = 6`\n- Output: `[1,3]`\n- Explanation: The sum of `2` and `4` is `6`. Therefore `index1 = 1`, `index2 = 3`. We return `[1, 3]`.\n\n### Example 3:\n- Input: `numbers = [-1,0], target = -1`\n- Output: `[1,2]`\n- Explanation: The sum of `-1` and `0` is `-1`. Therefore `index1 = 1`, `index2 = 2`. We return `[1, 2]`.\n\n### Constraints:\n\n- `2 <= numbers.length <= 3 * 10^4`\n- `-1000 <= numbers[i] <= 1000`\n- `numbers` is sorted in non-decreasing order.\n- `-1000 <= target <= 1000`\n- The tests are generated such that there is exactly one solution.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"twoSum(numbers, target)\"]) --> I[\"i = 0, j = len - 1\"]\n I --> W{\"i < j?\"}\n W -- no --> E([\"return []\"])\n W -- yes --> C[\"c = numbers[i] + numbers[j]\"]\n C --> Q{\"c vs target\"}\n Q -- equal --> R([\"return [i+1, j+1] — 1-indexed\"])\n Q -- \"c < target\" --> L[\"i += 1 — grow the sum\"]\n L --> W\n Q -- \"c > target\" --> H[\"j -= 1 — shrink the sum\"]\n H --> W\n```\n\n## Solution\n\n```py\nclass Solution:\n def twoSum(self, numbers: List[int], target: int) -> List[int]:\n i = 0\n j = len(numbers) - 1\n\n while i < j:\n c = numbers[i] + numbers[j]\n if c == target:\n return [i + 1, j + 1]\n elif c < target:\n i+=1\n else:\n j-=1\n return []\n```\n","/two-pointers/valid-palindrome":"---\ntitle: '125. Valid Palindrome'\ndescription: A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers\nicon: dot\ntopics:\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"String\", slug: \"string\" }\nvideo: \"jJXJ16kPFWg\"\nissue: \"https://github.com/prdlk/leetcode/issues/41\"\ncompanies:\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 58.2 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 79.9 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 66 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Deloitte\", period: \"Six Months\", frequency: 81 }\n - { name: \"Cognizant\", period: \"Six Months\", frequency: 76.3 }\n - { name: \"Axon\", period: \"Six Months\", frequency: 75.4 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 64.8 }\nsidebar:\n label: 'Valid Palindrome'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `s = \"A man, a plan, a canal: Panama\"`\n- Output: `true`\n- Explanation: `\"amanaplanacanalpanama\"` is a palindrome.\n\n### Example 2:\n- Input: `s = \"race a car\"`\n- Output: `false`\n- Explanation: `\"raceacar\"` is not a palindrome.\n\n### Example 3:\n- Input: `s = \" \"`\n- Output: `true`\n- Explanation: `s` is an empty string `\"\"` after removing non-alphanumeric characters. Since an empty string reads the same forward and backward, it is a palindrome.\n\n### Constraints:\n\n- `1 <= s.length <= 2 * 10^5`\n- `s` consists only of printable ASCII characters.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"isPalindrome(s)\"]) --> N[\"normal = s stripped of non-alphanumerics, lowercased\"]\n N --> I[\"i = 0, j = normal.length - 1\"]\n I --> W{\"i < j?\"}\n W -- no --> E([\"return true — the pointers met in the middle\"])\n W -- yes --> Q{\"normal[i] != normal[j]?\"}\n Q -- yes --> F([\"return false\"])\n Q -- no --> A[\"i++; j-- — step both inward\"]\n A --> W\n```\n\n## Solution\n\n```js\n/**\n * @param {string} s\n * @return {boolean}\n */\nvar isPalindrome = function(s) {\n let normal = s.replace(/[^a-zA-Z0-9]/g, \"\").toLowerCase() \n let i = 0, j = normal.length - 1;\n\n while (i < j) {\n if(normal[i] !== normal[j]) {\n return false;\n }\n i++;\n j--;\n }\n return true;\n};\n```\n","/union-find":"---\ntitle: Union Find\ndescription: 'Union-Find (Disjoint Set Union) is a data structure that maintains a collection of disjoint sets and supports efficiently finding which set an element belongs to and merging two sets.'\nhidden: true\n---\n\n\n\n\n","/union-find/number-of-connected-components-in-an-undirected-graph":"---\ntitle: '323. Number of Connected Components in an Undirected Graph'\ndescription: You have a graph of n nodes. You are given an integer n and an array edges where edges[i] = [ai, bi] indicates that there is an edge between ai and bi in the graph\nicon: dot\ntopics:\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Union-Find\", slug: \"union-find\" }\n - { name: \"Graph Theory\", slug: \"graph\" }\nissue: \"https://github.com/prdlk/leetcode/issues/147\"\nsidebar:\n label: 'Number of Connected Components in an Undirected Graph'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `n = 5, edges = [[0,1],[1,2],[3,4]]`\n- Output: `2`\n\n### Example 2:\n- Input: `n = 5, edges = [[0,1],[1,2],[2,3],[3,4]]`\n- Output: `1`\n\n### Constraints:\n\n- `1 <= n <= 2000`\n- `1 <= edges.length <= 5000`\n- `edges[i]` = [ai, bi]\n- `ai != bi`\n- There are no repeated edges.\n\n## Solution\n\n```py\nclass Solution:\n def countComponents(self, n: int, edges: list[list[int]]) -> int:\n par = [i for i in range(n + 1)]\n rank = [1] * (n + 1)\n\n def find(n):\n res = n\n while res != par[res]:\n par[res] = par[par[res]]\n res = par[res]\n return res\n\n def union(n1, n2):\n p1, p2 = find(n1), find(n2)\n if p1 == p2:\n return 0\n if rank[p1] > rank[p2]:\n par[p2] = p1\n rank[p1] += rank[p2]\n else:\n par[p1] = p2\n rank[p2] += rank[p1]\n return 1\n\n res = n\n for n1, n2 in edges:\n res -= union(n1, n2)\n return res\n```\n","/union-find/redundant-connection":"---\ntitle: '684. Redundant Connection'\ndescription: In this problem, a tree is an undirected graph that is connected and has no cycles\nicon: dot\ntopics:\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Union-Find\", slug: \"union-find\" }\n - { name: \"Graph Theory\", slug: \"graph\" }\nissue: \"https://github.com/prdlk/leetcode/issues/146\"\ncompanies:\n - { name: \"Box\", period: \"Three Months\", frequency: 100 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 21.2 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 24.1 }\nsidebar:\n label: 'Redundant Connection'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `edges = [[1,2],[1,3],[2,3]]`\n- Output: `[2,3]`\n\n### Example 2:\n- Input: `edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]`\n- Output: `[1,4]`\n\n### Constraints:\n\n- `n == edges.length`\n- `3 <= n <= 1000`\n- `edges[i].length == 2`\n- `1 <= ai < bi <= edges.length`\n- `ai != bi`\n- There are no repeated edges.\n- The given graph is connected.\n\n## Solution\n\n```py\nclass Solution:\n def findRedundantConnection(self, edges: list[list[int]]) -> list[int]:\n N = len(edges)\n par = [i for i in range(N + 1)]\n rank = [1] * (N + 1)\n\n def find(n):\n if n != par[n]:\n par[n] = find(par[n])\n return par[n]\n\n def union(n1, n2):\n p1, p2 = find(n1), find(n2)\n if p1 == p2:\n return False\n\n if rank[p1] > rank[p2]:\n par[p2] = p1\n rank[p1] += rank[p2]\n else:\n par[p1] = p2\n rank[p2] += rank[p1]\n\n return True\n\n for n1, n2 in edges:\n if not union(n1, n2):\n return [n1, n2]\n```\n"},"routes":[{"contentType":"doc","description":"Structured dependency spine based roadmap for solving DSA Problems on leetcode.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/","title":"Introduction","version":""},{"contentType":"doc","description":"The result for each state is stored in a list, and each new result is computed from the results before it.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/1d-dynamic-programming","title":"1-D Dynamic Programming","version":""},{"contentType":"doc","description":"You are climbing a staircase. It takes n steps to reach the top","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/1d-dynamic-programming/climbing-stairs","title":"70. Climbing Stairs","version":""},{"contentType":"doc","description":"You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/1d-dynamic-programming/coin-change","title":"322. Coin Change","version":""},{"contentType":"doc","description":"You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/1d-dynamic-programming/house-robber","title":"198. House Robber","version":""},{"contentType":"doc","description":"Backtracking is a depth-first search technique that builds a solution step by step and abandons (undoes) a partial choice as soon as it can’t lead to a valid complete solution.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/backtracking","title":"Backtracking","version":""},{"contentType":"doc","description":"Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/backtracking/combination-sum","title":"39. Combination Sum","version":""},{"contentType":"doc","description":"Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/backtracking/permutations","title":"46. Permutations","version":""},{"contentType":"doc","description":"Given an integer array nums of unique elements, return all possible subsets (the power set)","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/backtracking/subsets","title":"78. Subsets","version":""},{"contentType":"doc","description":"Half of a sorted range is removed at each step until the target is found.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/binary-search","title":"Binary Search","version":""},{"contentType":"doc","description":"Each node holds a value that is larger than its left subtree and smaller than its right subtree.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/binary-search-tree","title":"Binary Search Tree","version":""},{"contentType":"doc","description":"Given the root of a binary search tree, and an integer k, return the k^th smallest value (1-indexed) of all the values of the nodes in the tree","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/binary-search-tree/kth-smallest-element-in-a-bst","title":"230. Kth Smallest Element in a BST","version":""},{"contentType":"doc","description":"Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/binary-search-tree/lowest-common-ancestor-of-a-binary-search-tree","title":"235. Lowest Common Ancestor of a Binary Search Tree","version":""},{"contentType":"doc","description":"Given the root of a binary tree, determine if it is a valid binary search tree (BST)","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/binary-search-tree/validate-binary-search-tree","title":"98. Validate Binary Search Tree","version":""},{"contentType":"doc","description":"Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/binary-search/binary-search","title":"704. Binary Search","version":""},{"contentType":"doc","description":"Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/binary-search/find-first-and-last-position-of-element-in-sorted-array","title":"34. Find First and Last Position of Element in Sorted Array","version":""},{"contentType":"doc","description":"Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/binary-search/find-minimum-in-rotated-sorted-array","title":"153. Find Minimum in Rotated Sorted Array","version":""},{"contentType":"doc","description":"A peak element is an element that is strictly greater than its neighbors","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/binary-search/find-peak-element","title":"162. Find Peak Element","version":""},{"contentType":"doc","description":"Koko loves to eat bananas. There are n piles of bananas, the i^th pile has piles[i] bananas. The guards have gone and will come back in h hours","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/binary-search/koko-eating-bananas","title":"875. Koko Eating Bananas","version":""},{"contentType":"doc","description":"Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/binary-search/median-of-two-sorted-arrays","title":"4. Median of Two Sorted Arrays","version":""},{"contentType":"doc","description":"You are given an m x n integer matrix matrix with the following two properties:","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/binary-search/search-a-2d-matrix","title":"74. Search a 2D Matrix","version":""},{"contentType":"doc","description":"There is an integer array nums sorted in ascending order (with distinct values)","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/binary-search/search-in-rotated-sorted-array","title":"33. Search in Rotated Sorted Array","version":""},{"contentType":"doc","description":"Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/binary-search/search-insert-position","title":"35. Search Insert Position","version":""},{"contentType":"doc","description":"Nodes are visited in order of distance from the start, which gives the shortest path in an unweighted graph.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/graph-bfs","title":"Graph BFS","version":""},{"contentType":"doc","description":"You are given an m x n grid where each cell can have one of three values:","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/graph-bfs/rotting-oranges","title":"994. Rotting Oranges","version":""},{"contentType":"doc","description":"A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/graph-bfs/word-ladder","title":"127. Word Ladder","version":""},{"contentType":"doc","description":"Each edge is followed to its end, and a visited set prevents a repeat visit.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/graph-dfs","title":"Graph DFS","version":""},{"contentType":"doc","description":"Given a reference of a node in a connected undirected graph","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/graph-dfs/clone-graph","title":"133. Clone Graph","version":""},{"contentType":"doc","description":"Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/graph-dfs/number-of-islands","title":"200. Number of Islands","version":""},{"contentType":"doc","description":"There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/graph-dfs/pacific-atlantic-water-flow","title":"417. Pacific Atlantic Water Flow","version":""},{"contentType":"doc","description":"The best local choice is made at each step and is not changed later.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/greedy","title":"Greedy","version":""},{"contentType":"doc","description":"There are n gas stations along a circular route, where the amount of gas at the i^th station is gas[i]","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/greedy/gas-station","title":"134. Gas Station","version":""},{"contentType":"doc","description":"You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/greedy/jump-game","title":"55. Jump Game","version":""},{"contentType":"doc","description":"Given an integer array nums, find the subarray with the largest sum, and return its sum","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/greedy/maximum-subarray","title":"53. Maximum Subarray","version":""},{"contentType":"doc","description":"A key maps to a value in constant time, which removes the need for a full scan.","indexable":true,"lastModified":"2026-10-05T15:47:02-04:00","locale":"","route":"/hash-table","title":"Hash Table","version":""},{"contentType":"doc","description":"A pangram is a sentence where every letter of the English alphabet appears at least once","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/hash-table/check-if-the-sentence-is-pangram","title":"1832. Check if the Sentence Is Pangram","version":""},{"contentType":"doc","description":"Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/hash-table/contains-duplicate","title":"217. Contains Duplicate","version":""},{"contentType":"doc","description":"Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/hash-table/first-unique-character-in-a-string","title":"387. First Unique Character in a String","version":""},{"contentType":"doc","description":"Given an array of strings strs, group the anagrams together. You can return the answer in any order","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/hash-table/group-anagrams","title":"49. Group Anagrams","version":""},{"contentType":"doc","description":"Given two strings s and t, determine if they are isomorphic","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/hash-table/isomorphic-strings","title":"205. Isomorphic Strings","version":""},{"contentType":"doc","description":"Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/hash-table/longest-consecutive-sequence","title":"128. Longest Consecutive Sequence","version":""},{"contentType":"doc","description":"Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/hash-table/sort-characters-by-frequency","title":"451. Sort Characters By Frequency","version":""},{"contentType":"doc","description":"Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/hash-table/top-k-frequent-elements","title":"347. Top K Frequent Elements","version":""},{"contentType":"doc","description":"You are given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/hash-table/two-sum","title":"1. Two Sum","version":""},{"contentType":"doc","description":"Given two strings s and t, return true if t is an anagram of s, and false otherwise","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/hash-table/valid-anagram","title":"242. Valid Anagram","version":""},{"contentType":"doc","description":"Given a pattern and a string s, find if s follows the same pattern","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/hash-table/word-pattern","title":"290. Word Pattern","version":""},{"contentType":"doc","description":"The largest or smallest item is always at the top, and it is removed in logarithmic time.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/heap-priority-queue","title":"Heap / Priority Queue","version":""},{"contentType":"doc","description":"Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0)","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/heap-priority-queue/k-closest-points-to-origin","title":"973. K Closest Points to Origin","version":""},{"contentType":"doc","description":"Given an integer array nums and an integer k, return the k^th largest element in the array","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/heap-priority-queue/kth-largest-element-in-an-array","title":"215. Kth Largest Element in an Array","version":""},{"contentType":"doc","description":"You are given an array of k linked-lists lists, each linked-list is sorted in ascending order","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/heap-priority-queue/merge-k-sorted-lists","title":"23. Merge k Sorted Lists","version":""},{"contentType":"doc","description":"Two structures are combined, such as a hash map and a linked list, to get the best properties of each.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/hybrid-structures","title":"Hybrid Structures","version":""},{"contentType":"doc","description":"You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/hybrid-structures/add-two-numbers","title":"2. Add Two Numbers","version":""},{"contentType":"doc","description":"Implement the RandomizedSet class:","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/hybrid-structures/insert-delete-getrandom-o1","title":"380. Insert Delete GetRandom O(1)","version":""},{"contentType":"doc","description":"Design a data structure that follows the constraints of a Least Recently Used (LRU) cache","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/hybrid-structures/lru-cache","title":"146. LRU Cache","version":""},{"contentType":"doc","description":"Ranges with a start and an end are sorted, and then merged, counted, or scheduled.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/intervals","title":"Intervals","version":""},{"contentType":"doc","description":"You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the i^th interval and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/intervals/insert-interval","title":"57. Insert Interval","version":""},{"contentType":"doc","description":"Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/intervals/merge-intervals","title":"56. Merge Intervals","version":""},{"contentType":"doc","description":"Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/intervals/non-overlapping-intervals","title":"435. Non-overlapping Intervals","version":""},{"contentType":"doc","description":"Coding problems rarely tell you which algorithm to use, but the wording usually leaks it. \"K-th largest\" hints at a heap. \"Shortest path\" in an unweighted graph hints at BFS.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/keywords","title":"Keywords","version":""},{"contentType":"doc","description":"Each node points to the next node, and the pointers give the path through the data.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/linked-list","title":"Linked List","version":""},{"contentType":"doc","description":"Given head, the head of a linked list, determine if the linked list has a cycle in it","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/linked-list/linked-list-cycle","title":"141. Linked List Cycle","version":""},{"contentType":"doc","description":"You are given the heads of two sorted linked lists list1 and list2","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/linked-list/merge-two-sorted-lists","title":"21. Merge Two Sorted Lists","version":""},{"contentType":"doc","description":"Given the head of a singly linked list, return the middle node of the linked list","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/linked-list/middle-of-the-linked-list","title":"876. Middle of the Linked List","version":""},{"contentType":"doc","description":"You are given the head of a sorted linked list","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/linked-list/remove-duplicates-from-sorted-list-ii","title":"82. Remove Duplicates from Sorted List II","version":""},{"contentType":"doc","description":"Given the head of a linked list, remove the n^th node from the end of the list and return its head","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/linked-list/remove-nth-node-from-end-of-list","title":"19. Remove Nth Node From End of List","version":""},{"contentType":"doc","description":"You are given the head of a singly linked-list. The list can be represented as:","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/linked-list/reorder-list","title":"143. Reorder List","version":""},{"contentType":"doc","description":"Given the head of a singly linked list, reverse the list, and return the reversed list","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/linked-list/reverse-linked-list","title":"206. Reverse Linked List","version":""},{"contentType":"doc","description":"Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/linked-list/reverse-linked-list-ii","title":"92. Reverse Linked List II","version":""},{"contentType":"doc","description":"A two-dimensional grid is read as a flat array through a formula, and the neighbors are found by arithmetic.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/matrix-index-math","title":"Matrix Index Math","version":""},{"contentType":"doc","description":"You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise)","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/matrix-index-math/rotate-image","title":"48. Rotate Image","version":""},{"contentType":"doc","description":"Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/matrix-index-math/set-matrix-zeroes","title":"73. Set Matrix Zeroes","version":""},{"contentType":"doc","description":"Given an m x n matrix, return all elements of the matrix in spiral order","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/matrix-index-math/spiral-matrix","title":"54. Spiral Matrix","version":""},{"contentType":"doc","description":"Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/matrix-index-math/valid-sudoku","title":"36. Valid Sudoku","version":""},{"contentType":"doc","description":"A stack keeps its items in sorted order, which finds the next larger or smaller item in one pass.","indexable":true,"lastModified":"2026-10-06T12:53:26-04:00","locale":"","route":"/monotonic-stack","title":"Monotonic Stack","version":""},{"contentType":"doc","description":"There are n cars at given miles away from the starting mile 0, traveling to reach the mile target","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/monotonic-stack/car-fleet","title":"853. Car Fleet","version":""},{"contentType":"doc","description":"Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the i^th day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/monotonic-stack/daily-temperatures","title":"739. Daily Temperatures","version":""},{"contentType":"doc","description":"Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/monotonic-stack/largest-rectangle-in-histogram","title":"84. Largest Rectangle in Histogram","version":""},{"contentType":"doc","description":"The next greater element of some element x in an array is the first greater element that is to the right of x in the same array","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/monotonic-stack/next-greater-element-i","title":"496. Next Greater Element I","version":""},{"contentType":"doc","description":"Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/monotonic-stack/online-stock-span","title":"901. Online Stock Span","version":""},{"contentType":"doc","description":"The result for each state is stored in a table with two or more dimensions, and each cell is computed from its neighbors.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/multi-d-grid-dp","title":"Multi-D / Grid DP","version":""},{"contentType":"doc","description":"Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/multi-d-grid-dp/longest-common-subsequence","title":"1143. Longest Common Subsequence","version":""},{"contentType":"doc","description":"Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/multi-d-grid-dp/minimum-path-sum","title":"64. Minimum Path Sum","version":""},{"contentType":"doc","description":"There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/multi-d-grid-dp/unique-paths","title":"62. Unique Paths","version":""},{"contentType":"doc","description":"A running total is stored at each index, so the sum of any range is one subtraction.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/prefix-sum","title":"Prefix Sum","version":""},{"contentType":"doc","description":"Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/prefix-sum/contiguous-array","title":"525. Contiguous Array","version":""},{"contentType":"doc","description":"Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/prefix-sum/product-of-array-except-self","title":"238. Product of Array Except Self","version":""},{"contentType":"doc","description":"Given an integer array nums, handle multiple queries of the following type","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/prefix-sum/range-sum-query-immutable","title":"303. Range Sum Query - Immutable","version":""},{"contentType":"doc","description":"Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/prefix-sum/subarray-sum-equals-k","title":"560. Subarray Sum Equals K","version":""},{"contentType":"doc","description":"Given an integer array nums and an integer k, return the number of non-empty subarrays that have a sum divisible by k","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/prefix-sum/subarray-sums-divisible-by-k","title":"974. Subarray Sums Divisible by K","version":""},{"contentType":"doc","description":"Sliding window is a specialized two-pointer family where the pointers define a contiguous interval and you maintain information about that entire interval as it moves.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/sliding-window","title":"Sliding Window","version":""},{"contentType":"doc","description":"You are given an array prices where prices[i] is the price of a given stock on the i^th day","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/sliding-window/best-time-to-buy-and-sell-stock","title":"121. Best Time to Buy and Sell Stock","version":""},{"contentType":"doc","description":"You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/sliding-window/longest-repeating-character-replacement","title":"424. Longest Repeating Character Replacement","version":""},{"contentType":"doc","description":"Given a string s, find the length of the longest substring without duplicate characters","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/sliding-window/longest-substring-without-repeating-characters","title":"3. Longest Substring Without Repeating Characters","version":""},{"contentType":"doc","description":"Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/sliding-window/max-consecutive-ones-iii","title":"1004. Max Consecutive Ones III","version":""},{"contentType":"doc","description":"You are given an integer array nums consisting of n elements, and an integer k","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/sliding-window/maximum-average-subarray-i","title":"643. Maximum Average Subarray I","version":""},{"contentType":"doc","description":"Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/sliding-window/minimum-size-subarray-sum","title":"209. Minimum Size Subarray Sum","version":""},{"contentType":"doc","description":"Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string \"\"","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/sliding-window/minimum-window-substring","title":"76. Minimum Window Substring","version":""},{"contentType":"doc","description":"Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/sliding-window/permutation-in-string","title":"567. Permutation in String","version":""},{"contentType":"doc","description":"Items are added and removed from one end, so the last item added is the first item removed.","indexable":true,"lastModified":"2026-10-06T12:58:52-04:00","locale":"","route":"/stack","title":"Stack","version":""},{"contentType":"doc","description":"Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/stack/basic-calculator","title":"224. Basic Calculator","version":""},{"contentType":"doc","description":"You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/stack/evaluate-reverse-polish-notation","title":"150. Evaluate Reverse Polish Notation","version":""},{"contentType":"doc","description":"Design a stack that supports push, pop, top, and retrieving the minimum element in constant time","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/stack/min-stack","title":"155. Min Stack","version":""},{"contentType":"doc","description":"You are given an absolute path for a Unix-style file system, which always begins with a slash '/'. Your task is to transform this absolute path into its simplified canonical path","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/stack/simplify-path","title":"71. Simplify Path","version":""},{"contentType":"doc","description":"Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/stack/valid-parentheses","title":"20. Valid Parentheses","version":""},{"contentType":"doc","description":"Topological sort or topological ordering of a directed graph is an ordering of nodes such that every node appears in the ordering before all the nodes it points to. ","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/topological-sort","title":"Topological Sort","version":""},{"contentType":"doc","description":"There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/topological-sort/course-schedule","title":"207. Course Schedule","version":""},{"contentType":"doc","description":"There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/topological-sort/course-schedule-ii","title":"210. Course Schedule II","version":""},{"contentType":"doc","description":"All nodes at one level are visited before the nodes at the next level.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/tree-bfs","title":"Tree BFS","version":""},{"contentType":"doc","description":"Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level)","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/tree-bfs/binary-tree-level-order-traversal","title":"102. Binary Tree Level Order Traversal","version":""},{"contentType":"doc","description":"Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/tree-bfs/binary-tree-right-side-view","title":"199. Binary Tree Right Side View","version":""},{"contentType":"doc","description":"Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/tree-bfs/count-good-nodes-in-binary-tree","title":"1448. Count Good Nodes in Binary Tree","version":""},{"contentType":"doc","description":"Each branch is followed to its end before the next branch is examined.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/tree-dfs","title":"Tree DFS","version":""},{"contentType":"doc","description":"Given the root of a binary tree, invert the tree, and return its root","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/tree-dfs/invert-binary-tree","title":"226. Invert Binary Tree","version":""},{"contentType":"doc","description":"Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/tree-dfs/lowest-common-ancestor-of-a-binary-tree","title":"236. Lowest Common Ancestor of a Binary Tree","version":""},{"contentType":"doc","description":"Given the root of a binary tree, return its maximum depth","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/tree-dfs/maximum-depth-of-binary-tree","title":"104. Maximum Depth of Binary Tree","version":""},{"contentType":"doc","description":"Each node holds one character, and a path from the root spells a word.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/trie","title":"Trie","version":""},{"contentType":"doc","description":"Design a data structure that supports adding new words and finding if a string matches any previously added string","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/trie/design-add-and-search-words-data-structure","title":"211. Design Add and Search Words Data Structure","version":""},{"contentType":"doc","description":"A trie (pronounced as \"try\") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/trie/implement-trie-prefix-tree","title":"208. Implement Trie (Prefix Tree)","version":""},{"contentType":"doc","description":"Two indices move through the data from different positions and meet at the answer.","indexable":true,"lastModified":"2026-10-06T13:16:45-04:00","locale":"","route":"/two-pointers","title":"Two Pointers","version":""},{"contentType":"doc","description":"Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/two-pointers/3sum","title":"15. 3Sum","version":""},{"contentType":"doc","description":"You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the i^th line are (i, 0) and (i, height[i])","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/two-pointers/container-with-most-water","title":"11. Container With Most Water","version":""},{"contentType":"doc","description":"Given two strings s and t, return true if s is a subsequence of t, or false otherwise","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/two-pointers/is-subsequence","title":"392. Is Subsequence","version":""},{"contentType":"doc","description":"Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/two-pointers/remove-duplicates-from-sorted-array-ii","title":"80. Remove Duplicates from Sorted Array II","version":""},{"contentType":"doc","description":"Write a function that reverses a string. The input string is given as an array of characters s","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/two-pointers/reverse-string","title":"344. Reverse String","version":""},{"contentType":"doc","description":"Given an integer array nums, rotate the array to the right by k steps, where k is non-negative","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/two-pointers/rotate-array","title":"189. Rotate Array","version":""},{"contentType":"doc","description":"Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/two-pointers/squares-of-a-sorted-array","title":"977. Squares of a Sorted Array","version":""},{"contentType":"doc","description":"Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/two-pointers/trapping-rain-water","title":"42. Trapping Rain Water","version":""},{"contentType":"doc","description":"Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/two-pointers/two-sum-ii-input-array-is-sorted","title":"167. Two Sum II - Input Array Is Sorted","version":""},{"contentType":"doc","description":"A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/two-pointers/valid-palindrome","title":"125. Valid Palindrome","version":""},{"contentType":"doc","description":"Union-Find (Disjoint Set Union) is a data structure that maintains a collection of disjoint sets and supports efficiently finding which set an element belongs to and merging two sets.","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/union-find","title":"Union Find","version":""},{"contentType":"doc","description":"You have a graph of n nodes. You are given an integer n and an array edges where edges[i] = [ai, bi] indicates that there is an edge between ai and bi in the graph","indexable":true,"lastModified":"2026-10-05T14:22:28-04:00","locale":"","route":"/union-find/number-of-connected-components-in-an-undirected-graph","title":"323. Number of Connected Components in an Undirected Graph","version":""},{"contentType":"doc","description":"In this problem, a tree is an undirected graph that is connected and has no cycles","indexable":true,"lastModified":"2026-10-14T15:52:02-04:00","locale":"","route":"/union-find/redundant-connection","title":"684. Redundant Connection","version":""}],"site":"https://lc.prd.lk","version":"2.2.4"}
diff --git a/apps/docs/src/generated/openapi.json b/apps/docs/src/generated/openapi.json
new file mode 100644
index 0000000..0967ef4
--- /dev/null
+++ b/apps/docs/src/generated/openapi.json
@@ -0,0 +1 @@
+{}
diff --git a/apps/docs/src/generated/raw-markdown.json b/apps/docs/src/generated/raw-markdown.json
new file mode 100644
index 0000000..661a065
--- /dev/null
+++ b/apps/docs/src/generated/raw-markdown.json
@@ -0,0 +1 @@
+{"/":{"md":"---\ntitle: Introduction\ndescription: Structured dependency spine based roadmap for solving DSA Problems on leetcode.\nicon: play\n---\n\n## 1. Linear Structures\n\n:::tip\nData is read in one direction, from the first item to the last. The current position is recorded at each step. All later phases use this skill.\n:::\n\n**[Hash Table](/hash-table)**\n\nA key maps to a value in constant time, which removes the need for a full scan.\n\n**[Prefix Sum](/prefix-sum)**\n\nA running total is stored at each index, so the sum of any range is one subtraction.\n\n**[Two Pointers](/two-pointers)**\n\nTwo indices move through the data from different positions and meet at the answer.\n\n**[Sliding Window](/sliding-window)**\n\nA range with a start and an end moves across the data, and the contents are updated at each step.\n\n**[Binary Search](/binary-search)**\n\nHalf of a sorted range is removed at each step until the target is found.\n\n**[Stack](/stack)**\n\nItems are added and removed from one end, so the last item added is the first item removed.\n\n**[Monotonic Stack](/monotonic-stack)**\n\nA stack keeps its items in sorted order, which finds the next larger or smaller item in one pass.\n\n## 2. Nodal & Grid\n\n:::tip\nThe data is in separate parts. Each part points to the next part, or sits next to other parts on a flat surface. An index does not find the next item. The pointers or the neighbors give the path instead.\n:::\n\n**[Linked List](/linked-list)**\n\nEach node points to the next node, and the pointers give the path through the data.\n\n**[Hybrid Structures](/hybrid-structures)**\n\nTwo structures are combined, such as a hash map and a linked list, to get the best properties of each.\n\n**[Matrix Index Math](/matrix-index-math)**\n\nA two-dimensional grid is read as a flat array through a formula, and the neighbors are found by arithmetic.\n\n## 3. Hierarchical\n\n:::tip\nEach part points to one or more parts below it. Those parts do the same. The method that solves a small part also solves the large part. This is recursion.\n:::\n\n**[Tree DFS](/tree-dfs)**\n\nEach branch is followed to its end before the next branch is examined.\n\n**[Tree BFS](/tree-bfs)**\n\nAll nodes at one level are visited before the nodes at the next level.\n\n**[Binary Search Tree](/binary-search-tree)**\n\nEach node holds a value that is larger than its left subtree and smaller than its right subtree.\n\n**[Heap](/heap-priority-queue)**\n\nThe largest or smallest item is always at the top, and it is removed in logarithmic time.\n\n**[Trie](/trie)**\n\nEach node holds one character, and a path from the root spells a word.\n\n## 4. Relational\n\n:::tip\nEach part can point to any other part. Loops can occur. The method must visit each part one time only. The order of the parts, or the distance between them, is not given in the data. The method finds it.\n:::\n\n**[Graph DFS](/graph-dfs)**\n\nEach edge is followed to its end, and a visited set prevents a repeat visit.\n\n**[Graph BFS](/graph-bfs)**\n\nNodes are visited in order of distance from the start, which gives the shortest path in an unweighted graph.\n\n**[Topological Sort](/topological-sort)**\n\nNodes are put in an order where each node comes after all nodes that point to it.\n\n**[Union-Find](/union-find)**\n\nTwo sets are merged and the set of any item is found in near-constant time.\n\n## 5. Decision Space\n\n:::tip\nThe data has no structure. The method makes one. Each choice makes a new branch. The problem sets the correct method: examine all branches, keep a record of the results already found, or make one choice and do not go back.\n:::\n\n**[Backtracking](/backtracking)**\n\nEach choice makes a branch, and a branch is undone when it does not give a valid result.\n\n**[1-D DP](/1d-dynamic-programming)**\n\nThe result for each state is stored in a list, and each new result is computed from the results before it.\n\n**[Multi-D / Grid DP](/multi-d-grid-dp)**\n\nThe result for each state is stored in a table with two or more dimensions, and each cell is computed from its neighbors.\n\n**[Greedy](/greedy)**\n\nThe best local choice is made at each step and is not changed later.\n\n**[Intervals](/intervals)**\n\nRanges with a start and an end are sorted, and then merged, counted, or scheduled.\n","mdx":"---\ntitle: Introduction\ndescription: Structured dependency spine based roadmap for solving DSA Problems on leetcode.\nicon: play\n---\n\n## 1. Linear Structures\n\n:::tip\nData is read in one direction, from the first item to the last. The current position is recorded at each step. All later phases use this skill.\n:::\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n## 2. Nodal & Grid\n\n:::tip\nThe data is in separate parts. Each part points to the next part, or sits next to other parts on a flat surface. An index does not find the next item. The pointers or the neighbors give the path instead.\n:::\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n## 3. Hierarchical\n\n:::tip\nEach part points to one or more parts below it. Those parts do the same. The method that solves a small part also solves the large part. This is recursion.\n:::\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n## 4. Relational\n\n:::tip\nEach part can point to any other part. Loops can occur. The method must visit each part one time only. The order of the parts, or the distance between them, is not given in the data. The method finds it.\n:::\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n## 5. Decision Space\n\n:::tip\nThe data has no structure. The method makes one. Each choice makes a new branch. The problem sets the correct method: examine all branches, keep a record of the results already found, or make one choice and do not go back.\n:::\n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n"},"/1d-dynamic-programming":{"mdx":"---\ntitle: 1-D Dynamic Programming\ndescription: 'The result for each state is stored in a list, and each new result is computed from the results before it.'\nhidden: true\n---\n"},"/1d-dynamic-programming/climbing-stairs":{"mdx":"---\ntitle: '70. Climbing Stairs'\ndescription: You are climbing a staircase. It takes n steps to reach the top\nicon: dot\ntopics:\n - { name: \"Math\", slug: \"math\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\n - { name: \"Memoization\", slug: \"memoization\" }\nissue: \"https://github.com/prdlk/leetcode/issues/157\"\ncompanies:\n - { name: \"Infosys\", period: \"Thirty Days\", frequency: 80.2 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Intuit\", period: \"Thirty Days\", frequency: 66.1 }\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 65.6 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 36.7 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 48.3 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"Deloitte\", period: \"Six Months\", frequency: 81 }\nsidebar:\n label: 'Climbing Stairs'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `n = 2`\n- Output: `2`\n- Explanation: There are two ways to climb to the top. `1`. `1` step + `1` step `2`. `2` steps\n\n### Example 2:\n- Input: `n = 3`\n- Output: `3`\n- Explanation: There are three ways to climb to the top. `1`. `1` step + `1` step + `1` step `2`. `1` step + `2` steps `3`. `2` steps + `1` step\n\n### Constraints:\n\n- `1 <= n <= 45`\n\n## Solution\n\n```py\nclass Solution:\n def climbStairs(self, n: int) -> int:\n if n == 1:\n return 1\n if n == 2:\n return 2\n\n first, second = 1, 2\n\n for _ in range(3, n + 1):\n third = first + second\n first, second = second, third\n\n return second\n```\n"},"/1d-dynamic-programming/coin-change":{"mdx":"---\ntitle: '322. Coin Change'\ndescription: You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Knapsack Problem\", slug: \"knapsack-problem\" }\n - { name: \"Complete Knapsack\", slug: \"complete-knapsack\" }\nissue: \"https://github.com/prdlk/leetcode/issues/159\"\ncompanies:\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 59.7 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 48.7 }\n - { name: \"Intuit\", period: \"Six Months\", frequency: 71.5 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 67.9 }\n - { name: \"Pinterest\", period: \"Six Months\", frequency: 64.4 }\nsidebar:\n label: 'Coin Change'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `coins = [1,2,5], amount = 11`\n- Output: `3`\n- Explanation: `11 = 5 + 5 + 1`\n\n### Example 2:\n- Input: `coins = [2], amount = 3`\n- Output: `-1`\n\n### Example 3:\n- Input: `coins = [1], amount = 0`\n- Output: `0`\n\n### Constraints:\n\n- `1 <= coins.length <= 12`\n- `1 <= coins[i] <= 2^31 - 1`\n- `0 <= amount <= 10^4`\n\n## Solution\n\n```py\nfrom math import inf\n\n\nclass Solution:\n def coinChange(self, coins: list[int], amount: int) -> int:\n n = len(coins)\n memo = [[-1] * (amount + 1) for _ in range(n + 1)]\n\n def min_coins(coins, i, amount, memo):\n if amount == 0:\n return 0\n if i == 0 or amount < 0:\n return inf\n if memo[i][amount] != -1:\n return memo[i][amount]\n\n skip = min_coins(coins, i - 1, amount, memo)\n use = min_coins(coins, i, amount - coins[i - 1], memo)\n if use != inf:\n use += 1\n\n memo[i][amount] = min(skip, use)\n return memo[i][amount]\n\n result = min_coins(coins, n, amount, memo)\n return result if result != inf else -1 # pyright: ignore[reportReturnType]\n```\n"},"/1d-dynamic-programming/house-robber":{"mdx":"---\ntitle: '198. House Robber'\ndescription: You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\nissue: \"https://github.com/prdlk/leetcode/issues/158\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 83.1 }\n - { name: \"Infosys\", period: \"Thirty Days\", frequency: 68.6 }\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 65.6 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 50.2 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 53.2 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Freecharge\", period: \"Six Months\", frequency: 100 }\n - { name: \"Agoda\", period: \"Six Months\", frequency: 65 }\nsidebar:\n label: 'House Robber'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [1,2,3,1]`\n- Output: `4`\n- Explanation: Rob house `1` (money = `1`) and then rob house `3` (money = `3`). Total amount you can rob = `1 + 3 = 4`.\n\n### Example 2:\n- Input: `nums = [2,7,9,3,1]`\n- Output: `12`\n- Explanation: Rob house `1` (money = `2`), rob house `3` (money = `9`) and rob house `5` (money = `1`). Total amount you can rob = `2 + 9 + 1 = 12`.\n\n### Constraints:\n\n- `1 <= nums.length <= 100`\n- `0 <= nums[i] <= 400`\n\n## Solution\n\n```py\nclass Solution:\n def rob(self, nums: list[int]) -> int:\n n = len(nums)\n if n == 0:\n return 0\n if n <= 2:\n return max(nums)\n\n dp = [0] * n\n dp[0] = nums[0]\n dp[1] = max(nums[0], nums[1])\n for i in range(2, n):\n dp[i] = max(dp[i - 1], dp[i - 2] + nums[i])\n return dp[-1]\n```\n"},"/backtracking":{"mdx":"---\ntitle: Backtracking\ndescription: 'Backtracking is a depth-first search technique that builds a solution step by step and abandons (undoes) a partial choice as soon as it can’t lead to a valid complete solution.'\nhidden: true\n---\n\n## What it is\n\nBacktracking explores possible paths to a solution one step at a time. When a path reaches a dead end, you undo the last step and try a different option. Think of every problem as a decision tree. The root is an empty answer. Each level is a decision. Each branch is one option.\n\n## When to use it\n\nUse backtracking when you must generate **all** valid solutions or evaluate **all** combinations. Do not use it when you need only one solution or the optimal solution. For those, use greedy or dynamic programming. Backtracking has exponential time complexity.\n\n## The four components\n\nEvery backtracking problem has the same parts:\n\n1. **Base case** — when to save the current path and stop.\n2. **Choices** — the options at each step.\n3. **Constraints** — the rules that reject an invalid path early.\n4. **Backtrack step** — undo the last choice and try the next one.\n\n## The template\n\n```python\ndef solve(data):\n result = []\n path = []\n\n def backtrack(index):\n # 1. Base case\n if is_complete(index, path):\n result.append(path[:]) # copy — lists are passed by reference\n return\n\n # 2. Choices\n for choice in get_choices(index, data):\n # 3. Constraints\n if not is_valid(choice, path):\n continue\n\n path.append(choice) # make the choice\n backtrack(index + 1) # explore\n path.pop() # 4. Backtrack\n\n backtrack(0)\n return result\n```\n\n## Steps to apply the template\n\n1. Draw the decision tree for a small input. Name each level and each branch.\n2. Write the base case. Ask: when is the path complete?\n3. Write the choices. Ask: what can I add at this level?\n4. Write the constraints. Ask: what makes a path invalid? Reject it before recursion.\n5. Add the backtrack step. Pop what you appended.\n6. Copy the path when you save it. Never append the same list reference.\n\n## Four problems, one template\n\n### Subsets\n\nChoice: include or skip each number. No constraint. Base case: index equals list length.\n\n```python\ndef subsets(nums):\n result, path = [], []\n\n def backtrack(i):\n if i == len(nums):\n result.append(path[:])\n return\n path.append(nums[i]) # include\n backtrack(i + 1)\n path.pop()\n backtrack(i + 1) # skip\n\n backtrack(0)\n return result\n```\n\n### Permutations\n\nSame choices as subsets. Constraint: do not reuse a number already in the path. Base case: path length equals list length.\n\n```python\ndef permute(nums):\n result, path = [], []\n used = [False] * len(nums)\n\n def backtrack():\n if len(path) == len(nums):\n result.append(path[:])\n return\n for i in range(len(nums)):\n if used[i]:\n continue\n used[i] = True\n path.append(nums[i])\n backtrack()\n path.pop()\n used[i] = False\n\n backtrack()\n return result\n```\n\n### Combinations\n\nSame as subsets. Change one line: save the path only when its length equals `k`.\n\n```python\ndef combine(n, k):\n result, path = [], []\n\n def backtrack(start):\n if len(path) == k:\n result.append(path[:])\n return\n for i in range(start, n + 1):\n path.append(i)\n backtrack(i + 1)\n path.pop()\n\n backtrack(1)\n return result\n```\n\n### N-Queens\n\nChoice: which column to place the queen in the current row. Constraint: no shared column or diagonal. Base case: `n` queens placed. Backtrack: remove the last queen.\n\n```python\ndef solve_n_queens(n):\n result, cols = [], []\n used_cols, diag1, diag2 = set(), set(), set()\n\n def backtrack(row):\n if row == n:\n result.append(cols[:])\n return\n for col in range(n):\n if col in used_cols or row - col in diag1 or row + col in diag2:\n continue\n cols.append(col)\n used_cols.add(col); diag1.add(row - col); diag2.add(row + col)\n backtrack(row + 1)\n cols.pop()\n used_cols.remove(col); diag1.remove(row - col); diag2.remove(row + col)\n\n backtrack(0)\n return result\n```\n\n## Common mistakes\n\n- You append the `path` list itself instead of a copy. The result fills with empty lists.\n- You forget to pop. The path grows and never resets.\n- You check constraints after recursion instead of before. You waste time on dead paths.\n- You use backtracking when you need only one answer. Use greedy or DP instead.\n\n\n"},"/backtracking/combination-sum":{"mdx":"---\ntitle: '39. Combination Sum'\ndescription: Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Backtracking\", slug: \"backtracking\" }\nissue: \"https://github.com/prdlk/leetcode/issues/150\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Google\", period: \"Three Months\", frequency: 41 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"Amazon\", period: \"Six Months\", frequency: 30.1 }\nsidebar:\n label: 'Combination Sum'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `candidates = [2,3,6,7], target = 7`\n- Output: `[[2,2,3],[7]]`\n- Explanation: `2` and `3` are `candidates`, and `2 + 2 + 3 = 7`. Note that `2` can be used multiple times. `7` is a candidate, and `7 = 7`. These are the only two combinations.\n\n### Example 2:\n- Input: `candidates = [2,3,5], target = 8`\n- Output: `[[2,2,2,2],[2,3,3],[3,5]]`\n\n### Example 3:\n- Input: `candidates = [2], target = 1`\n- Output: `[]`\n\n### Constraints:\n\n- `1 <= candidates.length <= 30`\n- `2 <= candidates[i] <= 40`\n- All elements of `candidates` are distinct.\n- `1 <= target <= 40`\n\n## Solution\n\n```py\nclass Solution:\n def combinationSum(self, candidates: list[int], target: int) -> list[list[int]]:\n candidates.sort()\n result, path = [], []\n\n def backtrack(start, remaining):\n if remaining == 0:\n result.append(path[:])\n return\n for i in range(start, len(candidates)):\n if candidates[i] > remaining:\n break\n\n path.append(candidates[i])\n backtrack(i, remaining - candidates[i])\n path.pop()\n\n backtrack(0, target)\n return result\n```\n"},"/backtracking/permutations":{"mdx":"---\ntitle: '46. Permutations'\ndescription: Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Backtracking\", slug: \"backtracking\" }\nissue: \"https://github.com/prdlk/leetcode/issues/151\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Google\", period: \"Three Months\", frequency: 32.7 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 67.9 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 54.1 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 43.4 }\nsidebar:\n label: 'Permutations'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [1,2,3]`\n- Output: `[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]`\n\n### Example 2:\n- Input: `nums = [0,1]`\n- Output: `[[0,1],[1,0]]`\n\n### Example 3:\n- Input: `nums = [1]`\n- Output: `[[1]]`\n\n### Constraints:\n\n- `1 <= nums.length <= 6`\n- `-10 <= nums[i] <= 10`\n- All the integers of `nums` are unique.\n\n## Solution\n\n```py\nclass Solution:\n def permute(self, nums: list[int]) -> list[list[int]]:\n result, path = [], []\n used = [False] * len(nums)\n\n def backtrack():\n if len(path) == len(nums):\n result.append(path[:])\n return\n for i in range(len(nums)):\n if used[i]:\n continue\n used[i] = True\n path.append(nums[i])\n backtrack()\n path.pop()\n used[i] = False\n\n backtrack()\n return result\n```\n"},"/backtracking/subsets":{"mdx":"---\ntitle: '78. Subsets'\ndescription: Given an integer array nums of unique elements, return all possible subsets (the power set)\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Backtracking\", slug: \"backtracking\" }\n - { name: \"Bit Manipulation\", slug: \"bit-manipulation\" }\nissue: \"https://github.com/prdlk/leetcode/issues/149\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 61.3 }\n - { name: \"Agoda\", period: \"Three Months\", frequency: 75.3 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 48.3 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 43.3 }\n - { name: \"Google\", period: \"Three Months\", frequency: 23.9 }\n - { name: \"TikTok\", period: \"Six Months\", frequency: 64.9 }\nsidebar:\n label: 'Subsets'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [1,2,3]`\n- Output: `[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]`\n\n### Example 2:\n- Input: `nums = [0]`\n- Output: `[[],[0]]`\n\n### Constraints:\n\n- `1 <= nums.length <= 10`\n- `-10 <= nums[i] <= 10`\n- All the numbers of `nums` are unique.\n\n## Solution\n\n```py\nclass Solution:\n def subsets(self, nums: list[int]) -> list[list[int]]:\n res = []\n\n def backtrack(i, path):\n res.append(path[:])\n for num_idx in range(i, len(nums)):\n path.append(nums[num_idx])\n backtrack(num_idx + 1, path)\n path.pop()\n\n backtrack(0, [])\n return res\n```\n"},"/binary-search":{"mdx":"---\ntitle: Binary Search\ndescription: 'Half of a sorted range is removed at each step until the target is found.'\nhidden: true\n---\n\nBinary search is an efficient array search algorithm. It works by narrowing down the search range by half each time. If you have looked up a word in a physical dictionary, you've already used binary search in real life. Let's look at a simple example:\n\n*Given a sorted array of integers and an integer called target, find the element that equals the target and return its index. If the element is not found, return -1.*\n\nThe key observation here is that the array is sorted. We pick a random element in the array and compare it to the target.\n\n- If we happen to pick the element that equals the target (how lucky!), then bingo. We don't need to do any more work; we return its index.\n- If the element is smaller than the target, then we know the target cannot be found in the section to the left of the current element, since everything to the left is even smaller. So, we discard the current element and everything on the left from the search range.\n- If the element is larger than the target, then we know the target cannot be found in the section to the right of the current element, since everything to the right is even larger. So, we discard the current element and everything on the right from the search range.\n\nWe repeat this process until we find the target. Instead of picking a random element, we always pick the **middle** element in the current search range. This way, we can discard half of the options and shrink the search range by half each time. This gives us `O(log(N))` runtime.\n\nThis idea can be implemented both iteratively and recursively. However, the major difference is that the iterative version of binary search uses `O(1)` memory while the recursive version uses `O(log(N))` memory.\n\n## Implementation\n\nThe search range is represented by the `left` and `right` indices that start from both ends of the array and move towards each other as we search. When moving the index, we discard elements and shrink the search range.\n\nTime Complexity: `O(log(n))`\n\n\n\n \n \n \n \n \n \n \n \n \n\n```python\ndef binary_search(arr: list[int], target: int) -> int:\n left, right = 0, len(arr) - 1\n # <= because left and right could point to the same element, < would miss it\n while left <= right:\n # double slash for integer division in python 3,\n # we don't have to worry about integer `left + right` overflow\n # since python integers can be arbitrarily large\n mid = (left + right) // 2\n # found target, return its index\n if arr[mid] == target:\n return mid\n # middle less than target, discard left half by making left search boundary `mid + 1`\n if arr[mid] < target:\n left = mid + 1\n # middle greater than target, discard right half by making right search boundary `mid - 1`\n else:\n right = mid - 1\n return -1 # if we get here we didn't hit above return so we didn't find target\n```\n\n### Calculating `mid`\n\nNote that when calculating `mid`, if the number of elements is even, there are two elements in the middle. We usually follow the convention of picking the first one, equivalent to doing integer division `(left + right) / 2`.\n\nIn most programming languages, we calculate `mid` with `left + floor((right-left) / 2)` to avoid potential integer overflow. However, in Python, we do not need to worry about integer overflow with `left + right` because Python3 integers can be arbitrarily large.\n\n## Deduction\n\nIt's essential to understand and deduce the algorithm yourself instead of memorizing it. In an actual interview, interviewers may ask you additional questions to test your understanding, so simply memorizing the algorithm may not be enough to convince the interviewer.\n\nKey elements in writing a correct binary search:\n\n### 1. When to terminate the loop\n\nMake sure the `while` loop includes an equality comparison; otherwise, we might skip the loop and miss the potential match for the edge case of a one-element array.\n\n### 2. Whether/how to update `left` and `right` boundary in the `if` conditions\n\nConsider which side to discard. If `arr[mid]` is smaller than the `target`, we should discard everything on the left by making `left = mid + 1`.\n\n### 3. Should I discard the current element?\n\nFor vanilla binary search, we can discard it since it cannot be the final answer if it is not equal to the target. There might be situations where you would want to think twice before discarding the current element. We'll discuss this in the next module.\n\n## When to use binary search\n\nInterestingly, binary search works beyond sorted arrays. **You can use binary search whenever you make a binary decision to shrink the search range.**\n"},"/binary-search-tree":{"mdx":"---\ntitle: Binary Search Tree\ndescription: 'Each node holds a value that is larger than its left subtree and smaller than its right subtree.'\nhidden: true\n---\n"},"/binary-search-tree/kth-smallest-element-in-a-bst":{"mdx":"---\ntitle: '230. Kth Smallest Element in a BST'\ndescription: Given the root of a binary search tree, and an integer k, return the k^th smallest value (1-indexed) of all the values of the nodes in the tree\nicon: dot\ntopics:\n - { name: \"Tree\", slug: \"tree\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Binary Search Tree\", slug: \"binary-search-tree\" }\n - { name: \"Binary Tree\", slug: \"binary-tree\" }\nvideo: \"5LUXSvjmGCw\"\nissue: \"https://github.com/prdlk/leetcode/issues/112\"\ncompanies:\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 74.3 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 28.9 }\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"Uber\", period: \"Six Months\", frequency: 71.5 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 24.5 }\nsidebar:\n label: 'Kth Smallest Element in a BST'\n badge: 'Medium'\n---\n\n::::warning\nIf the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize?\n::::\n\n### Example 1:\n- Input: `root = [3,1,4,null,2], k = 1`\n- Output: `1`\n\n### Example 2:\n- Input: `root = [5,3,6,2,4,null,null,1], k = 3`\n- Output: `3`\n\n### Constraints:\n\n- The number of nodes in the tree is n.\n- `1 <= k <= n <= 10^4`\n- `0 <= Node.val <= 10^4`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"kthSmallest(root, k)\"]) --> I[\"n = 0, stack = [], cur = root\"]\n I --> W{\"cur or stack?\"}\n W -- no --> E([\"fall off the loop — unreachable, k <= n\"])\n W -- yes --> L{\"cur?\"}\n L -- yes --> P[\"stack.append(cur), cur = cur.left — push the whole left spine\"]\n P --> L\n L -- no --> O[\"cur = stack.pop(), n += 1 — nodes pop in ascending order\"]\n O --> K{\"n == k?\"}\n K -- yes --> R([\"return cur.val\"])\n K -- no --> N[\"cur = cur.right\"]\n N --> W\n```\n\n## Solution\n\n```py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:\n n = 0\n stack = []\n cur = root\n\n while cur or stack:\n while cur:\n stack.append(cur)\n cur = cur.left\n\n cur = stack.pop()\n n += 1\n if n == k:\n return cur.val\n cur = cur.right\n```\n"},"/binary-search-tree/lowest-common-ancestor-of-a-binary-search-tree":{"mdx":"---\ntitle: '235. Lowest Common Ancestor of a Binary Search Tree'\ndescription: Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST\nicon: dot\ntopics:\n - { name: \"Tree\", slug: \"tree\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Binary Search Tree\", slug: \"binary-search-tree\" }\n - { name: \"Binary Tree\", slug: \"binary-tree\" }\n - { name: \"Binary Lifting\", slug: \"binary-lifting\" }\n - { name: \"Lowest Common Ancestor\", slug: \"lowest-common-ancestor\" }\nvideo: \"gs2LMfuOR9k\"\nissue: \"https://github.com/prdlk/leetcode/issues/113\"\ncompanies:\n - { name: \"BlackRock\", period: \"Thirty Days\", frequency: 76.6 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 32 }\n - { name: \"Amazon\", period: \"Six Months\", frequency: 30.1 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\n - { name: \"Google\", period: \"Six Months\", frequency: 21.9 }\nsidebar:\n label: 'Lowest Common Ancestor of a Binary Search Tree'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8`\n- Output: `6`\n- Explanation: The LCA of nodes `2` and `8` is `6`.\n\n### Example 2:\n- Input: `root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4`\n- Output: `2`\n- Explanation: The LCA of nodes `2` and `4` is `2`, since a node can be a descendant of itself according to the LCA definition.\n\n### Example 3:\n- Input: `root = [2,1], p = 2, q = 1`\n- Output: `2`\n\n### Constraints:\n\n- The number of nodes in the tree is in the range [2, 10^5].\n- `-10^9 <= Node.val <= 10^9`\n- All Node.val are unique.\n- `p != q`\n- p and q will exist in the BST.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"lowestCommonAncestor(root, p, q)\"]) --> I[\"cur = root\"]\n I --> W{\"cur?\"}\n W -- no --> E([\"fall off the loop — unreachable, p and q exist in the BST\"])\n W -- yes --> A{\"p.val < cur.val and q.val < cur.val?\"}\n A -- yes --> L[\"cur = cur.left — both sit in the left subtree\"]\n L --> W\n A -- no --> B{\"p.val > cur.val and q.val > cur.val?\"}\n B -- yes --> R[\"cur = cur.right — both sit in the right subtree\"]\n R --> W\n B -- no --> C([\"return cur — the paths to p and q split here\"])\n```\n\n## Solution\n\n```py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.right = None\n\n\nclass Solution:\n def lowestCommonAncestor(\n self, root: \"TreeNode\", p: \"TreeNode\", q: \"TreeNode\"\n ) -> \"TreeNode\":\n cur = root\n while cur:\n if p.val < cur.val and q.val < cur.val:\n cur = cur.left\n elif p.val > cur.val and q.val > cur.val:\n cur = cur.right\n else:\n return cur\n```\n"},"/binary-search-tree/validate-binary-search-tree":{"mdx":"---\ntitle: '98. Validate Binary Search Tree'\ndescription: Given the root of a binary tree, determine if it is a valid binary search tree (BST)\nicon: dot\ntopics:\n - { name: \"Tree\", slug: \"tree\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Binary Search Tree\", slug: \"binary-search-tree\" }\n - { name: \"Binary Tree\", slug: \"binary-tree\" }\nvideo: \"s6ATEkipzow\"\nissue: \"https://github.com/prdlk/leetcode/issues/111\"\ncompanies:\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 76.7 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 46.8 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 34.6 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\nsidebar:\n label: 'Validate Binary Search Tree'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `root = [2,1,3]`\n- Output: `true`\n\n### Example 2:\n- Input: `root = [5,1,4,null,null,3,6]`\n- Output: `false`\n- Explanation: The `root` node's value is `5` but its right child's value is `4`.\n\n### Constraints:\n\n- The number of nodes in the tree is in the range [1, 10^4].\n- `-2^31 <= Node.val <= 2^31 - 1`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"isValidBST(root)\"]) --> C[\"valid(root, float('-inf'), float('inf')) — the root may hold any value\"]\n C --> E([\"return that result\"])\n subgraph valid[\"valid(node, low, high)\"]\n V1{\"not node?\"}\n V1 -- yes --> V2([\"return True\"])\n V1 -- no --> V3{\"not (low < node.val < high)?\"}\n V3 -- yes --> V4([\"return False — node breaks a bound set by an ancestor\"])\n V3 -- no --> V5[\"valid(node.left, low, node.val) and valid(node.right, node.val, high) — each child tightens one bound\"]\n V5 --> V6([\"return that result\"])\n end\n C --> V1\n```\n\n## Solution\n\n```py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def isValidBST(self, root: Optional[TreeNode]) -> bool:\n def valid(node, low, high):\n if not node:\n return True\n\n if not (low < node.val < high):\n return False\n\n return valid(node.left, low, node.val) and valid(node.right, node.val, high)\n\n return valid(root, float(\"-inf\"), float(\"inf\"))\n```\n"},"/binary-search/binary-search":{"mdx":"---\ntitle: '704. Binary Search'\ndescription: Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\nvideo: \"s4DPM8ct1pI\"\nissue: \"https://github.com/prdlk/leetcode/issues/58\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 53.2 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 49.8 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 39.1 }\nsidebar:\n label: 'Binary Search'\n badge: 'Easy'\n---\n\n::::warning\nYou must write an algorithm with O(log n) runtime complexity.\n::::\n\n### Example 1:\n- Input: `nums = [-1,0,3,5,9,12], target = 9`\n- Output: `4`\n- Explanation: `9` exists in `nums` and its index is `4`\n\n### Example 2:\n- Input: `nums = [-1,0,3,5,9,12], target = 2`\n- Output: `-1`\n- Explanation: `2` does not exist in `nums` so return `-1`\n\n### Constraints:\n\n- `1 <= nums.length <= 10^4`\n- `-10^4 < nums[i], target < 10^4`\n- All the integers in `nums` are unique.\n- `nums` is sorted in ascending order.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"search(nums, target)\"]) --> I[\"left = 0, right = n - 1\"]\n I --> W{\"left <= right?\"}\n W -- no --> E([\"return -1\"])\n W -- yes --> M[\"mid = left + floor((right - left) / 2)\"]\n M --> Q{\"nums[mid] vs target\"}\n Q -- equal --> R([\"return mid\"])\n Q -- \"nums[mid] < target\" --> L[\"left = mid + 1\"]\n L --> W\n Q -- \"nums[mid] > target\" --> H[\"right = mid - 1\"]\n H --> W\n```\n\n## Solution\n\n```js\n/**\n * @param {number[]} nums\n * @param {number} target\n * @return {number}\n */\nvar search = function(nums, target) {\n let left = 0;\n let right = nums.length - 1;\n\n while(left <= right) {\n const mid = left + Math.floor((right - left) / 2);\n if (nums[mid] === target) {\n return mid;\n } else if(nums[mid] < target){\n left = mid + 1;\n } else if (nums[mid] > target) {\n right = mid - 1;\n }\n }\n return -1;\n};\n```\n"},"/binary-search/find-first-and-last-position-of-element-in-sorted-array":{"mdx":"---\ntitle: '34. Find First and Last Position of Element in Sorted Array'\ndescription: Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\nissue: \"https://github.com/prdlk/leetcode/issues/65\"\ncompanies:\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 57.2 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 36.7 }\n - { name: \"TikTok\", period: \"Three Months\", frequency: 70.9 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 43.3 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 36.8 }\n - { name: \"Instacart\", period: \"Six Months\", frequency: 100 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 70.2 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 56.2 }\nsidebar:\n label: 'Find First and Last Position of Element in Sorted Array'\n badge: 'Medium'\n---\n\n::::warning\nYou must write an algorithm with O(log n) runtime complexity.\n::::\n\n### Example 1:\n- Input: `nums = [5,7,7,8,8,10], target = 8`\n- Output: `[3,4]`\n\n### Example 2:\n- Input: `nums = [5,7,7,8,8,10], target = 6`\n- Output: `[-1,-1]`\n\n### Example 3:\n- Input: `nums = [], target = 0`\n- Output: `[-1,-1]`\n\n### Constraints:\n\n- `0 <= nums.length <= 10^5`\n- `-10^9 <= nums[i] <= 10^9`\n- `nums` is a non-decreasing array.\n- `-10^9 <= target <= 10^9`\n\n## Solution\n\n```py\nclass Solution:\n def searchRange(self, nums: list[int], target: int) -> list[int]:\n def find(first):\n result = -1\n left, right = 0, len(nums) - 1\n while left <= right:\n mid = (left + right) // 2\n if nums[mid] == target:\n result = mid\n if first:\n right = mid - 1\n else:\n left = mid + 1\n elif nums[mid] < target:\n left = mid + 1\n else:\n right = mid - 1\n return result\n\n return [find(True), find(False)]\n```\n"},"/binary-search/find-minimum-in-rotated-sorted-array":{"mdx":"---\ntitle: '153. Find Minimum in Rotated Sorted Array'\ndescription: 'Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:'\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\nvideo: \"nIVW4P8b1VA\"\nissue: \"https://github.com/prdlk/leetcode/issues/62\"\ncompanies:\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 65.6 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"tcs\", period: \"Six Months\", frequency: 43.5 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 40.6 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 32.7 }\nsidebar:\n label: 'Find Minimum in Rotated Sorted Array'\n badge: 'Medium'\n---\n\n::::warning\nNotice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].\n::::\n\n### Example 1:\n- Input: `nums = [3,4,5,1,2]`\n- Output: `1`\n- Explanation: The original array was `[1,2,3,4,5]` rotated `3` times.\n\n### Example 2:\n- Input: `nums = [4,5,6,7,0,1,2]`\n- Output: `0`\n- Explanation: The original array was `[0,1,2,4,5,6,7]` and it was rotated `4` times.\n\n### Example 3:\n- Input: `nums = [11,13,15,17]`\n- Output: `11`\n- Explanation: The original array was `[11,13,15,17]` and it was rotated `4` times.\n\n### Constraints:\n\n- `n == nums.length`\n- `1 <= n <= 5000`\n- `-5000 <= nums[i] <= 5000`\n- All the integers of `nums` are unique.\n- `nums` is sorted and rotated between 1 and `n` times.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"findMin(nums)\"]) --> I[\"l = 0, r = n-1, lowest_index = -1\"]\n I --> W{\"l <= r?\"}\n W -- no --> E([\"return nums[lowest_index]\"])\n W -- yes --> M[\"m = (l + r) // 2\"]\n M --> C{\"nums[m] <= nums[-1]?\"}\n C -- yes --> A[\"in the right sorted run: record lowest_index = m, r = m - 1\"]\n A --> W\n C -- no --> B[\"still in the left sorted run: l = m + 1\"]\n B --> W\n```\n\n## Solution\n\n```py\nclass Solution:\n def findMin(self, nums: List[int]) -> int:\n l, r = 0, len(nums) - 1\n lowest_index = -1\n\n while l <= r:\n m = (l + r) // 2\n if nums[m] <= nums[-1]:\n lowest_index = m\n r = m - 1\n else:\n l = m + 1\n\n return nums[lowest_index]\n```\n"},"/binary-search/find-peak-element":{"mdx":"---\ntitle: '162. Find Peak Element'\ndescription: A peak element is an element that is strictly greater than its neighbors\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\nissue: \"https://github.com/prdlk/leetcode/issues/64\"\ncompanies:\n - { name: \"Infosys\", period: \"Thirty Days\", frequency: 68.6 }\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 67.6 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 54.8 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 36.8 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 34.6 }\n - { name: \"Uber\", period: \"Six Months\", frequency: 67.2 }\nsidebar:\n label: 'Find Peak Element'\n badge: 'Medium'\n---\n\n::::warning\nYou must write an algorithm that runs in O(log n) time.\n::::\n\n### Example 1:\n- Input: `nums = [1,2,3,1]`\n- Output: `2`\n- Explanation: `3` is a peak element and your function should return the index number `2`.\n\n### Example 2:\n- Input: `nums = [1,2,1,3,5,6,4]`\n- Output: `5`\n- Explanation: Your function can return either index number `1` where the peak element is `2`, or index number `5` where the peak element is `6`.\n\n### Constraints:\n\n- `1 <= nums.length <= 1000`\n- `-2^31 <= nums[i] <= 2^31 - 1`\n- `nums[i] != nums[i + 1] for all valid i.`\n\n## Solution\n\n```py\nclass Solution:\n def findPeakElement(self, nums: list[int]) -> int:\n left, right = 0, len(nums) - 1\n while left < right:\n mid = (left + right) // 2\n if nums[mid] < nums[mid + 1]:\n left = mid + 1\n else:\n right = mid\n\n return left\n```\n"},"/binary-search/koko-eating-bananas":{"mdx":"---\ntitle: '875. Koko Eating Bananas'\ndescription: Koko loves to eat bananas. There are n piles of bananas, the i^th pile has piles[i] bananas. The guards have gone and will come back in h hours\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\nvideo: \"U2SozAs9RzA\"\nissue: \"https://github.com/prdlk/leetcode/issues/59\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 69.6 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Nvidia\", period: \"Three Months\", frequency: 100 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 87.4 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 62.3 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Coupang\", period: \"Six Months\", frequency: 100 }\n - { name: \"Swiggy\", period: \"Six Months\", frequency: 100 }\n - { name: \"Accenture\", period: \"Six Months\", frequency: 75 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 56.9 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 44.8 }\nsidebar:\n label: 'Koko Eating Bananas'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `piles = [3,6,7,11], h = 8`\n- Output: `4`\n\n### Example 2:\n- Input: `piles = [30,11,23,4,20], h = 5`\n- Output: `30`\n\n### Example 3:\n- Input: `piles = [30,11,23,4,20], h = 6`\n- Output: `23`\n\n### Constraints:\n\n- `1 <= piles.length <= 10^4`\n- `piles.length <= h <= 10^9`\n- `1 <= piles[i] <= 10^9`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"minEatingSpeed(piles, h)\"]) --> I[\"l = 1, r = max(piles)\"]\n I --> W{\"l < r?\"}\n W -- no --> E([\"return l — the smallest workable speed\"])\n W -- yes --> M[\"mid = floor((l + r) / 2)\"]\n M --> K[\"kWorks(mid): hours = sum of ceil(p / mid)\"]\n K --> Q{\"hours <= h?\"}\n Q -- yes --> A[\"r = mid — keep mid, try slower\"]\n Q -- no --> B[\"l = mid + 1 — too slow, speed up\"]\n A --> W\n B --> W\n```\n\n## Solution\n\n```js\n/**\n * @param {number[]} piles\n * @param {number} h\n * @return {number}\n */\nvar minEatingSpeed = function(piles, h) {\n // Function to determine if K works for the H\n function kWorks(k){\n let hours = 0;\n for (let p of piles){\n hours += Math.ceil(p/k)\n }\n return hours <= h;\n }\n\n // Setup Binary Search\n let l = 1;\n let r = Math.max(...piles);\n\n // Find the K value which works for H and consumes all bananas\n while (l < r){\n const mid = Math.floor((l + r) / 2);\n if (kWorks(mid)){\n r = mid;\n }else{\n l = mid + 1;\n }\n }\n return l;\n};\n```\n"},"/binary-search/median-of-two-sorted-arrays":{"mdx":"---\ntitle: '4. Median of Two Sorted Arrays'\ndescription: Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\n - { name: \"Divide and Conquer\", slug: \"divide-and-conquer\" }\nissue: \"https://github.com/prdlk/leetcode/issues/66\"\ncompanies:\n - { name: \"Rippling\", period: \"Thirty Days\", frequency: 93 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 69.6 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 63.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 62 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 57.2 }\n - { name: \"eBay\", period: \"Three Months\", frequency: 82.2 }\n - { name: \"Goldman Sachs\", period: \"Three Months\", frequency: 57 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"Zoho\", period: \"Six Months\", frequency: 79.6 }\n - { name: \"DE Shaw\", period: \"Six Months\", frequency: 74.2 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 48.1 }\nsidebar:\n label: 'Median of Two Sorted Arrays'\n badge: 'Hard'\n---\n\n### Example 1:\n- Input: `nums1 = [1,3], nums2 = [2]`\n- Output: `2.00000`\n- Explanation: merged array = `[1,2,3]` and median is `2`.\n\n### Example 2:\n- Input: `nums1 = [1,2], nums2 = [3,4]`\n- Output: `2.50000`\n- Explanation: merged array = `[1,2,3,4]` and median is `(2 + 3`) / `2 = 2.5`.\n\n### Constraints:\n\n- `nums1.length == m`\n- `nums2.length == n`\n- `0 <= m <= 1000`\n- `0 <= n <= 1000`\n- `1 <= m + n <= 2000`\n- `-10^6 <= nums1[i], nums2[i] <= 10^6`\n\n## Solution\n\n```py\nclass Solution:\n def findMedianSortedArrays(self, nums1: list[int], nums2: list[int]) -> float:\n if len(nums1) > len(nums2):\n return self.findMedianSortedArrays(nums2, nums1)\n\n m, n = len(nums1), len(nums2)\n left, right = 0, m\n\n while left <= right:\n partA = (left + right) // 2\n partB = (m + n + 1) // 2 - partA\n\n maxLeftA = float(\"-inf\") if partA == 0 else nums1[partA - 1]\n minRightA = float(\"inf\") if partA == m else nums1[partA]\n maxLeftB = float(\"-inf\") if partB == 0 else nums2[partB - 1]\n minRightB = float(\"inf\") if partB == n else nums2[partB]\n\n if maxLeftA <= minRightB and maxLeftB <= minRightA:\n if (m + n) % 2 == 0:\n return (max(maxLeftA, maxLeftB) + min(minRightA, minRightB)) / 2\n else:\n return max(maxLeftA, maxLeftB)\n elif maxLeftA > minRightB:\n right = partA - 1\n else:\n left = partA + 1\n```\n"},"/binary-search/search-a-2d-matrix":{"mdx":"---\ntitle: '74. Search a 2D Matrix'\ndescription: 'You are given an m x n integer matrix matrix with the following two properties:'\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\n - { name: \"Matrix\", slug: \"matrix\" }\nvideo: \"Ber2pi2C0j0\"\nissue: \"https://github.com/prdlk/leetcode/issues/61\"\ncompanies:\n - { name: \"Goldman Sachs\", period: \"Thirty Days\", frequency: 62.9 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 57.2 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 34.6 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"SAP\", period: \"Six Months\", frequency: 100 }\n - { name: \"tcs\", period: \"Six Months\", frequency: 43.5 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\nsidebar:\n label: 'Search a 2D Matrix'\n badge: 'Medium'\n---\n\n::::warning\nYou must write a solution in O(log(m * n)) time complexity.\n::::\n\n### Example 1:\n- Input: `matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3`\n- Output: `true`\n\n### Example 2:\n- Input: `matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13`\n- Output: `false`\n\n### Constraints:\n\n- `m == matrix.length`\n- `n == matrix[i].length`\n- `1 <= m, n <= 100`\n- `-10^4 <= matrix[i][j], target <= 10^4`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"searchMatrix(matrix, target)\"]) --> I[\"top = 0, bot = ROWS - 1\"]\n I --> W1{\"top <= bot?\"}\n W1 -- yes --> R1[\"row = (top + bot) // 2\"]\n R1 --> C1{\"target vs that row's range\"}\n C1 -- \"above matrix[row][-1]\" --> U[\"top = row + 1\"]\n C1 -- \"below matrix[row][0]\" --> V[\"bot = row - 1\"]\n C1 -- inside --> B([\"break — row found\"])\n U --> W1\n V --> W1\n W1 -- no --> F([\"return False — no row can hold it\"])\n B --> P[\"row = (top + bot) // 2; l = 0, r = COLS - 1\"]\n P --> W2{\"l <= r?\"}\n W2 -- no --> G([\"return False\"])\n W2 -- yes --> M[\"m = (l + r) // 2\"]\n M --> C2{\"target vs matrix[row][m]\"}\n C2 -- greater --> X[\"l = m + 1\"]\n C2 -- smaller --> Y[\"r = m - 1\"]\n C2 -- equal --> T([\"return True\"])\n X --> W2\n Y --> W2\n```\n\n## Solution\n\n```py\nclass Solution:\n def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:\n ROWS, COLS = len(matrix), len(matrix[0])\n\n top, bot = 0, ROWS - 1\n while top <= bot:\n row = (top + bot) // 2\n if target > matrix[row][-1]:\n top = row + 1\n elif target < matrix[row][0]:\n bot = row - 1\n else:\n break\n\n if not (top <= bot):\n return False\n\n row = (top + bot) // 2\n l, r = 0, COLS - 1\n while l <= r:\n m = (l + r) // 2\n if target > matrix[row][m]:\n l = m + 1\n elif target < matrix[row][m]:\n r = m - 1\n else:\n return True\n return False\n```\n"},"/binary-search/search-in-rotated-sorted-array":{"mdx":"---\ntitle: '33. Search in Rotated Sorted Array'\ndescription: There is an integer array nums sorted in ascending order (with distinct values)\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\nvideo: \"U8XENwh8Oy8\"\nissue: \"https://github.com/prdlk/leetcode/issues/60\"\ncompanies:\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 36.7 }\n - { name: \"Oracle\", period: \"Three Months\", frequency: 100 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 68.3 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"TikTok\", period: \"Six Months\", frequency: 64.9 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 63.3 }\n - { name: \"Yandex\", period: \"Six Months\", frequency: 59.6 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 53.1 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 48.1 }\nsidebar:\n label: 'Search in Rotated Sorted Array'\n badge: 'Medium'\n---\n\n::::warning\nYou must write an algorithm with O(log n) runtime complexity.\n::::\n\n### Example 1:\n- Input: `nums = [4,5,6,7,0,1,2], target = 0`\n- Output: `4`\n\n### Example 2:\n- Input: `nums = [4,5,6,7,0,1,2], target = 3`\n- Output: `-1`\n\n### Example 3:\n- Input: `nums = [1], target = 0`\n- Output: `-1`\n\n### Constraints:\n\n- `1 <= nums.length <= 5000`\n- `-10^4 <= nums[i] <= 10^4`\n- All values of `nums` are unique.\n- `nums` is an ascending array that is possibly rotated.\n- `-10^4 <= target <= 10^4`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"search(nums, target)\"]) --> I[\"l = 0, r = n - 1\"]\n I --> W{\"l <= r?\"}\n W -- no --> E([\"return -1\"])\n W -- yes --> M[\"mid = floor((l + r) / 2)\"]\n M --> F{\"nums[mid] == target?\"}\n F -- yes --> R([\"return mid\"])\n F -- no --> H{\"nums[l] <= nums[mid]? — left half sorted\"}\n H -- yes --> A{\"target outside [nums[l], nums[mid]]?\"}\n A -- yes --> A1[\"l = mid + 1 — search the right half\"]\n A -- no --> A2[\"r = mid - 1\"]\n H -- no --> B{\"target outside [nums[mid], nums[r]]?\"}\n B -- yes --> B1[\"r = mid - 1 — search the left half\"]\n B -- no --> B2[\"l = mid + 1\"]\n A1 --> W\n A2 --> W\n B1 --> W\n B2 --> W\n```\n\n## Solution\n\n```js\n/**\n * @param {number[]} nums\n * @param {number} target\n * @return {number}\n */\nvar search = function(nums, target) {\n let l = 0, r = nums.length - 1; \n\n while (l <= r){\n let mid = Math.floor((l + r)/2);\n if (target === nums[mid]) {\n return mid\n }\n\n // Left sorted portion\n if (nums[l] <= nums[mid]){\n if (target > nums[mid] || target < nums[l]){\n l = mid + 1;\n }else{\n r = mid - 1;\n }\n }\n // Right sorted portion\n else{\n if(target < nums[mid] || target > nums[r]){\n r = mid - 1; \n }else{\n l = mid + 1;\n }\n }\n }\n return -1;\n};\n```\n"},"/binary-search/search-insert-position":{"mdx":"---\ntitle: '35. Search Insert Position'\ndescription: Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\nvideo: \"K-RYzDZkzCI\"\nissue: \"https://github.com/prdlk/leetcode/issues/63\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 50.2 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Grammarly\", period: \"Three Months\", frequency: 84.8 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 49.8 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 56.2 }\nsidebar:\n label: 'Search Insert Position'\n badge: 'Easy'\n---\n\n::::warning\nYou must write an algorithm with O(log n) runtime complexity.\n::::\n\n### Example 1:\n- Input: `nums = [1,3,5,6], target = 5`\n- Output: `2`\n\n### Example 2:\n- Input: `nums = [1,3,5,6], target = 2`\n- Output: `1`\n\n### Example 3:\n- Input: `nums = [1,3,5,6], target = 7`\n- Output: `4`\n\n### Constraints:\n\n- `1 <= nums.length <= 10^4`\n- `-10^4 <= nums[i] <= 10^4`\n- `nums` contains distinct values sorted in ascending order.\n- `-10^4 <= target <= 10^4`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"searchInsert(nums, target)\"]) --> I[\"l = 0, r = n - 1\"]\n I --> W{\"l <= r?\"}\n W -- yes --> M[\"m = (l + r) // 2\"]\n M --> Q{\"nums[m] vs target\"}\n Q -- equal --> R([\"return m\"])\n Q -- \"nums[m] < target\" --> L[\"l = m + 1\"]\n L --> W\n Q -- \"nums[m] > target\" --> H[\"r = m - 1\"]\n H --> W\n W -- no --> E([\"return (l + r) // 2 + 1 — the insert slot\"])\n```\n\n## Solution\n\n```py\nclass Solution:\n def searchInsert(self, nums: List[int], target: int) -> int:\n l, r = 0, len(nums) - 1\n\n while l <= r:\n m = (l + r) // 2\n if nums[m] == target:\n return m\n elif nums[m] < target:\n l = m + 1\n elif nums[m] > target:\n r = m - 1\n\n return (l + r) // 2 + 1\n```\n"},"/graph-bfs":{"mdx":"---\ntitle: Graph BFS\ndescription: 'Nodes are visited in order of distance from the start, which gives the shortest path in an unweighted graph.'\nhidden: true\n---\n"},"/graph-bfs/rotting-oranges":{"mdx":"---\ntitle: '994. Rotting Oranges'\ndescription: 'You are given an m x n grid where each cell can have one of three values:'\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Matrix\", slug: \"matrix\" }\nissue: \"https://github.com/prdlk/leetcode/issues/137\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 75.8 }\n - { name: \"OpenAI\", period: \"Thirty Days\", frequency: 69.7 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 57.2 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 42.3 }\n - { name: \"Anduril\", period: \"Three Months\", frequency: 94.2 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 66 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 43.3 }\n - { name: \"Cognizant\", period: \"Six Months\", frequency: 76.3 }\n - { name: \"Lyft\", period: \"Six Months\", frequency: 72.2 }\n - { name: \"Flipkart\", period: \"Six Months\", frequency: 57.1 }\n - { name: \"Pinterest\", period: \"Six Months\", frequency: 55.8 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 54.1 }\n - { name: \"Salesforce\", period: \"Six Months\", frequency: 50.2 }\n - { name: \"Uber\", period: \"Six Months\", frequency: 45.7 }\nsidebar:\n label: 'Rotting Oranges'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `grid = [[2,1,1],[1,1,0],[0,1,1]]`\n- Output: `4`\n\n### Example 2:\n- Input: `grid = [[2,1,1],[0,1,1],[1,0,1]]`\n- Output: `-1`\n- Explanation: The orange in the bottom left corner (row `2`, column `0`) is never rotten, because rotting only happens 4-directionally.\n\n### Example 3:\n- Input: `grid = [[0,2]]`\n- Output: `0`\n- Explanation: Since there are already no fresh oranges at minute `0`, the answer is just `0`.\n\n### Constraints:\n\n- `m == grid.length`\n- `n == grid[i].length`\n- `1 <= m, n <= 10`\n- grid[i][j] is 0, 1, or 2.\n\n## Solution\n\n```py\nfrom collections import deque\n\n\nclass Solution:\n def orangesRotting(self, grid: list[list[int]]) -> int:\n q = deque()\n time, fresh = 0, 0\n\n rows, cols = len(grid), len(grid[0])\n for r in range(rows):\n for c in range(cols):\n if grid[r][c] == 1:\n fresh += 1\n if grid[r][c] == 2:\n q.append([r, c])\n\n directions = [[0, 1], [0, -1], [1, 0], [-1, 0]]\n\n while q and fresh > 0:\n for i in range(len(q)):\n r, c = q.popleft()\n for dr, dc in directions:\n row, col = dr + r, dc + c\n # check if in bounds\n if (\n row < 0\n or col < 0\n or row == len(grid)\n or col == len(grid[0])\n or grid[row][col] != 1\n ):\n continue\n grid[row][col] = 2\n q.append([row, col])\n fresh -= 1\n time += 1\n return time if fresh == 0 else -1\n```\n"},"/graph-bfs/word-ladder":{"mdx":"---\ntitle: '127. Word Ladder'\ndescription: 'A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:'\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Bidirectional Search\", slug: \"bidirectional-search\" }\nissue: \"https://github.com/prdlk/leetcode/issues/138\"\ncompanies:\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 65.6 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 55.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 69 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 46.8 }\n - { name: \"Citadel\", period: \"Six Months\", frequency: 88 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 69.8 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 32.7 }\nsidebar:\n label: 'Word Ladder'\n badge: 'Hard'\n---\n\n### Example 1:\n- Input: `beginWord = \"hit\", endWord = \"cog\", wordList = [\"hot\",\"dot\",\"dog\",\"lot\",\"log\",\"cog\"]`\n- Output: `5`\n- Explanation: One shortest transformation sequence is \"hit\" -> \"hot\" -> \"dot\" -> \"dog\" -> cog\", which is `5` words long.\n\n### Example 2:\n- Input: `beginWord = \"hit\", endWord = \"cog\", wordList = [\"hot\",\"dot\",\"dog\",\"lot\",\"log\"]`\n- Output: `0`\n- Explanation: The `endWord` \"cog\" is not in `wordList`, therefore there is no valid transformation sequence.\n\n### Constraints:\n\n- `1 <= beginWord.length <= 10`\n- `endWord.length == beginWord.length`\n- `1 <= wordList.length <= 5000`\n- `wordList[i].length == beginWord.length`\n- `beginWord`, `endWord`, and `wordList[i]` consist of lowercase English letters.\n- `beginWord != endWord`\n- All the words in `wordList` are unique.\n\n## Solution\n\n```py\nimport collections\n\n\nclass Solution:\n def ladderLength(self, beginWord: str, endWord: str, wordList: list[str]) -> int:\n if endWord not in wordList:\n return 0\n\n adj = collections.defaultdict(list)\n wordList.append(beginWord)\n\n for word in wordList:\n for j in range(len(word)):\n pattern = word[:j] + \"*\" + word[j + 1 :]\n adj[pattern].append(word)\n\n visit = set([beginWord])\n q = collections.deque([beginWord])\n res = 1\n\n while q:\n for i in range(len(q)):\n word = q.popleft()\n if word == endWord:\n return res\n for j in range(len(word)):\n pattern = word[:j] + \"*\" + word[j + 1 :]\n for adjWord in adj[pattern]:\n if adjWord not in visit:\n visit.add(adjWord)\n q.append(adjWord)\n res += 1\n return 0\n```\n"},"/graph-dfs":{"mdx":"---\ntitle: Graph DFS\ndescription: 'Each edge is followed to its end, and a visited set prevents a repeat visit.'\nhidden: true\n---\n"},"/graph-dfs/clone-graph":{"mdx":"---\ntitle: '133. Clone Graph'\ndescription: Given a reference of a node in a connected undirected graph\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Graph Theory\", slug: \"graph\" }\nissue: \"https://github.com/prdlk/leetcode/issues/131\"\ncompanies:\n - { name: \"Amazon\", period: \"Three Months\", frequency: 21.2 }\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 24.5 }\nsidebar:\n label: 'Clone Graph'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `adjList = [[2,4],[1,3],[2,4],[1,3]]`\n- Output: `[[2,4],[1,3],[2,4],[1,3]]`\n- Explanation: There are `4` nodes in the graph. 1st `node` (val = 1)'s neighbors are 2nd `node` (val = `2`) and 4th `node` (val = `4`). 2nd `node` (val = 2)'s neighbors are 1st `node` (val = `1`) and 3rd `node` (val = `3`). 3rd `node` (val = 3)'s neighbors are 2nd `node` (val = `2`) and 4th `node` (val = `4`). 4th `node` (val = 4)'s neighbors are 1st `node` (val = `1`) and 3rd `node` (val = `3`).\n\n### Example 2:\n- Input: `adjList = [[]]`\n- Output: `[[]]`\n- Explanation: Note that the input contains one empty list. The graph consists of only one `node` with val = `1` and it does not have any neighbors.\n\n### Example 3:\n- Input: `adjList = []`\n- Output: `[]`\n- Explanation: This an empty graph, it does not have any nodes.\n\n### Constraints:\n\n- The number of nodes in the graph is in the range [0, 100].\n- `1 <= Node.val <= 100`\n- Node.val is unique for each node.\n- There are no repeated edges and no self-loops in the graph.\n- The Graph is connected and all nodes can be visited starting from the given node.\n\n## Solution\n\n```py\n\"\"\"\n# Definition for a Node.\nclass Node:\n def __init__(self, val = 0, neighbors = None):\n self.val = val\n self.neighbors = neighbors if neighbors is not None else []\n\"\"\"\n\nfrom typing import Optional\n\n\nclass Solution:\n def cloneGraph(self, node: Optional[\"Node\"]) -> Optional[\"Node\"]:\n oldToNew = {}\n\n def dfs(node):\n if node in oldToNew:\n return oldToNew[node]\n\n copy = Node(node.val)\n oldToNew[node] = copy\n\n for n in node.neighbors:\n copy.neighbors.append(dfs(n))\n return copy\n\n return dfs(node) if node else None\n```\n"},"/graph-dfs/number-of-islands":{"mdx":"---\ntitle: '200. Number of Islands'\ndescription: Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Union-Find\", slug: \"union-find\" }\n - { name: \"Matrix\", slug: \"matrix\" }\nissue: \"https://github.com/prdlk/leetcode/issues/130\"\ncompanies:\n - { name: \"Anduril\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Apple\", period: \"Thirty Days\", frequency: 81.7 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 65.8 }\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 65.6 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 50.2 }\n - { name: \"Qualcomm\", period: \"Three Months\", frequency: 100 }\n - { name: \"TikTok\", period: \"Three Months\", frequency: 89.3 }\n - { name: \"Uber\", period: \"Three Months\", frequency: 72.9 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 66 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 57.2 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Autodesk\", period: \"Six Months\", frequency: 100 }\n - { name: \"ByteDance\", period: \"Six Months\", frequency: 100 }\n - { name: \"Rivian\", period: \"Six Months\", frequency: 100 }\n - { name: \"Snap\", period: \"Six Months\", frequency: 100 }\n - { name: \"Waymo\", period: \"Six Months\", frequency: 100 }\n - { name: \"Walmart Labs\", period: \"Six Months\", frequency: 95.1 }\n - { name: \"Visa\", period: \"Six Months\", frequency: 80.9 }\n - { name: \"Nvidia\", period: \"Six Months\", frequency: 78.7 }\n - { name: \"Oracle\", period: \"Six Months\", frequency: 78.7 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 69.8 }\n - { name: \"Yandex\", period: \"Six Months\", frequency: 59.6 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 59 }\nsidebar:\n label: 'Number of Islands'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `grid = [ [\"1\",\"1\",\"1\",\"1\",\"0\"], [\"1\",\"1\",\"0\",\"1\",\"0\"], [\"1\",\"1\",\"0\",\"0\",\"0\"], [\"0\",\"0\",\"0\",\"0\",\"0\"] ]`\n- Output: `1`\n\n### Example 2:\n- Input: `grid = [ [\"1\",\"1\",\"0\",\"0\",\"0\"], [\"1\",\"1\",\"0\",\"0\",\"0\"], [\"0\",\"0\",\"1\",\"0\",\"0\"], [\"0\",\"0\",\"0\",\"1\",\"1\"] ]`\n- Output: `3`\n\n### Constraints:\n\n- `m == grid.length`\n- `n == grid[i].length`\n- `1 <= m, n <= 300`\n- grid[i][j] is '0' or '1'.\n\n## Solution\n\n```py\nclass Solution:\n def numIslands(self, grid: List[List[str]]) -> int:\n rows, cols = len(grid), len(grid[0])\n\n def dfs(r, c):\n if r < 0 or r >= rows or c < 0 or c >= cols or grid[r][c] != \"1\":\n return\n else:\n grid[r][c] = \"0\"\n dfs(r, c + 1) # right\n dfs(r + 1, c) # bottom\n dfs(r, c - 1) # left\n dfs(r - 1, c) # top\n\n num_islands = 0\n for r in range(rows):\n for c in range(cols):\n if grid[r][c] == \"1\":\n num_islands += 1\n dfs(r, c)\n\n return num_islands\n```\n"},"/graph-dfs/pacific-atlantic-water-flow":{"mdx":"---\ntitle: '417. Pacific Atlantic Water Flow'\ndescription: There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Matrix\", slug: \"matrix\" }\nissue: \"https://github.com/prdlk/leetcode/issues/132\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Google\", period: \"Three Months\", frequency: 23.9 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 24.1 }\nsidebar:\n label: 'Pacific Atlantic Water Flow'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]`\n- Output: `[[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]]`\n- Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below: `[0,4]`: `[0,4]` -> Pacific Ocean `[0,4]` -> Atlantic Ocean `[1,3]`: `[1,3]` -> `[0,3]` -> Pacific Ocean `[1,3]` -> `[1,4]` -> Atlantic Ocean `[1,4]`: `[1,4]` -> `[1,3]` -> `[0,3]` -> Pacific Ocean `[1,4]` -> Atlantic Ocean `[2,2]`: `[2,2]` -> `[1,2]` -> `[0,2]` -> Pacific Ocean `[2,2]` -> `[2,3]` -> `[2,4]` -> Atlantic Ocean `[3,0]`: `[3,0]` -> Pacific Ocean `[3,0]` -> `[4,0]` -> Atlantic Ocean `[3,1]`: `[3,1]` -> `[3,0]` -> Pacific Ocean `[3,1]` -> `[4,1]` -> Atlantic Ocean `[4,0]`: `[4,0]` -> Pacific Ocean `[4,0]` -> Atlantic Ocean Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans.\n\n### Example 2:\n- Input: `heights = [[1]]`\n- Output: `[[0,0]]`\n- Explanation: The water can flow from the only cell to the Pacific and Atlantic oceans.\n\n### Constraints:\n\n- `m == heights.length`\n- `n == heights[r].length`\n- `1 <= m, n <= 200`\n- `0 <= heights[r][c] <= 10^5`\n\n## Solution\n\n```py\nclass Solution:\n def pacificAtlantic(self, heights: list[list[int]]) -> list[list[int]]:\n rows, cols = len(heights), len(heights[0])\n pac, atl = set(), set()\n\n def dfs(r, c, seen, prevHeight):\n if (\n (r, c) in seen\n or r < 0\n or c < 0\n or r == rows\n or c == cols\n or heights[r][c] < prevHeight\n ):\n return\n seen.add((r, c))\n dfs(r + 1, c, seen, heights[r][c])\n dfs(r - 1, c, seen, heights[r][c])\n dfs(r, c + 1, seen, heights[r][c])\n dfs(r, c - 1, seen, heights[r][c])\n\n for c in range(cols):\n dfs(0, c, pac, heights[0][c])\n dfs(rows - 1, c, atl, heights[rows - 1][c])\n\n for r in range(rows):\n dfs(r, 0, pac, heights[r][0])\n dfs(r, cols - 1, atl, heights[r][cols - 1])\n\n res = []\n for r in range(rows):\n for c in range(cols):\n if (r, c) in pac and (r, c) in atl:\n res.append([r, c])\n\n return res\n```\n"},"/greedy":{"md":"---\ntitle: Greedy\ndescription: 'The best local choice is made at each step and is not changed later.'\nhidden: true\n---\n\n## When to use greedy algorithms?\n\n- Solving optimization problems.\n- Ask to optimize some value and calculate it.\n- keywords (min/max, longest/shortest, largest/smallest, etc.)\n\n## Optimization problems\n\n1. Return the minimum number of swaps required to make s1 equal to s2.\n2. Return the minimum number of steps to make s and t anagrams of each other.\n3. Given three integers a, b, and c, return the longest possible happy string, if there are multiple such strings return any of them. if there is no such string, return empty string.\n\n## What are greedy algorithms?\n\n- Greedy algorithms follow this format:\n- identify what choices you have at every step\n - find the best choice\n - just do what looks best until done\n- assume that the best choice is the one that will lead to the best outcome\n\n## How do we know greedy works?\n- Write out a formal proof\n - too complicated to write out\n - too much time\n- better to use intuition\n - try a bunch of ideas\n - find counterexamples\n\n## Steps to use greedy algorithms\n\n1. **Identify the different steps and choices**\n\n - Choices have to be comparable\n - must determine if a choice is better than another\n\n2. **Check if greedy works**\n\n - try finding small cases where greedy is not optimal\n - if cases pass, try to deduce why greedy is optimal\n\n## Why use greedy algorithms?\n\n- simple to code\n- doesn't use complex algorithms (BFS, DFS, etc.)\n- runtime is fast, usually 0(n) or O(nlogn)\n- however, greedy algorithms are not always optimal\n\n## The Greedy Skeleton\n\n### Type 1: Walk and Carry\n\n:::info\nNo sort. Walk the list once. Carry one number. At each step, decide: keep it, or drop it.\n:::\n\n```python\ndef walk_and_carry(nums):\n carry = start_value\n best = start_value\n for n in nums:\n if carry_is_useless(carry):\n carry = fresh_start\n carry = update(carry, n)\n best = max(best, carry)\n return best\n```\n\n### Type 2: Sort and Pick\n\n:::info\nUsed when order doesn't matter, so you pick the order. Meeting rooms, intervals, \"most tasks by a deadline.\"\n:::\n\n```python\ndef greedy(items):\n # 1. Sort by the greedy key (deadline, end time, size, ratio, ...)\n items.sort(key=greedy_key)\n result = 0\n state = initial_state()\n for item in items:\n # 2. Take the item only if the locally best choice is valid\n if is_feasible(item, state):\n result += take(item, state)\n # 3. Correctness relies on an exchange argument, not just passing tests\n return result\n```\n","mdx":"---\ntitle: Greedy\ndescription: 'The best local choice is made at each step and is not changed later.'\nhidden: true\n---\n\n## When to use greedy algorithms?\n\n- Solving optimization problems.\n- Ask to optimize some value and calculate it.\n- keywords (min/max, longest/shortest, largest/smallest, etc.)\n\n## Optimization problems\n\n1. Return the minimum number of swaps required to make s1 equal to s2.\n2. Return the minimum number of steps to make s and t anagrams of each other.\n3. Given three integers a, b, and c, return the longest possible happy string, if there are multiple such strings return any of them. if there is no such string, return empty string.\n\n## What are greedy algorithms?\n\n- Greedy algorithms follow this format:\n- identify what choices you have at every step\n - find the best choice\n - just do what looks best until done\n- assume that the best choice is the one that will lead to the best outcome\n\n## How do we know greedy works?\n- Write out a formal proof\n - too complicated to write out\n - too much time\n- better to use intuition\n - try a bunch of ideas\n - find counterexamples\n\n## Steps to use greedy algorithms\n\n\n \n - Choices have to be comparable\n - must determine if a choice is better than another\n \n \n - try finding small cases where greedy is not optimal\n - if cases pass, try to deduce why greedy is optimal\n \n \n\n## Why use greedy algorithms?\n\n- simple to code\n- doesn't use complex algorithms (BFS, DFS, etc.)\n- runtime is fast, usually 0(n) or O(nlogn)\n- however, greedy algorithms are not always optimal\n\n## The Greedy Skeleton\n\n### Type 1: Walk and Carry\n\n:::info\nNo sort. Walk the list once. Carry one number. At each step, decide: keep it, or drop it.\n:::\n\n```python\ndef walk_and_carry(nums):\n carry = start_value\n best = start_value\n for n in nums:\n if carry_is_useless(carry):\n carry = fresh_start\n carry = update(carry, n)\n best = max(best, carry)\n return best\n```\n\n### Type 2: Sort and Pick\n\n:::info\nUsed when order doesn't matter, so you pick the order. Meeting rooms, intervals, \"most tasks by a deadline.\"\n:::\n\n```python\ndef greedy(items):\n # 1. Sort by the greedy key (deadline, end time, size, ratio, ...)\n items.sort(key=greedy_key)\n result = 0\n state = initial_state()\n for item in items:\n # 2. Take the item only if the locally best choice is valid\n if is_feasible(item, state):\n result += take(item, state)\n # 3. Correctness relies on an exchange argument, not just passing tests\n return result\n```\n"},"/greedy/gas-station":{"mdx":"---\ntitle: '134. Gas Station'\ndescription: There are n gas stations along a circular route, where the amount of gas at the i^th station is gas[i]\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Greedy\", slug: \"greedy\" }\nissue: \"https://github.com/prdlk/leetcode/issues/176\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Sprinklr\", period: \"Three Months\", frequency: 95.9 }\n - { name: \"TikTok\", period: \"Three Months\", frequency: 70.9 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 28.9 }\n - { name: \"Adobe\", period: \"Six Months\", frequency: 74.3 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 44.8 }\n - { name: \"Google\", period: \"Six Months\", frequency: 17.4 }\nsidebar:\n label: 'Gas Station'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `gas = [1,2,3,4,5], cost = [3,4,5,1,2]`\n- Output: `3`\n- Explanation: Start at station `3` (index `3`) and fill up with `4` unit of `gas`. Your tank = `0 + 4 = 4` Travel to station `4`. Your tank = `4 - 1 + 5 = 8` Travel to station `0`. Your tank = `8 - 2 + 1 = 7` Travel to station `1`. Your tank = `7 - 3 + 2 = 6` Travel to station `2`. Your tank = `6 - 4 + 3 = 5` Travel to station `3`. The `cost` is `5`. Your `gas` is just enough to travel back to station `3`. Therefore, return `3` as the starting index.\n\n### Example 2:\n- Input: `gas = [2,3,4], cost = [3,4,3]`\n- Output: `-1`\n- Explanation: You can't start at station `0` or `1`, as there is not enough `gas` to travel to the next station. Let's start at station `2` and fill up with `4` unit of `gas`. Your tank = `0 + 4 = 4` Travel to station `0`. Your tank = `4 - 3 + 2 = 3` Travel to station `1`. Your tank = `3 - 3 + 3 = 3` You cannot travel back to station `2`, as it requires `4` unit of `gas` but you only have `3`. Therefore, you can't travel around the circuit once no matter where you start.\n\n### Constraints:\n\n- `n == gas.length == cost.length`\n- `1 <= n <= 10^5`\n- `0 <= gas[i], cost[i] <= 10^4`\n- The input is generated such that the answer is unique.\n\n## Solution\n\n```py\nclass Solution:\n def canCompleteCircuit(self, gas: list[int], cost: list[int]) -> int:\n start, tank, total = 0, 0, 0\n\n for i in range(len(gas)):\n drive = gas[i] - cost[i]\n tank = tank + drive\n\n if tank < 0:\n tank = 0\n start = i + 1\n total = total + drive\n\n return start if total >= 0 else -1\n```\n"},"/greedy/jump-game":{"mdx":"---\ntitle: '55. Jump Game'\ndescription: You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\n - { name: \"Greedy\", slug: \"greedy\" }\nissue: \"https://github.com/prdlk/leetcode/issues/175\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 54.8 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Google\", period: \"Three Months\", frequency: 45 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 56.2 }\nsidebar:\n label: 'Jump Game'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [2,3,1,1,4]`\n- Output: `true`\n- Explanation: Jump `1` step from index `0` to `1`, then `3` steps to the last index.\n\n### Example 2:\n- Input: `nums = [3,2,1,0,4]`\n- Output: `false`\n- Explanation: You will always arrive at index `3` no matter what. Its maximum jump length is `0`, which makes it impossible to reach the last index.\n\n### Constraints:\n\n- `1 <= nums.length <= 10^4`\n- `0 <= nums[i] <= 10^5`\n\n## Solution\n\n```py\nclass Solution:\n def canJump(self, nums: list[int]) -> bool:\n best, last = nums[0], len(nums) - 1\n\n for i in range(last):\n if i > best:\n return False\n else:\n pos = nums[i] + i\n best = max(best, pos)\n\n return best >= last\n```\n"},"/greedy/maximum-subarray":{"mdx":"---\ntitle: '53. Maximum Subarray'\ndescription: Given an integer array nums, find the subarray with the largest sum, and return its sum\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Divide and Conquer\", slug: \"divide-and-conquer\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\nissue: \"https://github.com/prdlk/leetcode/issues/174\"\ncompanies:\n - { name: \"Upstart\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Infosys\", period: \"Thirty Days\", frequency: 80.2 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 75.6 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 69.6 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 65.3 }\n - { name: \"Google\", period: \"Three Months\", frequency: 54.6 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 85.6 }\nsidebar:\n label: 'Maximum Subarray'\n badge: 'Medium'\n---\n\n::::warning\nIf you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.\n::::\n\n### Example 1:\n- Input: `nums = [-2,1,-3,4,-1,2,1,-5,4]`\n- Output: `6`\n- Explanation: The subarray `[4,-1,2,1]` has the largest sum `6`.\n\n### Example 2:\n- Input: `nums = [1]`\n- Output: `1`\n- Explanation: The subarray `[1]` has the largest sum `1`.\n\n### Example 3:\n- Input: `nums = [5,4,-1,7,8]`\n- Output: `23`\n- Explanation: The subarray `[5,4,-1,7,8]` has the largest sum `23`.\n\n### Constraints:\n\n- `1 <= nums.length <= 10^5`\n- `-10^4 <= nums[i] <= 10^4`\n\n## Solution\n\n```py\nclass Solution:\n def maxSubArray(self, nums: list[int]) -> int:\n best, total = nums[0], nums[0]\n\n for i in range(1, len(nums)):\n if total < 0:\n total = nums[i]\n else:\n total = total + nums[i]\n best = max(best, total)\n\n return best\n```\n"},"/hash-table":{"md":"---\ntitle: Hash Table\ndescription: 'A key maps to a value in constant time, which removes the need for a full scan.'\nhidden: true\n---\n\nSometimes when programming, we need a data structure that maps an arbitrary data type to another arbitrary data type, like an array with non-integer indices. For example, a phone book that allows lookup by a person's name requires such data type.\n\nA **hash table** (or a hash map) is one way of representing this data structure. The idea of hash table is this:\n\nData is stored in a fix-sized array, where each data value has an unique index. When we add a new `(key, value)` pair to the array, we use a hash function to generate a hash value within the range of the array with the `key`. We then store the data `value` at the index indicated by the generated hash value. Later, when we retrieve the data with the `key` from the hash table, we hash the `key` again and look up the data in the array with the corresponding index. Since the hash function always returns the same hash values with the same input, we will always find the correct item in that index.\n\n\n\n\nHash Table Operations\n\n\n## Efficiency\n\nAssuming the hash function uses constant time, the average time complexity is `O(n / k)` for each insertion/access operation on the table, where `n` is the number of entries in the hash table and `k` is the array size of the table. This assumes the hash function distributes keys uniformly across the `k` buckets, so each bucket holds about `n / k` entries on average.\n\n### Frequency Counting\n\n```python\ndef get_counter(arr: list[int]) -> dict[int, int]:\n # initialize a hash map to store each number and its count\n counter: dict[int, int] = {}\n for num in arr:\n # check if num is a key in the hash map\n if num in counter:\n # update the count of num to increase by 1\n counter[num] += 1\n else:\n # set the count of num to 1\n counter[num] = 1\n return counter\n```\n\n:::tip\nPython provides a built-in [`collections.Counter`](https://docs.python.org/3/library/collections.html#collections.Counter) class that does this for us.\n:::\n\n```python\nfrom collections import Counter\n\nfq = Counter(arr)\n```\n","mdx":"---\ntitle: Hash Table\ndescription: 'A key maps to a value in constant time, which removes the need for a full scan.'\nhidden: true\n---\n\nSometimes when programming, we need a data structure that maps an arbitrary data type to another arbitrary data type, like an array with non-integer indices. For example, a phone book that allows lookup by a person's name requires such data type.\n\nA **hash table** (or a hash map) is one way of representing this data structure. The idea of hash table is this:\n\nData is stored in a fix-sized array, where each data value has an unique index. When we add a new `(key, value)` pair to the array, we use a hash function to generate a hash value within the range of the array with the `key`. We then store the data `value` at the index indicated by the generated hash value. Later, when we retrieve the data with the `key` from the hash table, we hash the `key` again and look up the data in the array with the corresponding index. Since the hash function always returns the same hash values with the same input, we will always find the correct item in that index.\n\n\n \n\n\n\n\n\n\n## Efficiency\n\nAssuming the hash function uses constant time, the average time complexity is `O(n / k)` for each insertion/access operation on the table, where `n` is the number of entries in the hash table and `k` is the array size of the table. This assumes the hash function distributes keys uniformly across the `k` buckets, so each bucket holds about `n / k` entries on average.\n\n### Frequency Counting\n\n```python\ndef get_counter(arr: list[int]) -> dict[int, int]:\n # initialize a hash map to store each number and its count\n counter: dict[int, int] = {}\n for num in arr:\n # check if num is a key in the hash map\n if num in counter:\n # update the count of num to increase by 1\n counter[num] += 1\n else:\n # set the count of num to 1\n counter[num] = 1\n return counter\n```\n\n:::tip\nPython provides a built-in [`collections.Counter`](https://docs.python.org/3/library/collections.html#collections.Counter) class that does this for us.\n:::\n\n```python\nfrom collections import Counter\n\nfq = Counter(arr)\n```\n"},"/hash-table/check-if-the-sentence-is-pangram":{"mdx":"---\ntitle: '1832. Check if the Sentence Is Pangram'\ndescription: A pangram is a sentence where every letter of the English alphabet appears at least once\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\ncompanies:\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 34.2 }\n - { name: \"Amazon\", period: \"Six Months\", frequency: 14.3 }\n - { name: \"Google\", period: \"Six Months\", frequency: 11.8 }\nsidebar:\n label: 'Check if the Sentence Is Pangram'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `sentence = \"thequickbrownfoxjumpsoverthelazydog\"`\n- Output: `true`\n- Explanation: `sentence` contains at least one of every letter of the English alphabet.\n\n### Example 2:\n- Input: `sentence = \"leetcode\"`\n- Output: `false`\n\n### Constraints:\n\n- `1 <= sentence.length <= 1000`\n- `sentence` consists of lowercase English letters.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"checkIfPangram(sentence)\"]) --> I[\"freq = {}\"]\n I --> L{\"more char c?\"}\n L -- yes --> C[\"freq[c] = (freq[c] or 0) + 1\"]\n C --> L\n L -- no --> Q{\"distinct keys in freq == 26?\"}\n Q -- yes --> R([\"return true\"])\n Q -- no --> F([\"return false\"])\n```\n\n## Solution\n\n```js\n/**\n * @param {string} sentence\n * @return {boolean}\n */\nvar checkIfPangram = function(sentence) {\n const freq = {};\n for (let c of sentence) freq[c] = (freq[c] || 0) + 1;\n \n if (Object.keys(freq).length === 26){\n return true;\n }\n return false;\n};\n```\n"},"/hash-table/contains-duplicate":{"mdx":"---\ntitle: '217. Contains Duplicate'\ndescription: Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Sorting\", slug: \"sorting\" }\nvideo: \"3OamzN90kPg\"\nissue: \"https://github.com/prdlk/leetcode/issues/27\"\ncompanies:\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 50.2 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 48.3 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 36.8 }\nsidebar:\n label: 'Contains Duplicate'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `nums = [1,2,3,1]`\n- Output: `true`\n- Explanation: The element `1` occurs at the indices `0` and `3`.\n\n### Example 2:\n- Input: `nums = [1,2,3,4]`\n- Output: `false`\n- Explanation: All elements are distinct.\n\n### Example 3:\n- Input: `nums = [1,1,1,3,3,4,3,2,4,2]`\n- Output: `true`\n\n### Constraints:\n\n- `1 <= nums.length <= 10^5`\n- `-10^9 <= nums[i] <= 10^9`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"containsDuplicate(nums)\"]) --> I[\"freq = {}\"]\n I --> L{\"more n in nums?\"}\n L -- no --> E([\"return false\"])\n L -- yes --> C[\"freq[n] = (freq[n] or 0) + 1\"]\n C --> Q{\"freq[n] >= 2?\"}\n Q -- yes --> R([\"return true\"])\n Q -- no --> L\n```\n\n## Solution\n\n```py\nclass Solution:\n def containsDuplicate(self, nums: List[int]) -> bool:\n return len(set(nums)) != len(nums)\n```\n"},"/hash-table/first-unique-character-in-a-string":{"mdx":"---\ntitle: '387. First Unique Character in a String'\ndescription: Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Queue\", slug: \"queue\" }\n - { name: \"Counting\", slug: \"counting\" }\nvideo: \"rBENYgWy3xU\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"Yandex\", period: \"Three Months\", frequency: 76.1 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 73.7 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 46.8 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 43.3 }\n - { name: \"Google\", period: \"Three Months\", frequency: 23.9 }\n - { name: \"EPAM Systems\", period: \"Six Months\", frequency: 76.2 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 59 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 32 }\nsidebar:\n label: 'First Unique Character in a String'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `s = \"leetcode\"`\n- Output: `0`\n- Explanation: The character `'l'` at index `0` is the first character that does not occur at any other index.\n\n### Example 2:\n- Input: `s = \"loveleetcode\"`\n- Output: `2`\n\n### Example 3:\n- Input: `s = \"aabb\"`\n- Output: `-1`\n\n### Constraints:\n\n- `1 <= s.length <= 10^5`\n- `s` consists of only lowercase English letters.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"firstUniqChar(s)\"]) --> F[\"Pass 1 — freq: count every char of s\"]\n F --> L{\"Pass 2 — more i in 0..len-1?\"}\n L -- no --> E([\"return -1\"])\n L -- yes --> Q{\"freq[s[i]] == 1?\"}\n Q -- yes --> R([\"return i — leftmost wins because the scan is in order\"])\n Q -- no --> L\n```\n\n## Solution\n\n```js\n/**\n * @param {string} s\n * @return {number}\n */\nvar firstUniqChar = function (s) {\n const freq = {};\n for (let c of s) freq[c] = (freq[c] || 0) + 1;\n\n for (let i = 0; i < s.length; i++) {\n if (freq[s[i]] === 1) {\n return i;\n }\n }\n return -1;\n};\n```\n"},"/hash-table/group-anagrams":{"mdx":"---\ntitle: '49. Group Anagrams'\ndescription: Given an array of strings strs, group the anagrams together. You can return the answer in any order\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Sorting\", slug: \"sorting\" }\nvideo: \"vzdNOK2oB2E\"\nissue: \"https://github.com/prdlk/leetcode/issues/30\"\ncompanies:\n - { name: \"Apple\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 78.4 }\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 76.7 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 58.2 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 50.2 }\n - { name: \"CrowdStrike\", period: \"Three Months\", frequency: 100 }\n - { name: \"Deloitte\", period: \"Three Months\", frequency: 100 }\n - { name: \"HashedIn\", period: \"Three Months\", frequency: 100 }\n - { name: \"Smartsheet\", period: \"Three Months\", frequency: 100 }\n - { name: \"Walmart Labs\", period: \"Three Months\", frequency: 100 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"Expedia\", period: \"Six Months\", frequency: 100 }\n - { name: \"Wipro\", period: \"Six Months\", frequency: 100 }\n - { name: \"Adobe\", period: \"Six Months\", frequency: 93.7 }\n - { name: \"Zoho\", period: \"Six Months\", frequency: 79.6 }\n - { name: \"Nvidia\", period: \"Six Months\", frequency: 78.7 }\n - { name: \"Oracle\", period: \"Six Months\", frequency: 78.7 }\n - { name: \"EPAM Systems\", period: \"Six Months\", frequency: 76.2 }\n - { name: \"Palo Alto Networks\", period: \"Six Months\", frequency: 75.9 }\n - { name: \"Visa\", period: \"Six Months\", frequency: 69.7 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 67.9 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 53.1 }\nsidebar:\n label: 'Group Anagrams'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `strs = [\"eat\",\"tea\",\"tan\",\"ate\",\"nat\",\"bat\"]`\n- Output: `[[\"bat\"],[\"nat\",\"tan\"],[\"ate\",\"eat\",\"tea\"]]`\n- Explanation: There is no string in `strs` that can be rearranged to form `\"bat\"`. The strings `\"nat\"` and `\"tan\"` are anagrams as they can be rearranged to form each other. The strings `\"ate\"`, `\"eat\"`, and `\"tea\"` are anagrams as they can be rearranged to form each other.\n\n### Example 2:\n- Input: `strs = [\"\"]`\n- Output: `[[\"\"]]`\n\n### Example 3:\n- Input: `strs = [\"a\"]`\n- Output: `[[\"a\"]]`\n\n### Constraints:\n\n- `1 <= strs.length <= 10^4`\n- `0 <= strs[i].length <= 100`\n- `strs[i]` consists of lowercase English letters.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"groupAnagrams(strs)\"]) --> K[\"sorted[i] = letters of strs[i], sorted — the anagram key\"]\n K --> I[\"anagrams = {}\"]\n I --> L{\"more i in 0..n-1?\"}\n L -- no --> E([\"return Object.values(anagrams)\"])\n L -- yes --> Q{\"anagrams has sorted[i]?\"}\n Q -- no --> N[\"anagrams[sorted[i]] = [strs[i]]\"]\n Q -- yes --> P[\"anagrams[sorted[i]].push(strs[i])\"]\n N --> L\n P --> L\n```\n\n## Solution\n\n```js\n/**\n * @param {string[]} strs\n * @return {string[][]}\n */\nvar groupAnagrams = function(strs) {\n let sorted = strs.map(str => str.split(\"\").sort().join(\"\"));\n let anagrams = {};\n\n for (let i = 0; i < strs.length; i++){\n if(!anagrams[sorted[i]]){\n anagrams[sorted[i]] = [strs[i]]\n }else{\n anagrams[sorted[i]].push(strs[i])\n }\n }\n\n return Object.values(anagrams);\n};\n```\n"},"/hash-table/isomorphic-strings":{"mdx":"---\ntitle: '205. Isomorphic Strings'\ndescription: Given two strings s and t, determine if they are isomorphic\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\nissue: \"https://github.com/prdlk/leetcode/issues/33\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 36.7 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 24.5 }\nsidebar:\n label: 'Isomorphic Strings'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `s = \"egg\", t = \"add\"`\n- Output: `true`\n- Explanation: The strings `s` and `t` can be made identical by: • Mapping 'e' to 'a'. • Mapping 'g' to 'd'.\n\n### Example 2:\n- Input: `s = \"f11\", t = \"b23\"`\n- Output: `false`\n- Explanation: The strings `s` and `t` can not be made identical as `'1`' needs to be mapped to both `'2`' and `'3`'.\n\n### Example 3:\n- Input: `s = \"paper\", t = \"title\"`\n- Output: `true`\n\n### Constraints:\n\n- `1 <= s.length <= 5 * 10^4`\n- `t.length == s.length`\n- `s` and `t` consist of any valid ascii character.\n\n## Solution\n\n```py\nclass Solution:\n def isIsomorphic(self, s: str, t: str) -> bool:\n N = len(t)\n seen = {}\n\n for i in range(N):\n c1, c2 = s[i], t[i]\n if c1 in seen and seen[c1] != c2:\n return False\n elif c1 in seen and seen[c1] == c2:\n continue\n else:\n seen[c1] = c2\n\n return len(set(s)) == len(set(t))\n```\n"},"/hash-table/longest-consecutive-sequence":{"mdx":"---\ntitle: '128. Longest Consecutive Sequence'\ndescription: Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Union-Find\", slug: \"union-find\" }\nissue: \"https://github.com/prdlk/leetcode/issues/34\"\ncompanies:\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 74.3 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 65.8 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 57.2 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 53.2 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 69 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Paytm\", period: \"Six Months\", frequency: 100 }\n - { name: \"Nvidia\", period: \"Six Months\", frequency: 91.1 }\n - { name: \"TikTok\", period: \"Six Months\", frequency: 55.2 }\n - { name: \"Salesforce\", period: \"Six Months\", frequency: 50.2 }\nsidebar:\n label: 'Longest Consecutive Sequence'\n badge: 'Medium'\n---\n\n::::warning\nYou must write an algorithm that runs in O(n) time.\n::::\n\n### Example 1:\n- Input: `nums = [100,4,200,1,3,2]`\n- Output: `4`\n- Explanation: The longest consecutive elements sequence is [1, `2`, `3`, 4]. Therefore its length is `4`.\n\n### Example 2:\n- Input: `nums = [0,3,7,2,5,8,4,6,0,1]`\n- Output: `9`\n\n### Example 3:\n- Input: `nums = [1,0,1,2]`\n- Output: `3`\n\n### Constraints:\n\n- `0 <= nums.length <= 10^5`\n- `-10^9 <= nums[i] <= 10^9`\n\n## Solution\n\n```py\nclass Solution:\n def longestConsecutive(self, nums: list[int]) -> int:\n seen = set(nums)\n best = 0\n\n for n in seen:\n if n - 1 in seen:\n continue\n\n length = 1\n while length + n in seen:\n length += 1\n\n best = max(length, best)\n\n return best\n```\n"},"/hash-table/sort-characters-by-frequency":{"mdx":"---\ntitle: '451. Sort Characters By Frequency'\ndescription: Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Sorting\", slug: \"sorting\" }\n - { name: \"Heap (Priority Queue)\", slug: \"heap-priority-queue\" }\n - { name: \"Bucket Sort\", slug: \"bucket-sort\" }\n - { name: \"Counting\", slug: \"counting\" }\nvideo: \"OXdXc9HTrIg\"\ncompanies:\n - { name: \"Meta\", period: \"Three Months\", frequency: 49.8 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 48.3 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 46.8 }\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"Amazon\", period: \"Six Months\", frequency: 20.9 }\nsidebar:\n label: 'Sort Characters By Frequency'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `s = \"tree\"`\n- Output: `\"eert\"`\n- Explanation: `'e'` appears twice while `'r'` and `'t'` both appear once. So `'e'` must appear before both `'r'` and `'t'`. Therefore `\"eetr\"` is also a valid answer.\n\n### Example 2:\n- Input: `s = \"cccaaa\"`\n- Output: `\"aaaccc\"`\n- Explanation: Both `'c'` and `'a'` appear three times, so both `\"cccaaa\"` and `\"aaaccc\"` are valid answers. Note that `\"cacaca\"` is incorrect, as the same characters must be together.\n\n### Example 3:\n- Input: `s = \"Aabb\"`\n- Output: `\"bbAa\"`\n- Explanation: `\"bbaA\"` is also a valid answer, but `\"Aabb\"` is incorrect. Note that `'A'` and `'a'` are treated as two different characters.\n\n### Constraints:\n\n- `1 <= s.length <= 5 * 10^5`\n- `s` consists of uppercase and lowercase English letters and digits.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"frequencySort(s)\"]) --> F[\"freq: count every char of s\"]\n F --> C[\"split s and sort with a two-key comparator\"]\n C --> K1[\"primary: freq[b] - freq[a] — most frequent first\"]\n C --> K2[\"tie-break: a.localeCompare(b) — alphabetical\"]\n K1 --> R([\"join and return\"])\n K2 --> R\n```\n\n## Solution\n\n```js\n/**\n * @param {string} s\n * @return {string}\n */\nvar frequencySort = function (s) {\n // count the frequency of each character\n const freq = {};\n for (let c of s) freq[c] = (freq[c] || 0) + 1;\n\n // sort the characters by frequency\n return s\n .split(\"\")\n .sort((a, b) => freq[b] - freq[a] || a.localeCompare(b))\n .join(\"\");\n};\n```\n"},"/hash-table/top-k-frequent-elements":{"mdx":"---\ntitle: '347. Top K Frequent Elements'\ndescription: Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Divide and Conquer\", slug: \"divide-and-conquer\" }\n - { name: \"Sorting\", slug: \"sorting\" }\n - { name: \"Heap (Priority Queue)\", slug: \"heap-priority-queue\" }\n - { name: \"Bucket Sort\", slug: \"bucket-sort\" }\n - { name: \"Counting\", slug: \"counting\" }\n - { name: \"Quickselect\", slug: \"quickselect\" }\nvideo: \"YPTqKIgVk-k\"\nissue: \"https://github.com/prdlk/leetcode/issues/31\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 72.9 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 42.3 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Snap\", period: \"Six Months\", frequency: 100 }\n - { name: \"SoFi\", period: \"Six Months\", frequency: 100 }\n - { name: \"PayPal\", period: \"Six Months\", frequency: 88.4 }\n - { name: \"Walmart Labs\", period: \"Six Months\", frequency: 81.4 }\n - { name: \"Nvidia\", period: \"Six Months\", frequency: 78.7 }\n - { name: \"eBay\", period: \"Six Months\", frequency: 75.2 }\n - { name: \"Visa\", period: \"Six Months\", frequency: 69.7 }\n - { name: \"Snowflake\", period: \"Six Months\", frequency: 58.7 }\n - { name: \"TikTok\", period: \"Six Months\", frequency: 55.2 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 50.6 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 48.1 }\n - { name: \"tcs\", period: \"Six Months\", frequency: 43.5 }\nsidebar:\n label: 'Top K Frequent Elements'\n badge: 'Medium'\n---\n\n::::warning\nYour algorithm's time complexity must be better than O(n log n), where n is the array's size.\n::::\n\n### Example 1:\n- Input: `nums = [1,1,1,2,2,3], k = 2`\n- Output: `[1,2]`\n\n### Example 2:\n- Input: `nums = [1], k = 1`\n- Output: `[1]`\n\n### Example 3:\n- Input: `nums = [1,2,1,2,1,2,3,1,3,2], k = 2`\n- Output: `[1,2]`\n\n### Constraints:\n\n- `1 <= nums.length <= 10^5`\n- `-10^4 <= nums[i] <= 10^4`\n- `k` is in the range `[1, the number of unique elements in the array]`.\n- It is guaranteed that the answer is unique.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"topKFrequent(nums, k)\"]) --> F[\"freq: count every value in nums\"]\n F --> K[\"keys of freq, cast to Number\"]\n K --> O[\"sort by freq[b] - freq[a] — most frequent first\"]\n O --> R([\"return slice(0, k)\"])\n```\n\n## Solution\n\n```js\n/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number[]}\n */\nvar topKFrequent = function (nums, k) {\n const freq = {};\n for (let n of nums) freq[n] = (freq[n] || 0) + 1;\n\n return Object.keys(freq)\n .map(Number)\n .sort((a, b) => freq[b] - freq[a])\n .slice(0, k);\n};\n```\n"},"/hash-table/two-sum":{"mdx":"---\ntitle: '1. Two Sum'\ndescription: You are given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\nvideo: \"KLlXCFG5TnA\"\nissue: \"https://github.com/prdlk/leetcode/issues/29\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 79.6 }\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 76.7 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Infosys\", period: \"Thirty Days\", frequency: 68.6 }\n - { name: \"Altimetrik\", period: \"Three Months\", frequency: 100 }\n - { name: \"Capgemini\", period: \"Three Months\", frequency: 100 }\n - { name: \"Microstrategy\", period: \"Three Months\", frequency: 100 }\n - { name: \"MongoDB\", period: \"Three Months\", frequency: 100 }\n - { name: \"Cognizant\", period: \"Three Months\", frequency: 88.7 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 80.4 }\n - { name: \"Barclays\", period: \"Six Months\", frequency: 100 }\n - { name: \"Cisco\", period: \"Six Months\", frequency: 100 }\n - { name: \"Deloitte\", period: \"Six Months\", frequency: 100 }\n - { name: \"Expedia\", period: \"Six Months\", frequency: 100 }\n - { name: \"NetApp\", period: \"Six Months\", frequency: 100 }\n - { name: \"Samsung\", period: \"Six Months\", frequency: 100 }\n - { name: \"Sony\", period: \"Six Months\", frequency: 100 }\n - { name: \"Oracle\", period: \"Six Months\", frequency: 91.2 }\n - { name: \"Qualcomm\", period: \"Six Months\", frequency: 80.7 }\n - { name: \"IBM\", period: \"Six Months\", frequency: 77.2 }\n - { name: \"eBay\", period: \"Six Months\", frequency: 75.2 }\n - { name: \"Walmart Labs\", period: \"Six Months\", frequency: 70.5 }\n - { name: \"Flipkart\", period: \"Six Months\", frequency: 57.1 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 44.8 }\nsidebar:\n label: 'Two Sum'\n badge: 'Easy'\n---\n\n::::warning\nCan you come up with an algorithm that is less than O(n^2) time complexity?\n::::\n\n### Example 1:\n- Input: `nums = [2,7,11,15], target = 9`\n- Output: `[0,1]`\n- Explanation: Because `nums[0] + nums[1]` == `9`, we return [0, 1].\n\n### Example 2:\n- Input: `nums = [3,2,4], target = 6`\n- Output: `[1,2]`\n\n### Example 3:\n- Input: `nums = [3,3], target = 6`\n- Output: `[0,1]`\n\n### Constraints:\n\n- `2 <= nums.length <= 10^4`\n- `-10^9 <= nums[i] <= 10^9`\n- `-10^9 <= target <= 10^9`\n- Only one valid answer exists.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"twoSum(nums, target)\"]) --> I[\"seen = {} — value to index\"]\n I --> L{\"more (i, num) in nums?\"}\n L -- no --> E([\"return []\"])\n L -- yes --> C[\"complement = target - num\"]\n C --> H{\"complement in seen?\"}\n H -- yes --> R([\"return [seen[complement], i]\"])\n H -- no --> W[\"seen[num] = i\"]\n W --> L\n```\n\n## Solution\n\n```py\nclass Solution:\n def twoSum(self, nums: List[int], target: int) -> List[int]:\n seen = {}\n for i, num in enumerate(nums):\n complement = target - num\n if complement in seen:\n return [seen[complement], i]\n seen[num] = i\n return []\n```\n"},"/hash-table/valid-anagram":{"mdx":"---\ntitle: '242. Valid Anagram'\ndescription: Given two strings s and t, return true if t is an anagram of s, and false otherwise\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Sorting\", slug: \"sorting\" }\nvideo: \"9UtInBqnCgA\"\nissue: \"https://github.com/prdlk/leetcode/issues/28\"\ncompanies:\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 58.2 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 53.2 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"IBM\", period: \"Three Months\", frequency: 100 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 66 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 64.4 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 58.9 }\n - { name: \"Deloitte\", period: \"Six Months\", frequency: 81 }\nsidebar:\n label: 'Valid Anagram'\n badge: 'Easy'\n---\n\n::::warning\nWhat if the inputs contain Unicode characters? How would you adapt your solution to such a case?\n::::\n\n### Example 1:\n- Input: `s = \"anagram\", t = \"nagaram\"`\n- Output: `true`\n\n### Example 2:\n- Input: `s = \"rat\", t = \"car\"`\n- Output: `false`\n\n### Constraints:\n\n- `1 <= s.length, t.length <= 5 * 10^4`\n- `s` and `t` consist of lowercase English letters.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"isAnagram(s, t)\"]) --> I[\"freq: count every char of s\"]\n I --> L{\"more char c in t?\"}\n L -- no --> E([\"return true\"])\n L -- yes --> Q{\"freq[c] missing or 0?\"}\n Q -- yes --> R([\"return false — t has a char s cannot cover\"])\n Q -- no --> D[\"freq[c] -= 1\"]\n D --> L\n```\n\n## Solution\n\n```js\n/**\n * @param {string} s\n * @param {string} t\n * @return {boolean}\n */\nvar isAnagram = function(s, t) {\n let freq = {};\n for (let c of s) freq[c] = (freq[c] || 0) + 1;\n \n for (let c of t) {\n if(!freq[c] || freq[c] === 0){\n return false;\n }\n freq[c] = freq[c] - 1;\n }\n return true;\n};\n```\n"},"/hash-table/word-pattern":{"mdx":"---\ntitle: '290. Word Pattern'\ndescription: Given a pattern and a string s, find if s follows the same pattern\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\nissue: \"https://github.com/prdlk/leetcode/issues/35\"\ncompanies:\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 24.1 }\n - { name: \"Amazon\", period: \"Six Months\", frequency: 20.9 }\nsidebar:\n label: 'Word Pattern'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `pattern = \"abba\", s = \"dog cat cat dog\"`\n- Output: `true`\n- Explanation: The bijection can be established as: • 'a' maps to \"dog\". • 'b' maps to \"cat\".\n\n### Example 2:\n- Input: `pattern = \"abba\", s = \"dog cat cat fish\"`\n- Output: `false`\n\n### Example 3:\n- Input: `pattern = \"aaaa\", s = \"dog cat cat dog\"`\n- Output: `false`\n\n### Constraints:\n\n- `1 <= pattern.length <= 300`\n- `pattern` contains only lower-case English letters.\n- `1 <= s.length <= 3000`\n- `s` contains only lowercase English letters and spaces ' '.\n- `s` does not contain any leading or trailing spaces.\n- All the words in `s` are separated by a single space.\n\n## Solution\n\n```py\nclass Solution:\n def wordPattern(self, pattern: str, s: str) -> bool:\n if len(pattern) != len(s.split()):\n return False\n\n words = s.split()\n map = {}\n\n for i in range(len(pattern)):\n c = pattern[i]\n w = words[i]\n\n if c in map and map[c] != w:\n return False\n else:\n map[c] = w\n\n return len(set(pattern)) == len(set(words))\n```\n"},"/heap-priority-queue":{"mdx":"---\ntitle: Heap / Priority Queue\ndescription: 'The largest or smallest item is always at the top, and it is removed in logarithmic time.'\nhidden: true\n---\n"},"/heap-priority-queue/k-closest-points-to-origin":{"mdx":"---\ntitle: '973. K Closest Points to Origin'\ndescription: Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0)\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Math\", slug: \"math\" }\n - { name: \"Divide and Conquer\", slug: \"divide-and-conquer\" }\n - { name: \"Geometry\", slug: \"geometry\" }\n - { name: \"Sorting\", slug: \"sorting\" }\n - { name: \"Heap (Priority Queue)\", slug: \"heap-priority-queue\" }\n - { name: \"Quickselect\", slug: \"quickselect\" }\n - { name: \"K-D Tree\", slug: \"k-d-tree\" }\nvideo: \"rI2EBUEMfTk\"\nissue: \"https://github.com/prdlk/leetcode/issues/119\"\ncompanies:\n - { name: \"Amazon\", period: \"Three Months\", frequency: 28.9 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 47.3 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\nsidebar:\n label: 'K Closest Points to Origin'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `points = [[1,3],[-2,2]], k = 1`\n- Output: `[[-2,2]]`\n- Explanation: The distance between `(1`, `3`) and the origin is sqrt(10). The distance between `(-2`, `2`) and the origin is sqrt(8). Since sqrt(8) < sqrt(10), `(-2`, `2`) is closer to the origin. We only want the closest `k = 1 points` from the origin, so the answer is just [[-2,2]].\n\n### Example 2:\n- Input: `points = [[3,3],[5,-1],[-2,4]], k = 2`\n- Output: `[[3,3],[-2,4]]`\n- Explanation: The answer [[-2,4],[3,3]] would also be accepted.\n\n### Constraints:\n\n- `1 <= k <= points.length <= 10^4`\n- `-10^4 <= xi, yi <= 10^4`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"kClosest(points, k)\"]) --> I[\"minHeap = []\"]\n I --> F{\"more x, y in points?\"}\n F -- yes --> D[\"dist = x**2 + y**2 — squared distance orders the same as sqrt\"]\n D --> A[\"minHeap.append([dist, x, y])\"]\n A --> F\n F -- no --> H[\"heapq.heapify(minHeap), res = [] — O(n) build, smallest dist on top\"]\n H --> W{\"k > 0?\"}\n W -- yes --> P[\"dist, x, y = heapq.heappop(minHeap)\"]\n P --> R[\"res.append([x, y]), k -= 1\"]\n R --> W\n W -- no --> E([\"return res\"])\n```\n\n## Solution\n\n```py\nimport heapq\n\n\nclass Solution:\n def kClosest(self, points: List[List[int]], k: int) -> List[List[int]]:\n minHeap = []\n for x, y in points:\n dist = (x**2) + (y**2)\n minHeap.append([dist, x, y])\n\n heapq.heapify(minHeap)\n res = []\n\n while k > 0:\n dist, x, y = heapq.heappop(minHeap)\n res.append([x, y])\n k -= 1\n\n return res\n```\n"},"/heap-priority-queue/kth-largest-element-in-an-array":{"mdx":"---\ntitle: '215. Kth Largest Element in an Array'\ndescription: Given an integer array nums and an integer k, return the k^th largest element in the array\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Divide and Conquer\", slug: \"divide-and-conquer\" }\n - { name: \"Sorting\", slug: \"sorting\" }\n - { name: \"Heap (Priority Queue)\", slug: \"heap-priority-queue\" }\n - { name: \"Quickselect\", slug: \"quickselect\" }\nvideo: \"XEmy13g1Qxc\"\nissue: \"https://github.com/prdlk/leetcode/issues/118\"\ncompanies:\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 70.8 }\n - { name: \"Spotify\", period: \"Three Months\", frequency: 90.3 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 36.8 }\n - { name: \"Google\", period: \"Three Months\", frequency: 32.7 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 28.9 }\n - { name: \"Walmart Labs\", period: \"Six Months\", frequency: 70.5 }\n - { name: \"IBM\", period: \"Six Months\", frequency: 70.1 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 63.3 }\n - { name: \"Flipkart\", period: \"Six Months\", frequency: 57.1 }\n - { name: \"TikTok\", period: \"Six Months\", frequency: 55.2 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 37.9 }\nsidebar:\n label: 'Kth Largest Element in an Array'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [3,2,1,5,6,4], k = 2`\n- Output: `5`\n\n### Example 2:\n- Input: `nums = [3,2,3,1,2,4,5,5,6], k = 4`\n- Output: `4`\n\n### Constraints:\n\n- `1 <= k <= nums.length <= 10^5`\n- `-10^4 <= nums[i] <= 10^4`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"findKthLargest(nums, k)\"]) --> F{\"more i in range(len(nums))?\"}\n F -- yes --> N[\"nums[i] = -nums[i] — negate so the min-heap acts as a max-heap\"]\n N --> F\n F -- no --> H[\"heapq.heapify(nums)\"]\n H --> W{\"more _ in range(k - 1)?\"}\n W -- yes --> P[\"heapq.heappop(nums) — discard the k-1 largest\"]\n P --> W\n W -- no --> E([\"return -heapq.heappop(nums)\"])\n```\n\n## Solution\n\n```py\nimport heapq\n\n\nclass Solution:\n def findKthLargest(self, nums: List[int], k: int) -> int:\n for i in range(len(nums)):\n nums[i] = -nums[i]\n\n heapq.heapify(nums)\n\n for _ in range(k - 1):\n heapq.heappop(nums)\n\n return -heapq.heappop(nums)\n```\n"},"/heap-priority-queue/merge-k-sorted-lists":{"mdx":"---\ntitle: '23. Merge k Sorted Lists'\ndescription: You are given an array of k linked-lists lists, each linked-list is sorted in ascending order\nicon: dot\ntopics:\n - { name: \"Linked List\", slug: \"linked-list\" }\n - { name: \"Divide and Conquer\", slug: \"divide-and-conquer\" }\n - { name: \"Heap (Priority Queue)\", slug: \"heap-priority-queue\" }\n - { name: \"Merge Sort\", slug: \"merge-sort\" }\n - { name: \"Tournament Sort\", slug: \"tournament-sort\" }\nvideo: \"q5a5OiGbT6Q\"\nissue: \"https://github.com/prdlk/leetcode/issues/120\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"FreshWorks\", period: \"Three Months\", frequency: 100 }\n - { name: \"Snap\", period: \"Three Months\", frequency: 100 }\n - { name: \"Deloitte\", period: \"Three Months\", frequency: 89.1 }\n - { name: \"Salesforce\", period: \"Three Months\", frequency: 64.7 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Google\", period: \"Three Months\", frequency: 23.9 }\n - { name: \"Nvidia\", period: \"Six Months\", frequency: 78.7 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 57.9 }\nsidebar:\n label: 'Merge k Sorted Lists'\n badge: 'Hard'\n---\n\n### Example 1:\n- Input: `lists = [[1,4,5],[1,3,4],[2,6]]`\n- Output: `[1,1,2,3,4,4,5,6]`\n- Explanation: The linked-lists are: [ 1->4->5, 1->3->4, 2->6 ] merging them into one sorted linked list: 1->1->2->3->4->4->5->6\n\n### Example 2:\n- Input: `lists = []`\n- Output: `[]`\n\n### Example 3:\n- Input: `lists = [[]]`\n- Output: `[]`\n\n### Constraints:\n\n- `k == lists.length`\n- `0 <= k <= 10^4`\n- `0 <= lists[i].length <= 500`\n- `-10^4 <= lists[i][j] <= 10^4`\n- `lists[i]` is sorted in ascending order.\n- The sum of lists[i].length will not exceed 10^4.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"mergeKLists(lists)\"]) --> I[\"heap = []\"]\n I --> F{\"more i, node in enumerate(lists)?\"}\n F -- yes --> C{\"node?\"}\n C -- yes --> P[\"heapq.heappush(heap, (node.val, i, node)) — i breaks ties so nodes are never compared\"]\n P --> F\n C -- no --> F\n F -- no --> D[\"D = ListNode(), cur = D\"]\n D --> W{\"heap?\"}\n W -- no --> E([\"return D.next\"])\n W -- yes --> O[\"val, i, node = heapq.heappop(heap) — the smallest head across all lists\"]\n O --> L[\"cur.next = node, cur = node, node = node.next\"]\n L --> N{\"node?\"}\n N -- yes --> Q[\"heapq.heappush(heap, (node.val, i, node))\"]\n Q --> W\n N -- no --> W\n```\n\n## Solution\n\n```py\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nimport heapq\n\n\nclass Solution:\n def mergeKLists(self, lists: List[Optional[ListNode]]) -> Optional[ListNode]:\n heap = []\n\n # K log K\n for i, node in enumerate(lists):\n if node:\n heapq.heappush(heap, (node.val, i, node))\n\n D = ListNode()\n cur = D\n\n # n log k\n while heap:\n val, i, node = heapq.heappop(heap)\n cur.next = node\n cur = node\n node = node.next\n\n if node:\n heapq.heappush(heap, (node.val, i, node))\n\n # Time: O(N log k)\n # Space: O(n)\n return D.next\n```\n"},"/hybrid-structures":{"mdx":"---\ntitle: Hybrid Structures\ndescription: 'Two structures are combined, such as a hash map and a linked list, to get the best properties of each.'\nhidden: true\n---\n"},"/hybrid-structures/add-two-numbers":{"mdx":"---\ntitle: '2. Add Two Numbers'\ndescription: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list\nicon: dot\ntopics:\n - { name: \"Linked List\", slug: \"linked-list\" }\n - { name: \"Math\", slug: \"math\" }\n - { name: \"Recursion\", slug: \"recursion\" }\nvideo: \"wgFPrzTjm7s\"\nissue: \"https://github.com/prdlk/leetcode/issues/88\"\ncompanies:\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 95.8 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 83.1 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 72.8 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 63.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 61.3 }\n - { name: \"EarnIn\", period: \"Six Months\", frequency: 100 }\n - { name: \"Jane Street\", period: \"Six Months\", frequency: 100 }\n - { name: \"Palo Alto Networks\", period: \"Six Months\", frequency: 75.9 }\n - { name: \"tcs\", period: \"Six Months\", frequency: 43.5 }\nsidebar:\n label: 'Add Two Numbers'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `l1 = [2,4,3], l2 = [5,6,4]`\n- Output: `[7,0,8]`\n- Explanation: `342 + 465 = 807`.\n\n### Example 2:\n- Input: `l1 = [0], l2 = [0]`\n- Output: `[0]`\n\n### Example 3:\n- Input: `l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]`\n- Output: `[8,9,9,9,0,0,0,1]`\n\n### Constraints:\n\n- The number of nodes in each linked list is in the range [1, 100].\n- `0 <= Node.val <= 9`\n- It is guaranteed that the list represents a number that does not have leading zeros.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"addTwoNumbers(l1, l2)\"]) --> I[\"dummy = ListNode(), carry = 0, curr = dummy — dummy.next will be the head\"]\n I --> W{\"l1 or l2 or carry?\"}\n W -- no --> E([\"return dummy.next\"])\n W -- yes --> A[\"s = (l1.val if l1 else 0) + (l2.val if l2 else 0) + carry — a missing digit counts as 0\"]\n A --> D[\"carry, val = divmod(s, 10)\"]\n D --> N[\"curr.next = ListNode(val), curr = curr.next\"]\n N --> V[\"l1 = l1.next if l1 else None, l2 = l2.next if l2 else None\"]\n V --> W\n```\n\n## Solution\n\n```py\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def addTwoNumbers(\n self, l1: Optional[ListNode], l2: Optional[ListNode]\n ) -> Optional[ListNode]:\n\n dummy = ListNode()\n carry = 0\n curr = dummy\n while l1 or l2 or carry:\n s = (l1.val if l1 else 0) + (l2.val if l2 else 0) + carry\n carry, val = divmod(s, 10)\n\n curr.next = ListNode(val)\n curr = curr.next\n\n l1 = l1.next if l1 else None\n l2 = l2.next if l2 else None\n\n return dummy.next\n```\n"},"/hybrid-structures/insert-delete-getrandom-o1":{"mdx":"---\ntitle: '380. Insert Delete GetRandom O(1)'\ndescription: 'Implement the RandomizedSet class:'\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Math\", slug: \"math\" }\n - { name: \"Design\", slug: \"design\" }\n - { name: \"Randomized\", slug: \"randomized\" }\nissue: \"https://github.com/prdlk/leetcode/issues/90\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 80.4 }\n - { name: \"Google\", period: \"Three Months\", frequency: 32.7 }\n - { name: \"Axon\", period: \"Six Months\", frequency: 100 }\n - { name: \"Okta\", period: \"Six Months\", frequency: 100 }\n - { name: \"Quora\", period: \"Six Months\", frequency: 100 }\n - { name: \"SoFi\", period: \"Six Months\", frequency: 100 }\n - { name: \"Uber\", period: \"Six Months\", frequency: 61.9 }\n - { name: \"Yandex\", period: \"Six Months\", frequency: 59.6 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 54.1 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 24.1 }\nsidebar:\n label: 'Insert Delete GetRandom O(1)'\n badge: 'Medium'\n---\n\n::::warning\nYou must implement the functions of the class such that each function works in average O(1) time complexity.\n::::\n\n### Example 1:\n- Input: ``\n- Output: ``\n- Explanation: Input [\"RandomizedSet\", \"insert\", \"remove\", \"insert\", \"getRandom\", \"remove\", \"insert\", \"getRandom\"] `[[]`, `[1]`, `[2]`, `[2]`, `[]`, `[1]`, `[2]`, []] Output [null, true, false, true, `2`, true, false, 2] Explanation RandomizedSet randomizedSet = new RandomizedSet(); randomizedSet.insert(1); // Inserts `1` to the set. Returns true as `1` was inserted successfully. randomizedSet.remove(2); // Returns false as `2` does not exist in the set. randomizedSet.insert(2); // Inserts `2` to the set, returns true. Set now contains `[1,2]`. randomizedSet.getRandom(); // getRandom() should return either `1` or `2` randomly. randomizedSet.remove(1); // Removes `1` from the set, returns true. Set now contains `[2]`. randomizedSet.insert(2); // `2` was already in the set, so return false. randomizedSet.getRandom(); // Since `2` is the only number in the set, getRandom() will always return `2`.\n\n### Constraints:\n\n- `-2^31 <= val <= 2^31 - 1`\n- At most 2 * 10^5 calls will be made to insert, remove, and getRandom.\n- There will be at least one element in the data structure when getRandom is called.\n\n## Solution\n\n```py\nfrom random import choice\n\n\nclass RandomizedSet:\n def __init__(self):\n self.list = []\n self.dict = {}\n\n def insert(self, val: int) -> bool:\n if val in self.dict:\n return False\n self.dict[val] = len(self.list)\n self.list.append(val)\n return True\n\n def remove(self, val: int) -> bool:\n if val in self.dict:\n last, idx = self.list[-1], self.dict[val]\n self.list[idx], self.dict[last] = last, idx\n self.list.pop()\n del self.dict[val]\n return True\n return False\n\n def getRandom(self) -> int:\n return choice(self.list)\n\n\n# Your RandomizedSet object will be instantiated and called as such:\n# obj = RandomizedSet()\n# param_1 = obj.insert(val)\n# param_2 = obj.remove(val)\n# param_3 = obj.getRandom()\n```\n"},"/hybrid-structures/lru-cache":{"mdx":"---\ntitle: '146. LRU Cache'\ndescription: Design a data structure that follows the constraints of a Least Recently Used (LRU) cache\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Linked List\", slug: \"linked-list\" }\n - { name: \"Design\", slug: \"design\" }\n - { name: \"Doubly-Linked List\", slug: \"doubly-linked-list\" }\nvideo: \"7ABFKPK2hD4\"\nissue: \"https://github.com/prdlk/leetcode/issues/87\"\ncompanies:\n - { name: \"NetApp\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Adobe\", period: \"Thirty Days\", frequency: 83.5 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 69.6 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 36.7 }\n - { name: \"TikTok\", period: \"Three Months\", frequency: 100 }\n - { name: \"Palo Alto Networks\", period: \"Three Months\", frequency: 92.8 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 88.5 }\n - { name: \"Goldman Sachs\", period: \"Three Months\", frequency: 66.3 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 53.2 }\n - { name: \"Airwallex\", period: \"Six Months\", frequency: 100 }\n - { name: \"Docusign\", period: \"Six Months\", frequency: 100 }\n - { name: \"Moloco\", period: \"Six Months\", frequency: 100 }\n - { name: \"Nutanix\", period: \"Six Months\", frequency: 100 }\n - { name: \"Oracle\", period: \"Six Months\", frequency: 100 }\n - { name: \"Twitch\", period: \"Six Months\", frequency: 100 }\n - { name: \"Snap\", period: \"Six Months\", frequency: 92.4 }\n - { name: \"Qualcomm\", period: \"Six Months\", frequency: 92 }\n - { name: \"Nvidia\", period: \"Six Months\", frequency: 91.1 }\n - { name: \"ByteDance\", period: \"Six Months\", frequency: 88.6 }\n - { name: \"MongoDB\", period: \"Six Months\", frequency: 87.7 }\n - { name: \"Expedia\", period: \"Six Months\", frequency: 87.5 }\n - { name: \"eBay\", period: \"Six Months\", frequency: 86.1 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 74.9 }\n - { name: \"Visa\", period: \"Six Months\", frequency: 69.7 }\n - { name: \"Uber\", period: \"Six Months\", frequency: 61.9 }\n - { name: \"Salesforce\", period: \"Six Months\", frequency: 50.2 }\nsidebar:\n label: 'LRU Cache'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: ``\n- Output: ``\n- Explanation: Input [\"LRUCache\", \"put\", \"put\", \"get\", \"put\", \"get\", \"put\", \"get\", \"get\", \"get\"] `[[2]`, [1, 1], [2, 2], `[1]`, [3, 3], `[2]`, [4, 4], `[1]`, `[3]`, [4]] Output [null, null, null, `1`, null, `-1`, null, `-1`, `3`, 4] Explanation LRUCache lRUCache = new LRUCache(2); lRUCache.put(1, `1`); // cache is {1=1} lRUCache.put(2, `2`); // cache is {1=1, 2=2} lRUCache.get(1); // return `1` lRUCache.put(3, `3`); // LRU `key` was `2`, evicts `key 2`, cache is {1=1, 3=3} lRUCache.get(2); // returns `-1` (not found) lRUCache.put(4, `4`); // LRU `key` was `1`, evicts `key 1`, cache is {4=4, 3=3} lRUCache.get(1); // return `-1` (not found) lRUCache.get(3); // return `3` lRUCache.get(4); // return `4`\n\n### Constraints:\n\n- `1 <= capacity <= 3000`\n- `0 <= key <= 10^4`\n- `0 <= value <= 10^5`\n- At most 2 * 10^5 calls will be made to get and put.\n\n## Approach\n\n```mermaid\nflowchart TD\n subgraph state[\"state — dict for O(1) lookup, doubly linked list for O(1) reorder\"]\n H[\"head — least recent\"] --- N[\"... nodes ...\"] --- T[\"tail — most recent\"]\n end\n subgraph get[\"get(key)\"]\n G1{\"key in cache?\"} -- no --> G2([\"return -1\"])\n G1 -- yes --> G3[\"_remove_node then _add_node — move it beside the tail\"]\n G3 --> G4([\"return node.value\"])\n end\n subgraph put[\"put(key, value)\"]\n P1{\"key in cache?\"} -- yes --> P2[\"update value, _remove_node then _add_node\"]\n P1 -- no --> P3[\"new Node, cache[key] = node, _add_node\"]\n P3 --> P4{\"len(cache) > capacity?\"}\n P4 -- yes --> P5[\"evict head.next — the least recent — and del cache[lru.key]\"]\n P4 -- no --> P6([\"done\"])\n P2 --> P6\n P5 --> P6\n end\n state --> get\n state --> put\n```\n\n## Solution\n\n```py\nclass Node:\n def __init__(self, key=0, value=0):\n self.key = key\n self.value = value\n self.prev = None\n self.next = None\n\n\nclass LRUCache:\n def __init__(self, capacity: int):\n self.capacity = capacity\n self.cache = {}\n self.head = Node()\n self.tail = Node()\n self.head.next = self.tail # pyright: ignore[reportAttributeAccessIssue]\n self.tail.prev = self.head # pyright: ignore[reportAttributeAccessIssue]\n\n def _remove_node(self, node):\n prev_node = node.prev\n next_node = node.next\n prev_node.next = next_node\n next_node.prev = prev_node\n\n def _add_node(self, node):\n # Insert just before the tail (most recent at end)\n node.next = self.tail\n node.prev = self.tail.prev\n self.tail.prev.next = node # pyright: ignore[reportAttributeAccessIssue]\n self.tail.prev = node\n\n def get(self, key: int) -> int:\n if key not in self.cache:\n return -1\n\n node = self.cache[key]\n self._remove_node(node)\n self._add_node(node)\n return node.value\n\n def put(self, key: int, value: int) -> None:\n if key in self.cache:\n node = self.cache[key]\n node.value = value\n self._remove_node(node)\n self._add_node(node)\n else:\n new_node = Node(key, value)\n self.cache[key] = new_node\n self._add_node(new_node)\n\n if len(self.cache) > self.capacity:\n lru = self.head.next # Oldest node is next to the head\n self._remove_node(lru)\n del self.cache[lru.key] # pyright: ignore[reportAttributeAccessIssue]\n\n\n# Your LRUCache object will be instantiated and called as such:\n# obj = LRUCache(capacity)\n# param_1 = obj.get(key)\n# obj.put(key,value)\n```\n"},"/intervals":{"md":"---\ntitle: Intervals\ndescription: 'Ranges with a start and an end are sorted, and then merged, counted, or scheduled.'\nhidden: true\n---\n\nAn interval is a pair `[start, end]` marking a stretch on a line:\n- the minutes a meeting occupies\n- the range a task runs\n- the span a sensor was active\n\n**What interval problems ask you?**\n\n- Do any two collide?\n- What does the timeline look like once you merge everything that touches?\n- How many rooms do you need so no two meetings share one?\n\n:::tip\nEvery one of those questions reduces to a single primitive: deciding whether two intervals overlap, and if they do, how to merge them or what part they share. Once that primitive is clear, the rest of the pattern is bookkeeping.\n:::\n\n## When two intervals overlap\n\nIt is easier to state when two intervals do not overlap. Interval `a` sits entirely before interval `b` when `a` finishes before `b` begins, that is `a.end < b.start`. By symmetry they also miss each other when `b` ends before `a` starts. If neither of those holds, the intervals overlap.\n\n\n\nNegating `a.end < b.start` or `b.end < a.start` gives the positive test directly: two intervals overlap exactly when `a.start <= b.end` and `b.start <= a.end`. That pair of comparisons is the whole overlap check.\n\n## The Merged Region\n\nWhen two intervals overlap, you can merge them into one interval that covers both. Its start is the earlier of the two starts, and its end is the later of the two ends: `[min(a.start, b.start), max(a.end, b.end)]`. The merged interval begins as soon as either interval starts, and it ends only once both have stopped.\n\n\n\n## The Shared Region\n\nWhen two intervals overlap, the part they share is itself an interval. Its start is the later of the two starts, and its end is the earlier of the two ends: `[max(a.start, b.start), min(a.end, b.end)]`. The overlap begins only once both intervals have started, and it ends the moment either one stops.\n\n\n\n:::tip\nMerge grows, overlap shrinks. Merge uses `[min, max]` and covers both intervals. Overlap uses `[max, min]` and keeps only the part both share.\n:::\n\n## Sort by Start first\n\nLeft unsorted, checking a list of intervals means comparing every pair, which is `O(n^2)`. Sorting the intervals by start time removes that cost. Once the list is in start order, any interval can only overlap ones that come after it and begin before it ends, so a single left-to-right pass is enough to merge, count, or detect collisions.\n\nSorting by start is the standard first line of almost every interval solution, and the problems below all lean on it.\n\nThat gives the template every interval problem in this section builds on: sort by start, then sweep once, comparing each interval against the last one you kept.\n\n\n:::note[Input]\n`[[1,3], [8,10], [2,6], [4,5], [10,12]]`\n:::\n\n:::note[After sort]\n`[[1,3], [2,6], [4,5], [8,10], [10,12]]`\n:::\n\n```python\n# Time: O(n log n)\n# Space: O(n)\ndef interval_pattern(intervals):\n # 1. Sort by start time\n intervals.sort(key=lambda x: x[0])\n result = []\n for start, end in intervals:\n # 2. Overlap with the last kept interval? start <= last end\n if result and start <= result[-1][1]:\n # Merge: extend the end of the last interval\n result[-1][1] = max(result[-1][1], end)\n else:\n # 3. No overlap: keep this interval as-is\n result.append([start, end])\n return result\n```\n\n**Show trace table**\n\n| i | `[start, end]` | `result[-1]` | `start <= result[-1][1]`? | Action | `result` after |\n|---|---|---|---|---|---|\n| 0 | `[1,3]` | — | — (empty) | append | `[[1,3]]` |\n| 1 | `[2,6]` | `[1,3]` | `2 <= 3` ✅ | end = `max(3,6)` = 6 | `[[1,6]]` |\n| 2 | `[4,5]` | `[1,6]` | `4 <= 6` ✅ | end = `max(6,5)` = 6 | `[[1,6]]` |\n| 3 | `[8,10]` | `[1,6]` | `8 <= 6` ❌ | append | `[[1,6], [8,10]]` |\n| 4 | `[10,12]` | `[8,10]` | `10 <= 10` ✅ | end = `max(10,12)` = 12 | `[[1,6], [8,12]]` |\n\n\n:::tip[Output]\n`[[1,6], [8,12]]`\n - **Row 2:** `[4,5]` sits inside `[1,6]`. Setting `end = 5` without `max` would cut the interval short. `max` keeps 6.\n - **Row 4:** `[8,10]` and `[10,12]` only touch at 10. The `<=` check merges them. With `<`, you would get two intervals.\n:::\n\n\n## Insert into a sorted list\n\nSometimes the list is already sorted and has no overlaps, and you must add one new interval. You could append it, sort, and run the template. That works, but it pays `O(n log n)` for a sort you do not need.\n\nA sorted list with no overlaps splits into three parts around the new interval, in this order:\n\n1. Intervals that end **before** the new one starts. Keep them as they are.\n2. Intervals that **overlap** the new one. Merge each into it with `[min, max]`.\n3. Intervals that start **after** the new one ends. Keep them as they are.\n\nWalk the list once and handle each part in turn. No sort, so the cost drops to `O(n)`.\n\n:::note[Intervals]\n`[[1,2], [3,5], [6,7], [8,10], [12,16]]`\n:::\n\n:::note[New interval]\n`[4,8]`\n:::\n\n```python\n# Time: O(n)\n# Space: O(n)\ndef insert_interval(intervals, new_interval):\n result = []\n i, n = 0, len(intervals)\n start, end = new_interval\n\n # 1. Ends before new starts: keep\n while i < n and intervals[i][1] < start:\n result.append(intervals[i])\n i += 1\n\n # 2. Overlaps new: grow new to cover it\n while i < n and intervals[i][0] <= end:\n start = min(start, intervals[i][0])\n end = max(end, intervals[i][1])\n i += 1\n result.append([start, end])\n\n # 3. Starts after new ends: keep\n result.extend(intervals[i:])\n return result\n```\n\n**Show trace table**\n\n| i | interval | Part | Check | Action | `[start, end]` | `result` after |\n|---|---|---|---|---|---|---|\n| 0 | `[1,2]` | 1 | `2 < 4` ✅ | keep | `[4,8]` | `[[1,2]]` |\n| 1 | `[3,5]` | 1 | `5 < 4` ❌ | go to part 2 | `[4,8]` | `[[1,2]]` |\n| 1 | `[3,5]` | 2 | `3 <= 8` ✅ | start = `min(4,3)` = 3 | `[3,8]` | `[[1,2]]` |\n| 2 | `[6,7]` | 2 | `6 <= 8` ✅ | end = `max(8,7)` = 8 | `[3,8]` | `[[1,2]]` |\n| 3 | `[8,10]` | 2 | `8 <= 8` ✅ | end = `max(8,10)` = 10 | `[3,10]` | `[[1,2]]` |\n| 4 | `[12,16]` | 2 | `12 <= 10` ❌ | append new | `[3,10]` | `[[1,2], [3,10]]` |\n| 4 | `[12,16]` | 3 | — | extend rest | — | `[[1,2], [3,10], [12,16]]` |\n\n:::tip[Output]\n`[[1,2], [3,10], [12,16]]`\n - **Row i=1:** The same interval shows up twice. The part 1 check fails, so that loop stops, and part 2 reads the same interval again. `i` does not move.\n - **Row i=3:** `[8,10]` only touches `[3,8]` at 8. The `<=` merges them, since one shared point counts as an overlap.\n - **Part 1 uses `<`, part 2 uses `<=`:** An interval that ends exactly at `start` touches the new interval. It must fail part 1 so part 2 can merge it.\n:::\n\n:::warning\nIf the input is already sorted, do not sort again. Interviewers add \"sorted\" to the problem on purpose. Name the `O(n log n)` cost of re-sorting before they do, then offer the `O(n)` three-part walk.\n:::\n\n### Footnote\n\nThe positive overlap test comes straight from De Morgan's law. Negating `(a.end < b.start)` or `(b.end < a.start)` gives `(a.end >= b.start)` and `(b.end >= a.start)`, which is the `a.start <= b.end` and `b.start <= a.end` condition rewritten.\n","mdx":"---\ntitle: Intervals\ndescription: 'Ranges with a start and an end are sorted, and then merged, counted, or scheduled.'\nhidden: true\n---\n\nAn interval is a pair `[start, end]` marking a stretch on a line:\n- the minutes a meeting occupies\n- the range a task runs\n- the span a sensor was active\n\n**What interval problems ask you?**\n\n- Do any two collide?\n- What does the timeline look like once you merge everything that touches?\n- How many rooms do you need so no two meetings share one?\n\n:::tip\nEvery one of those questions reduces to a single primitive: deciding whether two intervals overlap, and if they do, how to merge them or what part they share. Once that primitive is clear, the rest of the pattern is bookkeeping.\n:::\n\n## When two intervals overlap\n\nIt is easier to state when two intervals do not overlap. Interval `a` sits entirely before interval `b` when `a` finishes before `b` begins, that is `a.end < b.start`. By symmetry they also miss each other when `b` ends before `a` starts. If neither of those holds, the intervals overlap.\n\n\n\nNegating `a.end < b.start` or `b.end < a.start` gives the positive test directly: two intervals overlap exactly when `a.start <= b.end` and `b.start <= a.end`. That pair of comparisons is the whole overlap check.\n\n## The Merged Region\n\nWhen two intervals overlap, you can merge them into one interval that covers both. Its start is the earlier of the two starts, and its end is the later of the two ends: `[min(a.start, b.start), max(a.end, b.end)]`. The merged interval begins as soon as either interval starts, and it ends only once both have stopped.\n\n\n\n## The Shared Region\n\nWhen two intervals overlap, the part they share is itself an interval. Its start is the later of the two starts, and its end is the earlier of the two ends: `[max(a.start, b.start), min(a.end, b.end)]`. The overlap begins only once both intervals have started, and it ends the moment either one stops.\n\n\n\n:::tip\nMerge grows, overlap shrinks. Merge uses `[min, max]` and covers both intervals. Overlap uses `[max, min]` and keeps only the part both share.\n:::\n\n## Sort by Start first\n\nLeft unsorted, checking a list of intervals means comparing every pair, which is `O(n^2)`. Sorting the intervals by start time removes that cost. Once the list is in start order, any interval can only overlap ones that come after it and begin before it ends, so a single left-to-right pass is enough to merge, count, or detect collisions.\n\nSorting by start is the standard first line of almost every interval solution, and the problems below all lean on it.\n\nThat gives the template every interval problem in this section builds on: sort by start, then sweep once, comparing each interval against the last one you kept.\n\n\n:::note[Input]\n`[[1,3], [8,10], [2,6], [4,5], [10,12]]`\n:::\n\n:::note[After sort]\n`[[1,3], [2,6], [4,5], [8,10], [10,12]]`\n:::\n\n```python\n# Time: O(n log n)\n# Space: O(n)\ndef interval_pattern(intervals):\n # 1. Sort by start time\n intervals.sort(key=lambda x: x[0])\n result = []\n for start, end in intervals:\n # 2. Overlap with the last kept interval? start <= last end\n if result and start <= result[-1][1]:\n # Merge: extend the end of the last interval\n result[-1][1] = max(result[-1][1], end)\n else:\n # 3. No overlap: keep this interval as-is\n result.append([start, end])\n return result\n```\n\n\n\n | i | `[start, end]` | `result[-1]` | `start <= result[-1][1]`? | Action | `result` after |\n |---|---|---|---|---|---|\n | 0 | `[1,3]` | — | — (empty) | append | `[[1,3]]` |\n | 1 | `[2,6]` | `[1,3]` | `2 <= 3` ✅ | end = `max(3,6)` = 6 | `[[1,6]]` |\n | 2 | `[4,5]` | `[1,6]` | `4 <= 6` ✅ | end = `max(6,5)` = 6 | `[[1,6]]` |\n | 3 | `[8,10]` | `[1,6]` | `8 <= 6` ❌ | append | `[[1,6], [8,10]]` |\n | 4 | `[10,12]` | `[8,10]` | `10 <= 10` ✅ | end = `max(10,12)` = 12 | `[[1,6], [8,12]]` |\n\n\n :::tip[Output]\n `[[1,6], [8,12]]`\n - **Row 2:** `[4,5]` sits inside `[1,6]`. Setting `end = 5` without `max` would cut the interval short. `max` keeps 6.\n - **Row 4:** `[8,10]` and `[10,12]` only touch at 10. The `<=` check merges them. With `<`, you would get two intervals.\n :::\n\n \n\n\n## Insert into a sorted list\n\nSometimes the list is already sorted and has no overlaps, and you must add one new interval. You could append it, sort, and run the template. That works, but it pays `O(n log n)` for a sort you do not need.\n\nA sorted list with no overlaps splits into three parts around the new interval, in this order:\n\n1. Intervals that end **before** the new one starts. Keep them as they are.\n2. Intervals that **overlap** the new one. Merge each into it with `[min, max]`.\n3. Intervals that start **after** the new one ends. Keep them as they are.\n\nWalk the list once and handle each part in turn. No sort, so the cost drops to `O(n)`.\n\n:::note[Intervals]\n`[[1,2], [3,5], [6,7], [8,10], [12,16]]`\n:::\n\n:::note[New interval]\n`[4,8]`\n:::\n\n```python\n# Time: O(n)\n# Space: O(n)\ndef insert_interval(intervals, new_interval):\n result = []\n i, n = 0, len(intervals)\n start, end = new_interval\n\n # 1. Ends before new starts: keep\n while i < n and intervals[i][1] < start:\n result.append(intervals[i])\n i += 1\n\n # 2. Overlaps new: grow new to cover it\n while i < n and intervals[i][0] <= end:\n start = min(start, intervals[i][0])\n end = max(end, intervals[i][1])\n i += 1\n result.append([start, end])\n\n # 3. Starts after new ends: keep\n result.extend(intervals[i:])\n return result\n```\n\n\n\n\n | i | interval | Part | Check | Action | `[start, end]` | `result` after |\n |---|---|---|---|---|---|---|\n | 0 | `[1,2]` | 1 | `2 < 4` ✅ | keep | `[4,8]` | `[[1,2]]` |\n | 1 | `[3,5]` | 1 | `5 < 4` ❌ | go to part 2 | `[4,8]` | `[[1,2]]` |\n | 1 | `[3,5]` | 2 | `3 <= 8` ✅ | start = `min(4,3)` = 3 | `[3,8]` | `[[1,2]]` |\n | 2 | `[6,7]` | 2 | `6 <= 8` ✅ | end = `max(8,7)` = 8 | `[3,8]` | `[[1,2]]` |\n | 3 | `[8,10]` | 2 | `8 <= 8` ✅ | end = `max(8,10)` = 10 | `[3,10]` | `[[1,2]]` |\n | 4 | `[12,16]` | 2 | `12 <= 10` ❌ | append new | `[3,10]` | `[[1,2], [3,10]]` |\n | 4 | `[12,16]` | 3 | — | extend rest | — | `[[1,2], [3,10], [12,16]]` |\n\n :::tip[Output]\n `[[1,2], [3,10], [12,16]]`\n - **Row i=1:** The same interval shows up twice. The part 1 check fails, so that loop stops, and part 2 reads the same interval again. `i` does not move.\n - **Row i=3:** `[8,10]` only touches `[3,8]` at 8. The `<=` merges them, since one shared point counts as an overlap.\n - **Part 1 uses `<`, part 2 uses `<=`:** An interval that ends exactly at `start` touches the new interval. It must fail part 1 so part 2 can merge it.\n :::\n\n \n\n:::warning\nIf the input is already sorted, do not sort again. Interviewers add \"sorted\" to the problem on purpose. Name the `O(n log n)` cost of re-sorting before they do, then offer the `O(n)` three-part walk.\n:::\n\n### Footnote\n\nThe positive overlap test comes straight from De Morgan's law. Negating `(a.end < b.start)` or `(b.end < a.start)` gives `(a.end >= b.start)` and `(b.end >= a.start)`, which is the `a.start <= b.end` and `b.start <= a.end` condition rewritten.\n"},"/intervals/insert-interval":{"mdx":"---\ntitle: '57. Insert Interval'\ndescription: You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the i^th interval and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\nissue: \"https://github.com/prdlk/leetcode/issues/181\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 39.1 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 24.1 }\nsidebar:\n label: 'Insert Interval'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `intervals = [[1,3],[6,9]], newInterval = [2,5]`\n- Output: `[[1,5],[6,9]]`\n\n### Example 2:\n- Input: `intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]`\n- Output: `[[1,2],[3,10],[12,16]]`\n- Explanation: Because the new interval `[4,8]` overlaps with [3,5],[6,7],[8,10].\n\n### Constraints:\n\n- `0 <= intervals.length <= 10^4`\n- `intervals[i].length == 2`\n- `0 <= starti <= endi <= 10^5`\n- intervals is sorted by starti in ascending order.\n- `newInterval.length == 2`\n- `0 <= start <= end <= 10^5`\n\n## Solution\n\n```py\nclass Solution:\n def insert(\n self, intervals: list[list[int]], newInterval: list[int]\n ) -> list[list[int]]:\n result = []\n i, n = 0, len(intervals)\n start, end = newInterval\n\n # Everything before\n while i < n and intervals[i][1] < start:\n result.append(intervals[i])\n i += 1\n\n # Everything merged\n while i < n and intervals[i][0] <= end:\n start = min(start, intervals[i][0])\n end = max(end, intervals[i][1])\n i += 1\n result.append([start, end])\n\n # Everything after\n result.extend(intervals[i:])\n return result\n```\n"},"/intervals/merge-intervals":{"mdx":"---\ntitle: '56. Merge Intervals'\ndescription: Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Sorting\", slug: \"sorting\" }\n - { name: \"Quicksort\", slug: \"quicksort\" }\nissue: \"https://github.com/prdlk/leetcode/issues/182\"\ncompanies:\n - { name: \"Palo Alto Networks\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 83.1 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 75.8 }\n - { name: \"Infosys\", period: \"Thirty Days\", frequency: 68.6 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 36.7 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 100 }\n - { name: \"Coupang\", period: \"Three Months\", frequency: 100 }\n - { name: \"IBM\", period: \"Three Months\", frequency: 100 }\n - { name: \"MongoDB\", period: \"Three Months\", frequency: 100 }\n - { name: \"Atlassian\", period: \"Three Months\", frequency: 89.6 }\n - { name: \"Salesforce\", period: \"Three Months\", frequency: 88 }\n - { name: \"Yandex\", period: \"Three Months\", frequency: 86.7 }\n - { name: \"Visa\", period: \"Three Months\", frequency: 80.9 }\n - { name: \"Goldman Sachs\", period: \"Three Months\", frequency: 57 }\n - { name: \"American Express\", period: \"Six Months\", frequency: 100 }\n - { name: \"Expedia\", period: \"Six Months\", frequency: 100 }\n - { name: \"General Motors\", period: \"Six Months\", frequency: 100 }\n - { name: \"Nextdoor\", period: \"Six Months\", frequency: 100 }\n - { name: \"Ozon\", period: \"Six Months\", frequency: 100 }\n - { name: \"Ripple\", period: \"Six Months\", frequency: 100 }\n - { name: \"Tesco\", period: \"Six Months\", frequency: 100 }\n - { name: \"Morgan Stanley\", period: \"Six Months\", frequency: 83.1 }\n - { name: \"Walmart Labs\", period: \"Six Months\", frequency: 81.4 }\n - { name: \"Tesla\", period: \"Six Months\", frequency: 80.9 }\n - { name: \"Zoho\", period: \"Six Months\", frequency: 79.6 }\n - { name: \"eBay\", period: \"Six Months\", frequency: 75.2 }\n - { name: \"TikTok\", period: \"Six Months\", frequency: 64.9 }\n - { name: \"Capital One\", period: \"Six Months\", frequency: 64.2 }\n - { name: \"Uber\", period: \"Six Months\", frequency: 55.2 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 54.1 }\nsidebar:\n label: 'Merge Intervals'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `intervals = [[1,3],[2,6],[8,10],[15,18]]`\n- Output: `[[1,6],[8,10],[15,18]]`\n- Explanation: Since `intervals [1,3]` and `[2,6]` overlap, merge them into `[1,6]`.\n\n### Example 2:\n- Input: `intervals = [[1,4],[4,5]]`\n- Output: `[[1,5]]`\n- Explanation: Intervals `[1,4]` and `[4,5]` are considered overlapping.\n\n### Example 3:\n- Input: `intervals = [[4,7],[1,4]]`\n- Output: `[[1,7]]`\n- Explanation: Intervals `[1,4]` and `[4,7]` are considered overlapping.\n\n### Constraints:\n\n- `1 <= intervals.length <= 10^4`\n- `intervals[i].length == 2`\n- `0 <= starti <= endi <= 10^4`\n\n## Solution\n\n```py\nclass Solution:\n def merge(self, intervals: list[list[int]]) -> list[list[int]]:\n intervals.sort(key=lambda x: x[0])\n result = []\n\n for start, end in intervals:\n if result and start <= result[-1][1]:\n result[-1][1] = max(result[-1][1], end)\n else:\n result.append([start, end])\n\n return result\n```\n"},"/intervals/non-overlapping-intervals":{"mdx":"---\ntitle: '435. Non-overlapping Intervals'\ndescription: Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\n - { name: \"Greedy\", slug: \"greedy\" }\n - { name: \"Sorting\", slug: \"sorting\" }\nissue: \"https://github.com/prdlk/leetcode/issues/183\"\ncompanies:\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 57.2 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Grammarly\", period: \"Three Months\", frequency: 100 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 28.9 }\n - { name: \"IBM\", period: \"Six Months\", frequency: 60.1 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\nsidebar:\n label: 'Non-overlapping Intervals'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `intervals = [[1,2],[2,3],[3,4],[1,3]]`\n- Output: `1`\n- Explanation: `[1,3]` can be removed and the rest of the `intervals` are non-overlapping.\n\n### Example 2:\n- Input: `intervals = [[1,2],[1,2],[1,2]]`\n- Output: `2`\n- Explanation: You need to remove two `[1,2]` to make the rest of the `intervals` non-overlapping.\n\n### Example 3:\n- Input: `intervals = [[1,2],[2,3]]`\n- Output: `0`\n- Explanation: You don't need to remove any of the `intervals` since they're already non-overlapping.\n\n### Constraints:\n\n- `1 <= intervals.length <= 10^5`\n- `intervals[i].length == 2`\n- `-5 * 10^4 <= starti < endi <= 5 * 10^4`\n\n## Solution\n\n```py\nclass Solution:\n def eraseOverlapIntervals(self, intervals: list[list[int]]) -> int:\n intervals.sort(key=lambda x: x[0])\n result = []\n\n for start, end in intervals:\n # Use '<' to avoid intervals which touch\n if result and start < result[-1][1]:\n result[-1][1] = min(result[-1][1], end)\n else:\n result.append([start, end])\n\n # Difference between original and result is to remove\n return len(intervals) - len(result)\n```\n"},"/keywords":{"mdx":"---\ntitle: Keywords\ndescription: Coding problems rarely tell you which algorithm to use, but the wording usually leaks it. \"K-th largest\" hints at a heap. \"Shortest path\" in an unweighted graph hints at BFS.\nicon: map\n---\n\nEach entry below names the keyword, the algorithm it points to, and a one-line reason. Where a keyword has more than one possible technique, each option is listed with the sub-signal that picks it. Techniques you haven't studied yet are covered later in the course.\n\n## \"Top K\"\n\nHeap — track the K-item boundary in O(n log K) instead of sorting all n. K Closest Points\n\n## \"Sorted\" / \"rotated sorted\" / \"in O(log n)\"\n\nBinary Search — halve the search space at each step using order. Works on any monotonic predicate, not just literal arrays. Binary Search\n\n## \"How many ways...\"\n\nCounting distinct outcomes — choose by what the input looks like.\n\n- DFS, when choices form a tree of decisions. Decode Ways\n- DP, when subproblems overlap on a grid or sequence. Robot Paths\n\n## \"Substring\"\n\nSliding Window — contiguous slices extend and shrink in O(1) at each step. Longest Substring Without Repeating Characters\n\n## \"Prefix\" / \"autocomplete\" / \"dictionary lookup\" / \"starts with\"\n\nTrie — share storage for common prefixes; lookup runs in O(length of word) regardless of dictionary size. Implement Trie\n\n## \"Palindrome\"\n\nPalindromes mirror around a center — choose by whether you're verifying or constructing.\n\n- Two Pointers, to verify a single string. Valid Palindrome\n- DFS, to enumerate every valid partition. Palindrome Partitioning\n- DP, to minimize cuts or count partitions. Palindrome Partitioning II\n\n## \"Tree\"\n\nOne question decides everything: do you care about depth, or just need to visit every node?\n\n- BFS, for level-order, shallowest depth, or \"right-side view\". Binary Tree Level-Order Traversal\n- DFS, for sums, heights, paths — anything not level-related. Max Depth\n\n## \"Parentheses\"\n\nStack — brackets close in reverse order, last opened first closed (LIFO). Valid Parentheses\n\n## \"Next greater\" / \"next smaller\" / \"warmer day\"\n\nMonotonic Stack — keep candidates in stack order so the answer for each element pops out in O(1) amortized. Next Greater Element II\n\n## \"Subarray\"\n\nContiguous slices, but the right tool depends on what you're computing.\n\n- Sliding Window, for fixed size or a monotonic constraint. Subarray Sum Fixed\n- Prefix Sum, for range-sum queries. Subarray Sum\n- Hashmap of prefix sums, for sum equals a target. Continuous Subarray Sum\n\n## \"Max subarray\"\n\nGreedy (Kadane's) — at each index, extend the run or restart; the choice is local. Kadane's Algorithm\n\n## \"Two sum / K sum\"\n\nTwo Pointers on a sorted array — each move strictly raises or lowers the sum. Two Sum\n\n## \"Max/longest sequence\"\n\n\"Longest\" usually means each position depends on earlier ones — except when a fixed window is involved.\n\n- DP / DFS with memoization, when a position depends on earlier positions. Longest Increasing Subsequence\n- Monotonic Deque, for max within a sliding window. Sliding Window Maximum\n\n## \"Minimum/Shortest\"\n\nThree very different problems wear the same word.\n\n- BFS, for fewest edges in an unweighted graph. Shortest Path\n- Dijkstra, for shortest path with non-negative edge weights. Dijkstra's Algorithm\n- DP / DFS, for cumulative cost on a grid or sequence. Minimal Path Sum\n\n## \"Partition/split\"\n\nDFS — each split point is a recursive choice; memoize if suffixes repeat. Decode Ways\n\n## \"Subsequence\"\n\nDP / DFS with memoization — pick-or-skip at each index, with overlapping subproblems. Longest Increasing Subsequence\n\n## \"All combinations / permutations / subsets\" / \"generate every\"\n\nBacktracking — DFS that records a partial solution, recurses, then undoes the choice on the way back up. Permutations, Subsets\n\n## \"Matrix\"\n\nA grid is a graph. Connectivity questions are BFS/DFS; optimization questions are DP.\n\n- BFS / DFS, for connectivity, flood-fill, or region counting. Flood Fill, Number of Islands\n- DP, for optimization where each cell builds on neighbors. Maximal Square\n\n## \"Jump\"\n\nGreedy when local-optimal is provably global; DP otherwise.\n\n- Greedy, when \"always reach as far as possible\" works (typically reachability). Jump Game\n- DP, when counting jumps or greedy fails. Jump Game II\n\n## \"Game\"\n\nDP on game states — \"can the player to move force a win?\" recurses with overlap. Divisor Game\n\n## \"Connected component\"\n\nUnion Find — near-O(1) union and find beat re-running BFS after each edge. Use BFS or DFS instead when the graph is built once and you only traverse a few times. Number of Connected Components\n\n## \"Schedule\" / \"prerequisites\" / \"build order\" / \"course\"\n\nTopological Sort (Kahn's BFS or DFS post-order) — directed dependencies must be linearized so every edge points forward. Detects cycles as a side effect. Course Schedule\n\n## \"Transitive relationship\"\n\nIf A relates to B and B relates to C means A relates to C, the relation is a graph in disguise. Items become nodes, relations become edges, and \"are these two related?\" becomes \"are they in the same component?\"\n\n- BFS, to answer a single query by traversing on the fly. Word Ladder\n- BFS or Union Find, for many queries on a fixed relation set. Sentence Similarity\n- BFS or Union Find, for numbers with divisional relationships. Evaluate Division\n\n## \"Interval\"\n\nGreedy after sorting — by start (merge) or by end (fit-in), then one pass. Interval Pattern\n"},"/linked-list":{"mdx":"---\ntitle: Linked List\ndescription: 'Each node points to the next node, and the pointers give the path through the data.'\nhidden: true\n---\n"},"/linked-list/linked-list-cycle":{"mdx":"---\ntitle: '141. Linked List Cycle'\ndescription: Given head, the head of a linked list, determine if the linked list has a cycle in it\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Linked List\", slug: \"linked-list\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"Floyd's Cycle Finding Algorithm\", slug: \"floyds-cycle-finding-algorithm\" }\nvideo: \"gBTe7lFR3vc\"\nissue: \"https://github.com/prdlk/leetcode/issues/79\"\ncompanies:\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Nvidia\", period: \"Three Months\", frequency: 87.7 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\nsidebar:\n label: 'Linked List Cycle'\n badge: 'Easy'\n---\n\n::::warning\nCan you solve it using O(1) (i.e. constant) memory?\n::::\n\n### Example 1:\n- Input: `head = [3,2,0,-4], pos = 1`\n- Output: `true`\n- Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).\n\n### Example 2:\n- Input: `head = [1,2], pos = 0`\n- Output: `true`\n- Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.\n\n### Example 3:\n- Input: `head = [1], pos = -1`\n- Output: `false`\n- Explanation: There is no cycle in the linked list.\n\n### Constraints:\n\n- The number of the nodes in the list is in the range [0, 10^4].\n- `-10^5 <= Node.val <= 10^5`\n- pos is -1 or a valid index in the linked-list.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"hasCycle(head)\"]) --> I[\"fast = slow = head\"]\n I --> W{\"fast and fast.next?\"}\n W -- no --> E([\"return False — ran off the end, no cycle\"])\n W -- yes --> A[\"fast = fast.next.next — two steps\"]\n A --> B[\"slow = slow.next — one step\"]\n B --> Q{\"fast is slow?\"}\n Q -- yes --> R([\"return True — the gap closed, so it loops\"])\n Q -- no --> W\n```\n\n## Solution\n\n```py\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, x):\n# self.val = x\n# self.next = None\n\n\nclass Solution:\n def hasCycle(self, head: Optional[ListNode]) -> bool:\n fast = slow = head\n\n while fast and fast.next:\n fast = fast.next.next\n slow = slow.next\n if fast is slow:\n return True\n return False\n```\n"},"/linked-list/merge-two-sorted-lists":{"mdx":"---\ntitle: '21. Merge Two Sorted Lists'\ndescription: You are given the heads of two sorted linked lists list1 and list2\nicon: dot\ntopics:\n - { name: \"Linked List\", slug: \"linked-list\" }\n - { name: \"Recursion\", slug: \"recursion\" }\nvideo: \"XIdigk956u0\"\nissue: \"https://github.com/prdlk/leetcode/issues/78\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 57.2 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Accenture\", period: \"Six Months\", frequency: 75 }\nsidebar:\n label: 'Merge Two Sorted Lists'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `list1 = [1,2,4], list2 = [1,3,4]`\n- Output: `[1,1,2,3,4,4]`\n\n### Example 2:\n- Input: `list1 = [], list2 = []`\n- Output: `[]`\n\n### Example 3:\n- Input: `list1 = [], list2 = [0]`\n- Output: `[0]`\n\n### Constraints:\n\n- The number of nodes in both lists is in the range [0, 50].\n- `-100 <= Node.val <= 100`\n- Both list1 and list2 are sorted in non-decreasing order.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"mergeTwoLists(list1, list2)\"]) --> G1{\"list1 empty?\"}\n G1 -- yes --> X1([\"return list2\"])\n G1 -- no --> G2{\"list2 empty?\"}\n G2 -- yes --> X2([\"return list1\"])\n G2 -- no --> H[\"head = the smaller first node; advance that list\"]\n H --> C[\"current = head\"]\n C --> W{\"list1 and list2 both non-empty?\"}\n W -- yes --> P[\"current.next = the smaller node; advance that list; current = current.next\"]\n P --> W\n W -- no --> T[\"current.next = list1 or list2 — append the leftover tail\"]\n T --> E([\"return head\"])\n```\n\n## Solution\n\n```py\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def mergeTwoLists(\n self, list1: Optional[ListNode], list2: Optional[ListNode]\n ) -> Optional[ListNode]:\n\n if not list1:\n return list2\n if not list2:\n return list1\n\n if list1.val < list2.val:\n head = list1\n list1 = list1.next\n else:\n head = list2\n list2 = list2.next\n\n current = head\n while list1 and list2:\n if list1.val < list2.val:\n current.next = list1\n list1 = list1.next\n else:\n current.next = list2\n list2 = list2.next\n current = current.next\n\n current.next = list1 or list2\n return head\n```\n"},"/linked-list/middle-of-the-linked-list":{"mdx":"---\ntitle: '876. Middle of the Linked List'\ndescription: Given the head of a singly linked list, return the middle node of the linked list\nicon: dot\ntopics:\n - { name: \"Linked List\", slug: \"linked-list\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\nissue: \"https://github.com/prdlk/leetcode/issues/82\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 50.2 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 49.8 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 39.1 }\n - { name: \"IBM\", period: \"Six Months\", frequency: 60.1 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 37.9 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\nsidebar:\n label: 'Middle of the Linked List'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `head = [1,2,3,4,5]`\n- Output: `[3,4,5]`\n- Explanation: The middle node of the list is node `3`.\n\n### Example 2:\n- Input: `head = [1,2,3,4,5,6]`\n- Output: `[4,5,6]`\n- Explanation: Since the list has two middle nodes with values `3` and `4`, we return the second one.\n\n### Constraints:\n\n- The number of nodes in the list is in the range [1, 100].\n- `1 <= Node.val <= 100`\n\n## Solution\n\n```py\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def middleNode(self, head: ListNode | None) -> ListNode | None:\n slow = fast = head\n while fast and fast.next:\n fast = fast.next.next\n slow = slow.next\n\n return slow\n```\n"},"/linked-list/remove-duplicates-from-sorted-list-ii":{"mdx":"---\ntitle: '82. Remove Duplicates from Sorted List II'\ndescription: You are given the head of a sorted linked list\nicon: dot\ntopics:\n - { name: \"Linked List\", slug: \"linked-list\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\nissue: \"https://github.com/prdlk/leetcode/issues/84\"\ncompanies:\n - { name: \"Amazon\", period: \"Three Months\", frequency: 34.6 }\n - { name: \"Google\", period: \"Three Months\", frequency: 23.9 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 34.2 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 24.5 }\nsidebar:\n label: 'Remove Duplicates from Sorted List II'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `head = [1,2,3,3,4,4,5]`\n- Output: `[1,2,5]`\n\n### Example 2:\n- Input: `head = [1,1,1,2,3]`\n- Output: `[2,3]`\n\n### Constraints:\n\n- The number of nodes in the list is in the range [0, 300].\n- `-100 <= Node.val <= 100`\n- The list is guaranteed to be sorted in ascending order.\n\n## Solution\n\n```py\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def deleteDuplicates(self, head: ListNode | None) -> ListNode | None:\n sentinel = ListNode(0, head)\n pred = sentinel\n\n while head:\n if head.next and head.val == head.next.val:\n while head.next and head.val == head.next.val:\n head = head.next\n\n pred.next = head.next\n else:\n pred = pred.next\n\n head = head.next\n\n return sentinel.next\n```\n"},"/linked-list/remove-nth-node-from-end-of-list":{"mdx":"---\ntitle: '19. Remove Nth Node From End of List'\ndescription: Given the head of a linked list, remove the n^th node from the end of the list and return its head\nicon: dot\ntopics:\n - { name: \"Linked List\", slug: \"linked-list\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\nissue: \"https://github.com/prdlk/leetcode/issues/80\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 51.1 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 46.8 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 43.3 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 24.1 }\nsidebar:\n label: 'Remove Nth Node From End of List'\n badge: 'Medium'\n---\n\n::::warning\nCould you do this in one pass?\n::::\n\n### Example 1:\n- Input: `head = [1,2,3,4,5], n = 2`\n- Output: `[1,2,3,5]`\n\n### Example 2:\n- Input: `head = [1], n = 1`\n- Output: `[]`\n\n### Example 3:\n- Input: `head = [1,2], n = 1`\n- Output: `[1]`\n\n### Constraints:\n\n- The number of nodes in the list is sz.\n- `1 <= sz <= 30`\n- `0 <= Node.val <= 100`\n- `1 <= n <= sz`\n\n## Solution\n\n```py\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def removeNthFromEnd(self, head: ListNode | None, n: int) -> ListNode | None:\n dummy = ListNode(0)\n dummy.next = head\n\n fast = dummy\n for _ in range(n):\n if fast.next is None:\n return head\n fast = fast.next\n\n slow = dummy\n while fast.next is not None:\n slow = slow.next\n fast = fast.next\n\n slow.next = slow.next.next\n\n return dummy.next\n```\n"},"/linked-list/reorder-list":{"mdx":"---\ntitle: '143. Reorder List'\ndescription: 'You are given the head of a singly linked-list. The list can be represented as:'\nicon: dot\ntopics:\n - { name: \"Linked List\", slug: \"linked-list\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"Stack\", slug: \"stack\" }\n - { name: \"Recursion\", slug: \"recursion\" }\nissue: \"https://github.com/prdlk/leetcode/issues/81\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 42.3 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 48.3 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 21.2 }\nsidebar:\n label: 'Reorder List'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `head = [1,2,3,4]`\n- Output: `[1,4,2,3]`\n\n### Example 2:\n- Input: `head = [1,2,3,4,5]`\n- Output: `[1,5,2,4,3]`\n\n### Constraints:\n\n- The number of nodes in the list is in the range [1, 5 * 10^4].\n- `1 <= Node.val <= 1000`\n\n## Solution\n\n```py\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def reorderList(self, head: ListNode | None) -> None:\n # Find middle\n slow, fast = head, head.next\n while fast and fast.next:\n slow = slow.next\n fast = fast.next.next\n\n # Reverse second half\n second = slow.next\n prev = slow.next = None\n while second:\n tmp = second.next\n second.next = prev\n prev = second\n second = tmp\n\n # merge two halfs\n first, second = head, prev\n while second:\n tmp1, tmp2 = first.next, second.next\n first.next = second\n second.next = tmp1\n first, second = tmp1, tmp2\n```\n"},"/linked-list/reverse-linked-list":{"mdx":"---\ntitle: '206. Reverse Linked List'\ndescription: Given the head of a singly linked list, reverse the list, and return the reversed list\nicon: dot\ntopics:\n - { name: \"Linked List\", slug: \"linked-list\" }\n - { name: \"Recursion\", slug: \"recursion\" }\nvideo: \"G0_I-ZF0S38\"\nissue: \"https://github.com/prdlk/leetcode/issues/77\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 50.2 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Nokia\", period: \"Three Months\", frequency: 100 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 66 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 57.2 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 78.4 }\nsidebar:\n label: 'Reverse Linked List'\n badge: 'Easy'\n---\n\n::::warning\nA linked list can be reversed either iteratively or recursively. Could you implement both?\n::::\n\n### Example 1:\n- Input: `head = [1,2,3,4,5]`\n- Output: `[5,4,3,2,1]`\n\n### Example 2:\n- Input: `head = [1,2]`\n- Output: `[2,1]`\n\n### Example 3:\n- Input: `head = []`\n- Output: `[]`\n\n### Constraints:\n\n- The number of nodes in the list is the range [0, 5000].\n- `-5000 <= Node.val <= 5000`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"reverseList(head)\"]) --> I[\"prev = None, curr = head\"]\n I --> W{\"curr?\"}\n W -- no --> E([\"return prev — the old tail is the new head\"])\n W -- yes --> A[\"next_ = curr.next — save it before overwriting\"]\n A --> B[\"curr.next = prev — flip the link\"]\n B --> C[\"prev = curr; curr = next_ — step forward\"]\n C --> W\n```\n\n## Solution\n\n```py\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:\n prev = None\n curr = head\n while curr:\n next_ = curr.next\n curr.next = prev\n prev = curr\n curr = next_\n return prev\n```\n"},"/linked-list/reverse-linked-list-ii":{"mdx":"---\ntitle: '92. Reverse Linked List II'\ndescription: Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list\nicon: dot\ntopics:\n - { name: \"Linked List\", slug: \"linked-list\" }\nissue: \"https://github.com/prdlk/leetcode/issues/83\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 83.1 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Google\", period: \"Three Months\", frequency: 28.8 }\nsidebar:\n label: 'Reverse Linked List II'\n badge: 'Medium'\n---\n\n::::warning\nCould you do it in one pass?\n::::\n\n### Example 1:\n- Input: `head = [1,2,3,4,5], left = 2, right = 4`\n- Output: `[1,4,3,2,5]`\n\n### Example 2:\n- Input: `head = [5], left = 1, right = 1`\n- Output: `[5]`\n\n### Constraints:\n\n- The number of nodes in the list is n.\n- `1 <= n <= 500`\n- `-500 <= Node.val <= 500`\n- `1 <= left <= right <= n`\n\n## Solution\n\n```py\n# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\nclass Solution:\n def reverseBetween(\n self, head: ListNode | None, left: int, right: int\n ) -> ListNode | None:\n cur, prev = head, None\n while left > 1:\n prev = cur\n cur = cur.next\n left, right = left - 1, right - 1\n\n tail, con = cur, prev\n while right:\n third = cur.next\n cur.next = prev\n prev = cur\n cur = third\n right -= 1\n\n if con:\n con.next = prev\n else:\n head = prev\n\n tail.next = cur\n return head\n```\n"},"/matrix-index-math":{"mdx":"---\ntitle: Matrix Index Math\ndescription: 'A two-dimensional grid is read as a flat array through a formula, and the neighbors are found by arithmetic.'\nhidden: true\n---\n"},"/matrix-index-math/rotate-image":{"mdx":"---\ntitle: '48. Rotate Image'\ndescription: You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise)\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Math\", slug: \"math\" }\n - { name: \"Matrix\", slug: \"matrix\" }\nvideo: \"fMSJSS7eO1w\"\nissue: \"https://github.com/prdlk/leetcode/issues/91\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 83.1 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Google\", period: \"Three Months\", frequency: 51.1 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 43.3 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 64.8 }\n - { name: \"Capital One\", period: \"Six Months\", frequency: 64.2 }\n - { name: \"IBM\", period: \"Six Months\", frequency: 60.1 }\nsidebar:\n label: 'Rotate Image'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `matrix = [[1,2,3],[4,5,6],[7,8,9]]`\n- Output: `[[7,4,1],[8,5,2],[9,6,3]]`\n\n### Example 2:\n- Input: `matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]`\n- Output: `[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]`\n\n### Constraints:\n\n- `n == matrix.length == matrix[i].length`\n- `1 <= n <= 20`\n- `-1000 <= matrix[i][j] <= 1000`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"rotate(matrix)\"]) --> N[\"n = len(matrix)\"]\n N --> F{\"more r in range(n)?\"}\n F -- yes --> G{\"more c in range(r + 1, n)?\"}\n G -- yes --> T[\"swap matrix[r][c] and matrix[c][r] — transpose across the diagonal\"]\n T --> G\n G -- no --> F\n F -- no --> H{\"more r in range(n)?\"}\n H -- yes --> R[\"matrix[r].reverse() — transpose then mirror is a clockwise turn\"]\n R --> H\n H -- no --> E([\"return None — rotated in place\"])\n```\n\n## Solution\n\n```py\nclass Solution:\n def rotate(self, matrix: List[List[int]]) -> None:\n n = len(matrix)\n for r in range(n):\n for c in range(r + 1, n):\n matrix[r][c], matrix[c][r] = matrix[c][r], matrix[r][c]\n\n for r in range(n):\n matrix[r].reverse()\n```\n"},"/matrix-index-math/set-matrix-zeroes":{"mdx":"---\ntitle: '73. Set Matrix Zeroes'\ndescription: Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Matrix\", slug: \"matrix\" }\nvideo: \"T41rL0L3Pnw\"\nissue: \"https://github.com/prdlk/leetcode/issues/93\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 48.7 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 43.3 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"Google\", period: \"Three Months\", frequency: 32.7 }\n - { name: \"tcs\", period: \"Six Months\", frequency: 43.5 }\nsidebar:\n label: 'Set Matrix Zeroes'\n badge: 'Medium'\n---\n\n::::warning\nYou must do it in place.\n::::\n\n### Example 1:\n- Input: `matrix = [[1,1,1],[1,0,1],[1,1,1]]`\n- Output: `[[1,0,1],[0,0,0],[1,0,1]]`\n\n### Example 2:\n- Input: `matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]`\n- Output: `[[0,0,0,0],[0,4,5,0],[0,3,1,0]]`\n\n### Constraints:\n\n- `m == matrix.length`\n- `n == matrix[0].length`\n- `1 <= m, n <= 200`\n- `-2^31 <= matrix[i][j] <= 2^31 - 1`\n- A straightforward solution using O(mn) space is probably a bad idea.\n- A simple improvement uses O(m + n) space, but still not the best solution.\n- Could you devise a constant space solution?\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"setZeroes(matrix)\"]) --> I[\"m = len(matrix), n = len(matrix[0])\"]\n I --> Z[\"row0_zero = any(matrix[0][c] == 0), col0_zero = any(matrix[r][0] == 0) — remember before they become markers\"]\n Z --> F{\"more r in range(1, m)?\"}\n F -- yes --> G{\"more c in range(1, n)?\"}\n G -- yes --> Q{\"matrix[r][c] == 0?\"}\n Q -- yes --> M[\"matrix[r][0] = 0, matrix[0][c] = 0 — row 0 and column 0 are the marker lists\"]\n M --> G\n Q -- no --> G\n G -- no --> F\n F -- no --> H{\"more r in range(1, m)?\"}\n H -- yes --> J{\"more c in range(1, n)?\"}\n J -- yes --> K{\"matrix[r][0] == 0 or matrix[0][c] == 0?\"}\n K -- yes --> W[\"matrix[r][c] = 0\"]\n W --> J\n K -- no --> J\n J -- no --> H\n H -- no --> A{\"row0_zero?\"}\n A -- yes --> B[\"matrix[0][c] = 0 for c in range(n)\"]\n B --> C{\"col0_zero?\"}\n A -- no --> C\n C -- yes --> D[\"matrix[r][0] = 0 for r in range(m)\"]\n D --> E([\"return None — zeroed in place\"])\n C -- no --> E\n```\n\n## Solution\n\n```py\nclass Solution:\n def setZeroes(self, matrix: List[List[int]]) -> None:\n \"\"\"\n Do not return anything, modify matrix in-place instead.\n \"\"\"\n m, n = len(matrix), len(matrix[0])\n\n # 1. remember if row 0 or column 0 have their own zeros\n row0_zero = any(matrix[0][c] == 0 for c in range(n))\n col0_zero = any(matrix[r][0] == 0 for r in range(m))\n\n # 2. use row 0 and column 0 as marker lists\n for r in range(1, m):\n for c in range(1, n):\n if matrix[r][c] == 0:\n matrix[r][0] = 0\n matrix[0][c] = 0\n\n # 3. zero the inner cells from the markers\n for r in range(1, m):\n for c in range(1, n):\n if matrix[r][0] == 0 or matrix[0][c] == 0:\n matrix[r][c] = 0\n\n # 4. handle row 0 and column 0 last\n if row0_zero:\n for c in range(n):\n matrix[0][c] = 0\n if col0_zero:\n for r in range(m):\n matrix[r][0] = 0\n```\n"},"/matrix-index-math/spiral-matrix":{"mdx":"---\ntitle: '54. Spiral Matrix'\ndescription: Given an m x n matrix, return all elements of the matrix in spiral order\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Matrix\", slug: \"matrix\" }\n - { name: \"Simulation\", slug: \"simulation\" }\nvideo: \"BJnMZNwUk1M\"\nissue: \"https://github.com/prdlk/leetcode/issues/92\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 57.2 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 36.7 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 46 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Capital One\", period: \"Six Months\", frequency: 73.8 }\n - { name: \"tcs\", period: \"Six Months\", frequency: 69.7 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 64.8 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 56.2 }\n - { name: \"TikTok\", period: \"Six Months\", frequency: 55.2 }\nsidebar:\n label: 'Spiral Matrix'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `matrix = [[1,2,3],[4,5,6],[7,8,9]]`\n- Output: `[1,2,3,6,9,8,7,4,5]`\n\n### Example 2:\n- Input: `matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]`\n- Output: `[1,2,3,4,8,12,11,10,9,5,6,7]`\n\n### Constraints:\n\n- `m == matrix.length`\n- `n == matrix[i].length`\n- `1 <= m, n <= 10`\n- `-100 <= matrix[i][j] <= 100`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"spiralOrder(matrix)\"]) --> I[\"res = [], top = 0, bottom = len(matrix) - 1, left = 0, right = len(matrix[0]) - 1\"]\n I --> W{\"top <= bottom and left <= right?\"}\n W -- no --> E([\"return res\"])\n W -- yes --> T[\"append matrix[top][c] for c in left..right, then top += 1 — top row, shrink from above\"]\n T --> R[\"append matrix[r][right] for r in top..bottom, then right -= 1 — right column\"]\n R --> B{\"top <= bottom?\"}\n B -- yes --> C[\"append matrix[bottom][c] for c in right down to left, then bottom -= 1 — bottom row\"]\n C --> L{\"left <= right?\"}\n B -- no --> L\n L -- yes --> D[\"append matrix[r][left] for r in bottom down to top, then left += 1 — left column\"]\n D --> W\n L -- no --> W\n```\n\n## Solution\n\n```py\nclass Solution:\n def spiralOrder(self, matrix: List[List[int]]) -> List[int]:\n res = []\n top, bottom = 0, len(matrix) - 1\n left, right = 0, len(matrix[0]) - 1\n\n while top <= bottom and left <= right:\n # 1. Top Row\n for c in range(left, right + 1):\n res.append(matrix[top][c])\n top += 1\n\n # 2. Right Column\n for r in range(top, bottom + 1):\n res.append(matrix[r][right])\n right -= 1\n\n # 3. Bottom Row\n if top <= bottom:\n for c in range(right, left - 1, -1):\n res.append(matrix[bottom][c])\n bottom -= 1\n\n # 4. Left Column\n if left <= right:\n for r in range(bottom, top - 1, -1):\n res.append(matrix[r][left])\n left += 1\n\n return res\n```\n"},"/matrix-index-math/valid-sudoku":{"mdx":"---\ntitle: '36. Valid Sudoku'\ndescription: 'Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:'\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Matrix\", slug: \"matrix\" }\nissue: \"https://github.com/prdlk/leetcode/issues/94\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 69 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 36.8 }\n - { name: \"Google\", period: \"Three Months\", frequency: 23.9 }\n - { name: \"Confluent\", period: \"Six Months\", frequency: 100 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 32.7 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 32 }\nsidebar:\n label: 'Valid Sudoku'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `board = [[\"5\",\"3\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\"] ,[\"6\",\".\",\".\",\"1\",\"9\",\"5\",\".\",\".\",\".\"] ,[\".\",\"9\",\"8\",\".\",\".\",\".\",\".\",\"6\",\".\"] ,[\"8\",\".\",\".\",\".\",\"6\",\".\",\".\",\".\",\"3\"] ,[\"4\",\".\",\".\",\"8\",\".\",\"3\",\".\",\".\",\"1\"] ,[\"7\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\",\"6\"] ,[\".\",\"6\",\".\",\".\",\".\",\".\",\"2\",\"8\",\".\"] ,[\".\",\".\",\".\",\"4\",\"1\",\"9\",\".\",\".\",\"5\"] ,[\".\",\".\",\".\",\".\",\"8\",\".\",\".\",\"7\",\"9\"]]`\n- Output: `true`\n\n### Example 2:\n- Input: `board = [[\"8\",\"3\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\"] ,[\"6\",\".\",\".\",\"1\",\"9\",\"5\",\".\",\".\",\".\"] ,[\".\",\"9\",\"8\",\".\",\".\",\".\",\".\",\"6\",\".\"] ,[\"8\",\".\",\".\",\".\",\"6\",\".\",\".\",\".\",\"3\"] ,[\"4\",\".\",\".\",\"8\",\".\",\"3\",\".\",\".\",\"1\"] ,[\"7\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\",\"6\"] ,[\".\",\"6\",\".\",\".\",\".\",\".\",\"2\",\"8\",\".\"] ,[\".\",\".\",\".\",\"4\",\"1\",\"9\",\".\",\".\",\"5\"] ,[\".\",\".\",\".\",\".\",\"8\",\".\",\".\",\"7\",\"9\"]]`\n- Output: `false`\n- Explanation: Same as Example `1`, except with the `5` in the top left corner being modified to `8`. Since there are two 8's in the top left 3x3 sub-box, it is invalid.\n\n### Constraints:\n\n- `board.length == 9`\n- `board[i].length == 9`\n- board[i][j] is a digit 1-9 or '.'.\n\n## Solution\n\n```py\nimport collections\n\n\nclass Solution:\n def isValidSudoku(self, board: list[list[str]]) -> bool:\n cols = collections.defaultdict(set)\n rows = collections.defaultdict(set)\n squares = collections.defaultdict(set) # (r/3, c/3)\n\n for r in range(9):\n for c in range(9):\n if board[r][c] == \".\":\n continue\n if (\n board[r][c] in rows[r]\n or board[r][c] in cols[c]\n or board[r][c] in squares[(r // 3, c // 3)]\n ):\n return False\n cols[c].add(board[r][c])\n rows[r].add(board[r][c])\n squares[(r // 3, c // 3)].add(board[r][c])\n\n return True\n```\n"},"/monotonic-stack":{"md":"---\ntitle: Monotonic Stack\ndescription: 'A stack keeps its items in sorted order, which finds the next larger or smaller item in one pass.'\nhidden: true\n---\n\n\n[Monotonic Stack](https://www.youtube.com/watch?v=Dq_ObZwTY_Q)\n\nA monotonic stack keeps its values in sorted order as you push elements. The order can be increasing or decreasing, depending on the problem.\n\nA decreasing monotonic stack is the most common version. Before pushing a new value `x`, we pop from the top while the top is smaller than or equal to `x`. After those pops, pushing `x` keeps the stack decreasing from bottom to top.\n\nThis push rule is the whole idea. A monotonic deque uses the same rule, but a deque also lets you remove old elements from the front, which is useful for sliding window problems.\n\n## Example Implementation\n\nThis implementation maintains a decreasing monotonic stack.\n\n```python\ndef mono_stack(insert_entries):\n stack = []\n for entry in insert_entries:\n # Pop smaller (or equal) values so stack stays decreasing.\n while stack and stack[-1] <= entry:\n stack.pop()\n # Do something with the popped item here\n stack.append(entry)\n```\n\n:::tip\nIn interview problems, the stack usually stores indices instead of values. You can read the value from the original array when needed, and the index gives you distance information such as **\"how many steps away is the next greater element.\"**\n:::\n\nFor an array `nums`, if index `j` pops when you process index `i`, then `i` is the first index to the right that satisfies the stack condition for `j`. In a next-greater-element problem, that means `nums[i]` is the next greater value for `nums[j]`.\n\n","mdx":"---\ntitle: Monotonic Stack\ndescription: 'A stack keeps its items in sorted order, which finds the next larger or smaller item in one pass.'\nhidden: true\n---\n\n\n \n\nA monotonic stack keeps its values in sorted order as you push elements. The order can be increasing or decreasing, depending on the problem.\n\nA decreasing monotonic stack is the most common version. Before pushing a new value `x`, we pop from the top while the top is smaller than or equal to `x`. After those pops, pushing `x` keeps the stack decreasing from bottom to top.\n\nThis push rule is the whole idea. A monotonic deque uses the same rule, but a deque also lets you remove old elements from the front, which is useful for sliding window problems.\n\n## Example Implementation\n\nThis implementation maintains a decreasing monotonic stack.\n\n```python\ndef mono_stack(insert_entries):\n stack = []\n for entry in insert_entries:\n # Pop smaller (or equal) values so stack stays decreasing.\n while stack and stack[-1] <= entry:\n stack.pop()\n # Do something with the popped item here\n stack.append(entry)\n```\n\n:::tip\nIn interview problems, the stack usually stores indices instead of values. You can read the value from the original array when needed, and the index gives you distance information such as **\"how many steps away is the next greater element.\"**\n:::\n\nFor an array `nums`, if index `j` pops when you process index `i`, then `i` is the first index to the right that satisfies the stack condition for `j`. In a next-greater-element problem, that means `nums[i]` is the next greater value for `nums[j]`.\n\n"},"/monotonic-stack/car-fleet":{"mdx":"---\ntitle: '853. Car Fleet'\ndescription: There are n cars at given miles away from the starting mile 0, traveling to reach the mile target\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Stack\", slug: \"stack\" }\n - { name: \"Sorting\", slug: \"sorting\" }\n - { name: \"Monotonic Stack\", slug: \"monotonic-stack\" }\nvideo: \"Pr6T-3yB9RM\"\nissue: \"https://github.com/prdlk/leetcode/issues/73\"\ncompanies:\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"Amazon\", period: \"Six Months\", frequency: 26 }\nsidebar:\n label: 'Car Fleet'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]`\n- Output: `3`\n- Explanation: • The cars starting at `10 (speed 2`) and `8 (speed 4`) become a fleet, meeting each other at `12`. The fleet forms at `target`. • The car starting at `0 (speed 1`) does not catch up to any other car, so it is a fleet by itself. • The cars starting at `5 (speed 1`) and `3 (speed 3`) become a fleet, meeting each other at `6`. The fleet moves at `speed 1` until it reaches `target`.\n\n### Example 2:\n- Input: `target = 10, position = [3], speed = [3]`\n- Output: `1`\n- Explanation: There is only one car, hence there is only one fleet.\n\n### Example 3:\n- Input: `target = 100, position = [0,2,4], speed = [4,2,1]`\n- Output: `1`\n- Explanation: • The cars starting at `0 (speed 4`) and `2 (speed 2`) become a fleet, meeting each other at `4`. The car starting at `4 (speed 1`) travels to `5`. • Then, the fleet at `4 (speed 2`) and the car at `position 5 (speed 1`) become one fleet, meeting each other at `6`. The fleet moves at `speed 1` until it reaches `target`.\n\n### Constraints:\n\n- `n == position.length == speed.length`\n- `1 <= n <= 10^5`\n- `0 < target <= 10^6`\n- `0 <= position[i] < target`\n- All the values of `position` are unique.\n- `0 < speed[i] <= 10^6`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"carFleet(target, position, speed)\"]) --> O[\"zip and sort by position, descending — nearest the target first\"]\n O --> I[\"stack = [] — arrival time of each fleet's leader\"]\n I --> L{\"more (pos, spd)?\"}\n L -- no --> E([\"return len(stack) — one entry per fleet\"])\n L -- yes --> T[\"time = (target - pos) / spd\"]\n T --> Q{\"stack empty or time > stack[-1]?\"}\n Q -- yes --> A[\"stack.append(time) — slower, so it starts a new fleet\"]\n Q -- no --> B[\"discard — it catches the car ahead and joins that fleet\"]\n A --> L\n B --> L\n```\n\n## Solution\n\n```py\nclass Solution:\n def carFleet(self, target: int, position: List[int], speed: List[int]) -> int:\n stack = []\n for pos, spd in sorted(zip(position, speed), reverse=True):\n time = (target - pos) / spd\n if not stack or time > stack[-1]:\n stack.append(time)\n\n return len(stack)\n```\n"},"/monotonic-stack/daily-temperatures":{"mdx":"---\ntitle: '739. Daily Temperatures'\ndescription: Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the i^th day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Stack\", slug: \"stack\" }\n - { name: \"Monotonic Stack\", slug: \"monotonic-stack\" }\nvideo: \"cTBiBSnjO3c\"\nissue: \"https://github.com/prdlk/leetcode/issues/72\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Chime\", period: \"Three Months\", frequency: 100 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 59.7 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 42.8 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Anduril\", period: \"Six Months\", frequency: 92.8 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 44.8 }\n - { name: \"tcs\", period: \"Six Months\", frequency: 43.5 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 42.5 }\nsidebar:\n label: 'Daily Temperatures'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `temperatures = [73,74,75,71,69,72,76,73]`\n- Output: `[1,1,4,2,1,1,0,0]`\n\n### Example 2:\n- Input: `temperatures = [30,40,50,60]`\n- Output: `[1,1,1,0]`\n\n### Example 3:\n- Input: `temperatures = [30,60,90]`\n- Output: `[1,1,0]`\n\n### Constraints:\n\n- `1 <= temperatures.length <= 10^5`\n- `30 <= temperatures[i] <= 100`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"dailyTemperatures(temperatures)\"]) --> I[\"ans = zeros(n), stack = [] — indices of days still waiting\"]\n I --> L{\"more (i, temp)?\"}\n L -- no --> E([\"return ans\"])\n L -- yes --> W{\"stack non-empty and temperatures[stack[-1]] < temp?\"}\n W -- yes --> P[\"prev = stack.pop(); ans[prev] = i - prev — today resolves that day\"]\n P --> W\n W -- no --> A[\"stack.append(i)\"]\n A --> L\n```\n\n## Solution\n\n```py\nclass Solution:\n def dailyTemperatures(self, temperatures: List[int]) -> List[int]:\n ans = [0] * len(temperatures)\n stack = []\n for i, temp in enumerate(temperatures):\n while stack and temperatures[stack[-1]] < temp:\n prev = stack.pop()\n ans[prev] = i - prev\n stack.append(i)\n return ans\n```\n"},"/monotonic-stack/largest-rectangle-in-histogram":{"mdx":"---\ntitle: '84. Largest Rectangle in Histogram'\ndescription: Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Stack\", slug: \"stack\" }\n - { name: \"Monotonic Stack\", slug: \"monotonic-stack\" }\n - { name: \"Range Minimum/Maximum Query\", slug: \"range-minimum-maximum-query\" }\nissue: \"https://github.com/prdlk/leetcode/issues/76\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Accenture\", period: \"Three Months\", frequency: 88.6 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 73.7 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 36.8 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\nsidebar:\n label: 'Largest Rectangle in Histogram'\n badge: 'Hard'\n---\n\n### Example 1:\n- Input: `heights = [2,1,5,6,2,3]`\n- Output: `10`\n- Explanation: The above is a histogram where width of each bar is `1`. The largest rectangle is shown in the red area, which has an area = `10` units.\n\n### Example 2:\n- Input: `heights = [2,4]`\n- Output: `4`\n\n### Constraints:\n\n- `1 <= heights.length <= 10^5`\n- `0 <= heights[i] <= 10^4`\n\n## Solution\n\n```py\nclass Solution:\n def largestRectangleArea(self, heights: list[int]) -> int:\n maxArea = 0\n stack = []\n\n for i, h in enumerate(heights):\n start = i\n while stack and stack[-1][1] > h:\n index, height = stack.pop()\n width = i - index\n maxArea = max(maxArea, height * width)\n start = index\n stack.append((start, h))\n\n for i, h in stack:\n maxArea = max(maxArea, h * (len(heights) - i))\n\n return maxArea\n```\n"},"/monotonic-stack/next-greater-element-i":{"mdx":"---\ntitle: '496. Next Greater Element I'\ndescription: The next greater element of some element x in an array is the first greater element that is to the right of x in the same array\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Stack\", slug: \"stack\" }\n - { name: \"Monotonic Stack\", slug: \"monotonic-stack\" }\nissue: \"https://github.com/prdlk/leetcode/issues/74\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 46.6 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 59.7 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 43.3 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"tcs\", period: \"Six Months\", frequency: 43.5 }\nsidebar:\n label: 'Next Greater Element I'\n badge: 'Easy'\n---\n\n::::warning\nCould you find an O(nums1.length + nums2.length) solution?\n::::\n\n### Example 1:\n- Input: `nums1 = [4,1,2], nums2 = [1,3,4,2]`\n- Output: `[-1,3,-1]`\n- Explanation:\n - The next greater element for each value of `nums1` is as follows:\n - `4` is underlined in `nums2 = [1,3,4,2]`. There is no next greater element, so the answer is `-1`.\n - `1` is underlined in `nums2 = [1,3,4,2]`. The next greater element is `3`.\n - `2` is underlined in `nums2 = [1,3,4,2]`. There is no next greater element, so the answer is `-1`.\n\n### Example 2:\n- Input: `nums1 = [2,4], nums2 = [1,2,3,4]`\n- Output: `[3,-1]`\n- Explanation:\n - The next greater element for each value of `nums1` is as follows:\n - `2` is underlined in `nums2 = [1,2,3,4]`. The next greater element is `3`.\n - `4` is underlined in `nums2 = [1,2,3,4]`. There is no next greater element, so the answer is `-1`.\n\n### Constraints:\n\n- `1 <= nums1.length <= nums2.length <= 1000`\n- `0 <= nums1[i], nums2[i] <= 10^4`\n- All integers in `nums1` and `nums2` are unique.\n- All the integers of `nums1` also appear in nums2.\n\n## Solution\n\n```py\nclass Solution:\n def nextGreaterElement(self, nums1: list[int], nums2: list[int]) -> list[int]:\n mono_stack = []\n next_greater = {}\n\n for cur in nums2:\n while mono_stack and cur > mono_stack[-1]:\n val = mono_stack.pop()\n next_greater[val] = cur\n mono_stack.append(cur)\n\n return [next_greater.get(x, -1) for x in nums1]\n```\n"},"/monotonic-stack/online-stock-span":{"mdx":"---\ntitle: '901. Online Stock Span'\ndescription: Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day\nicon: dot\ntopics:\n - { name: \"Stack\", slug: \"stack\" }\n - { name: \"Design\", slug: \"design\" }\n - { name: \"Monotonic Stack\", slug: \"monotonic-stack\" }\n - { name: \"Data Stream\", slug: \"data-stream\" }\nissue: \"https://github.com/prdlk/leetcode/issues/75\"\ncompanies:\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"Amazon\", period: \"Six Months\", frequency: 26 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\nsidebar:\n label: 'Online Stock Span'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: ``\n- Output: ``\n- Explanation: Input [\"StockSpanner\", \"next\", \"next\", \"next\", \"next\", \"next\", \"next\", \"next\"] `[[]`, `[100]`, `[80]`, `[60]`, `[70]`, `[60]`, `[75]`, [85]] Output [null, `1`, `1`, `1`, `2`, `1`, `4`, 6] Explanation StockSpanner stockSpanner = new StockSpanner(); stockSpanner.next(100); // return `1` stockSpanner.next(80); // return `1` stockSpanner.next(60); // return `1` stockSpanner.next(70); // return `2` stockSpanner.next(60); // return `1` stockSpanner.next(75); // return `4`, because the last `4` prices (including today's `price` of `75`) were less than or equal to today's `price`. stockSpanner.next(85); // return `6`\n\n### Constraints:\n\n- `1 <= price <= 10^5`\n- At most 10^4 calls will be made to next.\n\n## Solution\n\n```py\nclass StockSpanner:\n def __init__(self):\n self.stack = []\n\n def next(self, price: int) -> int:\n ans = 1\n while self.stack and self.stack[-1][0] <= price:\n ans += self.stack.pop()[1]\n\n self.stack.append([price, ans])\n return ans\n\n\n# Your StockSpanner object will be instantiated and called as such:\n# obj = StockSpanner()\n# param_1 = obj.next(price)\n```\n"},"/multi-d-grid-dp":{"mdx":"---\ntitle: Multi-D / Grid DP\ndescription: 'The result for each state is stored in a table with two or more dimensions, and each cell is computed from its neighbors.'\nhidden: true\n---\n"},"/multi-d-grid-dp/longest-common-subsequence":{"mdx":"---\ntitle: '1143. Longest Common Subsequence'\ndescription: Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0\nicon: dot\ntopics:\n - { name: \"String\", slug: \"string\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\n - { name: \"Longest Common Subsequence\", slug: \"longest-common-subsequence\" }\nissue: \"https://github.com/prdlk/leetcode/issues/168\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 36.8 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 28.9 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 38.7 }\nsidebar:\n label: 'Longest Common Subsequence'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `text1 = \"abcde\", text2 = \"ace\"`\n- Output: `3`\n- Explanation: The longest common subsequence is \"ace\" and its length is `3`.\n\n### Example 2:\n- Input: `text1 = \"abc\", text2 = \"abc\"`\n- Output: `3`\n- Explanation: The longest common subsequence is \"abc\" and its length is `3`.\n\n### Example 3:\n- Input: `text1 = \"abc\", text2 = \"def\"`\n- Output: `0`\n- Explanation: There is no such common subsequence, so the result is `0`.\n\n### Constraints:\n\n- `1 <= text1.length, text2.length <= 1000`\n- `text1` and `text2` consist of only lowercase English characters.\n\n## Solution\n\n```py\nclass Solution:\n def longestCommonSubsequence(self, text1: str, text2: str) -> int:\n dp = [[0 for j in range(len(text2) + 1)] for i in range(len(text1) + 1)]\n\n for i in range(len(text1) - 1, -1, -1):\n for j in range(len(text2) - 1, -1, -1):\n if text1[i] == text2[j]:\n dp[i][j] = 1 + dp[i + 1][j + 1]\n else:\n dp[i][j] = max(dp[i][j + 1], dp[i + 1][j])\n\n return dp[0][0]\n```\n"},"/multi-d-grid-dp/minimum-path-sum":{"mdx":"---\ntitle: '64. Minimum Path Sum'\ndescription: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\n - { name: \"Matrix\", slug: \"matrix\" }\nissue: \"https://github.com/prdlk/leetcode/issues/167\"\ncompanies:\n - { name: \"Goldman Sachs\", period: \"Three Months\", frequency: 66.3 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 66 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 21.2 }\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 32 }\nsidebar:\n label: 'Minimum Path Sum'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `grid = [[1,3,1],[1,5,1],[4,2,1]]`\n- Output: `7`\n- Explanation: Because the path `1` → `3` → `1` → `1` → `1` minimizes the sum.\n\n### Example 2:\n- Input: `grid = [[1,2,3],[4,5,6]]`\n- Output: `12`\n\n### Constraints:\n\n- `m == grid.length`\n- `n == grid[i].length`\n- `1 <= m, n <= 200`\n- `0 <= grid[i][j] <= 200`\n\n## Solution\n\n```py\nclass Solution:\n def minPathSum(self, grid: list[list[int]]) -> int:\n if not grid or not grid[0]:\n return 0\n\n m = len(grid)\n n = len(grid[0])\n dp = [[0 for _ in range(n)] for _ in range(m)]\n\n for c in range(n):\n dp[0][c] = dp[0][c - 1] + grid[0][c] if c else grid[0][c]\n\n for r in range(m):\n dp[r][0] = dp[r - 1][0] + grid[r][0] if r else grid[r][0]\n\n for r in range(1, m):\n for c in range(1, n):\n dp[r][c] = min(dp[r - 1][c], dp[r][c - 1]) + grid[r][c]\n\n return dp[-1][-1]\n```\n"},"/multi-d-grid-dp/unique-paths":{"mdx":"---\ntitle: '62. Unique Paths'\ndescription: There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time\nicon: dot\ntopics:\n - { name: \"Math\", slug: \"math\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\n - { name: \"Combinatorics\", slug: \"combinatorics\" }\nissue: \"https://github.com/prdlk/leetcode/issues/166\"\ncompanies:\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 65.6 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 48.3 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 36.8 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 44.8 }\nsidebar:\n label: 'Unique Paths'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `m = 3, n = 7`\n- Output: `28`\n\n### Example 2:\n- Input: `m = 3, n = 2`\n- Output: `3`\n- Explanation: From the top-left corner, there are a total of `3` ways to reach the bottom-right corner: `1`. Right -> Down -> Down `2`. Down -> Down -> Right `3`. Down -> Right -> Down\n\n### Constraints:\n\n- `1 <= m, n <= 100`\n\n## Solution\n\n```py\nclass Solution:\n def uniquePaths(self, m: int, n: int) -> int:\n dp = [[0 for _ in range(n)] for _ in range(m)]\n\n for c in range(n):\n dp[0][c] = 1\n\n for r in range(m):\n dp[r][0] = 1\n\n for r in range(1, m):\n for c in range(1, n):\n dp[r][c] = dp[r - 1][c] + dp[r][c - 1]\n\n return dp[-1][-1]\n```\n"},"/prefix-sum":{"md":"---\ntitle: Prefix Sum\ndescription: 'A running total is stored at each index, so the sum of any range is one subtraction.'\nhidden: true\n---\n\nSuppose you are handed an array and asked for the sum of the elements between index `i` and index `j`. One query is easy: loop from `i` to `j` and add. But now imagine thousands of such queries over the same array, or a search that inspects the sum of every possible subarray. Recomputing each range from scratch costs `O(n)` per query, and the work piles up fast.\n\nPrefix sum removes that repetition. You spend `O(n)` once to precompute running totals, and afterward every range sum collapses into a single subtraction that runs in `O(1)`, no matter how wide the range.\n\n[Prefix Sum Explained](https://www.youtube.com/watch?v=CRVxUgmIO8M)\n\n## Transformation\n\nA prefix sum turns an array into a new array of running totals. For an input array `arr` of length `n`, build a `prefix` array of length `n + 1`:\n\n`prefix[0] = 0`\n\n`prefix[k] = arr[0] + arr[1] + ... + arr[k-1]`\n\nEach entry is the previous total plus one more element, so a single pass builds the whole thing:\n\n`prefix[k] = prefix[k-1] + arr[k-1]`\n\nThe leading `prefix[0] = 0` is the sum of zero elements. That extra slot is what makes the range math below land exactly on the boundaries, which is why `prefix` has one more cell than `arr`.\n\n\n\n\nEach bar is the running total up to that index. Every step keeps the previous total (blue) and adds one new element (amber)\n\n## Any range sum is one subtraction\n\nThe sum of the subarray from index `i` up to (but not including) `j` is `prefix[j] - prefix[i]`. `prefix[j]` is the total of everything before `j`, and `prefix[i]` is the total of everything before `i`, so subtracting throws away the front you did not ask for and leaves the middle. \n\n## Watch the boundaries\n\nThe one place prefix sums trip people up is the off-by-one at the edges. Keep three facts straight: `prefix` has length `n + 1`, `prefix[0]` is `0`, and `sum(arr[i..j-1]) = prefix[j] - prefix[i]` uses an exclusive right index `j`. If a problem hands you an inclusive right index `r`, translate it to `prefix[r + 1] - prefix[i]`.\n\n## When to reach for it\n\nPrefix sums fit whenever you need repeated range sums over a fixed array, or when an algorithm searches across subarray boundaries. For a single one-off sum, a plain loop is simpler; the prefix array earns its `O(n)` setup only once the same array is queried many times.\n","mdx":"---\ntitle: Prefix Sum\ndescription: 'A running total is stored at each index, so the sum of any range is one subtraction.'\nhidden: true\n---\n\nSuppose you are handed an array and asked for the sum of the elements between index `i` and index `j`. One query is easy: loop from `i` to `j` and add. But now imagine thousands of such queries over the same array, or a search that inspects the sum of every possible subarray. Recomputing each range from scratch costs `O(n)` per query, and the work piles up fast.\n\nPrefix sum removes that repetition. You spend `O(n)` once to precompute running totals, and afterward every range sum collapses into a single subtraction that runs in `O(1)`, no matter how wide the range.\n\n \n\n## Transformation\n\nA prefix sum turns an array into a new array of running totals. For an input array `arr` of length `n`, build a `prefix` array of length `n + 1`:\n\n`prefix[0] = 0`\n\n`prefix[k] = arr[0] + arr[1] + ... + arr[k-1]`\n\nEach entry is the previous total plus one more element, so a single pass builds the whole thing:\n\n`prefix[k] = prefix[k-1] + arr[k-1]`\n\nThe leading `prefix[0] = 0` is the sum of zero elements. That extra slot is what makes the range math below land exactly on the boundaries, which is why `prefix` has one more cell than `arr`.\n\n\n \n\n\n\n\n\n\n## Any range sum is one subtraction\n\nThe sum of the subarray from index `i` up to (but not including) `j` is `prefix[j] - prefix[i]`. `prefix[j]` is the total of everything before `j`, and `prefix[i]` is the total of everything before `i`, so subtracting throws away the front you did not ask for and leaves the middle. \n\n## Watch the boundaries\n\nThe one place prefix sums trip people up is the off-by-one at the edges. Keep three facts straight: `prefix` has length `n + 1`, `prefix[0]` is `0`, and `sum(arr[i..j-1]) = prefix[j] - prefix[i]` uses an exclusive right index `j`. If a problem hands you an inclusive right index `r`, translate it to `prefix[r + 1] - prefix[i]`.\n\n## When to reach for it\n\nPrefix sums fit whenever you need repeated range sums over a fixed array, or when an algorithm searches across subarray boundaries. For a single one-off sum, a plain loop is simpler; the prefix array earns its `O(n)` setup only once the same array is queried many times.\n"},"/prefix-sum/contiguous-array":{"mdx":"---\ntitle: '525. Contiguous Array'\ndescription: Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Prefix Sum\", slug: \"prefix-sum\" }\nissue: \"https://github.com/prdlk/leetcode/issues/39\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Cisco\", period: \"Three Months\", frequency: 100 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 49.8 }\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"Akamai\", period: \"Six Months\", frequency: 100 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 40.6 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 32 }\nsidebar:\n label: 'Contiguous Array'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [0,1]`\n- Output: `2`\n- Explanation: [0, 1] is the longest contiguous subarray with an equal number of `0` and `1`.\n\n### Example 2:\n- Input: `nums = [0,1,0]`\n- Output: `2`\n- Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of `0` and `1`.\n\n### Example 3:\n- Input: `nums = [0,1,1,1,1,1,0,0,0]`\n- Output: `6`\n- Explanation: `[1,1,1,0,0,0]` is the longest contiguous subarray with equal number of `0` and `1`.\n\n### Constraints:\n\n- `1 <= nums.length <= 10^5`\n- `nums[i]` is either 0 or 1.\n\n## Solution\n\n```py\nclass Solution:\n def findMaxLength(self, nums: List[int]) -> int:\n map = {0: -1}\n count = 0\n longest = 0\n\n for i, num in enumerate(nums):\n count += 1 if num == 1 else -1\n\n if count in map:\n longest = max(longest, i - map[count])\n else:\n map[count] = i\n\n return longest\n```\n"},"/prefix-sum/product-of-array-except-self":{"mdx":"---\ntitle: '238. Product of Array Except Self'\ndescription: Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Prefix Sum\", slug: \"prefix-sum\" }\nvideo: \"bNvIQI2wAjk\"\nissue: \"https://github.com/prdlk/leetcode/issues/37\"\ncompanies:\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 70.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 61.3 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 57.2 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 42.3 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 59.7 }\n - { name: \"Asana\", period: \"Six Months\", frequency: 100 }\n - { name: \"tcs\", period: \"Six Months\", frequency: 43.5 }\nsidebar:\n label: 'Product of Array Except Self'\n badge: 'Medium'\n---\n\n::::warning\nYou must write an algorithm that runs in O(n) time and without using the division operation.\nCan you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.)\n::::\n\n### Example 1:\n- Input: `nums = [1,2,3,4]`\n- Output: `[24,12,8,6]`\n\n### Example 2:\n- Input: `nums = [-1,1,0,-3,3]`\n- Output: `[0,0,9,0,0]`\n\n### Constraints:\n\n- `2 <= nums.length <= 10^5`\n- `-30 <= nums[i] <= 30`\n- The input is generated such that `answer[i]` is guaranteed to fit in a 32-bit integer.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"productExceptSelf(nums)\"]) --> I[\"answer = ones(n), rightArr = ones(n)\"]\n I --> P1[\"Pass 1 — left to right: answer[i] = nums[i-1] * answer[i-1]\"]\n P1 --> P2[\"Pass 2 — right to left: rightArr[i] = nums[i+1] * rightArr[i+1]\"]\n P2 --> P3[\"Pass 3: answer[i] *= rightArr[i]\"]\n P3 --> E([\"return answer — no division needed\"])\n```\n\n## Solution\n\n```js\n/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar productExceptSelf = function(nums) {\n const n = nums.length;\n const answer = new Array(n).fill(1);\n const rightArr = new Array(n).fill(1);\n\n for (let i = 1; i < n; i++) {\n answer[i] = nums[i - 1] * answer[i - 1];\n }\n for (let i = n - 2; i >= 0; i--) {\n rightArr[i] = nums[i + 1] * rightArr[i + 1];\n }\n for (let i = 0; i < n; i++) {\n answer[i] *= rightArr[i];\n }\n return answer;\n};\n```\n"},"/prefix-sum/range-sum-query-immutable":{"mdx":"---\ntitle: '303. Range Sum Query - Immutable'\ndescription: Given an integer array nums, handle multiple queries of the following type\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Design\", slug: \"design\" }\n - { name: \"Prefix Sum\", slug: \"prefix-sum\" }\nvideo: \"2pndAmo_sMA\"\nissue: \"https://github.com/prdlk/leetcode/issues/36\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 42.3 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 24.5 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 24.1 }\nsidebar:\n label: 'Range Sum Query - Immutable'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `[\"NumArray\", \"sumRange\", \"sumRange\", \"sumRange\"]` `[[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]]`\n- Output: `[null, 1, -1, -3]`\n- Explanation: `NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]);` `numArray.sumRange(0, 2); // return (-2) + 0 + 3 = 1` `numArray.sumRange(2, 5); // return 3 + (-5) + 2 + (-1) = -1` `numArray.sumRange(0, 5); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3`\n\n### Constraints:\n\n- `1 <= nums.length <= 10^4`\n- `-10^5 <= nums[i] <= 10^5`\n- `0 <= left <= right < nums.length`\n- At most `10^4` calls will be made to `sumRange`.\n\n## Approach\n\n```mermaid\nflowchart TD\n subgraph build[\"constructor(nums) — O(n) once\"]\n A[\"prefix = [0]\"] --> B{\"more n in nums?\"}\n B -- yes --> C[\"prefix.push(last + n)\"]\n C --> B\n end\n build --> Q\n subgraph Q[\"sumRange(left, right) — O(1) per call\"]\n D([\"return prefix[right + 1] - prefix[left]\"])\n end\n```\n\n## Solution\n\n```js\n/**\n * @param {number[]} nums\n */\nclass NumArray {\n constructor(nums) {\n this.prefix = [0];\n for (let n of nums){\n this.prefix.push(this.prefix[this.prefix.length - 1] + n);\n }\n }\n /**\n * @param {number} left\n * @param {number} right\n * @return {number}\n */\n sumRange(left, right) {\n return this.prefix[right + 1] - this.prefix[left];\n }\n}\n\n\n/** \n * Your NumArray object will be instantiated and called as such:\n * var obj = new NumArray(nums)\n * var param_1 = obj.sumRange(left,right)\n */\n```\n"},"/prefix-sum/subarray-sum-equals-k":{"mdx":"---\ntitle: '560. Subarray Sum Equals K'\ndescription: Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Prefix Sum\", slug: \"prefix-sum\" }\nvideo: \"fFVZt-6sgyo\"\nissue: \"https://github.com/prdlk/leetcode/issues/38\"\ncompanies:\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 63.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 50.2 }\n - { name: \"Visa\", period: \"Three Months\", frequency: 80.9 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 74.8 }\n - { name: \"Salesforce\", period: \"Three Months\", frequency: 64.7 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 57.1 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 48.3 }\n - { name: \"Accenture\", period: \"Six Months\", frequency: 100 }\n - { name: \"IBM\", period: \"Six Months\", frequency: 94.5 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 76.3 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 74.7 }\n - { name: \"josh technology\", period: \"Six Months\", frequency: 74 }\n - { name: \"Walmart Labs\", period: \"Six Months\", frequency: 70.5 }\n - { name: \"Yandex\", period: \"Six Months\", frequency: 69.2 }\nsidebar:\n label: 'Subarray Sum Equals K'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [1,1,1], k = 2`\n- Output: `2`\n\n### Example 2:\n- Input: `nums = [1,2,3], k = 3`\n- Output: `2`\n\n### Constraints:\n\n- `1 <= nums.length <= 2 * 10^4`\n- `-1000 <= nums[i] <= 1000`\n- `-10^7 <= k <= 10^7`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"subarraySum(nums, k)\"]) --> I[\"map = {0: 1} — the empty prefix; sum = 0, count = 0\"]\n I --> L{\"more n in nums?\"}\n L -- no --> E([\"return count\"])\n L -- yes --> A[\"sum += n\"]\n A --> Q{\"map has sum - k?\"}\n Q -- yes --> C[\"count += map.get(sum - k) — every earlier prefix that closes a window\"]\n Q -- no --> W[\"map.set(sum, count of sum + 1)\"]\n C --> W\n W --> L\n```\n\n## Solution\n\n```js\n/**\n * @param {number[]} nums\n * @param {number} k\n * @return {number}\n */\nvar subarraySum = function(nums, k) {\n const map = new Map();\n map.set(0, 1);\n\n let sum = 0;\n let count = 0;\n\n for (let n of nums){\n sum += n;\n\n if(map.has(sum - k)){\n count += map.get(sum - k);\n }\n\n map.set(sum, (map.get(sum) || 0) + 1);\n }\n return count\n};\n```\n"},"/prefix-sum/subarray-sums-divisible-by-k":{"mdx":"---\ntitle: '974. Subarray Sums Divisible by K'\ndescription: Given an integer array nums and an integer k, return the number of non-empty subarrays that have a sum divisible by k\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Prefix Sum\", slug: \"prefix-sum\" }\nissue: \"https://github.com/prdlk/leetcode/issues/40\"\ncompanies:\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"TikTok\", period: \"Three Months\", frequency: 70.9 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 21.2 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 24.1 }\nsidebar:\n label: 'Subarray Sums Divisible by K'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [4,5,0,-2,-3,1], k = 5`\n- Output: `7`\n- Explanation: There are `7` subarrays with a sum divisible by `k = 5`: [4, `5`, `0`, `-2`, `-3`, 1], `[5]`, [5, 0], [5, `0`, `-2`, -3], `[0]`, [0, `-2`, -3], [-2, -3]\n\n### Example 2:\n- Input: `nums = [5], k = 9`\n- Output: `0`\n\n### Constraints:\n\n- `1 <= nums.length <= 3 * 10^4`\n- `-10^4 <= nums[i] <= 10^4`\n- `2 <= k <= 10^4`\n\n## Solution\n\n```py\nclass Solution:\n def subarraysDivByK(self, nums: list[int], k: int) -> int:\n remainder_counts = {0: 1}\n cur_sum = 0\n count = 0\n\n for n in nums:\n cur_sum += n\n remainder = cur_sum % k\n\n if remainder in remainder_counts:\n count += remainder_counts[remainder]\n\n remainder_counts[remainder] = remainder_counts.get(remainder, 0) + 1\n\n return count\n```\n"},"/sliding-window":{"md":"---\ntitle: Sliding Window\ndescription: 'Sliding window is a specialized two-pointer family where the pointers define a contiguous interval and you maintain information about that entire interval as it moves.'\nhidden: true\n---\n\n\n[Sliding Window](https://www.youtube.com/watch?v=tSTO9w3piAo)\n\n\nMany problems have two moving boundaries. That alone does not make them sliding window problems.\n\nA problem belongs to this family when:\n\n- the answer depends on the subarray or substring between left and right\n- you can update the window state incrementally as elements enter or leave\n- you maintain a condition that tells you whether the window is valid\n\n:::tip\nThe rule for all three: every element enters once and leaves once, so the whole thing runs in O(n).\n:::\n\n**What is an invariant?**\n\nAn invariant is a statement that stays true before and after every iteration. In a sliding window, that statement usually describes the window itself, such as “the window is valid,” “it has no duplicates,” or “its sum is at most the target.” It can also describe a helper structure, such as “the frequency map matches the characters currently inside the window.”\n\n**Why invariants matter?**\n\nWithout an invariant, shrinking the window is just a guess. You no longer know when the window is valid, when to update the answer, or why the left pointer should stop. The invariant is what makes the algorithm correct.\n\n**How sliding window differs from other two-pointer families?**\n\nSliding window is not the same as every other two-pointer pattern. In Container With Most Water, the interval is only the remaining search space. In sliding window, the interval itself is the thing you care about. In Remove Duplicates, the left side is already the finished answer prefix. In sliding window, both pointers describe a live window that is still being checked.\n\n\n## 1. Fixed-size windows\n\n:::tip\nThe window size never changes. The main task is updating the window state efficiently when one element enters and one leaves.\n:::\n\n\n\n\nSliding Window Fixed\n\n\n```python\ndef sliding_window_fixed(input, window_size):\n ans = window = input[0:window_size]\n for right in range(window_size, len(input)):\n left = right - window_size\n remove input[left] from window\n append input[right] to window\n ans = optimal(ans, window)\n return ans\n```\n\n1. Add `nums[right]` to the window.\n\n2. If the window has `k` elements, record the answer.\n\n3. Remove the leftmost element so the window stays at size `k`.\n\n\n## 2. Flexible-size windows with a validity rule\n\n:::tip\nHere the window expands until invalid, then shrinks until valid again.\n:::\n\n\n\nSliding Window Flexible\n\n\n**Longest Window**\n\n```python\ndef sliding_window_flexible_longest(input):\n initialize window, ans\n left = 0\n for right in range(len(input)):\n append input[right] to window\n while invalid(window): # update left until window is valid again\n remove input[left] from window\n left += 1\n ans = max(ans, window) # window is guaranteed to be valid here\n return ans\n\n```\n\n**Shortest Window**\n\n```python\ndef sliding_window_flexible_shortest(input):\n initialize window, ans\n left = 0\n for right in range(len(input)):\n append input[right] to window\n while valid(window):\n ans = min(ans, window) # window is guaranteed to be valid here\n remove input[left] from window\n left += 1\n return ans\n```\n\n1. Add `nums[right]` to the window.\n\n2. While the window is invalid, remove `nums[left]` and move `left` up.\n\n3. The window is now valid. Record its size.\n\n---\n\n## 3. Frequency-tracking windows\n\n:::tip\nThe difficulty comes from keeping the bookkeeping correct as characters enter and leave.\n:::\n\n\n\n\nFrequency-tracking windows\n\n```python\nfrom collections import defaultdict\n\ndef frequency_window(s):\n left = 0\n counts = defaultdict(int)\n best = 0\n\n for right in range(len(s)):\n counts[s[right]] += 1 # character enters\n\n while counts[s[right]] > 1: # rule: no repeats\n counts[s[left]] -= 1 # character leaves\n left += 1\n\n best = max(best, right - left + 1)\n\n return best\n```\n\n1. Add `s[right]` to the window.\n\n2. While the window has more than one character, remove `s[left]` and move `left` up.\n\n3. The window is now valid. Record its size.\n\n---\n\nFor anagram or \"minimum window substring\" problems, you also keep a target count. Track how many characters currently match the target with one integer (`matched`). Update it only when a count crosses the target value. This keeps each step `O(1)`.\n\n```python\ndef anagram_window(s, p):\n need = defaultdict(int)\n for c in p:\n need[c] += 1\n\n counts = defaultdict(int)\n matched = 0 # how many chars have counts[c] == need[c]\n result = []\n k = len(p)\n\n for right in range(len(s)):\n c = s[right]\n counts[c] += 1\n if counts[c] == need[c]:\n matched += 1\n\n if right >= k: # window too big, remove left\n d = s[right - k]\n if counts[d] == need[d]:\n matched -= 1\n counts[d] -= 1\n\n if matched == len(need):\n result.append(right - k + 1)\n\n return result\n```\n\n","mdx":"---\ntitle: Sliding Window\ndescription: 'Sliding window is a specialized two-pointer family where the pointers define a contiguous interval and you maintain information about that entire interval as it moves.'\nhidden: true\n---\n\n\n \n\n\nMany problems have two moving boundaries. That alone does not make them sliding window problems.\n\nA problem belongs to this family when:\n\n- the answer depends on the subarray or substring between left and right\n- you can update the window state incrementally as elements enter or leave\n- you maintain a condition that tells you whether the window is valid\n\n:::tip\nThe rule for all three: every element enters once and leaves once, so the whole thing runs in O(n).\n:::\n\n\n \n An invariant is a statement that stays true before and after every iteration. In a sliding window, that statement usually describes the window itself, such as “the window is valid,” “it has no duplicates,” or “its sum is at most the target.” It can also describe a helper structure, such as “the frequency map matches the characters currently inside the window.”\n \n \n Without an invariant, shrinking the window is just a guess. You no longer know when the window is valid, when to update the answer, or why the left pointer should stop. The invariant is what makes the algorithm correct.\n \n \n Sliding window is not the same as every other two-pointer pattern. In Container With Most Water, the interval is only the remaining search space. In sliding window, the interval itself is the thing you care about. In Remove Duplicates, the left side is already the finished answer prefix. In sliding window, both pointers describe a live window that is still being checked.\n \n \n\n\n## 1. Fixed-size windows\n\n:::tip\nThe window size never changes. The main task is updating the window state efficiently when one element enters and one leaves.\n:::\n\n\n \n\n\n\n\n\n\n```python\ndef sliding_window_fixed(input, window_size):\n ans = window = input[0:window_size]\n for right in range(window_size, len(input)):\n left = right - window_size\n remove input[left] from window\n append input[right] to window\n ans = optimal(ans, window)\n return ans\n```\n\n\n \n Add `nums[right]` to the window.\n \n \n If the window has `k` elements, record the answer.\n \n \n Remove the leftmost element so the window stays at size `k`.\n \n \n\n\n## 2. Flexible-size windows with a validity rule\n\n:::tip\nHere the window expands until invalid, then shrinks until valid again.\n:::\n\n \n\n\n\n\n\n\n\n \n\n```python\ndef sliding_window_flexible_longest(input):\n initialize window, ans\n left = 0\n for right in range(len(input)):\n append input[right] to window\n while invalid(window): # update left until window is valid again\n remove input[left] from window\n left += 1\n ans = max(ans, window) # window is guaranteed to be valid here\n return ans\n\n```\n\n \n \n\n```python\ndef sliding_window_flexible_shortest(input):\n initialize window, ans\n left = 0\n for right in range(len(input)):\n append input[right] to window\n while valid(window):\n ans = min(ans, window) # window is guaranteed to be valid here\n remove input[left] from window\n left += 1\n return ans\n```\n\n \n \n\n\n \n Add `nums[right]` to the window.\n \n \n While the window is invalid, remove `nums[left]` and move `left` up.\n \n \n The window is now valid. Record its size.\n \n \n\n---\n\n## 3. Frequency-tracking windows\n\n:::tip\nThe difficulty comes from keeping the bookkeeping correct as characters enter and leave.\n:::\n\n\n \n\n\n\n\n\n```python\nfrom collections import defaultdict\n\ndef frequency_window(s):\n left = 0\n counts = defaultdict(int)\n best = 0\n\n for right in range(len(s)):\n counts[s[right]] += 1 # character enters\n\n while counts[s[right]] > 1: # rule: no repeats\n counts[s[left]] -= 1 # character leaves\n left += 1\n\n best = max(best, right - left + 1)\n\n return best\n```\n\n\n \n Add `s[right]` to the window.\n \n \n While the window has more than one character, remove `s[left]` and move `left` up.\n \n \n The window is now valid. Record its size.\n \n \n\n---\n\nFor anagram or \"minimum window substring\" problems, you also keep a target count. Track how many characters currently match the target with one integer (`matched`). Update it only when a count crosses the target value. This keeps each step `O(1)`.\n\n```python\ndef anagram_window(s, p):\n need = defaultdict(int)\n for c in p:\n need[c] += 1\n\n counts = defaultdict(int)\n matched = 0 # how many chars have counts[c] == need[c]\n result = []\n k = len(p)\n\n for right in range(len(s)):\n c = s[right]\n counts[c] += 1\n if counts[c] == need[c]:\n matched += 1\n\n if right >= k: # window too big, remove left\n d = s[right - k]\n if counts[d] == need[d]:\n matched -= 1\n counts[d] -= 1\n\n if matched == len(need):\n result.append(right - k + 1)\n\n return result\n```\n\n"},"/sliding-window/best-time-to-buy-and-sell-stock":{"mdx":"---\ntitle: '121. Best Time to Buy and Sell Stock'\ndescription: You are given an array prices where prices[i] is the price of a given stock on the i^th day\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\nvideo: \"1pkOgXD63yU\"\nissue: \"https://github.com/prdlk/leetcode/issues/50\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 83.1 }\n - { name: \"Infosys\", period: \"Thirty Days\", frequency: 80.2 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 69.6 }\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 65.6 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 65.3 }\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 58.2 }\n - { name: \"Atlassian\", period: \"Three Months\", frequency: 100 }\n - { name: \"Media.net\", period: \"Three Months\", frequency: 100 }\n - { name: \"Nvidia\", period: \"Three Months\", frequency: 87.7 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 70.4 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 69 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 60.5 }\n - { name: \"Mastercard\", period: \"Six Months\", frequency: 79 }\n - { name: \"Accenture\", period: \"Six Months\", frequency: 75 }\n - { name: \"Adobe\", period: \"Six Months\", frequency: 74.3 }\n - { name: \"Walmart Labs\", period: \"Six Months\", frequency: 70.5 }\n - { name: \"IBM\", period: \"Six Months\", frequency: 70.1 }\n - { name: \"TikTok\", period: \"Six Months\", frequency: 55.2 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 53.1 }\nsidebar:\n label: 'Best Time to Buy and Sell Stock'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `prices = [7,1,5,3,6,4]`\n- Output: `5`\n- Explanation: Buy on day `2` (price = `1`) and sell on day `5` (price = `6`), profit = `6-1 = 5`. Note that buying on day `2` and selling on day `1` is not allowed because you must buy before you sell.\n\n### Example 2:\n- Input: `prices = [7,6,4,3,1]`\n- Output: `0`\n- Explanation: In this case, no transactions are done and the max profit = `0`.\n\n### Constraints:\n\n- `1 <= prices.length <= 10^5`\n- `0 <= prices[i] <= 10^4`\n\n## Approach\n\n::::warning\nThe diagram traces the code below literally. That code does not run: `curr` and `ans` are never initialised, so the first `curr > left` test raises `NameError`, and `left` holds a price rather than an index, so `prices[left]` indexes by value.\n::::\n\n```mermaid\nflowchart TD\n S([\"maxProfit(prices)\"]) --> I[\"left = min(prices)\"]\n I --> F{\"more right in range(len(prices))?\"}\n F -- no --> E([\"return ans\"])\n F -- yes --> W{\"curr > left?\"}\n W -- yes --> P[\"curr -= prices[left]; left += 1\"]\n P --> W\n W -- no --> M[\"ans = max(ans, curr)\"]\n M --> F\n```\n\n## Solution\n\n```py\nclass Solution:\n def maxProfit(self, prices: List[int]) -> int:\n left = min(prices)\n for right in range(len(prices)):\n while curr > left:\n curr -= prices[left]\n left += 1\n ans = max(ans, curr)\n return ans\n```\n"},"/sliding-window/longest-repeating-character-replacement":{"mdx":"---\ntitle: '424. Longest Repeating Character Replacement'\ndescription: You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Sliding Window\", slug: \"sliding-window\" }\nvideo: \"gqXU1UyA8pk\"\nissue: \"https://github.com/prdlk/leetcode/issues/52\"\ncompanies:\n - { name: \"Zoho\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 46.6 }\n - { name: \"DE Shaw\", period: \"Three Months\", frequency: 91.8 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 66 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 46.8 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 34.6 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 32.7 }\nsidebar:\n label: 'Longest Repeating Character Replacement'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `s = \"ABAB\", k = 2`\n- Output: `4`\n- Explanation: Replace the two 'A's with two 'B's or vice versa.\n\n### Example 2:\n- Input: `s = \"AABABBA\", k = 1`\n- Output: `4`\n- Explanation: Replace the one 'A' in the middle with 'B' and form \"AABBBBA\". The substring \"BBBB\" has the longest repeating letters, which is `4`. There may exists other ways to achieve this answer too.\n\n### Constraints:\n\n- `1 <= s.length <= 10^5`\n- `s` consists of only uppercase English letters.\n- `0 <= k <= s.length`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"characterReplacement(s, k)\"]) --> I[\"count = {}, res = 0, l = 0, maxF = 0\"]\n I --> L{\"more (r, c) in s?\"}\n L -- no --> E([\"return res\"])\n L -- yes --> A[\"count[c] += 1; maxF = max(maxF, count[c])\"]\n A --> W{\"(r - l + 1) - maxF > k? — too many chars to replace\"}\n W -- yes --> P[\"count[s[l]] -= 1; l += 1 — shrink from the left\"]\n P --> W\n W -- no --> M[\"res = max(res, r - l + 1)\"]\n M --> L\n```\n\n## Solution\n\n```py\nclass Solution:\n def characterReplacement(self, s: str, k: int) -> int:\n count = {}\n res = 0\n\n l = 0\n maxF = 0\n for r, c in enumerate(s):\n count[c] = 1 + count.get(c, 0)\n maxF = max(maxF, count[c])\n\n while (r - l + 1) - maxF > k:\n count[s[l]] -= 1\n l += 1\n\n res = max(res, r - l + 1)\n return res\n```\n"},"/sliding-window/longest-substring-without-repeating-characters":{"mdx":"---\ntitle: '3. Longest Substring Without Repeating Characters'\ndescription: Given a string s, find the length of the longest substring without duplicate characters\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Sliding Window\", slug: \"sliding-window\" }\nvideo: \"wiGpQwVHdE0\"\nissue: \"https://github.com/prdlk/leetcode/issues/51\"\ncompanies:\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Visa\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 84.9 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 75.6 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 63.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 63.7 }\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 58.2 }\n - { name: \"Cognizant\", period: \"Three Months\", frequency: 100 }\n - { name: \"Optum\", period: \"Three Months\", frequency: 100 }\n - { name: \"Spotify\", period: \"Three Months\", frequency: 100 }\n - { name: \"Deloitte\", period: \"Three Months\", frequency: 89.1 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 87.4 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 69 }\n - { name: \"Goldman Sachs\", period: \"Three Months\", frequency: 66.3 }\n - { name: \"Media.net\", period: \"Six Months\", frequency: 100 }\n - { name: \"Wipro\", period: \"Six Months\", frequency: 100 }\n - { name: \"Wissen Technology\", period: \"Six Months\", frequency: 100 }\n - { name: \"Zoho\", period: \"Six Months\", frequency: 100 }\n - { name: \"Qualcomm\", period: \"Six Months\", frequency: 92 }\n - { name: \"Turing\", period: \"Six Months\", frequency: 89 }\n - { name: \"EPAM Systems\", period: \"Six Months\", frequency: 86.7 }\n - { name: \"Netflix\", period: \"Six Months\", frequency: 83.1 }\n - { name: \"Tesla\", period: \"Six Months\", frequency: 80.9 }\n - { name: \"Yandex\", period: \"Six Months\", frequency: 69.2 }\n - { name: \"Agoda\", period: \"Six Months\", frequency: 65 }\n - { name: \"IBM\", period: \"Six Months\", frequency: 60.1 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 54.1 }\nsidebar:\n label: 'Longest Substring Without Repeating Characters'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `s = \"abcabcbb\"`\n- Output: `3`\n- Explanation: The answer is \"abc\", with the length of `3`. Note that \"bca\" and \"cab\" are also correct answers.\n\n### Example 2:\n- Input: `s = \"bbbbb\"`\n- Output: `1`\n- Explanation: The answer is \"b\", with the length of `1`.\n\n### Example 3:\n- Input: `s = \"pwwkew\"`\n- Output: `3`\n- Explanation: The answer is \"wke\", with the length of `3`. Notice that the answer must be a substring, \"pwke\" is a subsequence and not a substring.\n\n### Constraints:\n\n- `0 <= s.length <= 10^5`\n- `s` consists of English letters, digits, symbols and spaces.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"lengthOfLongestSubstring(s)\"]) --> I[\"left = 0, ans = 0, window = set()\"]\n I --> L{\"more (right, c) in s?\"}\n L -- no --> E([\"return ans\"])\n L -- yes --> W{\"c already in window?\"}\n W -- yes --> P[\"window.remove(s[left]); left += 1 — shrink until c is free\"]\n P --> W\n W -- no --> A[\"window.add(c)\"]\n A --> M[\"ans = max(ans, right - left + 1)\"]\n M --> L\n```\n\n## Solution\n\n```py\nclass Solution:\n def lengthOfLongestSubstring(self, s: str) -> int:\n left = 0\n ans = 0\n window = set()\n for right, c in enumerate(s):\n while c in window:\n window.remove(s[left])\n left += 1\n window.add(c)\n ans = max(ans, right - left + 1)\n\n return ans\n```\n"},"/sliding-window/max-consecutive-ones-iii":{"mdx":"---\ntitle: '1004. Max Consecutive Ones III'\ndescription: Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\n - { name: \"Sliding Window\", slug: \"sliding-window\" }\n - { name: \"Prefix Sum\", slug: \"prefix-sum\" }\nissue: \"https://github.com/prdlk/leetcode/issues/56\"\ncompanies:\n - { name: \"EPAM Systems\", period: \"Thirty Days\", frequency: 89.8 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Tekion\", period: \"Three Months\", frequency: 100 }\n - { name: \"DE Shaw\", period: \"Three Months\", frequency: 80.2 }\n - { name: \"Google\", period: \"Three Months\", frequency: 38.7 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 67.9 }\n - { name: \"Yandex\", period: \"Six Months\", frequency: 59.6 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 54.1 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 42.5 }\nsidebar:\n label: 'Max Consecutive Ones III'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2`\n- Output: `6`\n- Explanation: `[1,1,1,0,0,1,1,1,1,1,1]` Bolded numbers were flipped from `0` to `1`. The longest subarray is underlined.\n\n### Example 2:\n- Input: `nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3`\n- Output: `10`\n- Explanation: `[0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]` Bolded numbers were flipped from `0` to `1`. The longest subarray is underlined.\n\n### Constraints:\n\n- `1 <= nums.length <= 10^5`\n- `nums[i]` is either 0 or 1.\n- `0 <= k <= nums.length`\n\n## Solution\n\n```py\nclass Solution:\n def longestOnes(self, nums: list[int], k: int) -> int:\n left = 0\n cur = 0\n ans = 0\n\n for right in range(len(nums)):\n if nums[right] == 0:\n cur += 1\n while cur > k:\n if nums[left] == 0:\n cur -= 1\n left += 1\n\n ans = max(ans, right - left + 1)\n\n return ans\n```\n"},"/sliding-window/maximum-average-subarray-i":{"mdx":"---\ntitle: '643. Maximum Average Subarray I'\ndescription: You are given an integer array nums consisting of n elements, and an integer k\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Sliding Window\", slug: \"sliding-window\" }\nissue: \"https://github.com/prdlk/leetcode/issues/55\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 83.1 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 55.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 36.7 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 57.2 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 32.7 }\nsidebar:\n label: 'Maximum Average Subarray I'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `nums = [1,12,-5,-6,50,3], k = 4`\n- Output: `12.75000`\n- Explanation: Maximum average is `(12 - 5 - 6 + 50`) / `4 = 51 / 4 = 12.75`\n\n### Example 2:\n- Input: `nums = [5], k = 1`\n- Output: `5.00000`\n\n### Constraints:\n\n- `n == nums.length`\n- `1 <= k <= n <= 10^5`\n- `-10^4 <= nums[i] <= 10^4`\n\n## Solution\n\n```py\nclass Solution:\n def findMaxAverage(self, nums: list[int], k: int) -> float:\n window_sum = 0\n for i in range(k):\n window_sum += nums[i]\n largest = window_sum\n\n for right in range(k, len(nums)):\n left = right - k\n window_sum -= nums[left]\n window_sum += nums[right]\n largest = max(largest, window_sum)\n\n return largest / k\n```\n"},"/sliding-window/minimum-size-subarray-sum":{"mdx":"---\ntitle: '209. Minimum Size Subarray Sum'\ndescription: Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\n - { name: \"Sliding Window\", slug: \"sliding-window\" }\n - { name: \"Prefix Sum\", slug: \"prefix-sum\" }\nissue: \"https://github.com/prdlk/leetcode/issues/54\"\ncompanies:\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 46.8 }\n - { name: \"Google\", period: \"Three Months\", frequency: 45 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 28.9 }\nsidebar:\n label: 'Minimum Size Subarray Sum'\n badge: 'Medium'\n---\n\n::::warning\nIf you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log(n)).\n::::\n\n### Example 1:\n- Input: `target = 7, nums = [2,3,1,2,4,3]`\n- Output: `2`\n- Explanation: The subarray `[4,3]` has the minimal length under the problem constraint.\n\n### Example 2:\n- Input: `target = 4, nums = [1,4,4]`\n- Output: `1`\n\n### Example 3:\n- Input: `target = 11, nums = [1,1,1,1,1,1,1,1]`\n- Output: `0`\n\n### Constraints:\n\n- `1 <= target <= 10^9`\n- `1 <= nums.length <= 10^5`\n- `1 <= nums[i] <= 10^4`\n\n## Solution\n\n```py\nclass Solution:\n def minSubArrayLen(self, target: int, nums: list[int]) -> int:\n window_sum = 0\n length = len(nums) + 1\n left = 0\n\n for right in range(len(nums)):\n window_sum += nums[right]\n while window_sum >= target:\n length = min(length, right - left + 1)\n window_sum -= nums[left]\n left += 1\n if length > len(nums):\n return 0\n return length\n```\n"},"/sliding-window/minimum-window-substring":{"mdx":"---\ntitle: '76. Minimum Window Substring'\ndescription: Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string \"\"\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Sliding Window\", slug: \"sliding-window\" }\nissue: \"https://github.com/prdlk/leetcode/issues/57\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 55.9 }\n - { name: \"Lyft\", period: \"Three Months\", frequency: 93.3 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 48.3 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Google\", period: \"Three Months\", frequency: 23.9 }\n - { name: \"Oracle\", period: \"Six Months\", frequency: 78.7 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 44.8 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\nsidebar:\n label: 'Minimum Window Substring'\n badge: 'Hard'\n---\n\n::::warning\nCould you find an algorithm that runs in O(m + n) time?\n::::\n\n### Example 1:\n- Input: `s = \"ADOBECODEBANC\", t = \"ABC\"`\n- Output: `\"BANC\"`\n- Explanation: The minimum window substring \"BANC\" includes 'A', 'B', and 'C' from string `t`.\n\n### Example 2:\n- Input: `s = \"a\", t = \"a\"`\n- Output: `\"a\"`\n- Explanation: The entire string `s` is the minimum window.\n\n### Example 3:\n- Input: `s = \"a\", t = \"aa\"`\n- Output: `\"\"`\n- Explanation: Both 'a's from `t` must be included in the window. Since the largest window of `s` only has one 'a', return empty string.\n\n### Constraints:\n\n- `m == s.length`\n- `n == t.length`\n- `1 <= m, n <= 10^5`\n- `s` and `t` consist of uppercase and lowercase English letters.\n\n## Solution\n\n```py\nclass Solution:\n def minWindow(self, s: str, t: str) -> str:\n```\n"},"/sliding-window/permutation-in-string":{"mdx":"---\ntitle: '567. Permutation in String'\ndescription: Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Sliding Window\", slug: \"sliding-window\" }\nvideo: \"UbyhOgBN834\"\nissue: \"https://github.com/prdlk/leetcode/issues/53\"\ncompanies:\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 63.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 46.8 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 48.1 }\nsidebar:\n label: 'Permutation in String'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `s1 = \"ab\", s2 = \"eidbaooo\"`\n- Output: `true`\n- Explanation: `s2` contains one permutation of `s1` (\"ba\").\n\n### Example 2:\n- Input: `s1 = \"ab\", s2 = \"eidboaoo\"`\n- Output: `false`\n\n### Constraints:\n\n- `1 <= s1.length, s2.length <= 10^4`\n- `s1` and `s2` consist of lowercase English letters.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"checkInclusion(s1, s2)\"]) --> G{\"k = len(s1) > len(s2)?\"}\n G -- yes --> X([\"return False\"])\n G -- no --> I[\"need = Counter(s1), window = Counter()\"]\n I --> L{\"more (right, c) in s2?\"}\n L -- no --> E([\"return False\"])\n L -- yes --> A[\"window[c] += 1\"]\n A --> B{\"right >= k? — window is now longer than k\"}\n B -- yes --> C[\"drop s2[right - k] from window, deleting the key at 0\"]\n B -- no --> Q\n C --> Q{\"window == need?\"}\n Q -- yes --> R([\"return True\"])\n Q -- no --> L\n```\n\n## Solution\n\n```py\nfrom collections import Counter\n\n\nclass Solution:\n def checkInclusion(self, s1: str, s2: str) -> bool:\n k = len(s1)\n if k > len(s2):\n return False\n\n need = Counter(s1)\n window = Counter()\n\n for right, c in enumerate(s2):\n window[c] += 1\n if right >= k:\n left_char = s2[right - k]\n window[left_char] -= 1\n if window[left_char] == 0:\n del window[left_char]\n if window == need:\n return True\n\n return False\n```\n"},"/stack":{"mdx":"---\ntitle: Stack\ndescription: 'Items are added and removed from one end, so the last item added is the first item removed.'\nhidden: true\n---\n\nA stack works like a stack of plates in a cafeteria. You add new plates on top. When you need a plate, you take from the top. The last plate you added is the first one you remove.\n\nThis is **Last-In-First-Out (LIFO)**. The most recent item goes out first.\n\n\n\n## Core Stack Operations\n\nA stack has two fundamental operations:\n\n- **Push**: add an item to the top of the stack\n- **Pop**: remove the item at the top of the stack\n\nSome stacks have additional operations:\n\n- **Peek**: get the item at the top of the stack without removing it\n- **IsEmpty**: check if the stack is empty\n\n\n\n```python\n# Using Python list as a stack\nstack = []\n\n# Push elements\nstack.append(10)\nstack.append(20)\nstack.append(30)\n# Stack: [10, 20, 30] (30 is on top)\n\n# Pop element\ntop = stack.pop() # Returns 30\n# Stack: [10, 20]\n\n# Peek at top\nif stack:\n top_element = stack[-1] # Returns 20 without removing\n```\n\nPython lists provide `append()` for pushing and `pop()` for popping. The `peek()` method is not built-in, but you can implement it yourself.\n\n## Last in, First out\n\nThe defining characteristic of a stack is LIFO order. Whatever you put in last comes out first. This is different from a queue, which is First-In-First-Out (FIFO).\n\nAdd items: A, B, C\n\n- Stack: [A, B, C] (C on top)\n\nRemove items:\n\n- First pop: C\n- Second pop: B\n- Third pop: A\n\nThe order reverses. Items come out in the opposite order they went in.\n"},"/stack/basic-calculator":{"mdx":"---\ntitle: '224. Basic Calculator'\ndescription: Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation\nicon: dot\ntopics:\n - { name: \"Math\", slug: \"math\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Stack\", slug: \"stack\" }\n - { name: \"Recursion\", slug: \"recursion\" }\nissue: \"https://github.com/prdlk/leetcode/issues/71\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 28.9 }\n - { name: \"Google\", period: \"Three Months\", frequency: 23.9 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 24.5 }\nsidebar:\n label: 'Basic Calculator'\n badge: 'Hard'\n---\n\n### Example 1:\n- Input: `s = \"1 + 1\"`\n- Output: `2`\n\n### Example 2:\n- Input: `s = \" 2-1 + 2 \"`\n- Output: `3`\n\n### Example 3:\n- Input: `s = \"(1+(4+5+2)-3)+(6+8)\"`\n- Output: `23`\n\n### Constraints:\n\n- `1 <= s.length <= 3 * 10^5`\n- `s` consists of digits, '+', '-', '(', ')', and ' '.\n- `s` represents a valid expression.\n- '+' is not used as a unary operation (i.e., \"+1\" and \"+(2 + 3)\" is invalid).\n- '-' could be used as a unary operation (i.e., \"-1\" and \"-(2 + 3)\" is valid).\n- There will be no two consecutive operators in the input.\n- Every number and running calculation will fit in a signed 32-bit integer.\n\n## Solution\n\n```py\nclass Solution:\n def calculate(self, s: str) -> int:\n```\n"},"/stack/evaluate-reverse-polish-notation":{"mdx":"---\ntitle: '150. Evaluate Reverse Polish Notation'\ndescription: You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Math\", slug: \"math\" }\n - { name: \"Stack\", slug: \"stack\" }\nvideo: \"iu0082c4HDE\"\nissue: \"https://github.com/prdlk/leetcode/issues/69\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Google\", period: \"Three Months\", frequency: 17.3 }\n - { name: \"Yandex\", period: \"Six Months\", frequency: 59.6 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 38.7 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\nsidebar:\n label: 'Evaluate Reverse Polish Notation'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `tokens = [\"2\",\"1\",\"+\",\"3\",\"*\"]`\n- Output: `9`\n- Explanation: `((2 + 1`) * `3`) = `9`\n\n### Example 2:\n- Input: `tokens = [\"4\",\"13\",\"5\",\"/\",\"+\"]`\n- Output: `6`\n- Explanation: `(4 + (13 / 5`)) = `6`\n\n### Example 3:\n- Input: `tokens = [\"10\",\"6\",\"9\",\"3\",\"+\",\"-11\",\"*\",\"/\",\"*\",\"17\",\"+\",\"5\",\"+\"]`\n- Output: `22`\n- Explanation: `((10 * (6 / ((9 + 3`) * `-11`))) + `17`) + `5 = ((10 * (6 / (12 * -11`))) + `17`) + `5 = ((10 * (6 / -132`)) + `17`) + `5 = ((10 * 0`) + `17`) + `5 = (0 + 17`) + `5 = 17 + 5 = 22`\n\n### Constraints:\n\n- `1 <= tokens.length <= 10^4`\n- `tokens[i]` is either an operator: \"+\", \"-\", \"*\", or \"/\", or an integer in the range [-200, 200].\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"evalRPN(tokens)\"]) --> I[\"stack = [], operations = + - * /\"]\n I --> L{\"more token c?\"}\n L -- no --> E([\"return stack[0]\"])\n L -- yes --> Q{\"c is an operator?\"}\n Q -- no --> N[\"stack.append(int(c))\"]\n N --> L\n Q -- yes --> P[\"y = stack.pop(), x = stack.pop() — order matters\"]\n P --> C[\"stack.append(operations[c](x, y))\"]\n C --> L\n```\n\n## Solution\n\n```py\nclass Solution:\n def evalRPN(self, tokens: List[str]) -> int:\n stack = []\n operations = {\n \"+\": lambda x, y: int(x + y),\n \"-\": lambda x, y: int(x - y),\n \"*\": lambda x, y: int(x * y),\n \"/\": lambda x, y: int(x / y),\n }\n\n for c in tokens:\n if c in operations:\n y = stack.pop()\n x = stack.pop()\n calc_func = operations[c]\n result = calc_func(x, y)\n stack.append(result)\n else:\n stack.append(int(c))\n\n return stack[0]\n```\n"},"/stack/min-stack":{"mdx":"---\ntitle: '155. Min Stack'\ndescription: Design a stack that supports push, pop, top, and retrieving the minimum element in constant time\nicon: dot\ntopics:\n - { name: \"Stack\", slug: \"stack\" }\n - { name: \"Design\", slug: \"design\" }\nvideo: \"qkLl7nAwDPo\"\nissue: \"https://github.com/prdlk/leetcode/issues/68\"\ncompanies:\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 28.9 }\n - { name: \"Google\", period: \"Three Months\", frequency: 28.8 }\n - { name: \"Odoo\", period: \"Six Months\", frequency: 100 }\n - { name: \"UiPath\", period: \"Six Months\", frequency: 100 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 67.9 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 45.6 }\nsidebar:\n label: 'Min Stack'\n badge: 'Medium'\n---\n\n::::warning\nYou must implement a solution with O(1) time complexity for each function.\n::::\n\n### Example 1:\n- Input: ``\n- Output: ``\n- Explanation: Input `[\"MinStack\",\"push\",\"push\",\"push\",\"getMin\",\"pop\",\"top\",\"getMin\"]` [[],[-2],[0],[-3],[],[],[],[]] Output `[null,null,null,null,-3,null,0,-2]` Explanation MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); // return `-3` minStack.pop(); minStack.top(); // return `0` minStack.getMin(); // return `-2`\n\n### Constraints:\n\n- `-2^31 <= val <= 2^31 - 1`\n- Methods pop, top and getMin operations will always be called on non-empty stacks.\n- At most 3 * 10^4 calls will be made to push, pop, top, and getMin.\n\n## Approach\n\n```mermaid\nflowchart TD\n subgraph state[\"two parallel stacks — same depth, always\"]\n A[\"stack — the values\"]\n B[\"minStack — the minimum as of that depth\"]\n end\n state --> P\n subgraph P[\"push(value)\"]\n P1[\"stack.append(value)\"] --> P2[\"minStack.append(min(value, minStack[-1]))\"]\n end\n state --> O\n subgraph O[\"pop()\"]\n O1[\"pop both stacks together\"]\n end\n state --> R\n subgraph R[\"top() / getMin() — O(1)\"]\n R1([\"stack[-1] / minStack[-1]\"])\n end\n```\n\n## Solution\n\n```py\nclass MinStack:\n def __init__(self):\n self.stack = []\n self.minStack = []\n\n def push(self, value: int) -> None:\n self.stack.append(value)\n value = min(value, self.minStack[-1] if self.minStack else value)\n self.minStack.append(value)\n\n def pop(self) -> None:\n self.stack.pop()\n self.minStack.pop()\n\n def top(self) -> int:\n return self.stack[-1]\n\n def getMin(self) -> int:\n return self.minStack[-1]\n\n\n# Your MinStack object will be instantiated and called as such:\n# obj = MinStack()\n# obj.push(value)\n# obj.pop()\n# param_3 = obj.top()\n# param_4 = obj.getMin()\n```\n"},"/stack/simplify-path":{"mdx":"---\ntitle: '71. Simplify Path'\ndescription: You are given an absolute path for a Unix-style file system, which always begins with a slash '/'. Your task is to transform this absolute path into its simplified canonical path\nicon: dot\ntopics:\n - { name: \"String\", slug: \"string\" }\n - { name: \"Stack\", slug: \"stack\" }\nissue: \"https://github.com/prdlk/leetcode/issues/70\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Google\", period: \"Three Months\", frequency: 28.8 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 21.2 }\n - { name: \"Snowflake\", period: \"Six Months\", frequency: 58.7 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 50.6 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 24.1 }\nsidebar:\n label: 'Simplify Path'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `path = \"/home/\"`\n- Output: `\"/home\"`\n- Explanation: The trailing slash should be removed.\n\n### Example 2:\n- Input: `path = \"/home//foo/\"`\n- Output: `\"/home/foo\"`\n- Explanation: Multiple consecutive slashes are replaced by a single one.\n\n### Example 3:\n- Input: `path = \"/home/user/Documents/../Pictures\"`\n- Output: `\"/home/user/Pictures\"`\n- Explanation: A double period \"..\" refers to the directory up a level (the parent directory).\n\n### Example 4:\n- Input: `path = \"/../\"`\n- Output: `\"/\"`\n- Explanation: Going one level up from the root directory is not possible.\n\n### Example 5:\n- Input: `path = \"/.../a/../b/c/../d/./\"`\n- Output: `\"/.../b/d\"`\n- Explanation: \"...\" is a valid name for a directory in this problem.\n\n### Constraints:\n\n- `1 <= path.length <= 3000`\n- `path` consists of English letters, digits, period '.', slash '/' or '_'.\n- `path` is a valid absolute Unix path.\n\n## Solution\n\n```py\nclass Solution:\n def simplifyPath(self, path: str) -> str:\n stack = []\n segments = [item for item in path.split(\"/\") if item]\n\n for p in segments:\n if stack and p == \"..\":\n stack.pop()\n elif not stack and p == \"..\" or p == \".\":\n continue\n else:\n stack.append(p)\n\n return \"/\" + \"/\".join(stack)\n```\n"},"/stack/valid-parentheses":{"mdx":"---\ntitle: '20. Valid Parentheses'\ndescription: Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid\nicon: dot\ntopics:\n - { name: \"String\", slug: \"string\" }\n - { name: \"Stack\", slug: \"stack\" }\n - { name: \"Bracket Sequences\", slug: \"bracket-sequences\" }\nvideo: \"WTzjTskDFMg\"\nissue: \"https://github.com/prdlk/leetcode/issues/67\"\ncompanies:\n - { name: \"BlackRock\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 61.3 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 42.3 }\n - { name: \"Accolite\", period: \"Three Months\", frequency: 100 }\n - { name: \"Intuit\", period: \"Three Months\", frequency: 73.2 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 68.3 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 65.3 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 57.2 }\n - { name: \"Autodesk\", period: \"Six Months\", frequency: 100 }\n - { name: \"Criteo\", period: \"Six Months\", frequency: 100 }\n - { name: \"Odoo\", period: \"Six Months\", frequency: 100 }\n - { name: \"Tripadvisor\", period: \"Six Months\", frequency: 100 }\n - { name: \"Virtu Financial\", period: \"Six Months\", frequency: 100 }\n - { name: \"IBM\", period: \"Six Months\", frequency: 91.1 }\n - { name: \"Epic Systems\", period: \"Six Months\", frequency: 89.6 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 85.6 }\nsidebar:\n label: 'Valid Parentheses'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `s = \"()\"`\n- Output: `true`\n\n### Example 2:\n- Input: `s = \"()[]{}\"`\n- Output: `true`\n\n### Example 3:\n- Input: `s = \"(]\"`\n- Output: `false`\n\n### Example 4:\n- Input: `s = \"([])\"`\n- Output: `true`\n\n### Example 5:\n- Input: `s = \"([)]\"`\n- Output: `false`\n\n### Constraints:\n\n- `1 <= s.length <= 10^4`\n- `s` consists of parentheses only '()[]{}'.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"isValid(s)\"]) --> I[\"stack = [], pairs = closer to opener\"]\n I --> L{\"more char c?\"}\n L -- no --> Z{\"stack empty?\"}\n Z -- yes --> T([\"return True\"])\n Z -- no --> U([\"return False — unclosed openers left\"])\n L -- yes --> Q{\"c is a closer?\"}\n Q -- no --> A[\"stack.append(c) — it is an opener\"]\n A --> L\n Q -- yes --> M{\"stack non-empty and pairs[c] == stack[-1]?\"}\n M -- yes --> P[\"stack.pop() — matched\"]\n P --> L\n M -- no --> F([\"return False — mismatch\"])\n```\n\n## Solution\n\n```py\nclass Solution:\n def isValid(self, s: str) -> bool:\n stack = []\n pairs = {\")\": \"(\", \"]\": \"[\", \"}\": \"{\"}\n\n for c in s:\n if c in pairs:\n if stack and pairs[c] == stack[-1]:\n stack.pop()\n else:\n return False\n else:\n stack.append(c)\n\n return True if not stack else False\n```\n"},"/topological-sort":{"mdx":"---\ntitle: Topological Sort\ndescription: 'Topological sort or topological ordering of a directed graph is an ordering of nodes such that every node appears in the ordering before all the nodes it points to. '\nhidden: true\n---\n\n## Directed Graph\n\nBefore we get started on topological order, let's talk about directed graphs first. A graph is directed when its edges have directions (these edges are also called arcs). Suppose that v, w are vertices in a directed graph. Let's get familiar with a few concepts.\n\n- **in-edge** - an edge pointing to or coming into the node\n- **out-edge** - an edge pointing away or going out from the node (we will refer those nodes on the other end of the out-edges of v to v's neighbors)\n- **in-degree** - the number of in-edges coming into the node\n- **out-degree** - the number of out-edges going out from the node\n- **dependency** - \"v depends on w\" / \"v is dependent on w\" means that there is a directed path from w to v where v, w are vertices in a directed graph. With these new terms under our belt, it will make more sense when we talk about directed graphs.\n\n## What is a topological order?\n\nTopological sort is not unique. For example, for a graph like this:\n\n\n\nBoth `[4, 5, 2]` and `[5, 4, 2]` are valid topological orders. \n\n\n\nTask 3 is completely independent of task 2 and 4, and it can be anywhere in the order as long as it is before task 1 which depends on it. All of the following ordering are valid topological orderings.\n\n`[4, 2, 3, 1], [4, 3, 2, 1], [3, 4, 2, 1]`\n\n\n:::warning[Graphs with Cycles Do Not Have Topological Ordering]\nIt should be obvious that if a graph with a cycle does not have a topological ordering. In the above example, 2 has to come before 5 which has come before 4 which has to come before 2 which has to come before 5... which is impossible.\n:::\n\n## Kahn's Algorithm\n\nTo obtain a topological order, we can use Kahn's algorithm which is very similar to Breadth First Search.\n\nIn order to understand why we must use this algorithm we first look at the problem it is trying to solve.\n\nGiven a directed graph does there exist a way to remove the nodes such that each time we remove a node we guarantee that no other nodes point to that particular node?\n\nFor this algorithm, we systematically remove one node at a time, each time removing a node such that no other nodes point to that node (in-degree is 0). If no such node exists, then there must be a cycle, and there is no way to order the nodes such that \"every node appears in the ordering before all the nodes it points to\" (no solution). After removing the current node, for each neighboring node the current node points to, we check whether any nodes point to this node. If there isn't any, we push this node into the queue. Notice it is important to keep track of the number of nodes pointing to a node (in-degree) in the question as we only push the node into the queue once all nodes the current node depended on have been removed. \n\nHere is a graphic to demonstrate the idea.\n\n\n \n \n \n \n \n \n \n \n \n \n\n## Example Code\n\n:::tip\nNotice the topological sort algorithm is very similar to BFS. The main difference is that we only push nodes with 0 in-degree into the queue in topological sort whereas in BFS we push all the neighboring nodes into the queue.\n:::\n\n```py\nfrom collections import deque\n\ndef find_indegree(graph):\n indegree = { node: 0 for node in graph } # dict\n for node in graph:\n for neighbor in graph[node]:\n indegree[neighbor] += 1\n return indegree\n\n\ndef topo_sort(graph):\n res = []\n q = deque()\n indegree = find_indegree(graph)\n for node in indegree:\n if indegree[node] == 0:\n q.append(node)\n while len(q) > 0:\n node = q.popleft()\n res.append(node)\n for neighbor in graph[node]:\n indegree[neighbor] -= 1\n if indegree[neighbor] == 0:\n q.append(neighbor)\n return res if len(graph) == len(res) else None\n```\n"},"/topological-sort/course-schedule":{"mdx":"---\ntitle: '207. Course Schedule'\ndescription: There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai\nicon: dot\ntopics:\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Graph Theory\", slug: \"graph\" }\n - { name: \"Topological Sort\", slug: \"topological-sort\" }\n - { name: \"Directed Acyclic Graph\", slug: \"directed-acyclic-graph\" }\nissue: \"https://github.com/prdlk/leetcode/issues/142\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 69.6 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"TikTok\", period: \"Three Months\", frequency: 70.9 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 43.3 }\n - { name: \"ByteDance\", period: \"Six Months\", frequency: 88.6 }\n - { name: \"Walmart Labs\", period: \"Six Months\", frequency: 70.5 }\n - { name: \"Anduril\", period: \"Six Months\", frequency: 65.4 }\n - { name: \"Snowflake\", period: \"Six Months\", frequency: 58.7 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 57.9 }\n - { name: \"Infosys\", period: \"Six Months\", frequency: 56.2 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 44.8 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 32 }\nsidebar:\n label: 'Course Schedule'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `numCourses = 2, prerequisites = [[1,0]]`\n- Output: `true`\n- Explanation: There are a total of `2` courses to take. To take course `1` you should have finished course `0`. So it is possible.\n\n### Example 2:\n- Input: `numCourses = 2, prerequisites = [[1,0],[0,1]]`\n- Output: `false`\n- Explanation: There are a total of `2` courses to take. To take course `1` you should have finished course `0`, and to take course `0` you should also have finished course `1`. So it is impossible.\n\n### Constraints:\n\n- `1 <= numCourses <= 2000`\n- `0 <= prerequisites.length <= 5000`\n- `prerequisites[i].length == 2`\n- `0 <= ai, bi < numCourses`\n- All the pairs `prerequisites[i]` are unique.\n\n## Solution\n\n```py\nclass Solution:\n def canFinish(self, numCourses: int, prerequisites: list[list[int]]) -> bool:\n indegree = [0] * numCourses\n adj = [[] for x in range(numCourses)]\n for prereq in prerequisites:\n adj[prereq[1]].append(prereq[0])\n indegree[prereq[0]] += 1\n\n queue = []\n\n for i in range(numCourses):\n if indegree[i] == 0:\n queue.append(i)\n\n visited = 0\n while queue:\n node = queue.pop(0)\n visited += 1\n for neighbor in adj[node]:\n indegree[neighbor] -= 1\n if indegree[neighbor] == 0:\n queue.append(neighbor)\n\n return numCourses == visited\n```\n"},"/topological-sort/course-schedule-ii":{"mdx":"---\ntitle: '210. Course Schedule II'\ndescription: There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai\nicon: dot\ntopics:\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Graph Theory\", slug: \"graph\" }\n - { name: \"Topological Sort\", slug: \"topological-sort\" }\nissue: \"https://github.com/prdlk/leetcode/issues/143\"\ncompanies:\n - { name: \"Salesforce\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Walmart Labs\", period: \"Thirty Days\", frequency: 100 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 80.4 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 39.1 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 36.8 }\n - { name: \"Google\", period: \"Three Months\", frequency: 23.9 }\n - { name: \"Audible\", period: \"Six Months\", frequency: 100 }\n - { name: \"Aurora\", period: \"Six Months\", frequency: 100 }\n - { name: \"Anduril\", period: \"Six Months\", frequency: 92.8 }\n - { name: \"Netflix\", period: \"Six Months\", frequency: 90.1 }\n - { name: \"DoorDash\", period: \"Six Months\", frequency: 89.7 }\n - { name: \"Snowflake\", period: \"Six Months\", frequency: 79.8 }\n - { name: \"Uber\", period: \"Six Months\", frequency: 55.2 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 32 }\nsidebar:\n label: 'Course Schedule II'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `numCourses = 2, prerequisites = [[1,0]]`\n- Output: `[0,1]`\n- Explanation: There are a total of `2` courses to take. To take course `1` you should have finished course `0`. So the correct course order is `[0,1]`.\n\n### Example 2:\n- Input: `numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]`\n- Output: `[0,2,1,3]`\n- Explanation: There are a total of `4` courses to take. To take course `3` you should have finished both courses `1` and `2`. Both courses `1` and `2` should be taken after you finished course `0`. So one correct course order is `[0,1,2,3]`. Another correct ordering is `[0,2,1,3]`.\n\n### Example 3:\n- Input: `numCourses = 1, prerequisites = []`\n- Output: `[0]`\n\n### Constraints:\n\n- `1 <= numCourses <= 2000`\n- `0 <= prerequisites.length <= numCourses * (numCourses - 1)`\n- `prerequisites[i].length == 2`\n- `0 <= ai, bi < numCourses`\n- `ai != bi`\n- All the pairs [ai, bi] are distinct.\n\n## Solution\n\n```py\nclass Solution:\n def findOrder(self, numCourses: int, prerequisites: list[list[int]]) -> list[int]:\n prereq = {c: [] for c in range(numCourses)}\n for crs, pre in prerequisites:\n prereq[crs].append(pre)\n\n # course has 3 possible states:\n # visited -> crs has been added to output\n # visiting -> crs not added to output, but added to cycle\n # unvisited -> crs not added to output nor cycle\n output = []\n visit, cycle = set(), set()\n\n def dfs(crs):\n if crs in cycle:\n return False\n if crs in visit:\n return True\n\n cycle.add(crs)\n for pre in prereq[crs]:\n if dfs(pre) == False:\n return False\n\n cycle.remove(crs)\n visit.add(crs)\n output.append(crs)\n return True\n\n for c in range(numCourses):\n if dfs(c) == False:\n return []\n\n return output\n```\n"},"/tree-bfs":{"mdx":"---\ntitle: Tree BFS\ndescription: 'All nodes at one level are visited before the nodes at the next level.'\nhidden: true\n---\n"},"/tree-bfs/binary-tree-level-order-traversal":{"mdx":"---\ntitle: '102. Binary Tree Level Order Traversal'\ndescription: Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level)\nicon: dot\ntopics:\n - { name: \"Tree\", slug: \"tree\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Binary Tree\", slug: \"binary-tree\" }\nvideo: \"6ZnyEApgFYg\"\nissue: \"https://github.com/prdlk/leetcode/issues/106\"\ncompanies:\n - { name: \"Google\", period: \"Three Months\", frequency: 41 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 28.9 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 32 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\nsidebar:\n label: 'Binary Tree Level Order Traversal'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `root = [3,9,20,null,null,15,7]`\n- Output: `[[3],[9,20],[15,7]]`\n\n### Example 2:\n- Input: `root = [1]`\n- Output: `[[1]]`\n\n### Example 3:\n- Input: `root = []`\n- Output: `[]`\n\n### Constraints:\n\n- The number of nodes in the tree is in the range [0, 2000].\n- `-1000 <= Node.val <= 1000`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"levelOrder(root)\"]) --> I[\"res = [], q = deque(), q.append(root)\"]\n I --> W{\"q non-empty?\"}\n W -- no --> E([\"return res\"])\n W -- yes --> L[\"qLen = len(q), level = [] — qLen freezes the current level's size\"]\n L --> F{\"more i in range(qLen)?\"}\n F -- no --> V{\"level non-empty?\"}\n V -- yes --> A[\"res.append(level)\"]\n A --> W\n V -- no --> W\n F -- yes --> P[\"node = q.popleft()\"]\n P --> N{\"node is not None?\"}\n N -- no --> F\n N -- yes --> C[\"level.append(node.val), q.append(node.left), q.append(node.right) — None children are filtered on the next level\"]\n C --> F\n```\n\n## Solution\n\n```py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nfrom collections import deque\n\n\nclass Solution:\n # T: O(n) S: O(n)\n def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:\n res = []\n\n q = deque()\n q.append(root)\n\n while q:\n qLen = len(q)\n level = []\n for i in range(qLen):\n node = q.popleft()\n if node:\n level.append(node.val)\n q.append(node.left)\n q.append(node.right)\n if level:\n res.append(level)\n\n return res\n```\n"},"/tree-bfs/binary-tree-right-side-view":{"mdx":"---\ntitle: '199. Binary Tree Right Side View'\ndescription: Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom\nicon: dot\ntopics:\n - { name: \"Tree\", slug: \"tree\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Binary Tree\", slug: \"binary-tree\" }\nvideo: \"d4zLyf32e3I\"\nissue: \"https://github.com/prdlk/leetcode/issues/107\"\ncompanies:\n - { name: \"Yandex\", period: \"Three Months\", frequency: 76.1 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 42.8 }\n - { name: \"Google\", period: \"Three Months\", frequency: 23.9 }\n - { name: \"Oracle\", period: \"Six Months\", frequency: 78.7 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 62.3 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 32 }\nsidebar:\n label: 'Binary Tree Right Side View'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `root = [1,2,3,null,5,null,4]`\n- Output: `[1,3,4]`\n\n### Example 2:\n- Input: `root = [1,2,3,4,null,null,null,5]`\n- Output: `[1,3,4,5]`\n\n### Example 3:\n- Input: `root = [1,null,3]`\n- Output: `[1,3]`\n\n### Example 4:\n- Input: `root = []`\n- Output: `[]`\n\n### Constraints:\n\n- The number of nodes in the tree is in the range [0, 100].\n- `-100 <= Node.val <= 100`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"rightSideView(root)\"]) --> I[\"res = [], q = deque([root])\"]\n I --> W{\"q non-empty?\"}\n W -- no --> E([\"return res\"])\n W -- yes --> L[\"rightSide = None, qLen = len(q) — qLen freezes the current level's size\"]\n L --> F{\"more i in range(qLen)?\"}\n F -- no --> V{\"rightSide is not None?\"}\n V -- yes --> A[\"res.append(rightSide.val) — the last real node popped on this level is the rightmost\"]\n A --> W\n V -- no --> W\n F -- yes --> P[\"node = q.popleft()\"]\n P --> N{\"node is not None?\"}\n N -- no --> F\n N -- yes --> C[\"rightSide = node, q.append(node.left), q.append(node.right)\"]\n C --> F\n```\n\n## Solution\n\n```py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nfrom collections import deque\n\n\nclass Solution:\n def rightSideView(self, root: Optional[TreeNode]) -> List[int]:\n res = []\n q = deque([root])\n\n while q:\n rightSide = None\n qLen = len(q)\n\n for i in range(qLen):\n node = q.popleft()\n if node:\n rightSide = node\n q.append(node.left)\n q.append(node.right)\n\n if rightSide:\n res.append(rightSide.val)\n\n return res\n```\n"},"/tree-bfs/count-good-nodes-in-binary-tree":{"mdx":"---\ntitle: '1448. Count Good Nodes in Binary Tree'\ndescription: Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X\nicon: dot\ntopics:\n - { name: \"Tree\", slug: \"tree\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Binary Tree\", slug: \"binary-tree\" }\nvideo: \"7cp5imvDzl4\"\nissue: \"https://github.com/prdlk/leetcode/issues/108\"\ncompanies:\n - { name: \"josh technology\", period: \"Six Months\", frequency: 74 }\n - { name: \"Google\", period: \"Six Months\", frequency: 11.8 }\nsidebar:\n label: 'Count Good Nodes in Binary Tree'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `root = [3,1,4,3,null,1,5]`\n- Output: `4`\n- Explanation: Nodes in blue are good. Root Node `(3`) is always a good node. Node `4` -> (3,4) is the maximum value in the path starting from the `root`. Node `5` -> (3,4,5) is the maximum value in the path Node `3` -> (3,1,3) is the maximum value in the path.\n\n### Example 2:\n- Input: `root = [3,3,null,4,2]`\n- Output: `3`\n- Explanation: Node `2` -> `(3`, `3`, `2`) is not good, because `\"3`\" is higher than it.\n\n### Example 3:\n- Input: `root = [1]`\n- Output: `1`\n- Explanation: Root is considered as good.\n\n### Constraints:\n\n- The number of nodes in the binary tree is in the range [1, 10^5].\n- Each node's value is between [-10^4, 10^4].\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"goodNodes(root)\"]) --> C[\"dfs(root, root.val) — the root is always good\"]\n C --> E([\"return that count\"])\n subgraph dfs[\"dfs(node, max_val)\"]\n D1{\"node is None?\"} -- yes --> D2([\"return 0\"])\n D1 -- no --> D3[\"is_good = 1 if node.val >= max_val else 0 — max_val is the largest value on the path so far\"]\n D3 --> D4[\"new_max = max(max_val, node.val)\"]\n D4 --> D5[\"l = dfs(node.left, new_max), r = dfs(node.right, new_max)\"]\n D5 --> D6([\"return is_good + l + r\"])\n end\n C --> dfs\n```\n\n## Solution\n\n```py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def goodNodes(self, root: TreeNode) -> int:\n def dfs(node, max_val):\n if not node:\n return 0\n\n is_good = 1 if node.val >= max_val else 0\n new_max = max(max_val, node.val)\n\n return is_good + dfs(node.left, new_max) + dfs(node.right, new_max)\n\n return dfs(root, root.val)\n```\n"},"/tree-dfs":{"mdx":"---\ntitle: Tree DFS\ndescription: 'Each branch is followed to its end before the next branch is examined.'\nhidden: true\n---\n"},"/tree-dfs/invert-binary-tree":{"mdx":"---\ntitle: '226. Invert Binary Tree'\ndescription: Given the root of a binary tree, invert the tree, and return its root\nicon: dot\ntopics:\n - { name: \"Tree\", slug: \"tree\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Binary Tree\", slug: \"binary-tree\" }\nvideo: \"OnSn2XEQ4MY\"\nissue: \"https://github.com/prdlk/leetcode/issues/96\"\ncompanies:\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 57.2 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 32.7 }\nsidebar:\n label: 'Invert Binary Tree'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `root = [4,2,7,1,3,6,9]`\n- Output: `[4,7,2,9,6,3,1]`\n\n### Example 2:\n- Input: `root = [2,1,3]`\n- Output: `[2,3,1]`\n\n### Example 3:\n- Input: `root = []`\n- Output: `[]`\n\n### Constraints:\n\n- The number of nodes in the tree is in the range [0, 100].\n- `-100 <= Node.val <= 100`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"invertTree(root)\"]) --> B{\"root is None?\"}\n B -- yes --> Z([\"return None\"])\n B -- no --> L[\"left = invertTree(root.left), right = invertTree(root.right) — both subtrees are already inverted\"]\n L --> X[\"root.left = right, root.right = left\"]\n X --> E([\"return root\"])\n```\n\n## Solution\n\n```py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:\n if not root:\n return None\n\n left = self.invertTree(root.left)\n right = self.invertTree(root.right)\n\n root.left = right\n root.right = left\n\n return root\n```\n"},"/tree-dfs/lowest-common-ancestor-of-a-binary-tree":{"mdx":"---\ntitle: '236. Lowest Common Ancestor of a Binary Tree'\ndescription: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree\nicon: dot\ntopics:\n - { name: \"Tree\", slug: \"tree\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Binary Tree\", slug: \"binary-tree\" }\n - { name: \"Binary Lifting\", slug: \"binary-lifting\" }\n - { name: \"Lowest Common Ancestor\", slug: \"lowest-common-ancestor\" }\nissue: \"https://github.com/prdlk/leetcode/issues/98\"\ncompanies:\n - { name: \"BlackRock\", period: \"Thirty Days\", frequency: 76.6 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 55.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 36.7 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 36.8 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 56.1 }\nsidebar:\n label: 'Lowest Common Ancestor of a Binary Tree'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1`\n- Output: `3`\n- Explanation: The LCA of nodes `5` and `1` is `3`.\n\n### Example 2:\n- Input: `root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4`\n- Output: `5`\n- Explanation: The LCA of nodes `5` and `4` is `5`, since a node can be a descendant of itself according to the LCA definition.\n\n### Example 3:\n- Input: `root = [1,2], p = 1, q = 2`\n- Output: `1`\n\n### Constraints:\n\n- The number of nodes in the tree is in the range [2, 10^5].\n- `-10^9 <= Node.val <= 10^9`\n- All Node.val are unique.\n- `p != q`\n- p and q will exist in the tree.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"lowestCommonAncestor(root, p, q)\"]) --> B{\"not root?\"}\n B -- yes --> N([\"return None\"])\n B -- no --> M{\"root == p or root == q?\"}\n M -- yes --> R([\"return root — a node is its own ancestor\"])\n M -- no --> L[\"l = self.lowestCommonAncestor(root.left, p, q)\"]\n L --> Q[\"r = self.lowestCommonAncestor(root.right, p, q)\"]\n Q --> D{\"l and r?\"}\n D -- yes --> A([\"return root — p and q were found on different sides\"])\n D -- no --> E([\"return l or r — pass up whichever side found something\"])\n```\n\n## Solution\n\n```py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.right = None\n\n\nclass Solution:\n def lowestCommonAncestor(\n self, root: \"TreeNode\", p: \"TreeNode\", q: \"TreeNode\"\n ) -> \"TreeNode\":\n\n if not root:\n return None\n\n if root == p or root == q:\n return root\n\n l = self.lowestCommonAncestor(root.left, p, q)\n r = self.lowestCommonAncestor(root.right, p, q)\n\n if l and r:\n return root\n else:\n return l or r\n```\n"},"/tree-dfs/maximum-depth-of-binary-tree":{"mdx":"---\ntitle: '104. Maximum Depth of Binary Tree'\ndescription: Given the root of a binary tree, return its maximum depth\nicon: dot\ntopics:\n - { name: \"Tree\", slug: \"tree\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Binary Tree\", slug: \"binary-tree\" }\nvideo: \"hTM3phVI6YQ\"\nissue: \"https://github.com/prdlk/leetcode/issues/97\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 83.1 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 42.3 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 21.2 }\n - { name: \"LinkedIn\", period: \"Six Months\", frequency: 54.1 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 32.7 }\nsidebar:\n label: 'Maximum Depth of Binary Tree'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `root = [3,9,20,null,null,15,7]`\n- Output: `3`\n\n### Example 2:\n- Input: `root = [1,null,2]`\n- Output: `2`\n\n### Constraints:\n\n- The number of nodes in the tree is in the range [0, 10^4].\n- `-100 <= Node.val <= 100`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"maxDepth(root)\"]) --> B{\"root is None?\"}\n B -- yes --> Z([\"return 0\"])\n B -- no --> L[\"l = maxDepth(root.left), r = maxDepth(root.right)\"]\n L --> E([\"return 1 + max(l, r) — this node adds one level above its deeper subtree\"])\n```\n\n## Solution\n\n```py\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, val=0, left=None, right=None):\n# self.val = val\n# self.left = left\n# self.right = right\nclass Solution:\n def maxDepth(self, root: Optional[TreeNode]) -> int:\n if not root:\n return 0\n\n return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))\n```\n"},"/trie":{"mdx":"---\ntitle: Trie\ndescription: 'Each node holds one character, and a path from the root spells a word.'\nhidden: true\n---\n"},"/trie/design-add-and-search-words-data-structure":{"mdx":"---\ntitle: '211. Design Add and Search Words Data Structure'\ndescription: Design a data structure that supports adding new words and finding if a string matches any previously added string\nicon: dot\ntopics:\n - { name: \"String\", slug: \"string\" }\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Design\", slug: \"design\" }\n - { name: \"Trie\", slug: \"trie\" }\nvideo: \"BTf05gs_8iU\"\nissue: \"https://github.com/prdlk/leetcode/issues/128\"\ncompanies:\n - { name: \"Amazon\", period: \"Three Months\", frequency: 21.2 }\n - { name: \"Snowflake\", period: \"Six Months\", frequency: 58.7 }\nsidebar:\n label: 'Design Add and Search Words Data Structure'\n badge: 'Medium'\n---\n\n### Constraints:\n\n- `1 <= word.length <= 25`\n- `word` in addWord consists of lowercase English letters.\n- `word` in search consist of '.' or lowercase English letters.\n- There will be at most 2 dots in `word` for search queries.\n- At most 10^4 calls will be made to addWord and search.\n\n## Approach\n\n```mermaid\nflowchart TD\n subgraph state[\"state — root TrieNode; every node holds a children dict and an endOfWord flag\"]\n R[\"root\"] --- C[\"... children[c] ...\"] --- L[\"endOfWord = True — a word ends here\"]\n end\n subgraph addWord[\"addWord(word)\"]\n A1[\"cur = root\"] --> A2{\"more c in word?\"}\n A2 -- yes --> A3{\"c not in cur.children?\"}\n A3 -- yes --> A4[\"cur.children[c] = TrieNode()\"]\n A4 --> A5[\"cur = cur.children[c]\"]\n A3 -- no --> A5\n A5 --> A2\n A2 -- no --> A6[\"cur.endOfWord = True\"]\n A6 --> A7([\"done\"])\n end\n subgraph search[\"search(word)\"]\n S1[\"dfs(0, root) — match from index 0 at the root\"] --> S2[\"cur = root\"]\n S2 --> S3{\"more i in range(j, len(word))?\"}\n S3 -- no --> S4([\"return cur.endOfWord\"])\n S3 -- yes --> S5[\"c = word[i]\"]\n S5 --> S6{\"c == '.'?\"}\n S6 -- yes --> S7{\"more child in cur.children.values()?\"}\n S7 -- yes --> S8{\"dfs(i + 1, child)? — try the rest of the word from this child\"}\n S8 -- yes --> S9([\"return True\"])\n S8 -- no --> S7\n S7 -- no --> S10([\"return False — no child matched the wildcard\"])\n S6 -- no --> S11{\"c not in cur.children?\"}\n S11 -- yes --> S12([\"return False\"])\n S11 -- no --> S13[\"cur = cur.children[c]\"]\n S13 --> S3\n end\n state --> addWord\n state --> search\n```\n\n## Solution\n\n```py\nclass TrieNode:\n def __init__(self):\n self.children = {}\n self.endOfWord = False\n\n\nclass WordDictionary:\n def __init__(self):\n self.root = TrieNode()\n\n def addWord(self, word: str) -> None:\n cur = self.root\n for c in word:\n if c not in cur.children:\n cur.children[c] = TrieNode()\n cur = cur.children[c]\n cur.endOfWord = True\n\n # Leverage Recursion\n def search(self, word: str) -> bool:\n def dfs(j, root):\n cur = root\n for i in range(j, len(word)):\n c = word[i]\n\n if c == \".\":\n for child in cur.children.values():\n if dfs(i + 1, child):\n return True\n return False\n else:\n if c not in cur.children:\n return False\n cur = cur.children[c]\n return cur.endOfWord\n\n return dfs(0, self.root)\n\n\n# Your WordDictionary object will be instantiated and called as such:\n# obj = WordDictionary()\n# obj.addWord(word)\n# param_3 = obj.search(word)\n```\n"},"/trie/implement-trie-prefix-tree":{"mdx":"---\ntitle: '208. Implement Trie (Prefix Tree)'\ndescription: A trie (pronounced as \"try\") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker\nicon: dot\ntopics:\n - { name: \"Hash Table\", slug: \"hash-table\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Design\", slug: \"design\" }\n - { name: \"Trie\", slug: \"trie\" }\nvideo: \"oobqoCJlHA0\"\nissue: \"https://github.com/prdlk/leetcode/issues/127\"\ncompanies:\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 48.1 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 37.9 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 34.2 }\nsidebar:\n label: 'Implement Trie (Prefix Tree)'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: ``\n- Output: ``\n- Explanation: Input [\"Trie\", \"insert\", \"search\", \"search\", \"startsWith\", \"insert\", \"search\"] `[[]`, `[\"apple\"]`, `[\"apple\"]`, `[\"app\"]`, `[\"app\"]`, `[\"app\"]`, [\"app\"]] Output [null, null, true, false, true, null, true] Explanation Trie trie = new Trie(); trie.insert(\"apple\"); trie.search(\"apple\"); // return True trie.search(\"app\"); // return False trie.startsWith(\"app\"); // return True trie.insert(\"app\"); trie.search(\"app\"); // return True\n\n### Constraints:\n\n- `1 <= word.length, prefix.length <= 2000`\n- `word` and `prefix` consist only of lowercase English letters.\n- At most 3 * 10^4 calls in total will be made to insert, search, and startsWith.\n\n## Approach\n\n```mermaid\nflowchart TD\n subgraph state[\"state — root TrieNode; every node holds a children dict and an endOfWord flag\"]\n R[\"root\"] --- C[\"... children[c] ...\"] --- L[\"endOfWord = True — a word ends here\"]\n end\n subgraph insert[\"insert(word)\"]\n I1[\"cur = root\"] --> I2{\"more c in word?\"}\n I2 -- yes --> I3{\"c not in cur.children?\"}\n I3 -- yes --> I4[\"cur.children[c] = TrieNode()\"]\n I4 --> I5[\"cur = cur.children[c]\"]\n I3 -- no --> I5\n I5 --> I2\n I2 -- no --> I6[\"cur.endOfWord = True\"]\n I6 --> I7([\"done\"])\n end\n subgraph search[\"search(word)\"]\n S1[\"cur = root\"] --> S2{\"more c in word?\"}\n S2 -- yes --> S3{\"c not in cur.children?\"}\n S3 -- yes --> S4([\"return False\"])\n S3 -- no --> S5[\"cur = cur.children[c]\"]\n S5 --> S2\n S2 -- no --> S6([\"return cur.endOfWord — the path exists, but is it a whole word?\"])\n end\n subgraph startsWith[\"startsWith(prefix)\"]\n P1[\"cur = root\"] --> P2{\"more c in prefix?\"}\n P2 -- yes --> P3{\"c not in cur.children?\"}\n P3 -- yes --> P4([\"return False\"])\n P3 -- no --> P5[\"cur = cur.children[c]\"]\n P5 --> P2\n P2 -- no --> P6([\"return True — every prefix char had a path\"])\n end\n state --> insert\n state --> search\n state --> startsWith\n```\n\n## Solution\n\n```py\nclass TrieNode:\n def __init__(self):\n self.children = {}\n self.endOfWord = False\n\n\nclass Trie:\n def __init__(self):\n self.root = TrieNode()\n\n def insert(self, word: str) -> None:\n cur = self.root\n\n for c in word:\n if c not in cur.children:\n cur.children[c] = TrieNode()\n cur = cur.children[c]\n cur.endOfWord = True\n\n def search(self, word: str) -> bool:\n cur = self.root\n\n for c in word:\n if c not in cur.children:\n return False\n cur = cur.children[c]\n\n return cur.endOfWord\n\n def startsWith(self, prefix: str) -> bool:\n cur = self.root\n\n for c in prefix:\n if c not in cur.children:\n return False\n cur = cur.children[c]\n\n return True\n\n\n# Your Trie object will be instantiated and called as such:\n# obj = Trie()\n# obj.insert(word)\n# param_2 = obj.search(word)\n# param_3 = obj.startsWith(prefix)\n```\n"},"/two-pointers":{"mdx":"---\ntitle: Two Pointers\ndescription: 'Two indices move through the data from different positions and meet at the answer.'\nhidden: true\n---\n\nMost interview problems in this section fall into one of four families:\n\n- **Same direction pointers**: one pointer reads, one pointer writes, or one pointer stays behind another while scanning left to right.\n- **Opposite direction pointers**: one pointer starts on the left, one on the right, and each move eliminates impossible answers.\n- **Sliding window**: the two pointers define a contiguous interval, and you maintain some property of that interval as the window expands and shrinks.\n- **Fast and slow pointers**: both pointers move through a linked structure, but at different speeds or with a fixed gap.\n\n\n\n## Same Direction\n\nSame-direction pointer problems usually scan from left to right, but the two pointers do different jobs.\n\nUsually:\n\n- one pointer reads the next value from the original input\n- one pointer marks where the next kept value should go\n\nSometimes both pointers move forward together, but one pointer stays behind the other to preserve a gap or a relationship.\n\n### When this family applies\n\n\nReach for same-direction pointers when the problem asks you to:\n\n- rewrite an array in-place\n- remove or compress elements without extra memory\n- keep only the values that satisfy some condition\n- maintain a fixed gap between two forward-moving pointers\n\n### Core Invariant\n\nThe invariant in this family is usually about the prefix you have already processed.\n\nTypical invariant:\n\n> \"Everything before the write pointer is already in the correct final form.\"\n\nThat is what makes these problems feel different from opposite-direction scans. You are not searching from both sides. You are building the answer progressively as you sweep through the input.\n\n### Subtypes in this section\n\n#### 1. Compaction / Overwrite\n\nThe slow pointer marks the next output position. The fast pointer scans the input. When the current value should be kept, copy it forward and advance the slow pointer.\n\n\n\n#### 2. Relative Speed\n\nBoth pointers move forward, but one moves twice as fast. The invariant is about their distance and how much of the structure each pointer has consumed.\n\n\n\n#### 3. Fixed Gap\n\nBoth pointers move forward while preserving a gap. The invariant is not about a rewritten prefix. It is about the distance between the pointers.\n\n\n\n### What changes across problems\n\nThe family stays the same, but the invariant changes slightly:\n\n- in array compaction, the invariant is about the prefix being correct\n- in fast/slow midpoint problems, the invariant is about relative speed\n- in fixed-gap problems, the invariant is about distance\n\nSo the unifying idea is not \"both pointers move right.\" The unifying idea is:\n\n**\"the structure to the left of the pointers already has a known meaning\"**\n\n### What to watch out for\n\nCommon mistakes:\n\n- Updating the write pointer before copying the current value.\n- Forgetting exactly what region is already valid.\n- Losing track of whether the invariant is about a prefix, a gap, or relative speed.\n\nAs you go through the following problems, pay attention to one question:\n\n**What does the region before the slow pointer mean right now?**\n\n## Opposite Direction\n\nOpposite-direction pointer problems place one pointer on the left and one on the right, then move inward.\n\nThe hard part is never the loop itself. **The hard part is justifying which pointer is safe to move.**\n\n### When this family applies\n\nReach for opposite-direction pointers when the problem asks you to:\n\n- the input is sorted, or can be made monotonic by sorting\n- you are comparing a left choice and a right choice\n- the problem has symmetry\n- moving one side lets you eliminate a set of impossible answers\n\n### Core Invariant\n\nThe invariant in this family is usually:\n\n> \"All candidate answers outside the current `[left, right]` range have already been ruled out.\"\n\nThat means each move must come with a proof. If you move a pointer without knowing why the discarded cases cannot contain the optimum, the algorithm is just guessing.\n\n### Subtypes in this section\n\n#### 1. Monotonic sum elimination\n\n\nBecause the array is sorted:\n\n- if the sum is too small, moving the left pointer right is the only useful move\n- if the sum is too large, moving the right pointer left is the only useful move\n\nSorted order gives you a monotonic rule.\n\n\n\n#### 2. Symmetry checking\n\nHere the pointers compare mirrored positions. The movement rule is mostly \"move both,\" except when you must skip punctuation or spaces.\n\n\n\n#### 3. Bottleneck elimination\n\nThis one is important because it looks different from the other two. The key idea is that the shorter wall limits the area. If you keep that shorter wall and decrease the width, you cannot improve the answer. So it is safe to discard that wall.\n\n\n\n### Why this is not a sliding window\n\nOpposite-direction scans do not maintain an interval property like \"this window is valid.\"\n\nInstead, each step does one of these:\n\n- rules out sums that are too small or too large\n- checks mirrored characters\n- discards a bottleneck boundary\n\nThe interval itself is just the remaining search space.\n\n### What to watch out for\n\nCommon mistakes:\n\n- Using two pointers on an unsorted pair problem without first creating monotonicity.\n- Moving the taller wall in Container With Most Water without an elimination proof.\n- Calling the method sliding window just because two boundaries exist.\n\nAs you read the problems in this family, keep asking:\n\n**What exact set of candidate answers did this pointer move eliminate?**\n"},"/two-pointers/3sum":{"mdx":"---\ntitle: '15. 3Sum'\ndescription: Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"Sorting\", slug: \"sorting\" }\nvideo: \"jzZsG8n2R9A\"\nissue: \"https://github.com/prdlk/leetcode/issues/43\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 69.6 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 66.8 }\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 65.6 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 63.8 }\n - { name: \"Apple\", period: \"Three Months\", frequency: 80.4 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 66 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 57.2 }\n - { name: \"Cisco\", period: \"Six Months\", frequency: 100 }\n - { name: \"Wissen Technology\", period: \"Six Months\", frequency: 100 }\n - { name: \"eBay\", period: \"Six Months\", frequency: 75.2 }\n - { name: \"Accenture\", period: \"Six Months\", frequency: 75 }\n - { name: \"Visa\", period: \"Six Months\", frequency: 69.7 }\n - { name: \"Goldman Sachs\", period: \"Six Months\", frequency: 53.1 }\nsidebar:\n label: '3Sum'\n badge: 'Medium'\n---\n\n::::warning\nNotice that the solution set must not contain duplicate triplets.\n::::\n\n### Example 1:\n- Input: `nums = [-1,0,1,2,-1,-4]`\n- Output: `[[-1,-1,2],[-1,0,1]]`\n- Explanation: `nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0`. `nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0`. `nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0`. The distinct triplets are `[-1,0,1]` and `[-1,-1,2]`. Notice that the order of the output and the order of the triplets does not matter.\n\n### Example 2:\n- Input: `nums = [0,1,1]`\n- Output: `[]`\n- Explanation: The only possible triplet does not sum up to `0`.\n\n### Example 3:\n- Input: `nums = [0,0,0]`\n- Output: `[[0,0,0]]`\n- Explanation: The only possible triplet sums up to `0`.\n\n### Constraints:\n\n- `3 <= nums.length <= 3000`\n- `-10^5 <= nums[i] <= 10^5`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"threeSum(nums)\"]) --> O[\"nums.sort() — duplicates become adjacent\"]\n O --> F{\"more i in 0..n-1?\"}\n F -- no --> E([\"return result\"])\n F -- yes --> D{\"nums[i] == nums[i-1]?\"}\n D -- yes --> F\n D -- no --> P[\"left = i+1, right = n-1, target = -nums[i]\"]\n P --> W{\"left < right?\"}\n W -- no --> F\n W -- yes --> C[\"current = nums[left] + nums[right]\"]\n C --> Q{\"current vs target\"}\n Q -- equal --> A[\"append triplet, skip equal neighbours, left += 1, right -= 1\"]\n A --> W\n Q -- \"current < target\" --> L[\"left += 1 — need a bigger sum\"]\n L --> W\n Q -- \"current > target\" --> R[\"right -= 1 — need a smaller sum\"]\n R --> W\n```\n\n## Solution\n\n```py\nclass Solution:\n def threeSum(self, nums: list[int]) -> list[list[int]]:\n nums.sort()\n result = []\n n = len(nums)\n\n for i in range(n):\n # skip all zero\n if i > 0 and nums[i] == nums[i - 1]:\n continue\n\n # two pointers\n left = i + 1\n right = n - 1\n target = -nums[i]\n\n while left < right:\n current = nums[left] + nums[right]\n\n if current == target:\n result.append([nums[i], nums[left], nums[right]])\n\n # skip duplicates\n while left < right and nums[left] == nums[left + 1]:\n left += 1\n while left < right and nums[right] == nums[right - 1]:\n right -= 1\n\n # shift pointers\n left += 1\n right -= 1\n\n # since sorted, if current < target, then move left\n elif current < target:\n left += 1\n # since sorted, if current > target, then move right\n else:\n right -= 1\n\n return result\n```\n"},"/two-pointers/container-with-most-water":{"mdx":"---\ntitle: '11. Container With Most Water'\ndescription: You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the i^th line are (i, 0) and (i, height[i])\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"Greedy\", slug: \"greedy\" }\nvideo: \"UuiTKBwPgAo\"\nissue: \"https://github.com/prdlk/leetcode/issues/44\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 83.1 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 75.8 }\n - { name: \"Infosys\", period: \"Thirty Days\", frequency: 68.6 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 67.6 }\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 65.6 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 62 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Goldman Sachs\", period: \"Three Months\", frequency: 57 }\n - { name: \"Walmart Labs\", period: \"Six Months\", frequency: 70.5 }\n - { name: \"IBM\", period: \"Six Months\", frequency: 60.1 }\nsidebar:\n label: 'Container With Most Water'\n badge: 'Medium'\n---\n\n::::warning\nNotice that you may not slant the container.\n::::\n\n### Example 1:\n- Input: `height = [1,8,6,2,5,4,8,3,7]`\n- Output: `49`\n- Explanation: The above vertical lines are represented by array `[1,8,6,2,5,4,8,3,7]`. In this case, the max area of water (blue section) the container can contain is `49`.\n\n### Example 2:\n- Input: `height = [1,1]`\n- Output: `1`\n\n### Constraints:\n\n- `n == height.length`\n- `2 <= n <= 10^5`\n- `0 <= height[i] <= 10^4`\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"maxArea(height)\"]) --> I[\"res = 0, left = 0, right = len - 1\"]\n I --> W{\"left < right?\"}\n W -- no --> E([\"return res\"])\n W -- yes --> A[\"area = (right - left) * min(height[left], height[right])\"]\n A --> M[\"res = max(res, area)\"]\n M --> C{\"height[left] < height[right]?\"}\n C -- yes --> L[\"left += 1 — drop the shorter wall\"]\n C -- no --> R[\"right -= 1 — drop the shorter wall\"]\n L --> W\n R --> W\n```\n\n## Solution\n\n```py\nclass Solution:\n def maxArea(self, height: List[int]) -> int: # noqa: F821\n res = 0\n left = 0\n right = len(height) - 1\n\n while left < right:\n area = (right - left) * min(height[left], height[right])\n res = max(res, area)\n if height[left] < height[right]:\n left += 1\n else:\n right -= 1\n\n return res\n```\n"},"/two-pointers/is-subsequence":{"mdx":"---\ntitle: '392. Is Subsequence'\ndescription: Given two strings s and t, return true if s is a subsequence of t, or false otherwise\nicon: dot\ntopics:\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"String\", slug: \"string\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\nvideo: \"99RVfqklbCE\"\nissue: \"https://github.com/prdlk/leetcode/issues/45\"\ncompanies:\n - { name: \"Amazon\", period: \"Three Months\", frequency: 34.6 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\n - { name: \"Google\", period: \"Six Months\", frequency: 25.4 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 24.5 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 24.1 }\nsidebar:\n label: 'Is Subsequence'\n badge: 'Easy'\n---\n\n::::warning\nSuppose there are lots of incoming s, say s1, s2, ..., sk where k >= 10^9, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code?\n::::\n\n### Example 1:\n- Input: `s = \"abc\", t = \"ahbgdc\"`\n- Output: `true`\n\n### Example 2:\n- Input: `s = \"axc\", t = \"ahbgdc\"`\n- Output: `false`\n\n### Constraints:\n\n- `0 <= s.length <= 100`\n- `0 <= t.length <= 10^4`\n- `s` and `t` consist only of lowercase English letters.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"isSubsequence(s, t)\"]) --> G{\"len(s) > len(t)?\"}\n G -- yes --> X([\"return False\"])\n G -- no --> I[\"i = 0 into s, j = 0 into t\"]\n I --> W{\"i < len(s) and j < len(t)?\"}\n W -- no --> E([\"return i == len(s) — every char of s was matched\"])\n W -- yes --> Q{\"s[i] == t[j]?\"}\n Q -- yes --> A[\"i += 1 — consume the match\"]\n Q -- no --> B[\"j += 1 — always advance t\"]\n A --> B\n B --> W\n```\n\n## Solution\n\n```py\nclass Solution:\n def isSubsequence(self, s: str, t: str) -> bool:\n if len(s) > len(t):\n return False\n\n i, j = 0, 0\n while i < len(s) and j < len(t):\n if s[i] == t[j]:\n i += 1\n j += 1\n\n return i == len(s)\n```\n"},"/two-pointers/remove-duplicates-from-sorted-array-ii":{"mdx":"---\ntitle: '80. Remove Duplicates from Sorted Array II'\ndescription: Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\nissue: \"https://github.com/prdlk/leetcode/issues/48\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 34.6 }\n - { name: \"Meta\", period: \"Six Months\", frequency: 32.7 }\nsidebar:\n label: 'Remove Duplicates from Sorted Array II'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [1,1,1,2,2,3]`\n- Output: `5, nums = [1,1,2,2,3,_]`\n- Explanation: Your function should return `k = 5`, with the first five elements of `nums` being `1`, `1`, `2`, `2` and `3` respectively. It does not matter what you leave beyond the returned `k` (hence they are underscores).\n\n### Example 2:\n- Input: `nums = [0,0,1,1,1,1,2,3,3]`\n- Output: `7, nums = [0,0,1,1,2,3,3,_,_]`\n- Explanation: Your function should return `k = 7`, with the first seven elements of `nums` being `0`, `0`, `1`, `1`, `2`, `3` and `3` respectively. It does not matter what you leave beyond the returned `k` (hence they are underscores).\n\n### Constraints:\n\n- `1 <= nums.length <= 3 * 10^4`\n- `-10^4 <= nums[i] <= 10^4`\n- `nums` is sorted in non-decreasing order.\n\n## Solution\n\n```py\nclass Solution:\n def removeDuplicates(self, nums: list[int]) -> int:\n slow = 0\n for fast in range(len(nums)):\n if slow < 2 or nums[fast] != nums[slow - 2]:\n nums[slow] = nums[fast]\n slow += 1\n return slow\n```\n"},"/two-pointers/reverse-string":{"mdx":"---\ntitle: '344. Reverse String'\ndescription: Write a function that reverses a string. The input string is given as an array of characters s\nicon: dot\ntopics:\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"String\", slug: \"string\" }\nvideo: \"_d0T_2Lk2qA\"\ncompanies:\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 58.2 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 50.2 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 48.3 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 46.8 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 34.5 }\n - { name: \"Qualcomm\", period: \"Six Months\", frequency: 80.7 }\nsidebar:\n label: 'Reverse String'\n badge: 'Easy'\n---\n\n:::warning\nYou must do this by modifying the input array in-place with O(1) extra memory.\n:::\n\n### Example 1:\n- Input: `s = [\"h\",\"e\",\"l\",\"l\",\"o\"]`\n- Output: `[\"o\",\"l\",\"l\",\"e\",\"h\"]`\n\n### Example 2:\n- Input: `s = [\"H\",\"a\",\"n\",\"n\",\"a\",\"h\"]`\n- Output: `[\"h\",\"a\",\"n\",\"n\",\"a\",\"H\"]`\n\n### Constraints:\n\n- `1 <= s.length <= 10^5`\n- `s[i]` is a printable ascii character.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"reverseString(s)\"]) --> I[\"i = 0, j = s.length - 1\"]\n I --> W{\"i < j?\"}\n W -- no --> E([\"done — s was reversed in place, nothing returned\"])\n W -- yes --> A[\"swap s[i] and s[j]\"]\n A --> B[\"i++; j--\"]\n B --> W\n```\n\n## Solution\n\n```js\n/**\n * @param {character[]} s\n * @return {void} Do not return anything, modify s in-place instead.\n */\nvar reverseString = function(s) {\n let i = 0;\n let j = s.length - 1;\n \n while (i < j) {\n [s[i], s[j]] = [s[j], s[i]];\n j--;\n i++;\n } \n};\n```\n"},"/two-pointers/rotate-array":{"mdx":"---\ntitle: '189. Rotate Array'\ndescription: Given an integer array nums, rotate the array to the right by k steps, where k is non-negative\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Math\", slug: \"math\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\nissue: \"https://github.com/prdlk/leetcode/issues/47\"\ncompanies:\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Microsoft\", period: \"Thirty Days\", frequency: 67.9 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 48.9 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 46.6 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 66 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 57.2 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 54.8 }\n - { name: \"Box\", period: \"Six Months\", frequency: 100 }\n - { name: \"TikTok\", period: \"Six Months\", frequency: 55.2 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 48.1 }\nsidebar:\n label: 'Rotate Array'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `nums = [1,2,3,4,5,6,7], k = 3`\n- Output: `[5,6,7,1,2,3,4]`\n- Explanation: rotate `1` steps to the right: `[7,1,2,3,4,5,6]` rotate `2` steps to the right: `[6,7,1,2,3,4,5]` rotate `3` steps to the right: `[5,6,7,1,2,3,4]`\n\n### Example 2:\n- Input: `nums = [-1,-100,3,99], k = 2`\n- Output: `[3,99,-1,-100]`\n- Explanation: rotate `1` steps to the right: `[99,-1,-100,3]` rotate `2` steps to the right: `[3,99,-1,-100]`\n\n### Constraints:\n\n- `1 <= nums.length <= 10^5`\n- `-2^31 <= nums[i] <= 2^31 - 1`\n- `0 <= k <= 10^5`\n- Try to come up with as many solutions as you can. There are at least three different ways to solve this problem.\n- Could you do it in-place with O(1) extra space?\n\n## Solution\n\n```py\nclass Solution:\n def rotate(self, nums: list[int], k: int) -> None:\n N = len(nums) - 1\n cut = k % len(nums)\n\n def reverse(l, r):\n while l <= r:\n nums[l], nums[r] = nums[r], nums[l]\n l += 1\n r -= 1\n\n reverse(0, N)\n reverse(0, cut - 1)\n reverse(cut, N)\n```\n"},"/two-pointers/squares-of-a-sorted-array":{"mdx":"---\ntitle: '977. Squares of a Sorted Array'\ndescription: Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"Sorting\", slug: \"sorting\" }\nvideo: \"FPCZsG_AkUg\"\nissue: \"https://github.com/prdlk/leetcode/issues/46\"\ncompanies:\n - { name: \"Google\", period: \"Thirty Days\", frequency: 42.3 }\n - { name: \"Instacart\", period: \"Three Months\", frequency: 100 }\n - { name: \"Uber\", period: \"Three Months\", frequency: 80 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 54 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 49.8 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 46 }\n - { name: \"tcs\", period: \"Six Months\", frequency: 60 }\n - { name: \"Yandex\", period: \"Six Months\", frequency: 59.6 }\nsidebar:\n label: 'Squares of a Sorted Array'\n badge: 'Easy'\n---\n\n::::warning\nSquaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach?\n::::\n\n### Example 1:\n- Input: `nums = [-4,-1,0,3,10]`\n- Output: `[0,1,9,16,100]`\n- Explanation: After squaring, the array becomes `[16,1,0,9,100]`. After sorting, it becomes `[0,1,9,16,100]`.\n\n### Example 2:\n- Input: `nums = [-7,-3,2,3,11]`\n- Output: `[4,9,9,49,121]`\n\n### Constraints:\n\n- `1 <= nums.length <= 10^4`\n- `-10^4 <= nums[i] <= 10^4`\n- `nums` is sorted in non-decreasing order.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"sortedSquares(nums)\"]) --> I[\"ans = Array(n), left = 0, right = n - 1\"]\n I --> L{\"more i from n-1 down to 0?\"}\n L -- no --> E([\"return ans\"])\n L -- yes --> C{\"abs(nums[left]) < abs(nums[right])?\"}\n C -- yes --> R[\"square = nums[right]; right--\"]\n C -- no --> F[\"square = nums[left]; left++\"]\n R --> W[\"ans[i] = square * square — fill from the back, largest first\"]\n F --> W\n W --> L\n```\n\n## Solution\n\n```js\n/**\n * @param {number[]} nums\n * @return {number[]}\n */\nvar sortedSquares = function(nums) {\n let n = nums.length;\n let ans = new Array(nums.length);\n let left = 0, right = nums.length - 1;\n\n for (let i = n -1; i >= 0; i--){\n let square;\n if(Math.abs(nums[left]) < Math.abs(nums[right])){\n square = nums[right];\n right--;\n }else{\n square = nums[left];\n left++;\n }\n ans[i] = square*square;\n }\n return ans;\n};\n```\n"},"/two-pointers/trapping-rain-water":{"mdx":"---\ntitle: '42. Trapping Rain Water'\ndescription: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"Dynamic Programming\", slug: \"dynamic-programming\" }\n - { name: \"Stack\", slug: \"stack\" }\n - { name: \"Monotonic Stack\", slug: \"monotonic-stack\" }\nissue: \"https://github.com/prdlk/leetcode/issues/211\"\ncompanies:\n - { name: \"Ola Cabs\", period: \"Thirty Days\", frequency: 90.7 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 80.7 }\n - { name: \"Goldman Sachs\", period: \"Thirty Days\", frequency: 71.7 }\n - { name: \"Bloomberg\", period: \"Thirty Days\", frequency: 69.8 }\n - { name: \"Infosys\", period: \"Thirty Days\", frequency: 68.6 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 63.7 }\n - { name: \"Snowflake\", period: \"Three Months\", frequency: 90.3 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 58.9 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 57.2 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 47.7 }\n - { name: \"Docusign\", period: \"Six Months\", frequency: 100 }\n - { name: \"Qualcomm\", period: \"Six Months\", frequency: 80.7 }\n - { name: \"Nvidia\", period: \"Six Months\", frequency: 78.7 }\nsidebar:\n label: 'Trapping Rain Water'\n badge: 'Hard'\n---\n\n### Example 1:\n- Input: `height = [0,1,0,2,1,0,1,3,2,1,2,1]`\n- Output: `6`\n- Explanation: The above elevation map (black section) is represented by array `[0,1,0,2,1,0,1,3,2,1,2,1]`. In this case, `6` units of rain water (blue section) are being trapped.\n\n### Example 2:\n- Input: `height = [4,2,0,3,2,5]`\n- Output: `9`\n\n### Constraints:\n\n- `n == height.length`\n- `1 <= n <= 2 * 10^4`\n- `0 <= height[i] <= 10^5`\n\n## Solution\n\n```py\nclass Solution:\n def trap(self, height: list[int]) -> int:\n left, right = 0, len(height) - 1\n leftMax, rightMax = 0, 0\n total = 0\n\n while left < right:\n leftMax = max(leftMax, height[left])\n rightMax = max(rightMax, height[right])\n\n if height[left] < height[right]:\n total += leftMax - height[left]\n left += 1\n else:\n total += rightMax - height[right]\n right -= 1\n\n return total\n```\n"},"/two-pointers/two-sum-ii-input-array-is-sorted":{"mdx":"---\ntitle: '167. Two Sum II - Input Array Is Sorted'\ndescription: Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length\nicon: dot\ntopics:\n - { name: \"Array\", slug: \"array\" }\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"Binary Search\", slug: \"binary-search\" }\nvideo: \"cQ1Oz4ckceM\"\nissue: \"https://github.com/prdlk/leetcode/issues/42\"\ncompanies:\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 65.8 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 46.6 }\n - { name: \"tcs\", period: \"Three Months\", frequency: 57.2 }\n - { name: \"Meta\", period: \"Three Months\", frequency: 54.8 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 33.4 }\n - { name: \"Oracle\", period: \"Six Months\", frequency: 78.7 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 40.6 }\nsidebar:\n label: 'Two Sum II - Input Array Is Sorted'\n badge: 'Medium'\n---\n\n::::warning\nYour solution must use only constant extra space.\n::::\n\n### Example 1:\n- Input: `numbers = [2,7,11,15], target = 9`\n- Output: `[1,2]`\n- Explanation: The sum of `2` and `7` is `9`. Therefore, `index1 = 1`, `index2 = 2`. We return `[1, 2]`.\n\n### Example 2:\n- Input: `numbers = [2,3,4], target = 6`\n- Output: `[1,3]`\n- Explanation: The sum of `2` and `4` is `6`. Therefore `index1 = 1`, `index2 = 3`. We return `[1, 3]`.\n\n### Example 3:\n- Input: `numbers = [-1,0], target = -1`\n- Output: `[1,2]`\n- Explanation: The sum of `-1` and `0` is `-1`. Therefore `index1 = 1`, `index2 = 2`. We return `[1, 2]`.\n\n### Constraints:\n\n- `2 <= numbers.length <= 3 * 10^4`\n- `-1000 <= numbers[i] <= 1000`\n- `numbers` is sorted in non-decreasing order.\n- `-1000 <= target <= 1000`\n- The tests are generated such that there is exactly one solution.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"twoSum(numbers, target)\"]) --> I[\"i = 0, j = len - 1\"]\n I --> W{\"i < j?\"}\n W -- no --> E([\"return []\"])\n W -- yes --> C[\"c = numbers[i] + numbers[j]\"]\n C --> Q{\"c vs target\"}\n Q -- equal --> R([\"return [i+1, j+1] — 1-indexed\"])\n Q -- \"c < target\" --> L[\"i += 1 — grow the sum\"]\n L --> W\n Q -- \"c > target\" --> H[\"j -= 1 — shrink the sum\"]\n H --> W\n```\n\n## Solution\n\n```py\nclass Solution:\n def twoSum(self, numbers: List[int], target: int) -> List[int]:\n i = 0\n j = len(numbers) - 1\n\n while i < j:\n c = numbers[i] + numbers[j]\n if c == target:\n return [i + 1, j + 1]\n elif c < target:\n i+=1\n else:\n j-=1\n return []\n```\n"},"/two-pointers/valid-palindrome":{"mdx":"---\ntitle: '125. Valid Palindrome'\ndescription: A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers\nicon: dot\ntopics:\n - { name: \"Two Pointers\", slug: \"two-pointers\" }\n - { name: \"String\", slug: \"string\" }\nvideo: \"jJXJ16kPFWg\"\nissue: \"https://github.com/prdlk/leetcode/issues/41\"\ncompanies:\n - { name: \"tcs\", period: \"Thirty Days\", frequency: 58.2 }\n - { name: \"Meta\", period: \"Thirty Days\", frequency: 53.8 }\n - { name: \"Amazon\", period: \"Thirty Days\", frequency: 39.1 }\n - { name: \"Google\", period: \"Thirty Days\", frequency: 29 }\n - { name: \"Bloomberg\", period: \"Three Months\", frequency: 79.9 }\n - { name: \"Infosys\", period: \"Three Months\", frequency: 66 }\n - { name: \"Microsoft\", period: \"Three Months\", frequency: 42.1 }\n - { name: \"Deloitte\", period: \"Six Months\", frequency: 81 }\n - { name: \"Cognizant\", period: \"Six Months\", frequency: 76.3 }\n - { name: \"Axon\", period: \"Six Months\", frequency: 75.4 }\n - { name: \"Apple\", period: \"Six Months\", frequency: 64.8 }\nsidebar:\n label: 'Valid Palindrome'\n badge: 'Easy'\n---\n\n### Example 1:\n- Input: `s = \"A man, a plan, a canal: Panama\"`\n- Output: `true`\n- Explanation: `\"amanaplanacanalpanama\"` is a palindrome.\n\n### Example 2:\n- Input: `s = \"race a car\"`\n- Output: `false`\n- Explanation: `\"raceacar\"` is not a palindrome.\n\n### Example 3:\n- Input: `s = \" \"`\n- Output: `true`\n- Explanation: `s` is an empty string `\"\"` after removing non-alphanumeric characters. Since an empty string reads the same forward and backward, it is a palindrome.\n\n### Constraints:\n\n- `1 <= s.length <= 2 * 10^5`\n- `s` consists only of printable ASCII characters.\n\n## Approach\n\n```mermaid\nflowchart TD\n S([\"isPalindrome(s)\"]) --> N[\"normal = s stripped of non-alphanumerics, lowercased\"]\n N --> I[\"i = 0, j = normal.length - 1\"]\n I --> W{\"i < j?\"}\n W -- no --> E([\"return true — the pointers met in the middle\"])\n W -- yes --> Q{\"normal[i] != normal[j]?\"}\n Q -- yes --> F([\"return false\"])\n Q -- no --> A[\"i++; j-- — step both inward\"]\n A --> W\n```\n\n## Solution\n\n```js\n/**\n * @param {string} s\n * @return {boolean}\n */\nvar isPalindrome = function(s) {\n let normal = s.replace(/[^a-zA-Z0-9]/g, \"\").toLowerCase() \n let i = 0, j = normal.length - 1;\n\n while (i < j) {\n if(normal[i] !== normal[j]) {\n return false;\n }\n i++;\n j--;\n }\n return true;\n};\n```\n"},"/union-find":{"mdx":"---\ntitle: Union Find\ndescription: 'Union-Find (Disjoint Set Union) is a data structure that maintains a collection of disjoint sets and supports efficiently finding which set an element belongs to and merging two sets.'\nhidden: true\n---\n\n\n\n\n"},"/union-find/number-of-connected-components-in-an-undirected-graph":{"mdx":"---\ntitle: '323. Number of Connected Components in an Undirected Graph'\ndescription: You have a graph of n nodes. You are given an integer n and an array edges where edges[i] = [ai, bi] indicates that there is an edge between ai and bi in the graph\nicon: dot\ntopics:\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Union-Find\", slug: \"union-find\" }\n - { name: \"Graph Theory\", slug: \"graph\" }\nissue: \"https://github.com/prdlk/leetcode/issues/147\"\nsidebar:\n label: 'Number of Connected Components in an Undirected Graph'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `n = 5, edges = [[0,1],[1,2],[3,4]]`\n- Output: `2`\n\n### Example 2:\n- Input: `n = 5, edges = [[0,1],[1,2],[2,3],[3,4]]`\n- Output: `1`\n\n### Constraints:\n\n- `1 <= n <= 2000`\n- `1 <= edges.length <= 5000`\n- `edges[i]` = [ai, bi]\n- `ai != bi`\n- There are no repeated edges.\n\n## Solution\n\n```py\nclass Solution:\n def countComponents(self, n: int, edges: list[list[int]]) -> int:\n par = [i for i in range(n + 1)]\n rank = [1] * (n + 1)\n\n def find(n):\n res = n\n while res != par[res]:\n par[res] = par[par[res]]\n res = par[res]\n return res\n\n def union(n1, n2):\n p1, p2 = find(n1), find(n2)\n if p1 == p2:\n return 0\n if rank[p1] > rank[p2]:\n par[p2] = p1\n rank[p1] += rank[p2]\n else:\n par[p1] = p2\n rank[p2] += rank[p1]\n return 1\n\n res = n\n for n1, n2 in edges:\n res -= union(n1, n2)\n return res\n```\n"},"/union-find/redundant-connection":{"mdx":"---\ntitle: '684. Redundant Connection'\ndescription: In this problem, a tree is an undirected graph that is connected and has no cycles\nicon: dot\ntopics:\n - { name: \"Depth-First Search\", slug: \"depth-first-search\" }\n - { name: \"Breadth-First Search\", slug: \"breadth-first-search\" }\n - { name: \"Union-Find\", slug: \"union-find\" }\n - { name: \"Graph Theory\", slug: \"graph\" }\nissue: \"https://github.com/prdlk/leetcode/issues/146\"\ncompanies:\n - { name: \"Box\", period: \"Three Months\", frequency: 100 }\n - { name: \"Amazon\", period: \"Three Months\", frequency: 21.2 }\n - { name: \"Bloomberg\", period: \"Six Months\", frequency: 25.5 }\n - { name: \"Microsoft\", period: \"Six Months\", frequency: 24.1 }\nsidebar:\n label: 'Redundant Connection'\n badge: 'Medium'\n---\n\n### Example 1:\n- Input: `edges = [[1,2],[1,3],[2,3]]`\n- Output: `[2,3]`\n\n### Example 2:\n- Input: `edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]`\n- Output: `[1,4]`\n\n### Constraints:\n\n- `n == edges.length`\n- `3 <= n <= 1000`\n- `edges[i].length == 2`\n- `1 <= ai < bi <= edges.length`\n- `ai != bi`\n- There are no repeated edges.\n- The given graph is connected.\n\n## Solution\n\n```py\nclass Solution:\n def findRedundantConnection(self, edges: list[list[int]]) -> list[int]:\n N = len(edges)\n par = [i for i in range(N + 1)]\n rank = [1] * (N + 1)\n\n def find(n):\n if n != par[n]:\n par[n] = find(par[n])\n return par[n]\n\n def union(n1, n2):\n p1, p2 = find(n1), find(n2)\n if p1 == p2:\n return False\n\n if rank[p1] > rank[p2]:\n par[p2] = p1\n rank[p1] += rank[p2]\n else:\n par[p1] = p2\n rank[p2] += rank[p1]\n\n return True\n\n for n1, n2 in edges:\n if not union(n1, n2):\n return [n1, n2]\n```\n"}}
diff --git a/apps/docs/src/generated/search-client.ts b/apps/docs/src/generated/search-client.ts
new file mode 100644
index 0000000..ddf0d21
--- /dev/null
+++ b/apps/docs/src/generated/search-client.ts
@@ -0,0 +1,7 @@
+// Generated by Blume. Do not edit.
+import { createSearch as create } from "blume/components/layout/search/orama.ts";
+import { joinBase } from "blume/components/islands/base-path.ts";
+
+const indexUrl = joinBase(import.meta.env.BASE_URL, "blume-search.json");
+
+export const createSearch = () => create({ indexUrl });
diff --git a/apps/docs/src/generated/search.json b/apps/docs/src/generated/search.json
new file mode 100644
index 0000000..bd8091b
--- /dev/null
+++ b/apps/docs/src/generated/search.json
@@ -0,0 +1 @@
+[{"breadcrumb":[],"content":"1. Linear Structures Data is read in one direction, from the first item to the last. The current position is recorded at each step. All later phases use this skill. Hash Table A key maps to a value in constant time, which removes the need for a full scan. Prefix Sum A running total is stored at each index, so the sum of any range is one subtraction. Two Pointers Two indices move through the data from different positions and meet at the answer. Sliding Window A range with a start and an end moves across the data, and the contents are updated at each step. Binary Search Half of a sorted range is removed at each step until the target is found. Stack Items are added and removed from one end, so the last item added is the first item removed. Monotonic Stack A stack keeps its items in sorted order, which finds the next larger or smaller item in one pass. 2. Nodal & Grid The data is in separate parts. Each part points to the next part, or sits next to other parts on a flat surface. An index does not find the next item. The pointers or the neighbors give the path instead. Linked List Each node points to the next node, and the pointers give the path through the data. Hybrid Structures Two structures are combined, such as a hash map and a linked list, to get the best properties of each. Matrix Index Math A two-dimensional grid is read as a flat array through a formula, and the neighbors are found by arithmetic. 3. Hierarchical Each part points to one or more parts below it. Those parts do the same. The method that solves a small part also solves the large part. This is recursion. Tree DFS Each branch is followed to its end before the next branch is examined. Tree BFS All nodes at one level are visited before the nodes at the next level. Binary Search Tree Each node holds a value that is larger than its left subtree and smaller than its right subtree. Heap The largest or smallest item is always at the top, and it is removed in logarithmic time. Trie Each node holds one character, and a path from the root spells a word. 4. Relational Each part can point to any other part. Loops can occur. The method must visit each part one time only. The order of the parts, or the distance between them, is not given in the data. The method finds it. Graph DFS Each edge is followed to its end, and a visited set prevents a repeat visit. Graph BFS Nodes are visited in order of distance from the start, which gives the shortest path in an unweighted graph. Topological Sort Nodes are put in an order where each node comes after all nodes that point to it. Union-Find Two sets are merged and the set of any item is found in near-constant time. 5. Decision Space The data has no structure. The method makes one. Each choice makes a new branch. The problem sets the correct method: examine all branches, keep a record of the results already found, or make one choice and do not go back. Backtracking Each choice makes a branch, and a branch is undone when it does not give a valid result. 1-D DP The result for each state is stored in a list, and each new result is computed from the results before it. Multi-D / Grid DP The result for each state is stored in a table with two or more dimensions, and each cell is computed from its neighbors. Greedy The best local choice is made at each step and is not changed later. Intervals Ranges with a start and an end are sorted, and then merged, counted, or scheduled.","contentType":"doc","description":"Structured dependency spine based roadmap for solving DSA Problems on leetcode.","locale":"","route":"/","section":"Docs","title":"Introduction","version":""},{"breadcrumb":["Decision Space","1-D Dynamic Programming"],"content":"","contentType":"doc","description":"The result for each state is stored in a list, and each new result is computed from the results before it.","locale":"","route":"/1d-dynamic-programming","section":"1-D Dynamic Programming","title":"1-D Dynamic Programming","version":""},{"breadcrumb":["Decision Space","1-D Dynamic Programming"],"content":"Example 1: Input: n = 2 Output: 2 Explanation: There are two ways to climb to the top. 1. 1 step + 1 step 2. 2 steps Example 2: Input: n = 3 Output: 3 Explanation: There are three ways to climb to the top. 1. 1 step + 1 step + 1 step 2. 1 step + 2 steps 3. 2 steps + 1 step Constraints: 1 <= n <= 45 Solution","contentType":"doc","description":"You are climbing a staircase. It takes n steps to reach the top","locale":"","route":"/1d-dynamic-programming/climbing-stairs","section":"1-D Dynamic Programming","title":"70. Climbing Stairs","version":""},{"breadcrumb":["Decision Space","1-D Dynamic Programming"],"content":"Example 1: Input: coins = [1,2,5], amount = 11 Output: 3 Explanation: 11 = 5 + 5 + 1 Example 2: Input: coins = [2], amount = 3 Output: -1 Example 3: Input: coins = [1], amount = 0 Output: 0 Constraints: 1 <= coins.length <= 12 1 <= coins[i] <= 2^31 - 1 0 <= amount <= 10^4 Solution","contentType":"doc","description":"You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money","locale":"","route":"/1d-dynamic-programming/coin-change","section":"1-D Dynamic Programming","title":"322. Coin Change","version":""},{"breadcrumb":["Decision Space","1-D Dynamic Programming"],"content":"Example 1: Input: nums = [1,2,3,1] Output: 4 Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3). Total amount you can rob = 1 + 3 = 4. Example 2: Input: nums = [2,7,9,3,1] Output: 12 Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1). Total amount you can rob = 2 + 9 + 1 = 12. Constraints: 1 <= nums.length <= 100 0 <= nums[i] <= 400 Solution","contentType":"doc","description":"You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night","locale":"","route":"/1d-dynamic-programming/house-robber","section":"1-D Dynamic Programming","title":"198. House Robber","version":""},{"breadcrumb":["Decision Space","Backtracking"],"content":"What it is Backtracking explores possible paths to a solution one step at a time. When a path reaches a dead end, you undo the last step and try a different option. Think of every problem as a decision tree. The root is an empty answer. Each level is a decision. Each branch is one option. When to use it Use backtracking when you must generate all valid solutions or evaluate all combinations. Do not use it when you need only one solution or the optimal solution. For those, use greedy or dynamic programming. Backtracking has exponential time complexity. The four components Every backtracking problem has the same parts: Base case — when to save the current path and stop. Choices — the options at each step. Constraints — the rules that reject an invalid path early. Backtrack step — undo the last choice and try the next one. The template Steps to apply the template Draw the decision tree for a small input. Name each level and each branch. Write the base case. Ask: when is the path complete? Write the choices. Ask: what can I add at this level? Write the constraints. Ask: what makes a path invalid? Reject it before recursion. Add the backtrack step. Pop what you appended. Copy the path when you save it. Never append the same list reference. Four problems, one template Subsets Choice: include or skip each number. No constraint. Base case: index equals list length. Permutations Same choices as subsets. Constraint: do not reuse a number already in the path. Base case: path length equals list length. Combinations Same as subsets. Change one line: save the path only when its length equals k. N-Queens Choice: which column to place the queen in the current row. Constraint: no shared column or diagonal. Base case: n queens placed. Backtrack: remove the last queen. Common mistakes You append the path list itself instead of a copy. The result fills with empty lists. You forget to pop. The path grows and never resets. You check constraints after recursion instead of before. You waste time on dead paths. You use backtracking when you need only one answer. Use greedy or DP instead.","contentType":"doc","description":"Backtracking is a depth-first search technique that builds a solution step by step and abandons (undoes) a partial choice as soon as it can’t lead to a valid complete solution.","locale":"","route":"/backtracking","section":"Backtracking","title":"Backtracking","version":""},{"breadcrumb":["Decision Space","Backtracking"],"content":"Example 1: Input: candidates = [2,3,6,7], target = 7 Output: [[2,2,3],[7]] Explanation: 2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times. 7 is a candidate, and 7 = 7. These are the only two combinations. Example 2: Input: candidates = [2,3,5], target = 8 Output: [[2,2,2,2],[2,3,3],[3,5]] Example 3: Input: candidates = [2], target = 1 Output: [] Constraints: 1 <= candidates.length <= 30 2 <= candidates[i] <= 40 All elements of candidates are distinct. 1 <= target <= 40 Solution","contentType":"doc","description":"Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order","locale":"","route":"/backtracking/combination-sum","section":"Backtracking","title":"39. Combination Sum","version":""},{"breadcrumb":["Decision Space","Backtracking"],"content":"Example 1: Input: nums = [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] Example 2: Input: nums = [0,1] Output: [[0,1],[1,0]] Example 3: Input: nums = [1] Output: [[1]] Constraints: 1 <= nums.length <= 6 -10 <= nums[i] <= 10 All the integers of nums are unique. Solution","contentType":"doc","description":"Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order","locale":"","route":"/backtracking/permutations","section":"Backtracking","title":"46. Permutations","version":""},{"breadcrumb":["Decision Space","Backtracking"],"content":"Example 1: Input: nums = [1,2,3] Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]] Example 2: Input: nums = [0] Output: [[],[0]] Constraints: 1 <= nums.length <= 10 -10 <= nums[i] <= 10 All the numbers of nums are unique. Solution","contentType":"doc","description":"Given an integer array nums of unique elements, return all possible subsets (the power set)","locale":"","route":"/backtracking/subsets","section":"Backtracking","title":"78. Subsets","version":""},{"breadcrumb":["Linear Structures","Binary Search"],"content":"Binary search is an efficient array search algorithm. It works by narrowing down the search range by half each time. If you have looked up a word in a physical dictionary, you've already used binary search in real life. Let's look at a simple example: Given a sorted array of integers and an integer called target, find the element that equals the target and return its index. If the element is not found, return -1. The key observation here is that the array is sorted. We pick a random element in the array and compare it to the target. If we happen to pick the element that equals the target (how lucky!), then bingo. We don't need to do any more work; we return its index. If the element is smaller than the target, then we know the target cannot be found in the section to the left of the current element, since everything to the left is even smaller. So, we discard the current element and everything on the left from the search range. If the element is larger than the target, then we know the target cannot be found in the section to the right of the current element, since everything to the right is even larger. So, we discard the current element and everything on the right from the search range. We repeat this process until we find the target. Instead of picking a random element, we always pick the middle element in the current search range. This way, we can discard half of the options and shrink the search range by half each time. This gives us O(log(N)) runtime. This idea can be implemented both iteratively and recursively. However, the major difference is that the iterative version of binary search uses O(1) memory while the recursive version uses O(log(N)) memory. Implementation The search range is represented by the left and right indices that start from both ends of the array and move towards each other as we search. When moving the index, we discard elements and shrink the search range. Time Complexity: O(log(n)) Calculating mid Note that when calculating mid, if the number of elements is even, there are two elements in the middle. We usually follow the convention of picking the first one, equivalent to doing integer division (left + right) / 2. In most programming languages, we calculate mid with left + floor((right-left) / 2) to avoid potential integer overflow. However, in Python, we do not need to worry about integer overflow with left + right because Python3 integers can be arbitrarily large. Deduction It's essential to understand and deduce the algorithm yourself instead of memorizing it. In an actual interview, interviewers may ask you additional questions to test your understanding, so simply memorizing the algorithm may not be enough to convince the interviewer. Key elements in writing a correct binary search: 1. When to terminate the loop Make sure the while loop includes an equality comparison; otherwise, we might skip the loop and miss the potential match for the edge case of a one-element array. 2. Whether/how to update left and right boundary in the if conditions Consider which side to discard. If arr[mid] is smaller than the target, we should discard everything on the left by making left = mid + 1. 3. Should I discard the current element? For vanilla binary search, we can discard it since it cannot be the final answer if it is not equal to the target. There might be situations where you would want to think twice before discarding the current element. We'll discuss this in the next module. When to use binary search Interestingly, binary search works beyond sorted arrays. You can use binary search whenever you make a binary decision to shrink the search range.","contentType":"doc","description":"Half of a sorted range is removed at each step until the target is found.","locale":"","route":"/binary-search","section":"Binary Search","title":"Binary Search","version":""},{"breadcrumb":["Hierarchical","Binary Search Tree"],"content":"","contentType":"doc","description":"Each node holds a value that is larger than its left subtree and smaller than its right subtree.","locale":"","route":"/binary-search-tree","section":"Binary Search Tree","title":"Binary Search Tree","version":""},{"breadcrumb":["Hierarchical","Binary Search Tree"],"content":"If the BST is modified often (i.e., we can do insert and delete operations) and you need to find the kth smallest frequently, how would you optimize? Example 1: Input: root = [3,1,4,null,2], k = 1 Output: 1 Example 2: Input: root = [5,3,6,2,4,null,null,1], k = 3 Output: 3 Constraints: The number of nodes in the tree is n. 1 <= k <= n <= 10^4 0 <= Node.val <= 10^4 Approach Solution","contentType":"doc","description":"Given the root of a binary search tree, and an integer k, return the k^th smallest value (1-indexed) of all the values of the nodes in the tree","locale":"","route":"/binary-search-tree/kth-smallest-element-in-a-bst","section":"Binary Search Tree","title":"230. Kth Smallest Element in a BST","version":""},{"breadcrumb":["Hierarchical","Binary Search Tree"],"content":"Example 1: Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8 Output: 6 Explanation: The LCA of nodes 2 and 8 is 6. Example 2: Input: root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4 Output: 2 Explanation: The LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition. Example 3: Input: root = [2,1], p = 2, q = 1 Output: 2 Constraints: The number of nodes in the tree is in the range [2, 10^5]. -10^9 <= Node.val <= 10^9 All Node.val are unique. p != q p and q will exist in the BST. Approach Solution","contentType":"doc","description":"Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST","locale":"","route":"/binary-search-tree/lowest-common-ancestor-of-a-binary-search-tree","section":"Binary Search Tree","title":"235. Lowest Common Ancestor of a Binary Search Tree","version":""},{"breadcrumb":["Hierarchical","Binary Search Tree"],"content":"Example 1: Input: root = [2,1,3] Output: true Example 2: Input: root = [5,1,4,null,null,3,6] Output: false Explanation: The root node's value is 5 but its right child's value is 4. Constraints: The number of nodes in the tree is in the range [1, 10^4]. -2^31 <= Node.val <= 2^31 - 1 Approach Solution","contentType":"doc","description":"Given the root of a binary tree, determine if it is a valid binary search tree (BST)","locale":"","route":"/binary-search-tree/validate-binary-search-tree","section":"Binary Search Tree","title":"98. Validate Binary Search Tree","version":""},{"breadcrumb":["Linear Structures","Binary Search"],"content":"You must write an algorithm with O(log n) runtime complexity. Example 1: Input: nums = [-1,0,3,5,9,12], target = 9 Output: 4 Explanation: 9 exists in nums and its index is 4 Example 2: Input: nums = [-1,0,3,5,9,12], target = 2 Output: -1 Explanation: 2 does not exist in nums so return -1 Constraints: 1 <= nums.length <= 10^4 -10^4 < nums[i], target < 10^4 All the integers in nums are unique. nums is sorted in ascending order. Approach Solution","contentType":"doc","description":"Given an array of integers nums which is sorted in ascending order, and an integer target, write a function to search target in nums. If target exists, then return its index. Otherwise, return -1","locale":"","route":"/binary-search/binary-search","section":"Binary Search","title":"704. Binary Search","version":""},{"breadcrumb":["Linear Structures","Binary Search"],"content":"You must write an algorithm with O(log n) runtime complexity. Example 1: Input: nums = [5,7,7,8,8,10], target = 8 Output: [3,4] Example 2: Input: nums = [5,7,7,8,8,10], target = 6 Output: [-1,-1] Example 3: Input: nums = [], target = 0 Output: [-1,-1] Constraints: 0 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9 nums is a non-decreasing array. -10^9 <= target <= 10^9 Solution","contentType":"doc","description":"Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value","locale":"","route":"/binary-search/find-first-and-last-position-of-element-in-sorted-array","section":"Binary Search","title":"34. Find First and Last Position of Element in Sorted Array","version":""},{"breadcrumb":["Linear Structures","Binary Search"],"content":"Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]. Example 1: Input: nums = [3,4,5,1,2] Output: 1 Explanation: The original array was [1,2,3,4,5] rotated 3 times. Example 2: Input: nums = [4,5,6,7,0,1,2] Output: 0 Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times. Example 3: Input: nums = [11,13,15,17] Output: 11 Explanation: The original array was [11,13,15,17] and it was rotated 4 times. Constraints: n == nums.length 1 <= n <= 5000 -5000 <= nums[i] <= 5000 All the integers of nums are unique. nums is sorted and rotated between 1 and n times. Approach Solution","contentType":"doc","description":"Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:","locale":"","route":"/binary-search/find-minimum-in-rotated-sorted-array","section":"Binary Search","title":"153. Find Minimum in Rotated Sorted Array","version":""},{"breadcrumb":["Linear Structures","Binary Search"],"content":"You must write an algorithm that runs in O(log n) time. Example 1: Input: nums = [1,2,3,1] Output: 2 Explanation: 3 is a peak element and your function should return the index number 2. Example 2: Input: nums = [1,2,1,3,5,6,4] Output: 5 Explanation: Your function can return either index number 1 where the peak element is 2, or index number 5 where the peak element is 6. Constraints: 1 <= nums.length <= 1000 -2^31 <= nums[i] <= 2^31 - 1 nums[i] != nums[i + 1] for all valid i. Solution","contentType":"doc","description":"A peak element is an element that is strictly greater than its neighbors","locale":"","route":"/binary-search/find-peak-element","section":"Binary Search","title":"162. Find Peak Element","version":""},{"breadcrumb":["Linear Structures","Binary Search"],"content":"Example 1: Input: piles = [3,6,7,11], h = 8 Output: 4 Example 2: Input: piles = [30,11,23,4,20], h = 5 Output: 30 Example 3: Input: piles = [30,11,23,4,20], h = 6 Output: 23 Constraints: 1 <= piles.length <= 10^4 piles.length <= h <= 10^9 1 <= piles[i] <= 10^9 Approach Solution","contentType":"doc","description":"Koko loves to eat bananas. There are n piles of bananas, the i^th pile has piles[i] bananas. The guards have gone and will come back in h hours","locale":"","route":"/binary-search/koko-eating-bananas","section":"Binary Search","title":"875. Koko Eating Bananas","version":""},{"breadcrumb":["Linear Structures","Binary Search"],"content":"Example 1: Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2. Example 2: Input: nums1 = [1,2], nums2 = [3,4] Output: 2.50000 Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5. Constraints: nums1.length == m nums2.length == n 0 <= m <= 1000 0 <= n <= 1000 1 <= m + n <= 2000 -10^6 <= nums1[i], nums2[i] <= 10^6 Solution","contentType":"doc","description":"Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays","locale":"","route":"/binary-search/median-of-two-sorted-arrays","section":"Binary Search","title":"4. Median of Two Sorted Arrays","version":""},{"breadcrumb":["Linear Structures","Binary Search"],"content":"You must write a solution in O(log(m * n)) time complexity. Example 1: Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3 Output: true Example 2: Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13 Output: false Constraints: m == matrix.length n == matrix[i].length 1 <= m, n <= 100 -10^4 <= matrix[i][j], target <= 10^4 Approach Solution","contentType":"doc","description":"You are given an m x n integer matrix matrix with the following two properties:","locale":"","route":"/binary-search/search-a-2d-matrix","section":"Binary Search","title":"74. Search a 2D Matrix","version":""},{"breadcrumb":["Linear Structures","Binary Search"],"content":"You must write an algorithm with O(log n) runtime complexity. Example 1: Input: nums = [4,5,6,7,0,1,2], target = 0 Output: 4 Example 2: Input: nums = [4,5,6,7,0,1,2], target = 3 Output: -1 Example 3: Input: nums = [1], target = 0 Output: -1 Constraints: 1 <= nums.length <= 5000 -10^4 <= nums[i] <= 10^4 All values of nums are unique. nums is an ascending array that is possibly rotated. -10^4 <= target <= 10^4 Approach Solution","contentType":"doc","description":"There is an integer array nums sorted in ascending order (with distinct values)","locale":"","route":"/binary-search/search-in-rotated-sorted-array","section":"Binary Search","title":"33. Search in Rotated Sorted Array","version":""},{"breadcrumb":["Linear Structures","Binary Search"],"content":"You must write an algorithm with O(log n) runtime complexity. Example 1: Input: nums = [1,3,5,6], target = 5 Output: 2 Example 2: Input: nums = [1,3,5,6], target = 2 Output: 1 Example 3: Input: nums = [1,3,5,6], target = 7 Output: 4 Constraints: 1 <= nums.length <= 10^4 -10^4 <= nums[i] <= 10^4 nums contains distinct values sorted in ascending order. -10^4 <= target <= 10^4 Approach Solution","contentType":"doc","description":"Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order","locale":"","route":"/binary-search/search-insert-position","section":"Binary Search","title":"35. Search Insert Position","version":""},{"breadcrumb":["Relational","Graph BFS"],"content":"","contentType":"doc","description":"Nodes are visited in order of distance from the start, which gives the shortest path in an unweighted graph.","locale":"","route":"/graph-bfs","section":"Graph BFS","title":"Graph BFS","version":""},{"breadcrumb":["Relational","Graph BFS"],"content":"Example 1: Input: grid = [[2,1,1],[1,1,0],[0,1,1]] Output: 4 Example 2: Input: grid = [[2,1,1],[0,1,1],[1,0,1]] Output: -1 Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally. Example 3: Input: grid = [[0,2]] Output: 0 Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0. Constraints: m == grid.length n == grid[i].length 1 <= m, n <= 10 grid[i][j] is 0, 1, or 2. Solution","contentType":"doc","description":"You are given an m x n grid where each cell can have one of three values:","locale":"","route":"/graph-bfs/rotting-oranges","section":"Graph BFS","title":"994. Rotting Oranges","version":""},{"breadcrumb":["Relational","Graph BFS"],"content":"Example 1: Input: beginWord = \"hit\", endWord = \"cog\", wordList = [\"hot\",\"dot\",\"dog\",\"lot\",\"log\",\"cog\"] Output: 5 Explanation: One shortest transformation sequence is \"hit\" -> \"hot\" -> \"dot\" -> \"dog\" -> cog\", which is 5 words long. Example 2: Input: beginWord = \"hit\", endWord = \"cog\", wordList = [\"hot\",\"dot\",\"dog\",\"lot\",\"log\"] Output: 0 Explanation: The endWord \"cog\" is not in wordList, therefore there is no valid transformation sequence. Constraints: 1 <= beginWord.length <= 10 endWord.length == beginWord.length 1 <= wordList.length <= 5000 wordList[i].length == beginWord.length beginWord, endWord, and wordList[i] consist of lowercase English letters. beginWord != endWord All the words in wordList are unique. Solution","contentType":"doc","description":"A transformation sequence from word beginWord to word endWord using a dictionary wordList is a sequence of words beginWord -> s1 -> s2 -> ... -> sk such that:","locale":"","route":"/graph-bfs/word-ladder","section":"Graph BFS","title":"127. Word Ladder","version":""},{"breadcrumb":["Relational","Graph DFS"],"content":"","contentType":"doc","description":"Each edge is followed to its end, and a visited set prevents a repeat visit.","locale":"","route":"/graph-dfs","section":"Graph DFS","title":"Graph DFS","version":""},{"breadcrumb":["Relational","Graph DFS"],"content":"Example 1: Input: adjList = [[2,4],[1,3],[2,4],[1,3]] Output: [[2,4],[1,3],[2,4],[1,3]] Explanation: There are 4 nodes in the graph. 1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4). 2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3). 3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4). 4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3). Example 2: Input: adjList = [[]] Output: [[]] Explanation: Note that the input contains one empty list. The graph consists of only one node with val = 1 and it does not have any neighbors. Example 3: Input: adjList = [] Output: [] Explanation: This an empty graph, it does not have any nodes. Constraints: The number of nodes in the graph is in the range [0, 100]. 1 <= Node.val <= 100 Node.val is unique for each node. There are no repeated edges and no self-loops in the graph. The Graph is connected and all nodes can be visited starting from the given node. Solution","contentType":"doc","description":"Given a reference of a node in a connected undirected graph","locale":"","route":"/graph-dfs/clone-graph","section":"Graph DFS","title":"133. Clone Graph","version":""},{"breadcrumb":["Relational","Graph DFS"],"content":"Example 1: Input: grid = [ [\"1\",\"1\",\"1\",\"1\",\"0\"], [\"1\",\"1\",\"0\",\"1\",\"0\"], [\"1\",\"1\",\"0\",\"0\",\"0\"], [\"0\",\"0\",\"0\",\"0\",\"0\"] ] Output: 1 Example 2: Input: grid = [ [\"1\",\"1\",\"0\",\"0\",\"0\"], [\"1\",\"1\",\"0\",\"0\",\"0\"], [\"0\",\"0\",\"1\",\"0\",\"0\"], [\"0\",\"0\",\"0\",\"1\",\"1\"] ] Output: 3 Constraints: m == grid.length n == grid[i].length 1 <= m, n <= 300 grid[i][j] is '0' or '1'. Solution","contentType":"doc","description":"Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands","locale":"","route":"/graph-dfs/number-of-islands","section":"Graph DFS","title":"200. Number of Islands","version":""},{"breadcrumb":["Relational","Graph DFS"],"content":"Example 1: Input: heights = [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]] Output: [[0,4],[1,3],[1,4],[2,2],[3,0],[3,1],[4,0]] Explanation: The following cells can flow to the Pacific and Atlantic oceans, as shown below: [0,4]: [0,4] -> Pacific Ocean [0,4] -> Atlantic Ocean [1,3]: [1,3] -> [0,3] -> Pacific Ocean [1,3] -> [1,4] -> Atlantic Ocean [1,4]: [1,4] -> [1,3] -> [0,3] -> Pacific Ocean [1,4] -> Atlantic Ocean [2,2]: [2,2] -> [1,2] -> [0,2] -> Pacific Ocean [2,2] -> [2,3] -> [2,4] -> Atlantic Ocean [3,0]: [3,0] -> Pacific Ocean [3,0] -> [4,0] -> Atlantic Ocean [3,1]: [3,1] -> [3,0] -> Pacific Ocean [3,1] -> [4,1] -> Atlantic Ocean [4,0]: [4,0] -> Pacific Ocean [4,0] -> Atlantic Ocean Note that there are other possible paths for these cells to flow to the Pacific and Atlantic oceans. Example 2: Input: heights = [[1]] Output: [[0,0]] Explanation: The water can flow from the only cell to the Pacific and Atlantic oceans. Constraints: m == heights.length n == heights[r].length 1 <= m, n <= 200 0 <= heights[r][c] <= 10^5 Solution","contentType":"doc","description":"There is an m x n rectangular island that borders both the Pacific Ocean and Atlantic Ocean. The Pacific Ocean touches the island's left and top edges, and the Atlantic Ocean touches the island's right and bottom edges","locale":"","route":"/graph-dfs/pacific-atlantic-water-flow","section":"Graph DFS","title":"417. Pacific Atlantic Water Flow","version":""},{"breadcrumb":["Decision Space","Greedy"],"content":"When to use greedy algorithms? Solving optimization problems. Ask to optimize some value and calculate it. keywords (min/max, longest/shortest, largest/smallest, etc.) Optimization problems Return the minimum number of swaps required to make s1 equal to s2. Return the minimum number of steps to make s and t anagrams of each other. Given three integers a, b, and c, return the longest possible happy string, if there are multiple such strings return any of them. if there is no such string, return empty string. What are greedy algorithms? Greedy algorithms follow this format: identify what choices you have at every step find the best choice just do what looks best until done assume that the best choice is the one that will lead to the best outcome How do we know greedy works? Write out a formal proof too complicated to write out too much time better to use intuition try a bunch of ideas find counterexamples Steps to use greedy algorithms Identify the different steps and choices Choices have to be comparable must determine if a choice is better than another Check if greedy works try finding small cases where greedy is not optimal if cases pass, try to deduce why greedy is optimal Why use greedy algorithms? simple to code doesn't use complex algorithms (BFS, DFS, etc.) runtime is fast, usually 0(n) or O(nlogn) however, greedy algorithms are not always optimal The Greedy Skeleton Type 1: Walk and Carry No sort. Walk the list once. Carry one number. At each step, decide: keep it, or drop it. Type 2: Sort and Pick Used when order doesn't matter, so you pick the order. Meeting rooms, intervals, \"most tasks by a deadline.\"","contentType":"doc","description":"The best local choice is made at each step and is not changed later.","locale":"","route":"/greedy","section":"Greedy","title":"Greedy","version":""},{"breadcrumb":["Decision Space","Greedy"],"content":"Example 1: Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2] Output: 3 Explanation: Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4 Travel to station 4. Your tank = 4 - 1 + 5 = 8 Travel to station 0. Your tank = 8 - 2 + 1 = 7 Travel to station 1. Your tank = 7 - 3 + 2 = 6 Travel to station 2. Your tank = 6 - 4 + 3 = 5 Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3. Therefore, return 3 as the starting index. Example 2: Input: gas = [2,3,4], cost = [3,4,3] Output: -1 Explanation: You can't start at station 0 or 1, as there is not enough gas to travel to the next station. Let's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4 Travel to station 0. Your tank = 4 - 3 + 2 = 3 Travel to station 1. Your tank = 3 - 3 + 3 = 3 You cannot travel back to station 2, as it requires 4 unit of gas but you only have 3. Therefore, you can't travel around the circuit once no matter where you start. Constraints: n == gas.length == cost.length 1 <= n <= 10^5 0 <= gas[i], cost[i] <= 10^4 The input is generated such that the answer is unique. Solution","contentType":"doc","description":"There are n gas stations along a circular route, where the amount of gas at the i^th station is gas[i]","locale":"","route":"/greedy/gas-station","section":"Greedy","title":"134. Gas Station","version":""},{"breadcrumb":["Decision Space","Greedy"],"content":"Example 1: Input: nums = [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: nums = [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index. Constraints: 1 <= nums.length <= 10^4 0 <= nums[i] <= 10^5 Solution","contentType":"doc","description":"You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position","locale":"","route":"/greedy/jump-game","section":"Greedy","title":"55. Jump Game","version":""},{"breadcrumb":["Decision Space","Greedy"],"content":"If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle. Example 1: Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: The subarray [4,-1,2,1] has the largest sum 6. Example 2: Input: nums = [1] Output: 1 Explanation: The subarray [1] has the largest sum 1. Example 3: Input: nums = [5,4,-1,7,8] Output: 23 Explanation: The subarray [5,4,-1,7,8] has the largest sum 23. Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 Solution","contentType":"doc","description":"Given an integer array nums, find the subarray with the largest sum, and return its sum","locale":"","route":"/greedy/maximum-subarray","section":"Greedy","title":"53. Maximum Subarray","version":""},{"breadcrumb":["Linear Structures","Hash Table"],"content":"Sometimes when programming, we need a data structure that maps an arbitrary data type to another arbitrary data type, like an array with non-integer indices. For example, a phone book that allows lookup by a person's name requires such data type. A hash table (or a hash map) is one way of representing this data structure. The idea of hash table is this: Data is stored in a fix-sized array, where each data value has an unique index. When we add a new (key, value) pair to the array, we use a hash function to generate a hash value within the range of the array with the key. We then store the data value at the index indicated by the generated hash value. Later, when we retrieve the data with the key from the hash table, we hash the key again and look up the data in the array with the corresponding index. Since the hash function always returns the same hash values with the same input, we will always find the correct item in that index. Hash Table Operations Efficiency Assuming the hash function uses constant time, the average time complexity is O(n / k) for each insertion/access operation on the table, where n is the number of entries in the hash table and k is the array size of the table. This assumes the hash function distributes keys uniformly across the k buckets, so each bucket holds about n / k entries on average. Frequency Counting Python provides a built-in collections.Counter class that does this for us.","contentType":"doc","description":"A key maps to a value in constant time, which removes the need for a full scan.","locale":"","route":"/hash-table","section":"Hash Table","title":"Hash Table","version":""},{"breadcrumb":["Linear Structures","Hash Table"],"content":"Example 1: Input: sentence = \"thequickbrownfoxjumpsoverthelazydog\" Output: true Explanation: sentence contains at least one of every letter of the English alphabet. Example 2: Input: sentence = \"leetcode\" Output: false Constraints: 1 <= sentence.length <= 1000 sentence consists of lowercase English letters. Approach Solution","contentType":"doc","description":"A pangram is a sentence where every letter of the English alphabet appears at least once","locale":"","route":"/hash-table/check-if-the-sentence-is-pangram","section":"Hash Table","title":"1832. Check if the Sentence Is Pangram","version":""},{"breadcrumb":["Linear Structures","Hash Table"],"content":"Example 1: Input: nums = [1,2,3,1] Output: true Explanation: The element 1 occurs at the indices 0 and 3. Example 2: Input: nums = [1,2,3,4] Output: false Explanation: All elements are distinct. Example 3: Input: nums = [1,1,1,3,3,4,3,2,4,2] Output: true Constraints: 1 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9 Approach Solution","contentType":"doc","description":"Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct","locale":"","route":"/hash-table/contains-duplicate","section":"Hash Table","title":"217. Contains Duplicate","version":""},{"breadcrumb":["Linear Structures","Hash Table"],"content":"Example 1: Input: s = \"leetcode\" Output: 0 Explanation: The character 'l' at index 0 is the first character that does not occur at any other index. Example 2: Input: s = \"loveleetcode\" Output: 2 Example 3: Input: s = \"aabb\" Output: -1 Constraints: 1 <= s.length <= 10^5 s consists of only lowercase English letters. Approach Solution","contentType":"doc","description":"Given a string s, find the first non-repeating character in it and return its index. If it does not exist, return -1","locale":"","route":"/hash-table/first-unique-character-in-a-string","section":"Hash Table","title":"387. First Unique Character in a String","version":""},{"breadcrumb":["Linear Structures","Hash Table"],"content":"Example 1: Input: strs = [\"eat\",\"tea\",\"tan\",\"ate\",\"nat\",\"bat\"] Output: [[\"bat\"],[\"nat\",\"tan\"],[\"ate\",\"eat\",\"tea\"]] Explanation: There is no string in strs that can be rearranged to form \"bat\". The strings \"nat\" and \"tan\" are anagrams as they can be rearranged to form each other. The strings \"ate\", \"eat\", and \"tea\" are anagrams as they can be rearranged to form each other. Example 2: Input: strs = [\"\"] Output: [[\"\"]] Example 3: Input: strs = [\"a\"] Output: [[\"a\"]] Constraints: 1 <= strs.length <= 10^4 0 <= strs[i].length <= 100 strs[i] consists of lowercase English letters. Approach Solution","contentType":"doc","description":"Given an array of strings strs, group the anagrams together. You can return the answer in any order","locale":"","route":"/hash-table/group-anagrams","section":"Hash Table","title":"49. Group Anagrams","version":""},{"breadcrumb":["Linear Structures","Hash Table"],"content":"Example 1: Input: s = \"egg\", t = \"add\" Output: true Explanation: The strings s and t can be made identical by: • Mapping 'e' to 'a'. • Mapping 'g' to 'd'. Example 2: Input: s = \"f11\", t = \"b23\" Output: false Explanation: The strings s and t can not be made identical as '1' needs to be mapped to both '2' and '3'. Example 3: Input: s = \"paper\", t = \"title\" Output: true Constraints: 1 <= s.length <= 5 * 10^4 t.length == s.length s and t consist of any valid ascii character. Solution","contentType":"doc","description":"Given two strings s and t, determine if they are isomorphic","locale":"","route":"/hash-table/isomorphic-strings","section":"Hash Table","title":"205. Isomorphic Strings","version":""},{"breadcrumb":["Linear Structures","Hash Table"],"content":"You must write an algorithm that runs in O(n) time. Example 1: Input: nums = [100,4,200,1,3,2] Output: 4 Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4. Example 2: Input: nums = [0,3,7,2,5,8,4,6,0,1] Output: 9 Example 3: Input: nums = [1,0,1,2] Output: 3 Constraints: 0 <= nums.length <= 10^5 -10^9 <= nums[i] <= 10^9 Solution","contentType":"doc","description":"Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence","locale":"","route":"/hash-table/longest-consecutive-sequence","section":"Hash Table","title":"128. Longest Consecutive Sequence","version":""},{"breadcrumb":["Linear Structures","Hash Table"],"content":"Example 1: Input: s = \"tree\" Output: \"eert\" Explanation: 'e' appears twice while 'r' and 't' both appear once. So 'e' must appear before both 'r' and 't'. Therefore \"eetr\" is also a valid answer. Example 2: Input: s = \"cccaaa\" Output: \"aaaccc\" Explanation: Both 'c' and 'a' appear three times, so both \"cccaaa\" and \"aaaccc\" are valid answers. Note that \"cacaca\" is incorrect, as the same characters must be together. Example 3: Input: s = \"Aabb\" Output: \"bbAa\" Explanation: \"bbaA\" is also a valid answer, but \"Aabb\" is incorrect. Note that 'A' and 'a' are treated as two different characters. Constraints: 1 <= s.length <= 5 * 10^5 s consists of uppercase and lowercase English letters and digits. Approach Solution","contentType":"doc","description":"Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string","locale":"","route":"/hash-table/sort-characters-by-frequency","section":"Hash Table","title":"451. Sort Characters By Frequency","version":""},{"breadcrumb":["Linear Structures","Hash Table"],"content":"Your algorithm's time complexity must be better than O(n log n), where n is the array's size. Example 1: Input: nums = [1,1,1,2,2,3], k = 2 Output: [1,2] Example 2: Input: nums = [1], k = 1 Output: [1] Example 3: Input: nums = [1,2,1,2,1,2,3,1,3,2], k = 2 Output: [1,2] Constraints: 1 <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 k is in the range [1, the number of unique elements in the array]. It is guaranteed that the answer is unique. Approach Solution","contentType":"doc","description":"Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order","locale":"","route":"/hash-table/top-k-frequent-elements","section":"Hash Table","title":"347. Top K Frequent Elements","version":""},{"breadcrumb":["Linear Structures","Hash Table"],"content":"Can you come up with an algorithm that is less than O(n^2) time complexity? Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. Example 2: Input: nums = [3,2,4], target = 6 Output: [1,2] Example 3: Input: nums = [3,3], target = 6 Output: [0,1] Constraints: 2 <= nums.length <= 10^4 -10^9 <= nums[i] <= 10^9 -10^9 <= target <= 10^9 Only one valid answer exists. Approach Solution","contentType":"doc","description":"You are given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target","locale":"","route":"/hash-table/two-sum","section":"Hash Table","title":"1. Two Sum","version":""},{"breadcrumb":["Linear Structures","Hash Table"],"content":"What if the inputs contain Unicode characters? How would you adapt your solution to such a case? Example 1: Input: s = \"anagram\", t = \"nagaram\" Output: true Example 2: Input: s = \"rat\", t = \"car\" Output: false Constraints: 1 <= s.length, t.length <= 5 * 10^4 s and t consist of lowercase English letters. Approach Solution","contentType":"doc","description":"Given two strings s and t, return true if t is an anagram of s, and false otherwise","locale":"","route":"/hash-table/valid-anagram","section":"Hash Table","title":"242. Valid Anagram","version":""},{"breadcrumb":["Linear Structures","Hash Table"],"content":"Example 1: Input: pattern = \"abba\", s = \"dog cat cat dog\" Output: true Explanation: The bijection can be established as: • 'a' maps to \"dog\". • 'b' maps to \"cat\". Example 2: Input: pattern = \"abba\", s = \"dog cat cat fish\" Output: false Example 3: Input: pattern = \"aaaa\", s = \"dog cat cat dog\" Output: false Constraints: 1 <= pattern.length <= 300 pattern contains only lower-case English letters. 1 <= s.length <= 3000 s contains only lowercase English letters and spaces ' '. s does not contain any leading or trailing spaces. All the words in s are separated by a single space. Solution","contentType":"doc","description":"Given a pattern and a string s, find if s follows the same pattern","locale":"","route":"/hash-table/word-pattern","section":"Hash Table","title":"290. Word Pattern","version":""},{"breadcrumb":["Hierarchical","Heap / Priority Queue"],"content":"","contentType":"doc","description":"The largest or smallest item is always at the top, and it is removed in logarithmic time.","locale":"","route":"/heap-priority-queue","section":"Heap / Priority Queue","title":"Heap / Priority Queue","version":""},{"breadcrumb":["Hierarchical","Heap / Priority Queue"],"content":"Example 1: Input: points = [[1,3],[-2,2]], k = 1 Output: [[-2,2]] Explanation: The distance between (1, 3) and the origin is sqrt(10). The distance between (-2, 2) and the origin is sqrt(8). Since sqrt(8) < sqrt(10), (-2, 2) is closer to the origin. We only want the closest k = 1 points from the origin, so the answer is just [[-2,2]]. Example 2: Input: points = [[3,3],[5,-1],[-2,4]], k = 2 Output: [[3,3],[-2,4]] Explanation: The answer [[-2,4],[3,3]] would also be accepted. Constraints: 1 <= k <= points.length <= 10^4 -10^4 <= xi, yi <= 10^4 Approach Solution","contentType":"doc","description":"Given an array of points where points[i] = [xi, yi] represents a point on the X-Y plane and an integer k, return the k closest points to the origin (0, 0)","locale":"","route":"/heap-priority-queue/k-closest-points-to-origin","section":"Heap / Priority Queue","title":"973. K Closest Points to Origin","version":""},{"breadcrumb":["Hierarchical","Heap / Priority Queue"],"content":"Example 1: Input: nums = [3,2,1,5,6,4], k = 2 Output: 5 Example 2: Input: nums = [3,2,3,1,2,4,5,5,6], k = 4 Output: 4 Constraints: 1 <= k <= nums.length <= 10^5 -10^4 <= nums[i] <= 10^4 Approach Solution","contentType":"doc","description":"Given an integer array nums and an integer k, return the k^th largest element in the array","locale":"","route":"/heap-priority-queue/kth-largest-element-in-an-array","section":"Heap / Priority Queue","title":"215. Kth Largest Element in an Array","version":""},{"breadcrumb":["Hierarchical","Heap / Priority Queue"],"content":"Example 1: Input: lists = [[1,4,5],[1,3,4],[2,6]] Output: [1,1,2,3,4,4,5,6] Explanation: The linked-lists are: [ 1->4->5, 1->3->4, 2->6 ] merging them into one sorted linked list: 1->1->2->3->4->4->5->6 Example 2: Input: lists = [] Output: [] Example 3: Input: lists = [[]] Output: [] Constraints: k == lists.length 0 <= k <= 10^4 0 <= lists[i].length <= 500 -10^4 <= lists[i][j] <= 10^4 lists[i] is sorted in ascending order. The sum of lists[i].length will not exceed 10^4. Approach Solution","contentType":"doc","description":"You are given an array of k linked-lists lists, each linked-list is sorted in ascending order","locale":"","route":"/heap-priority-queue/merge-k-sorted-lists","section":"Heap / Priority Queue","title":"23. Merge k Sorted Lists","version":""},{"breadcrumb":["Nodal & Grid","Hybrid Structures"],"content":"","contentType":"doc","description":"Two structures are combined, such as a hash map and a linked list, to get the best properties of each.","locale":"","route":"/hybrid-structures","section":"Hybrid Structures","title":"Hybrid Structures","version":""},{"breadcrumb":["Nodal & Grid","Hybrid Structures"],"content":"Example 1: Input: l1 = [2,4,3], l2 = [5,6,4] Output: [7,0,8] Explanation: 342 + 465 = 807. Example 2: Input: l1 = [0], l2 = [0] Output: [0] Example 3: Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9] Output: [8,9,9,9,0,0,0,1] Constraints: The number of nodes in each linked list is in the range [1, 100]. 0 <= Node.val <= 9 It is guaranteed that the list represents a number that does not have leading zeros. Approach Solution","contentType":"doc","description":"You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list","locale":"","route":"/hybrid-structures/add-two-numbers","section":"Hybrid Structures","title":"2. Add Two Numbers","version":""},{"breadcrumb":["Nodal & Grid","Hybrid Structures"],"content":"You must implement the functions of the class such that each function works in average O(1) time complexity. Example 1: Input: `` Output: `` Explanation: Input [\"RandomizedSet\", \"insert\", \"remove\", \"insert\", \"getRandom\", \"remove\", \"insert\", \"getRandom\"] [[], [1], [2], [2], [], [1], [2], []] Output [null, true, false, true, 2, true, false, 2] Explanation RandomizedSet randomizedSet = new RandomizedSet(); randomizedSet.insert(1); // Inserts 1 to the set. Returns true as 1 was inserted successfully. randomizedSet.remove(2); // Returns false as 2 does not exist in the set. randomizedSet.insert(2); // Inserts 2 to the set, returns true. Set now contains [1,2]. randomizedSet.getRandom(); // getRandom() should return either 1 or 2 randomly. randomizedSet.remove(1); // Removes 1 from the set, returns true. Set now contains [2]. randomizedSet.insert(2); // 2 was already in the set, so return false. randomizedSet.getRandom(); // Since 2 is the only number in the set, getRandom() will always return 2. Constraints: -2^31 <= val <= 2^31 - 1 At most 2 * 10^5 calls will be made to insert, remove, and getRandom. There will be at least one element in the data structure when getRandom is called. Solution","contentType":"doc","description":"Implement the RandomizedSet class:","locale":"","route":"/hybrid-structures/insert-delete-getrandom-o1","section":"Hybrid Structures","title":"380. Insert Delete GetRandom O(1)","version":""},{"breadcrumb":["Nodal & Grid","Hybrid Structures"],"content":"Example 1: Input: `` Output: `` Explanation: Input [\"LRUCache\", \"put\", \"put\", \"get\", \"put\", \"get\", \"put\", \"get\", \"get\", \"get\"] [[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]] Output [null, null, null, 1, null, -1, null, -1, 3, 4] Explanation LRUCache lRUCache = new LRUCache(2); lRUCache.put(1, 1); // cache is {1=1} lRUCache.put(2, 2); // cache is {1=1, 2=2} lRUCache.get(1); // return 1 lRUCache.put(3, 3); // LRU key was 2, evicts key 2, cache is {1=1, 3=3} lRUCache.get(2); // returns -1 (not found) lRUCache.put(4, 4); // LRU key was 1, evicts key 1, cache is {4=4, 3=3} lRUCache.get(1); // return -1 (not found) lRUCache.get(3); // return 3 lRUCache.get(4); // return 4 Constraints: 1 <= capacity <= 3000 0 <= key <= 10^4 0 <= value <= 10^5 At most 2 * 10^5 calls will be made to get and put. Approach Solution","contentType":"doc","description":"Design a data structure that follows the constraints of a Least Recently Used (LRU) cache","locale":"","route":"/hybrid-structures/lru-cache","section":"Hybrid Structures","title":"146. LRU Cache","version":""},{"breadcrumb":["Decision Space","Intervals"],"content":"An interval is a pair [start, end] marking a stretch on a line: the minutes a meeting occupies the range a task runs the span a sensor was active What interval problems ask you? Do any two collide? What does the timeline look like once you merge everything that touches? How many rooms do you need so no two meetings share one? Every one of those questions reduces to a single primitive: deciding whether two intervals overlap, and if they do, how to merge them or what part they share. Once that primitive is clear, the rest of the pattern is bookkeeping. When two intervals overlap It is easier to state when two intervals do not overlap. Interval a sits entirely before interval b when a finishes before b begins, that is a.end < b.start. By symmetry they also miss each other when b ends before a starts. If neither of those holds, the intervals overlap. Negating a.end < b.start or b.end < a.start gives the positive test directly: two intervals overlap exactly when a.start <= b.end and b.start <= a.end. That pair of comparisons is the whole overlap check. The Merged Region When two intervals overlap, you can merge them into one interval that covers both. Its start is the earlier of the two starts, and its end is the later of the two ends: [min(a.start, b.start), max(a.end, b.end)]. The merged interval begins as soon as either interval starts, and it ends only once both have stopped. The Shared Region When two intervals overlap, the part they share is itself an interval. Its start is the later of the two starts, and its end is the earlier of the two ends: [max(a.start, b.start), min(a.end, b.end)]. The overlap begins only once both intervals have started, and it ends the moment either one stops. Merge grows, overlap shrinks. Merge uses [min, max] and covers both intervals. Overlap uses [max, min] and keeps only the part both share. Sort by Start first Left unsorted, checking a list of intervals means comparing every pair, which is O(n^2). Sorting the intervals by start time removes that cost. Once the list is in start order, any interval can only overlap ones that come after it and begin before it ends, so a single left-to-right pass is enough to merge, count, or detect collisions. Sorting by start is the standard first line of almost every interval solution, and the problems below all lean on it. That gives the template every interval problem in this section builds on: sort by start, then sweep once, comparing each interval against the last one you kept. Input [[1,3], [8,10], [2,6], [4,5], [10,12]] After sort [[1,3], [2,6], [4,5], [8,10], [10,12]] Show trace table i [start, end] result[-1] start <= result[-1][1]? Action result after 0 [1,3] — — (empty) append [[1,3]] 1 [2,6] [1,3] 2 <= 3 ✅ end = max(3,6) = 6 [[1,6]] 2 [4,5] [1,6] 4 <= 6 ✅ end = max(6,5) = 6 [[1,6]] 3 [8,10] [1,6] 8 <= 6 ❌ append [[1,6], [8,10]] 4 [10,12] [8,10] 10 <= 10 ✅ end = max(10,12) = 12 [[1,6], [8,12]] :::tip[Output] [[1,6], [8,12]] Row 2: [4,5] sits inside [1,6]. Setting end = 5 without max would cut the interval short. max keeps 6. Row 4: [8,10] and [10,12] only touch at 10. The <= check merges them. With <, you would get two intervals. ::: Insert into a sorted list Sometimes the list is already sorted and has no overlaps, and you must add one new interval. You could append it, sort, and run the template. That works, but it pays O(n log n) for a sort you do not need. A sorted list with no overlaps splits into three parts around the new interval, in this order: Intervals that end before the new one starts. Keep them as they are. Intervals that overlap the new one. Merge each into it with [min, max]. Intervals that start after the new one ends. Keep them as they are. Walk the list once and handle each part in turn. No sort, so the cost drops to O(n). Intervals [[1,2], [3,5], [6,7], [8,10], [12,16]] New interval [4,8] Show trace table i interval Part Check Action [start, end] result after 0 [1,2] 1 2 < 4 ✅ keep [4,8] [[1,2]] 1 [3,5] 1 5 < 4 ❌ go to part 2 [4,8] [[1,2]] 1 [3,5] 2 3 <= 8 ✅ start = min(4,3) = 3 [3,8] [[1,2]] 2 [6,7] 2 6 <= 8 ✅ end = max(8,7) = 8 [3,8] [[1,2]] 3 [8,10] 2 8 <= 8 ✅ end = max(8,10) = 10 [3,10] [[1,2]] 4 [12,16] 2 12 <= 10 ❌ append new [3,10] [[1,2], [3,10]] 4 [12,16] 3 — extend rest — [[1,2], [3,10], [12,16]] :::tip[Output] [[1,2], [3,10], [12,16]] Row i=1: The same interval shows up twice. The part 1 check fails, so that loop stops, and part 2 reads the same interval again. i does not move. Row i=3: [8,10] only touches [3,8] at 8. The <= merges them, since one shared point counts as an overlap. Part 1 uses <, part 2 uses <=: An interval that ends exactly at start touches the new interval. It must fail part 1 so part 2 can merge it. ::: If the input is already sorted, do not sort again. Interviewers add \"sorted\" to the problem on purpose. Name the O(n log n) cost of re-sorting before they do, then offer the O(n) three-part walk. Footnote The positive overlap test comes straight from De Morgan's law. Negating (a.end < b.start) or (b.end < a.start) gives (a.end >= b.start) and (b.end >= a.start), which is the a.start <= b.end and b.start <= a.end condition rewritten.","contentType":"doc","description":"Ranges with a start and an end are sorted, and then merged, counted, or scheduled.","locale":"","route":"/intervals","section":"Intervals","title":"Intervals","version":""},{"breadcrumb":["Decision Space","Intervals"],"content":"Example 1: Input: intervals = [[1,3],[6,9]], newInterval = [2,5] Output: [[1,5],[6,9]] Example 2: Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8] Output: [[1,2],[3,10],[12,16]] Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10]. Constraints: 0 <= intervals.length <= 10^4 intervals[i].length == 2 0 <= starti <= endi <= 10^5 intervals is sorted by starti in ascending order. newInterval.length == 2 0 <= start <= end <= 10^5 Solution","contentType":"doc","description":"You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the i^th interval and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval","locale":"","route":"/intervals/insert-interval","section":"Intervals","title":"57. Insert Interval","version":""},{"breadcrumb":["Decision Space","Intervals"],"content":"Example 1: Input: intervals = [[1,3],[2,6],[8,10],[15,18]] Output: [[1,6],[8,10],[15,18]] Explanation: Since intervals [1,3] and [2,6] overlap, merge them into [1,6]. Example 2: Input: intervals = [[1,4],[4,5]] Output: [[1,5]] Explanation: Intervals [1,4] and [4,5] are considered overlapping. Example 3: Input: intervals = [[4,7],[1,4]] Output: [[1,7]] Explanation: Intervals [1,4] and [4,7] are considered overlapping. Constraints: 1 <= intervals.length <= 10^4 intervals[i].length == 2 0 <= starti <= endi <= 10^4 Solution","contentType":"doc","description":"Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input","locale":"","route":"/intervals/merge-intervals","section":"Intervals","title":"56. Merge Intervals","version":""},{"breadcrumb":["Decision Space","Intervals"],"content":"Example 1: Input: intervals = [[1,2],[2,3],[3,4],[1,3]] Output: 1 Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping. Example 2: Input: intervals = [[1,2],[1,2],[1,2]] Output: 2 Explanation: You need to remove two [1,2] to make the rest of the intervals non-overlapping. Example 3: Input: intervals = [[1,2],[2,3]] Output: 0 Explanation: You don't need to remove any of the intervals since they're already non-overlapping. Constraints: 1 <= intervals.length <= 10^5 intervals[i].length == 2 -5 * 10^4 <= starti < endi <= 5 * 10^4 Solution","contentType":"doc","description":"Given an array of intervals intervals where intervals[i] = [starti, endi], return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping","locale":"","route":"/intervals/non-overlapping-intervals","section":"Intervals","title":"435. Non-overlapping Intervals","version":""},{"breadcrumb":[],"content":"Each entry below names the keyword, the algorithm it points to, and a one-line reason. Where a keyword has more than one possible technique, each option is listed with the sub-signal that picks it. Techniques you haven't studied yet are covered later in the course. \"Top K\" Heap — track the K-item boundary in O(n log K) instead of sorting all n. K Closest Points \"Sorted\" / \"rotated sorted\" / \"in O(log n)\" Binary Search — halve the search space at each step using order. Works on any monotonic predicate, not just literal arrays. Binary Search \"How many ways...\" Counting distinct outcomes — choose by what the input looks like. DFS, when choices form a tree of decisions. Decode Ways DP, when subproblems overlap on a grid or sequence. Robot Paths \"Substring\" Sliding Window — contiguous slices extend and shrink in O(1) at each step. Longest Substring Without Repeating Characters \"Prefix\" / \"autocomplete\" / \"dictionary lookup\" / \"starts with\" Trie — share storage for common prefixes; lookup runs in O(length of word) regardless of dictionary size. Implement Trie \"Palindrome\" Palindromes mirror around a center — choose by whether you're verifying or constructing. Two Pointers, to verify a single string. Valid Palindrome DFS, to enumerate every valid partition. Palindrome Partitioning DP, to minimize cuts or count partitions. Palindrome Partitioning II \"Tree\" One question decides everything: do you care about depth, or just need to visit every node? BFS, for level-order, shallowest depth, or \"right-side view\". Binary Tree Level-Order Traversal DFS, for sums, heights, paths — anything not level-related. Max Depth \"Parentheses\" Stack — brackets close in reverse order, last opened first closed (LIFO). Valid Parentheses \"Next greater\" / \"next smaller\" / \"warmer day\" Monotonic Stack — keep candidates in stack order so the answer for each element pops out in O(1) amortized. Next Greater Element II \"Subarray\" Contiguous slices, but the right tool depends on what you're computing. Sliding Window, for fixed size or a monotonic constraint. Subarray Sum Fixed Prefix Sum, for range-sum queries. Subarray Sum Hashmap of prefix sums, for sum equals a target. Continuous Subarray Sum \"Max subarray\" Greedy (Kadane's) — at each index, extend the run or restart; the choice is local. Kadane's Algorithm \"Two sum / K sum\" Two Pointers on a sorted array — each move strictly raises or lowers the sum. Two Sum \"Max/longest sequence\" \"Longest\" usually means each position depends on earlier ones — except when a fixed window is involved. DP / DFS with memoization, when a position depends on earlier positions. Longest Increasing Subsequence Monotonic Deque, for max within a sliding window. Sliding Window Maximum \"Minimum/Shortest\" Three very different problems wear the same word. BFS, for fewest edges in an unweighted graph. Shortest Path Dijkstra, for shortest path with non-negative edge weights. Dijkstra's Algorithm DP / DFS, for cumulative cost on a grid or sequence. Minimal Path Sum \"Partition/split\" DFS — each split point is a recursive choice; memoize if suffixes repeat. Decode Ways \"Subsequence\" DP / DFS with memoization — pick-or-skip at each index, with overlapping subproblems. Longest Increasing Subsequence \"All combinations / permutations / subsets\" / \"generate every\" Backtracking — DFS that records a partial solution, recurses, then undoes the choice on the way back up. Permutations, Subsets \"Matrix\" A grid is a graph. Connectivity questions are BFS/DFS; optimization questions are DP. BFS / DFS, for connectivity, flood-fill, or region counting. Flood Fill, Number of Islands DP, for optimization where each cell builds on neighbors. Maximal Square \"Jump\" Greedy when local-optimal is provably global; DP otherwise. Greedy, when \"always reach as far as possible\" works (typically reachability). Jump Game DP, when counting jumps or greedy fails. Jump Game II \"Game\" DP on game states — \"can the player to move force a win?\" recurses with overlap. Divisor Game \"Connected component\" Union Find — near-O(1) union and find beat re-running BFS after each edge. Use BFS or DFS instead when the graph is built once and you only traverse a few times. Number of Connected Components \"Schedule\" / \"prerequisites\" / \"build order\" / \"course\" Topological Sort (Kahn's BFS or DFS post-order) — directed dependencies must be linearized so every edge points forward. Detects cycles as a side effect. Course Schedule \"Transitive relationship\" If A relates to B and B relates to C means A relates to C, the relation is a graph in disguise. Items become nodes, relations become edges, and \"are these two related?\" becomes \"are they in the same component?\" BFS, to answer a single query by traversing on the fly. Word Ladder BFS or Union Find, for many queries on a fixed relation set. Sentence Similarity BFS or Union Find, for numbers with divisional relationships. Evaluate Division \"Interval\" Greedy after sorting — by start (merge) or by end (fit-in), then one pass. Interval Pattern","contentType":"doc","description":"Coding problems rarely tell you which algorithm to use, but the wording usually leaks it. \"K-th largest\" hints at a heap. \"Shortest path\" in an unweighted graph hints at BFS.","locale":"","route":"/keywords","section":"Docs","title":"Keywords","version":""},{"breadcrumb":["Nodal & Grid","Linked List"],"content":"","contentType":"doc","description":"Each node points to the next node, and the pointers give the path through the data.","locale":"","route":"/linked-list","section":"Linked List","title":"Linked List","version":""},{"breadcrumb":["Nodal & Grid","Linked List"],"content":"Can you solve it using O(1) (i.e. constant) memory? Example 1: Input: head = [3,2,0,-4], pos = 1 Output: true Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed). Example 2: Input: head = [1,2], pos = 0 Output: true Explanation: There is a cycle in the linked list, where the tail connects to the 0th node. Example 3: Input: head = [1], pos = -1 Output: false Explanation: There is no cycle in the linked list. Constraints: The number of the nodes in the list is in the range [0, 10^4]. -10^5 <= Node.val <= 10^5 pos is -1 or a valid index in the linked-list. Approach Solution","contentType":"doc","description":"Given head, the head of a linked list, determine if the linked list has a cycle in it","locale":"","route":"/linked-list/linked-list-cycle","section":"Linked List","title":"141. Linked List Cycle","version":""},{"breadcrumb":["Nodal & Grid","Linked List"],"content":"Example 1: Input: list1 = [1,2,4], list2 = [1,3,4] Output: [1,1,2,3,4,4] Example 2: Input: list1 = [], list2 = [] Output: [] Example 3: Input: list1 = [], list2 = [0] Output: [0] Constraints: The number of nodes in both lists is in the range [0, 50]. -100 <= Node.val <= 100 Both list1 and list2 are sorted in non-decreasing order. Approach Solution","contentType":"doc","description":"You are given the heads of two sorted linked lists list1 and list2","locale":"","route":"/linked-list/merge-two-sorted-lists","section":"Linked List","title":"21. Merge Two Sorted Lists","version":""},{"breadcrumb":["Nodal & Grid","Linked List"],"content":"Example 1: Input: head = [1,2,3,4,5] Output: [3,4,5] Explanation: The middle node of the list is node 3. Example 2: Input: head = [1,2,3,4,5,6] Output: [4,5,6] Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one. Constraints: The number of nodes in the list is in the range [1, 100]. 1 <= Node.val <= 100 Solution","contentType":"doc","description":"Given the head of a singly linked list, return the middle node of the linked list","locale":"","route":"/linked-list/middle-of-the-linked-list","section":"Linked List","title":"876. Middle of the Linked List","version":""},{"breadcrumb":["Nodal & Grid","Linked List"],"content":"Example 1: Input: head = [1,2,3,3,4,4,5] Output: [1,2,5] Example 2: Input: head = [1,1,1,2,3] Output: [2,3] Constraints: The number of nodes in the list is in the range [0, 300]. -100 <= Node.val <= 100 The list is guaranteed to be sorted in ascending order. Solution","contentType":"doc","description":"You are given the head of a sorted linked list","locale":"","route":"/linked-list/remove-duplicates-from-sorted-list-ii","section":"Linked List","title":"82. Remove Duplicates from Sorted List II","version":""},{"breadcrumb":["Nodal & Grid","Linked List"],"content":"Could you do this in one pass? Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5] Example 2: Input: head = [1], n = 1 Output: [] Example 3: Input: head = [1,2], n = 1 Output: [1] Constraints: The number of nodes in the list is sz. 1 <= sz <= 30 0 <= Node.val <= 100 1 <= n <= sz Solution","contentType":"doc","description":"Given the head of a linked list, remove the n^th node from the end of the list and return its head","locale":"","route":"/linked-list/remove-nth-node-from-end-of-list","section":"Linked List","title":"19. Remove Nth Node From End of List","version":""},{"breadcrumb":["Nodal & Grid","Linked List"],"content":"Example 1: Input: head = [1,2,3,4] Output: [1,4,2,3] Example 2: Input: head = [1,2,3,4,5] Output: [1,5,2,4,3] Constraints: The number of nodes in the list is in the range [1, 5 * 10^4]. 1 <= Node.val <= 1000 Solution","contentType":"doc","description":"You are given the head of a singly linked-list. The list can be represented as:","locale":"","route":"/linked-list/reorder-list","section":"Linked List","title":"143. Reorder List","version":""},{"breadcrumb":["Nodal & Grid","Linked List"],"content":"A linked list can be reversed either iteratively or recursively. Could you implement both? Example 1: Input: head = [1,2,3,4,5] Output: [5,4,3,2,1] Example 2: Input: head = [1,2] Output: [2,1] Example 3: Input: head = [] Output: [] Constraints: The number of nodes in the list is the range [0, 5000]. -5000 <= Node.val <= 5000 Approach Solution","contentType":"doc","description":"Given the head of a singly linked list, reverse the list, and return the reversed list","locale":"","route":"/linked-list/reverse-linked-list","section":"Linked List","title":"206. Reverse Linked List","version":""},{"breadcrumb":["Nodal & Grid","Linked List"],"content":"Could you do it in one pass? Example 1: Input: head = [1,2,3,4,5], left = 2, right = 4 Output: [1,4,3,2,5] Example 2: Input: head = [5], left = 1, right = 1 Output: [5] Constraints: The number of nodes in the list is n. 1 <= n <= 500 -500 <= Node.val <= 500 1 <= left <= right <= n Solution","contentType":"doc","description":"Given the head of a singly linked list and two integers left and right where left <= right, reverse the nodes of the list from position left to position right, and return the reversed list","locale":"","route":"/linked-list/reverse-linked-list-ii","section":"Linked List","title":"92. Reverse Linked List II","version":""},{"breadcrumb":["Nodal & Grid","Matrix Index Math"],"content":"","contentType":"doc","description":"A two-dimensional grid is read as a flat array through a formula, and the neighbors are found by arithmetic.","locale":"","route":"/matrix-index-math","section":"Matrix Index Math","title":"Matrix Index Math","version":""},{"breadcrumb":["Nodal & Grid","Matrix Index Math"],"content":"Example 1: Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [[7,4,1],[8,5,2],[9,6,3]] Example 2: Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]] Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]] Constraints: n == matrix.length == matrix[i].length 1 <= n <= 20 -1000 <= matrix[i][j] <= 1000 Approach Solution","contentType":"doc","description":"You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise)","locale":"","route":"/matrix-index-math/rotate-image","section":"Matrix Index Math","title":"48. Rotate Image","version":""},{"breadcrumb":["Nodal & Grid","Matrix Index Math"],"content":"You must do it in place. Example 1: Input: matrix = [[1,1,1],[1,0,1],[1,1,1]] Output: [[1,0,1],[0,0,0],[1,0,1]] Example 2: Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]] Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]] Constraints: m == matrix.length n == matrix[0].length 1 <= m, n <= 200 -2^31 <= matrix[i][j] <= 2^31 - 1 A straightforward solution using O(mn) space is probably a bad idea. A simple improvement uses O(m + n) space, but still not the best solution. Could you devise a constant space solution? Approach Solution","contentType":"doc","description":"Given an m x n integer matrix matrix, if an element is 0, set its entire row and column to 0's","locale":"","route":"/matrix-index-math/set-matrix-zeroes","section":"Matrix Index Math","title":"73. Set Matrix Zeroes","version":""},{"breadcrumb":["Nodal & Grid","Matrix Index Math"],"content":"Example 1: Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [1,2,3,6,9,8,7,4,5] Example 2: Input: matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]] Output: [1,2,3,4,8,12,11,10,9,5,6,7] Constraints: m == matrix.length n == matrix[i].length 1 <= m, n <= 10 -100 <= matrix[i][j] <= 100 Approach Solution","contentType":"doc","description":"Given an m x n matrix, return all elements of the matrix in spiral order","locale":"","route":"/matrix-index-math/spiral-matrix","section":"Matrix Index Math","title":"54. Spiral Matrix","version":""},{"breadcrumb":["Nodal & Grid","Matrix Index Math"],"content":"Example 1: Input: board = [[\"5\",\"3\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\"] ,[\"6\",\".\",\".\",\"1\",\"9\",\"5\",\".\",\".\",\".\"] ,[\".\",\"9\",\"8\",\".\",\".\",\".\",\".\",\"6\",\".\"] ,[\"8\",\".\",\".\",\".\",\"6\",\".\",\".\",\".\",\"3\"] ,[\"4\",\".\",\".\",\"8\",\".\",\"3\",\".\",\".\",\"1\"] ,[\"7\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\",\"6\"] ,[\".\",\"6\",\".\",\".\",\".\",\".\",\"2\",\"8\",\".\"] ,[\".\",\".\",\".\",\"4\",\"1\",\"9\",\".\",\".\",\"5\"] ,[\".\",\".\",\".\",\".\",\"8\",\".\",\".\",\"7\",\"9\"]] Output: true Example 2: Input: board = [[\"8\",\"3\",\".\",\".\",\"7\",\".\",\".\",\".\",\".\"] ,[\"6\",\".\",\".\",\"1\",\"9\",\"5\",\".\",\".\",\".\"] ,[\".\",\"9\",\"8\",\".\",\".\",\".\",\".\",\"6\",\".\"] ,[\"8\",\".\",\".\",\".\",\"6\",\".\",\".\",\".\",\"3\"] ,[\"4\",\".\",\".\",\"8\",\".\",\"3\",\".\",\".\",\"1\"] ,[\"7\",\".\",\".\",\".\",\"2\",\".\",\".\",\".\",\"6\"] ,[\".\",\"6\",\".\",\".\",\".\",\".\",\"2\",\"8\",\".\"] ,[\".\",\".\",\".\",\"4\",\"1\",\"9\",\".\",\".\",\"5\"] ,[\".\",\".\",\".\",\".\",\"8\",\".\",\".\",\"7\",\"9\"]] Output: false Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid. Constraints: board.length == 9 board[i].length == 9 board[i][j] is a digit 1-9 or '.'. Solution","contentType":"doc","description":"Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:","locale":"","route":"/matrix-index-math/valid-sudoku","section":"Matrix Index Math","title":"36. Valid Sudoku","version":""},{"breadcrumb":["Linear Structures","Monotonic Stack"],"content":"Monotonic Stack A monotonic stack keeps its values in sorted order as you push elements. The order can be increasing or decreasing, depending on the problem. A decreasing monotonic stack is the most common version. Before pushing a new value x, we pop from the top while the top is smaller than or equal to x. After those pops, pushing x keeps the stack decreasing from bottom to top. This push rule is the whole idea. A monotonic deque uses the same rule, but a deque also lets you remove old elements from the front, which is useful for sliding window problems. Example Implementation This implementation maintains a decreasing monotonic stack. In interview problems, the stack usually stores indices instead of values. You can read the value from the original array when needed, and the index gives you distance information such as \"how many steps away is the next greater element.\" For an array nums, if index j pops when you process index i, then i is the first index to the right that satisfies the stack condition for j. In a next-greater-element problem, that means nums[i] is the next greater value for nums[j].","contentType":"doc","description":"A stack keeps its items in sorted order, which finds the next larger or smaller item in one pass.","locale":"","route":"/monotonic-stack","section":"Monotonic Stack","title":"Monotonic Stack","version":""},{"breadcrumb":["Linear Structures","Monotonic Stack"],"content":"Example 1: Input: target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3] Output: 3 Explanation: • The cars starting at 10 (speed 2) and 8 (speed 4) become a fleet, meeting each other at 12. The fleet forms at target. • The car starting at 0 (speed 1) does not catch up to any other car, so it is a fleet by itself. • The cars starting at 5 (speed 1) and 3 (speed 3) become a fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target. Example 2: Input: target = 10, position = [3], speed = [3] Output: 1 Explanation: There is only one car, hence there is only one fleet. Example 3: Input: target = 100, position = [0,2,4], speed = [4,2,1] Output: 1 Explanation: • The cars starting at 0 (speed 4) and 2 (speed 2) become a fleet, meeting each other at 4. The car starting at 4 (speed 1) travels to 5. • Then, the fleet at 4 (speed 2) and the car at position 5 (speed 1) become one fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target. Constraints: n == position.length == speed.length 1 <= n <= 10^5 0 < target <= 10^6 0 <= position[i] < target All the values of position are unique. 0 < speed[i] <= 10^6 Approach Solution","contentType":"doc","description":"There are n cars at given miles away from the starting mile 0, traveling to reach the mile target","locale":"","route":"/monotonic-stack/car-fleet","section":"Monotonic Stack","title":"853. Car Fleet","version":""},{"breadcrumb":["Linear Structures","Monotonic Stack"],"content":"Example 1: Input: temperatures = [73,74,75,71,69,72,76,73] Output: [1,1,4,2,1,1,0,0] Example 2: Input: temperatures = [30,40,50,60] Output: [1,1,1,0] Example 3: Input: temperatures = [30,60,90] Output: [1,1,0] Constraints: 1 <= temperatures.length <= 10^5 30 <= temperatures[i] <= 100 Approach Solution","contentType":"doc","description":"Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the i^th day to get a warmer temperature. If there is no future day for which this is possible, keep answer[i] == 0 instead","locale":"","route":"/monotonic-stack/daily-temperatures","section":"Monotonic Stack","title":"739. Daily Temperatures","version":""},{"breadcrumb":["Linear Structures","Monotonic Stack"],"content":"Example 1: Input: heights = [2,1,5,6,2,3] Output: 10 Explanation: The above is a histogram where width of each bar is 1. The largest rectangle is shown in the red area, which has an area = 10 units. Example 2: Input: heights = [2,4] Output: 4 Constraints: 1 <= heights.length <= 10^5 0 <= heights[i] <= 10^4 Solution","contentType":"doc","description":"Given an array of integers heights representing the histogram's bar height where the width of each bar is 1, return the area of the largest rectangle in the histogram","locale":"","route":"/monotonic-stack/largest-rectangle-in-histogram","section":"Monotonic Stack","title":"84. Largest Rectangle in Histogram","version":""},{"breadcrumb":["Linear Structures","Monotonic Stack"],"content":"Could you find an O(nums1.length + nums2.length) solution? Example 1: Input: nums1 = [4,1,2], nums2 = [1,3,4,2] Output: [-1,3,-1] Explanation: The next greater element for each value of nums1 is as follows: 4 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1. 1 is underlined in nums2 = [1,3,4,2]. The next greater element is 3. 2 is underlined in nums2 = [1,3,4,2]. There is no next greater element, so the answer is -1. Example 2: Input: nums1 = [2,4], nums2 = [1,2,3,4] Output: [3,-1] Explanation: The next greater element for each value of nums1 is as follows: 2 is underlined in nums2 = [1,2,3,4]. The next greater element is 3. 4 is underlined in nums2 = [1,2,3,4]. There is no next greater element, so the answer is -1. Constraints: 1 <= nums1.length <= nums2.length <= 1000 0 <= nums1[i], nums2[i] <= 10^4 All integers in nums1 and nums2 are unique. All the integers of nums1 also appear in nums2. Solution","contentType":"doc","description":"The next greater element of some element x in an array is the first greater element that is to the right of x in the same array","locale":"","route":"/monotonic-stack/next-greater-element-i","section":"Monotonic Stack","title":"496. Next Greater Element I","version":""},{"breadcrumb":["Linear Structures","Monotonic Stack"],"content":"Example 1: Input: `` Output: `` Explanation: Input [\"StockSpanner\", \"next\", \"next\", \"next\", \"next\", \"next\", \"next\", \"next\"] [[], [100], [80], [60], [70], [60], [75], [85]] Output [null, 1, 1, 1, 2, 1, 4, 6] Explanation StockSpanner stockSpanner = new StockSpanner(); stockSpanner.next(100); // return 1 stockSpanner.next(80); // return 1 stockSpanner.next(60); // return 1 stockSpanner.next(70); // return 2 stockSpanner.next(60); // return 1 stockSpanner.next(75); // return 4, because the last 4 prices (including today's price of 75) were less than or equal to today's price. stockSpanner.next(85); // return 6 Constraints: 1 <= price <= 10^5 At most 10^4 calls will be made to next. Solution","contentType":"doc","description":"Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day","locale":"","route":"/monotonic-stack/online-stock-span","section":"Monotonic Stack","title":"901. Online Stock Span","version":""},{"breadcrumb":["Decision Space","Multi-D / Grid DP"],"content":"","contentType":"doc","description":"The result for each state is stored in a table with two or more dimensions, and each cell is computed from its neighbors.","locale":"","route":"/multi-d-grid-dp","section":"Multi-D / Grid DP","title":"Multi-D / Grid DP","version":""},{"breadcrumb":["Decision Space","Multi-D / Grid DP"],"content":"Example 1: Input: text1 = \"abcde\", text2 = \"ace\" Output: 3 Explanation: The longest common subsequence is \"ace\" and its length is 3. Example 2: Input: text1 = \"abc\", text2 = \"abc\" Output: 3 Explanation: The longest common subsequence is \"abc\" and its length is 3. Example 3: Input: text1 = \"abc\", text2 = \"def\" Output: 0 Explanation: There is no such common subsequence, so the result is 0. Constraints: 1 <= text1.length, text2.length <= 1000 text1 and text2 consist of only lowercase English characters. Solution","contentType":"doc","description":"Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0","locale":"","route":"/multi-d-grid-dp/longest-common-subsequence","section":"Multi-D / Grid DP","title":"1143. Longest Common Subsequence","version":""},{"breadcrumb":["Decision Space","Multi-D / Grid DP"],"content":"Example 1: Input: grid = [[1,3,1],[1,5,1],[4,2,1]] Output: 7 Explanation: Because the path 1 → 3 → 1 → 1 → 1 minimizes the sum. Example 2: Input: grid = [[1,2,3],[4,5,6]] Output: 12 Constraints: m == grid.length n == grid[i].length 1 <= m, n <= 200 0 <= grid[i][j] <= 200 Solution","contentType":"doc","description":"Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right, which minimizes the sum of all numbers along its path","locale":"","route":"/multi-d-grid-dp/minimum-path-sum","section":"Multi-D / Grid DP","title":"64. Minimum Path Sum","version":""},{"breadcrumb":["Decision Space","Multi-D / Grid DP"],"content":"Example 1: Input: m = 3, n = 7 Output: 28 Example 2: Input: m = 3, n = 2 Output: 3 Explanation: From the top-left corner, there are a total of 3 ways to reach the bottom-right corner: 1. Right -> Down -> Down 2. Down -> Down -> Right 3. Down -> Right -> Down Constraints: 1 <= m, n <= 100 Solution","contentType":"doc","description":"There is a robot on an m x n grid. The robot is initially located at the top-left corner (i.e., grid[0][0]). The robot tries to move to the bottom-right corner (i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any point in time","locale":"","route":"/multi-d-grid-dp/unique-paths","section":"Multi-D / Grid DP","title":"62. Unique Paths","version":""},{"breadcrumb":["Linear Structures","Prefix Sum"],"content":"Suppose you are handed an array and asked for the sum of the elements between index i and index j. One query is easy: loop from i to j and add. But now imagine thousands of such queries over the same array, or a search that inspects the sum of every possible subarray. Recomputing each range from scratch costs O(n) per query, and the work piles up fast. Prefix sum removes that repetition. You spend O(n) once to precompute running totals, and afterward every range sum collapses into a single subtraction that runs in O(1), no matter how wide the range. Prefix Sum Explained Transformation A prefix sum turns an array into a new array of running totals. For an input array arr of length n, build a prefix array of length n + 1: prefix[0] = 0 prefix[k] = arr[0] + arr[1] + ... + arr[k-1] Each entry is the previous total plus one more element, so a single pass builds the whole thing: prefix[k] = prefix[k-1] + arr[k-1] The leading prefix[0] = 0 is the sum of zero elements. That extra slot is what makes the range math below land exactly on the boundaries, which is why prefix has one more cell than arr. Each bar is the running total up to that index. Every step keeps the previous total (blue) and adds one new element (amber) Any range sum is one subtraction The sum of the subarray from index i up to (but not including) j is prefix[j] - prefix[i]. prefix[j] is the total of everything before j, and prefix[i] is the total of everything before i, so subtracting throws away the front you did not ask for and leaves the middle. Watch the boundaries The one place prefix sums trip people up is the off-by-one at the edges. Keep three facts straight: prefix has length n + 1, prefix[0] is 0, and sum(arr[i..j-1]) = prefix[j] - prefix[i] uses an exclusive right index j. If a problem hands you an inclusive right index r, translate it to prefix[r + 1] - prefix[i]. When to reach for it Prefix sums fit whenever you need repeated range sums over a fixed array, or when an algorithm searches across subarray boundaries. For a single one-off sum, a plain loop is simpler; the prefix array earns its O(n) setup only once the same array is queried many times.","contentType":"doc","description":"A running total is stored at each index, so the sum of any range is one subtraction.","locale":"","route":"/prefix-sum","section":"Prefix Sum","title":"Prefix Sum","version":""},{"breadcrumb":["Linear Structures","Prefix Sum"],"content":"Example 1: Input: nums = [0,1] Output: 2 Explanation: [0, 1] is the longest contiguous subarray with an equal number of 0 and 1. Example 2: Input: nums = [0,1,0] Output: 2 Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1. Example 3: Input: nums = [0,1,1,1,1,1,0,0,0] Output: 6 Explanation: [1,1,1,0,0,0] is the longest contiguous subarray with equal number of 0 and 1. Constraints: 1 <= nums.length <= 10^5 nums[i] is either 0 or 1. Solution","contentType":"doc","description":"Given a binary array nums, return the maximum length of a contiguous subarray with an equal number of 0 and 1","locale":"","route":"/prefix-sum/contiguous-array","section":"Prefix Sum","title":"525. Contiguous Array","version":""},{"breadcrumb":["Linear Structures","Prefix Sum"],"content":"You must write an algorithm that runs in O(n) time and without using the division operation. Can you solve the problem in O(1) extra space complexity? (The output array does not count as extra space for space complexity analysis.) Example 1: Input: nums = [1,2,3,4] Output: [24,12,8,6] Example 2: Input: nums = [-1,1,0,-3,3] Output: [0,0,9,0,0] Constraints: 2 <= nums.length <= 10^5 -30 <= nums[i] <= 30 The input is generated such that answer[i] is guaranteed to fit in a 32-bit integer. Approach Solution","contentType":"doc","description":"Given an integer array nums, return an array answer such that answer[i] is equal to the product of all the elements of nums except nums[i]","locale":"","route":"/prefix-sum/product-of-array-except-self","section":"Prefix Sum","title":"238. Product of Array Except Self","version":""},{"breadcrumb":["Linear Structures","Prefix Sum"],"content":"Example 1: Input: [\"NumArray\", \"sumRange\", \"sumRange\", \"sumRange\"] [[[-2, 0, 3, -5, 2, -1]], [0, 2], [2, 5], [0, 5]] Output: [null, 1, -1, -3] Explanation: NumArray numArray = new NumArray([-2, 0, 3, -5, 2, -1]); numArray.sumRange(0, 2); // return (-2) + 0 + 3 = 1 numArray.sumRange(2, 5); // return 3 + (-5) + 2 + (-1) = -1 numArray.sumRange(0, 5); // return (-2) + 0 + 3 + (-5) + 2 + (-1) = -3 Constraints: 1 <= nums.length <= 10^4 -10^5 <= nums[i] <= 10^5 0 <= left <= right < nums.length At most 10^4 calls will be made to sumRange. Approach Solution","contentType":"doc","description":"Given an integer array nums, handle multiple queries of the following type","locale":"","route":"/prefix-sum/range-sum-query-immutable","section":"Prefix Sum","title":"303. Range Sum Query - Immutable","version":""},{"breadcrumb":["Linear Structures","Prefix Sum"],"content":"Example 1: Input: nums = [1,1,1], k = 2 Output: 2 Example 2: Input: nums = [1,2,3], k = 3 Output: 2 Constraints: 1 <= nums.length <= 2 * 10^4 -1000 <= nums[i] <= 1000 -10^7 <= k <= 10^7 Approach Solution","contentType":"doc","description":"Given an array of integers nums and an integer k, return the total number of subarrays whose sum equals to k","locale":"","route":"/prefix-sum/subarray-sum-equals-k","section":"Prefix Sum","title":"560. Subarray Sum Equals K","version":""},{"breadcrumb":["Linear Structures","Prefix Sum"],"content":"Example 1: Input: nums = [4,5,0,-2,-3,1], k = 5 Output: 7 Explanation: There are 7 subarrays with a sum divisible by k = 5: [4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3] Example 2: Input: nums = [5], k = 9 Output: 0 Constraints: 1 <= nums.length <= 3 * 10^4 -10^4 <= nums[i] <= 10^4 2 <= k <= 10^4 Solution","contentType":"doc","description":"Given an integer array nums and an integer k, return the number of non-empty subarrays that have a sum divisible by k","locale":"","route":"/prefix-sum/subarray-sums-divisible-by-k","section":"Prefix Sum","title":"974. Subarray Sums Divisible by K","version":""},{"breadcrumb":["Linear Structures","Sliding Window"],"content":"Sliding Window Many problems have two moving boundaries. That alone does not make them sliding window problems. A problem belongs to this family when: the answer depends on the subarray or substring between left and right you can update the window state incrementally as elements enter or leave you maintain a condition that tells you whether the window is valid The rule for all three: every element enters once and leaves once, so the whole thing runs in O(n). What is an invariant? An invariant is a statement that stays true before and after every iteration. In a sliding window, that statement usually describes the window itself, such as “the window is valid,” “it has no duplicates,” or “its sum is at most the target.” It can also describe a helper structure, such as “the frequency map matches the characters currently inside the window.” Why invariants matter? Without an invariant, shrinking the window is just a guess. You no longer know when the window is valid, when to update the answer, or why the left pointer should stop. The invariant is what makes the algorithm correct. How sliding window differs from other two-pointer families? Sliding window is not the same as every other two-pointer pattern. In Container With Most Water, the interval is only the remaining search space. In sliding window, the interval itself is the thing you care about. In Remove Duplicates, the left side is already the finished answer prefix. In sliding window, both pointers describe a live window that is still being checked. 1. Fixed-size windows The window size never changes. The main task is updating the window state efficiently when one element enters and one leaves. Sliding Window Fixed Add nums[right] to the window. If the window has k elements, record the answer. Remove the leftmost element so the window stays at size k. 2. Flexible-size windows with a validity rule Here the window expands until invalid, then shrinks until valid again. Sliding Window Flexible Longest Window Shortest Window Add nums[right] to the window. While the window is invalid, remove nums[left] and move left up. The window is now valid. Record its size. 3. Frequency-tracking windows The difficulty comes from keeping the bookkeeping correct as characters enter and leave. Frequency-tracking windows Add s[right] to the window. While the window has more than one character, remove s[left] and move left up. The window is now valid. Record its size. For anagram or \"minimum window substring\" problems, you also keep a target count. Track how many characters currently match the target with one integer (matched). Update it only when a count crosses the target value. This keeps each step O(1).","contentType":"doc","description":"Sliding window is a specialized two-pointer family where the pointers define a contiguous interval and you maintain information about that entire interval as it moves.","locale":"","route":"/sliding-window","section":"Sliding Window","title":"Sliding Window","version":""},{"breadcrumb":["Linear Structures","Sliding Window"],"content":"Example 1: Input: prices = [7,1,5,3,6,4] Output: 5 Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell. Example 2: Input: prices = [7,6,4,3,1] Output: 0 Explanation: In this case, no transactions are done and the max profit = 0. Constraints: 1 <= prices.length <= 10^5 0 <= prices[i] <= 10^4 Approach The diagram traces the code below literally. That code does not run: curr and ans are never initialised, so the first curr > left test raises NameError, and left holds a price rather than an index, so prices[left] indexes by value. Solution","contentType":"doc","description":"You are given an array prices where prices[i] is the price of a given stock on the i^th day","locale":"","route":"/sliding-window/best-time-to-buy-and-sell-stock","section":"Sliding Window","title":"121. Best Time to Buy and Sell Stock","version":""},{"breadcrumb":["Linear Structures","Sliding Window"],"content":"Example 1: Input: s = \"ABAB\", k = 2 Output: 4 Explanation: Replace the two 'A's with two 'B's or vice versa. Example 2: Input: s = \"AABABBA\", k = 1 Output: 4 Explanation: Replace the one 'A' in the middle with 'B' and form \"AABBBBA\". The substring \"BBBB\" has the longest repeating letters, which is 4. There may exists other ways to achieve this answer too. Constraints: 1 <= s.length <= 10^5 s consists of only uppercase English letters. 0 <= k <= s.length Approach Solution","contentType":"doc","description":"You are given a string s and an integer k. You can choose any character of the string and change it to any other uppercase English character. You can perform this operation at most k times","locale":"","route":"/sliding-window/longest-repeating-character-replacement","section":"Sliding Window","title":"424. Longest Repeating Character Replacement","version":""},{"breadcrumb":["Linear Structures","Sliding Window"],"content":"Example 1: Input: s = \"abcabcbb\" Output: 3 Explanation: The answer is \"abc\", with the length of 3. Note that \"bca\" and \"cab\" are also correct answers. Example 2: Input: s = \"bbbbb\" Output: 1 Explanation: The answer is \"b\", with the length of 1. Example 3: Input: s = \"pwwkew\" Output: 3 Explanation: The answer is \"wke\", with the length of 3. Notice that the answer must be a substring, \"pwke\" is a subsequence and not a substring. Constraints: 0 <= s.length <= 10^5 s consists of English letters, digits, symbols and spaces. Approach Solution","contentType":"doc","description":"Given a string s, find the length of the longest substring without duplicate characters","locale":"","route":"/sliding-window/longest-substring-without-repeating-characters","section":"Sliding Window","title":"3. Longest Substring Without Repeating Characters","version":""},{"breadcrumb":["Linear Structures","Sliding Window"],"content":"Example 1: Input: nums = [1,1,1,0,0,0,1,1,1,1,0], k = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], k = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Constraints: 1 <= nums.length <= 10^5 nums[i] is either 0 or 1. 0 <= k <= nums.length Solution","contentType":"doc","description":"Given a binary array nums and an integer k, return the maximum number of consecutive 1's in the array if you can flip at most k 0's","locale":"","route":"/sliding-window/max-consecutive-ones-iii","section":"Sliding Window","title":"1004. Max Consecutive Ones III","version":""},{"breadcrumb":["Linear Structures","Sliding Window"],"content":"Example 1: Input: nums = [1,12,-5,-6,50,3], k = 4 Output: 12.75000 Explanation: Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75 Example 2: Input: nums = [5], k = 1 Output: 5.00000 Constraints: n == nums.length 1 <= k <= n <= 10^5 -10^4 <= nums[i] <= 10^4 Solution","contentType":"doc","description":"You are given an integer array nums consisting of n elements, and an integer k","locale":"","route":"/sliding-window/maximum-average-subarray-i","section":"Sliding Window","title":"643. Maximum Average Subarray I","version":""},{"breadcrumb":["Linear Structures","Sliding Window"],"content":"If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log(n)). Example 1: Input: target = 7, nums = [2,3,1,2,4,3] Output: 2 Explanation: The subarray [4,3] has the minimal length under the problem constraint. Example 2: Input: target = 4, nums = [1,4,4] Output: 1 Example 3: Input: target = 11, nums = [1,1,1,1,1,1,1,1] Output: 0 Constraints: 1 <= target <= 10^9 1 <= nums.length <= 10^5 1 <= nums[i] <= 10^4 Solution","contentType":"doc","description":"Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead","locale":"","route":"/sliding-window/minimum-size-subarray-sum","section":"Sliding Window","title":"209. Minimum Size Subarray Sum","version":""},{"breadcrumb":["Linear Structures","Sliding Window"],"content":"Could you find an algorithm that runs in O(m + n) time? Example 1: Input: s = \"ADOBECODEBANC\", t = \"ABC\" Output: \"BANC\" Explanation: The minimum window substring \"BANC\" includes 'A', 'B', and 'C' from string t. Example 2: Input: s = \"a\", t = \"a\" Output: \"a\" Explanation: The entire string s is the minimum window. Example 3: Input: s = \"a\", t = \"aa\" Output: \"\" Explanation: Both 'a's from t must be included in the window. Since the largest window of s only has one 'a', return empty string. Constraints: m == s.length n == t.length 1 <= m, n <= 10^5 s and t consist of uppercase and lowercase English letters. Solution","contentType":"doc","description":"Given two strings s and t of lengths m and n respectively, return the minimum window substring of s such that every character in t (including duplicates) is included in the window. If there is no such substring, return the empty string \"\"","locale":"","route":"/sliding-window/minimum-window-substring","section":"Sliding Window","title":"76. Minimum Window Substring","version":""},{"breadcrumb":["Linear Structures","Sliding Window"],"content":"Example 1: Input: s1 = \"ab\", s2 = \"eidbaooo\" Output: true Explanation: s2 contains one permutation of s1 (\"ba\"). Example 2: Input: s1 = \"ab\", s2 = \"eidboaoo\" Output: false Constraints: 1 <= s1.length, s2.length <= 10^4 s1 and s2 consist of lowercase English letters. Approach Solution","contentType":"doc","description":"Given two strings s1 and s2, return true if s2 contains a permutation of s1, or false otherwise","locale":"","route":"/sliding-window/permutation-in-string","section":"Sliding Window","title":"567. Permutation in String","version":""},{"breadcrumb":["Linear Structures","Stack"],"content":"A stack works like a stack of plates in a cafeteria. You add new plates on top. When you need a plate, you take from the top. The last plate you added is the first one you remove. This is Last-In-First-Out (LIFO). The most recent item goes out first. Core Stack Operations A stack has two fundamental operations: Push: add an item to the top of the stack Pop: remove the item at the top of the stack Some stacks have additional operations: Peek: get the item at the top of the stack without removing it IsEmpty: check if the stack is empty Python lists provide append() for pushing and pop() for popping. The peek() method is not built-in, but you can implement it yourself. Last in, First out The defining characteristic of a stack is LIFO order. Whatever you put in last comes out first. This is different from a queue, which is First-In-First-Out (FIFO). Add items: A, B, C Stack: [A, B, C] (C on top) Remove items: First pop: C Second pop: B Third pop: A The order reverses. Items come out in the opposite order they went in.","contentType":"doc","description":"Items are added and removed from one end, so the last item added is the first item removed.","locale":"","route":"/stack","section":"Stack","title":"Stack","version":""},{"breadcrumb":["Linear Structures","Stack"],"content":"Example 1: Input: s = \"1 + 1\" Output: 2 Example 2: Input: s = \" 2-1 + 2 \" Output: 3 Example 3: Input: s = \"(1+(4+5+2)-3)+(6+8)\" Output: 23 Constraints: 1 <= s.length <= 3 * 10^5 s consists of digits, '+', '-', '(', ')', and ' '. s represents a valid expression. '+' is not used as a unary operation (i.e., \"+1\" and \"+(2 + 3)\" is invalid). '-' could be used as a unary operation (i.e., \"-1\" and \"-(2 + 3)\" is valid). There will be no two consecutive operators in the input. Every number and running calculation will fit in a signed 32-bit integer. Solution","contentType":"doc","description":"Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation","locale":"","route":"/stack/basic-calculator","section":"Stack","title":"224. Basic Calculator","version":""},{"breadcrumb":["Linear Structures","Stack"],"content":"Example 1: Input: tokens = [\"2\",\"1\",\"+\",\"3\",\"*\"] Output: 9 Explanation: ((2 + 1) * 3) = 9 Example 2: Input: tokens = [\"4\",\"13\",\"5\",\"/\",\"+\"] Output: 6 Explanation: (4 + (13 / 5)) = 6 Example 3: Input: tokens = [\"10\",\"6\",\"9\",\"3\",\"+\",\"-11\",\"*\",\"/\",\"*\",\"17\",\"+\",\"5\",\"+\"] Output: 22 Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5 = ((10 * (6 / (12 * -11))) + 17) + 5 = ((10 * (6 / -132)) + 17) + 5 = ((10 * 0) + 17) + 5 = (0 + 17) + 5 = 17 + 5 = 22 Constraints: 1 <= tokens.length <= 10^4 tokens[i] is either an operator: \"+\", \"-\", \"*\", or \"/\", or an integer in the range [-200, 200]. Approach Solution","contentType":"doc","description":"You are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation","locale":"","route":"/stack/evaluate-reverse-polish-notation","section":"Stack","title":"150. Evaluate Reverse Polish Notation","version":""},{"breadcrumb":["Linear Structures","Stack"],"content":"You must implement a solution with O(1) time complexity for each function. Example 1: Input: `` Output: `` Explanation: Input [\"MinStack\",\"push\",\"push\",\"push\",\"getMin\",\"pop\",\"top\",\"getMin\"] [[],[-2],[0],[-3],[],[],[],[]] Output [null,null,null,null,-3,null,0,-2] Explanation MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); // return -3 minStack.pop(); minStack.top(); // return 0 minStack.getMin(); // return -2 Constraints: -2^31 <= val <= 2^31 - 1 Methods pop, top and getMin operations will always be called on non-empty stacks. At most 3 * 10^4 calls will be made to push, pop, top, and getMin. Approach Solution","contentType":"doc","description":"Design a stack that supports push, pop, top, and retrieving the minimum element in constant time","locale":"","route":"/stack/min-stack","section":"Stack","title":"155. Min Stack","version":""},{"breadcrumb":["Linear Structures","Stack"],"content":"Example 1: Input: path = \"/home/\" Output: \"/home\" Explanation: The trailing slash should be removed. Example 2: Input: path = \"/home//foo/\" Output: \"/home/foo\" Explanation: Multiple consecutive slashes are replaced by a single one. Example 3: Input: path = \"/home/user/Documents/../Pictures\" Output: \"/home/user/Pictures\" Explanation: A double period \"..\" refers to the directory up a level (the parent directory). Example 4: Input: path = \"/../\" Output: \"/\" Explanation: Going one level up from the root directory is not possible. Example 5: Input: path = \"/.../a/../b/c/../d/./\" Output: \"/.../b/d\" Explanation: \"...\" is a valid name for a directory in this problem. Constraints: 1 <= path.length <= 3000 path consists of English letters, digits, period '.', slash '/' or '_'. path is a valid absolute Unix path. Solution","contentType":"doc","description":"You are given an absolute path for a Unix-style file system, which always begins with a slash '/'. Your task is to transform this absolute path into its simplified canonical path","locale":"","route":"/stack/simplify-path","section":"Stack","title":"71. Simplify Path","version":""},{"breadcrumb":["Linear Structures","Stack"],"content":"Example 1: Input: s = \"()\" Output: true Example 2: Input: s = \"()[]{}\" Output: true Example 3: Input: s = \"(]\" Output: false Example 4: Input: s = \"([])\" Output: true Example 5: Input: s = \"([)]\" Output: false Constraints: 1 <= s.length <= 10^4 s consists of parentheses only '()[]'. Approach Solution","contentType":"doc","description":"Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid","locale":"","route":"/stack/valid-parentheses","section":"Stack","title":"20. Valid Parentheses","version":""},{"breadcrumb":["Relational","Topological Sort"],"content":"Directed Graph Before we get started on topological order, let's talk about directed graphs first. A graph is directed when its edges have directions (these edges are also called arcs). Suppose that v, w are vertices in a directed graph. Let's get familiar with a few concepts. in-edge - an edge pointing to or coming into the node out-edge - an edge pointing away or going out from the node (we will refer those nodes on the other end of the out-edges of v to v's neighbors) in-degree - the number of in-edges coming into the node out-degree - the number of out-edges going out from the node dependency - \"v depends on w\" / \"v is dependent on w\" means that there is a directed path from w to v where v, w are vertices in a directed graph. With these new terms under our belt, it will make more sense when we talk about directed graphs. What is a topological order? Topological sort is not unique. For example, for a graph like this: Both [4, 5, 2] and [5, 4, 2] are valid topological orders. Task 3 is completely independent of task 2 and 4, and it can be anywhere in the order as long as it is before task 1 which depends on it. All of the following ordering are valid topological orderings. [4, 2, 3, 1], [4, 3, 2, 1], [3, 4, 2, 1] Graphs with Cycles Do Not Have Topological Ordering It should be obvious that if a graph with a cycle does not have a topological ordering. In the above example, 2 has to come before 5 which has come before 4 which has to come before 2 which has to come before 5... which is impossible. Kahn's Algorithm To obtain a topological order, we can use Kahn's algorithm which is very similar to Breadth First Search. In order to understand why we must use this algorithm we first look at the problem it is trying to solve. Given a directed graph does there exist a way to remove the nodes such that each time we remove a node we guarantee that no other nodes point to that particular node? For this algorithm, we systematically remove one node at a time, each time removing a node such that no other nodes point to that node (in-degree is 0). If no such node exists, then there must be a cycle, and there is no way to order the nodes such that \"every node appears in the ordering before all the nodes it points to\" (no solution). After removing the current node, for each neighboring node the current node points to, we check whether any nodes point to this node. If there isn't any, we push this node into the queue. Notice it is important to keep track of the number of nodes pointing to a node (in-degree) in the question as we only push the node into the queue once all nodes the current node depended on have been removed. Here is a graphic to demonstrate the idea. Example Code Notice the topological sort algorithm is very similar to BFS. The main difference is that we only push nodes with 0 in-degree into the queue in topological sort whereas in BFS we push all the neighboring nodes into the queue.","contentType":"doc","description":"Topological sort or topological ordering of a directed graph is an ordering of nodes such that every node appears in the ordering before all the nodes it points to. ","locale":"","route":"/topological-sort","section":"Topological Sort","title":"Topological Sort","version":""},{"breadcrumb":["Relational","Topological Sort"],"content":"Example 1: Input: numCourses = 2, prerequisites = [[1,0]] Output: true Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So it is possible. Example 2: Input: numCourses = 2, prerequisites = [[1,0],[0,1]] Output: false Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible. Constraints: 1 <= numCourses <= 2000 0 <= prerequisites.length <= 5000 prerequisites[i].length == 2 0 <= ai, bi < numCourses All the pairs prerequisites[i] are unique. Solution","contentType":"doc","description":"There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai","locale":"","route":"/topological-sort/course-schedule","section":"Topological Sort","title":"207. Course Schedule","version":""},{"breadcrumb":["Relational","Topological Sort"],"content":"Example 1: Input: numCourses = 2, prerequisites = [[1,0]] Output: [0,1] Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1]. Example 2: Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]] Output: [0,2,1,3] Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3]. Example 3: Input: numCourses = 1, prerequisites = [] Output: [0] Constraints: 1 <= numCourses <= 2000 0 <= prerequisites.length <= numCourses * (numCourses - 1) prerequisites[i].length == 2 0 <= ai, bi < numCourses ai != bi All the pairs [ai, bi] are distinct. Solution","contentType":"doc","description":"There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai","locale":"","route":"/topological-sort/course-schedule-ii","section":"Topological Sort","title":"210. Course Schedule II","version":""},{"breadcrumb":["Hierarchical","Tree BFS"],"content":"","contentType":"doc","description":"All nodes at one level are visited before the nodes at the next level.","locale":"","route":"/tree-bfs","section":"Tree BFS","title":"Tree BFS","version":""},{"breadcrumb":["Hierarchical","Tree BFS"],"content":"Example 1: Input: root = [3,9,20,null,null,15,7] Output: [[3],[9,20],[15,7]] Example 2: Input: root = [1] Output: [[1]] Example 3: Input: root = [] Output: [] Constraints: The number of nodes in the tree is in the range [0, 2000]. -1000 <= Node.val <= 1000 Approach Solution","contentType":"doc","description":"Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level)","locale":"","route":"/tree-bfs/binary-tree-level-order-traversal","section":"Tree BFS","title":"102. Binary Tree Level Order Traversal","version":""},{"breadcrumb":["Hierarchical","Tree BFS"],"content":"Example 1: Input: root = [1,2,3,null,5,null,4] Output: [1,3,4] Example 2: Input: root = [1,2,3,4,null,null,null,5] Output: [1,3,4,5] Example 3: Input: root = [1,null,3] Output: [1,3] Example 4: Input: root = [] Output: [] Constraints: The number of nodes in the tree is in the range [0, 100]. -100 <= Node.val <= 100 Approach Solution","contentType":"doc","description":"Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom","locale":"","route":"/tree-bfs/binary-tree-right-side-view","section":"Tree BFS","title":"199. Binary Tree Right Side View","version":""},{"breadcrumb":["Hierarchical","Tree BFS"],"content":"Example 1: Input: root = [3,1,4,3,null,1,5] Output: 4 Explanation: Nodes in blue are good. Root Node (3) is always a good node. Node 4 -> (3,4) is the maximum value in the path starting from the root. Node 5 -> (3,4,5) is the maximum value in the path Node 3 -> (3,1,3) is the maximum value in the path. Example 2: Input: root = [3,3,null,4,2] Output: 3 Explanation: Node 2 -> (3, 3, 2) is not good, because \"3\" is higher than it. Example 3: Input: root = [1] Output: 1 Explanation: Root is considered as good. Constraints: The number of nodes in the binary tree is in the range [1, 10^5]. Each node's value is between [-104, 104]. Approach Solution","contentType":"doc","description":"Given a binary tree root, a node X in the tree is named good if in the path from root to X there are no nodes with a value greater than X","locale":"","route":"/tree-bfs/count-good-nodes-in-binary-tree","section":"Tree BFS","title":"1448. Count Good Nodes in Binary Tree","version":""},{"breadcrumb":["Hierarchical","Tree DFS"],"content":"","contentType":"doc","description":"Each branch is followed to its end before the next branch is examined.","locale":"","route":"/tree-dfs","section":"Tree DFS","title":"Tree DFS","version":""},{"breadcrumb":["Hierarchical","Tree DFS"],"content":"Example 1: Input: root = [4,2,7,1,3,6,9] Output: [4,7,2,9,6,3,1] Example 2: Input: root = [2,1,3] Output: [2,3,1] Example 3: Input: root = [] Output: [] Constraints: The number of nodes in the tree is in the range [0, 100]. -100 <= Node.val <= 100 Approach Solution","contentType":"doc","description":"Given the root of a binary tree, invert the tree, and return its root","locale":"","route":"/tree-dfs/invert-binary-tree","section":"Tree DFS","title":"226. Invert Binary Tree","version":""},{"breadcrumb":["Hierarchical","Tree DFS"],"content":"Example 1: Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 Output: 3 Explanation: The LCA of nodes 5 and 1 is 3. Example 2: Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4 Output: 5 Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition. Example 3: Input: root = [1,2], p = 1, q = 2 Output: 1 Constraints: The number of nodes in the tree is in the range [2, 10^5]. -10^9 <= Node.val <= 10^9 All Node.val are unique. p != q p and q will exist in the tree. Approach Solution","contentType":"doc","description":"Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree","locale":"","route":"/tree-dfs/lowest-common-ancestor-of-a-binary-tree","section":"Tree DFS","title":"236. Lowest Common Ancestor of a Binary Tree","version":""},{"breadcrumb":["Hierarchical","Tree DFS"],"content":"Example 1: Input: root = [3,9,20,null,null,15,7] Output: 3 Example 2: Input: root = [1,null,2] Output: 2 Constraints: The number of nodes in the tree is in the range [0, 10^4]. -100 <= Node.val <= 100 Approach Solution","contentType":"doc","description":"Given the root of a binary tree, return its maximum depth","locale":"","route":"/tree-dfs/maximum-depth-of-binary-tree","section":"Tree DFS","title":"104. Maximum Depth of Binary Tree","version":""},{"breadcrumb":["Hierarchical","Trie"],"content":"","contentType":"doc","description":"Each node holds one character, and a path from the root spells a word.","locale":"","route":"/trie","section":"Trie","title":"Trie","version":""},{"breadcrumb":["Hierarchical","Trie"],"content":"Constraints: 1 <= word.length <= 25 word in addWord consists of lowercase English letters. word in search consist of '.' or lowercase English letters. There will be at most 2 dots in word for search queries. At most 10^4 calls will be made to addWord and search. Approach Solution","contentType":"doc","description":"Design a data structure that supports adding new words and finding if a string matches any previously added string","locale":"","route":"/trie/design-add-and-search-words-data-structure","section":"Trie","title":"211. Design Add and Search Words Data Structure","version":""},{"breadcrumb":["Hierarchical","Trie"],"content":"Example 1: Input: `` Output: `` Explanation: Input [\"Trie\", \"insert\", \"search\", \"search\", \"startsWith\", \"insert\", \"search\"] [[], [\"apple\"], [\"apple\"], [\"app\"], [\"app\"], [\"app\"], [\"app\"]] Output [null, null, true, false, true, null, true] Explanation Trie trie = new Trie(); trie.insert(\"apple\"); trie.search(\"apple\"); // return True trie.search(\"app\"); // return False trie.startsWith(\"app\"); // return True trie.insert(\"app\"); trie.search(\"app\"); // return True Constraints: 1 <= word.length, prefix.length <= 2000 word and prefix consist only of lowercase English letters. At most 3 * 10^4 calls in total will be made to insert, search, and startsWith. Approach Solution","contentType":"doc","description":"A trie (pronounced as \"try\") or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. There are various applications of this data structure, such as autocomplete and spellchecker","locale":"","route":"/trie/implement-trie-prefix-tree","section":"Trie","title":"208. Implement Trie (Prefix Tree)","version":""},{"breadcrumb":["Linear Structures","Two Pointers"],"content":"Most interview problems in this section fall into one of four families: Same direction pointers: one pointer reads, one pointer writes, or one pointer stays behind another while scanning left to right. Opposite direction pointers: one pointer starts on the left, one on the right, and each move eliminates impossible answers. Sliding window: the two pointers define a contiguous interval, and you maintain some property of that interval as the window expands and shrinks. Fast and slow pointers: both pointers move through a linked structure, but at different speeds or with a fixed gap. Same Direction Same-direction pointer problems usually scan from left to right, but the two pointers do different jobs. Usually: one pointer reads the next value from the original input one pointer marks where the next kept value should go Sometimes both pointers move forward together, but one pointer stays behind the other to preserve a gap or a relationship. When this family applies Reach for same-direction pointers when the problem asks you to: rewrite an array in-place remove or compress elements without extra memory keep only the values that satisfy some condition maintain a fixed gap between two forward-moving pointers Core Invariant The invariant in this family is usually about the prefix you have already processed. Typical invariant: \"Everything before the write pointer is already in the correct final form.\" That is what makes these problems feel different from opposite-direction scans. You are not searching from both sides. You are building the answer progressively as you sweep through the input. Subtypes in this section 1. Compaction / Overwrite The slow pointer marks the next output position. The fast pointer scans the input. When the current value should be kept, copy it forward and advance the slow pointer. 2. Relative Speed Both pointers move forward, but one moves twice as fast. The invariant is about their distance and how much of the structure each pointer has consumed. 3. Fixed Gap Both pointers move forward while preserving a gap. The invariant is not about a rewritten prefix. It is about the distance between the pointers. What changes across problems The family stays the same, but the invariant changes slightly: in array compaction, the invariant is about the prefix being correct in fast/slow midpoint problems, the invariant is about relative speed in fixed-gap problems, the invariant is about distance So the unifying idea is not \"both pointers move right.\" The unifying idea is: \"the structure to the left of the pointers already has a known meaning\" What to watch out for Common mistakes: Updating the write pointer before copying the current value. Forgetting exactly what region is already valid. Losing track of whether the invariant is about a prefix, a gap, or relative speed. As you go through the following problems, pay attention to one question: What does the region before the slow pointer mean right now? Opposite Direction Opposite-direction pointer problems place one pointer on the left and one on the right, then move inward. The hard part is never the loop itself. The hard part is justifying which pointer is safe to move. When this family applies Reach for opposite-direction pointers when the problem asks you to: the input is sorted, or can be made monotonic by sorting you are comparing a left choice and a right choice the problem has symmetry moving one side lets you eliminate a set of impossible answers Core Invariant The invariant in this family is usually: \"All candidate answers outside the current [left, right] range have already been ruled out.\" That means each move must come with a proof. If you move a pointer without knowing why the discarded cases cannot contain the optimum, the algorithm is just guessing. Subtypes in this section 1. Monotonic sum elimination Because the array is sorted: if the sum is too small, moving the left pointer right is the only useful move if the sum is too large, moving the right pointer left is the only useful move Sorted order gives you a monotonic rule. 2. Symmetry checking Here the pointers compare mirrored positions. The movement rule is mostly \"move both,\" except when you must skip punctuation or spaces. 3. Bottleneck elimination This one is important because it looks different from the other two. The key idea is that the shorter wall limits the area. If you keep that shorter wall and decrease the width, you cannot improve the answer. So it is safe to discard that wall. Why this is not a sliding window Opposite-direction scans do not maintain an interval property like \"this window is valid.\" Instead, each step does one of these: rules out sums that are too small or too large checks mirrored characters discards a bottleneck boundary The interval itself is just the remaining search space. What to watch out for Common mistakes: Using two pointers on an unsorted pair problem without first creating monotonicity. Moving the taller wall in Container With Most Water without an elimination proof. Calling the method sliding window just because two boundaries exist. As you read the problems in this family, keep asking: What exact set of candidate answers did this pointer move eliminate?","contentType":"doc","description":"Two indices move through the data from different positions and meet at the answer.","locale":"","route":"/two-pointers","section":"Two Pointers","title":"Two Pointers","version":""},{"breadcrumb":["Linear Structures","Two Pointers"],"content":"Notice that the solution set must not contain duplicate triplets. Example 1: Input: nums = [-1,0,1,2,-1,-4] Output: [[-1,-1,2],[-1,0,1]] Explanation: nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0. nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0. nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0. The distinct triplets are [-1,0,1] and [-1,-1,2]. Notice that the order of the output and the order of the triplets does not matter. Example 2: Input: nums = [0,1,1] Output: [] Explanation: The only possible triplet does not sum up to 0. Example 3: Input: nums = [0,0,0] Output: [[0,0,0]] Explanation: The only possible triplet sums up to 0. Constraints: 3 <= nums.length <= 3000 -10^5 <= nums[i] <= 10^5 Approach Solution","contentType":"doc","description":"Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0","locale":"","route":"/two-pointers/3sum","section":"Two Pointers","title":"15. 3Sum","version":""},{"breadcrumb":["Linear Structures","Two Pointers"],"content":"Notice that you may not slant the container. Example 1: Input: height = [1,8,6,2,5,4,8,3,7] Output: 49 Explanation: The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49. Example 2: Input: height = [1,1] Output: 1 Constraints: n == height.length 2 <= n <= 10^5 0 <= height[i] <= 10^4 Approach Solution","contentType":"doc","description":"You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the i^th line are (i, 0) and (i, height[i])","locale":"","route":"/two-pointers/container-with-most-water","section":"Two Pointers","title":"11. Container With Most Water","version":""},{"breadcrumb":["Linear Structures","Two Pointers"],"content":"Suppose there are lots of incoming s, say s1, s2, ..., sk where k >= 10^9, and you want to check one by one to see if t has its subsequence. In this scenario, how would you change your code? Example 1: Input: s = \"abc\", t = \"ahbgdc\" Output: true Example 2: Input: s = \"axc\", t = \"ahbgdc\" Output: false Constraints: 0 <= s.length <= 100 0 <= t.length <= 10^4 s and t consist only of lowercase English letters. Approach Solution","contentType":"doc","description":"Given two strings s and t, return true if s is a subsequence of t, or false otherwise","locale":"","route":"/two-pointers/is-subsequence","section":"Two Pointers","title":"392. Is Subsequence","version":""},{"breadcrumb":["Linear Structures","Two Pointers"],"content":"Example 1: Input: nums = [1,1,1,2,2,3] Output: 5, nums = [1,1,2,2,3,_] Explanation: Your function should return k = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively. It does not matter what you leave beyond the returned k (hence they are underscores). Example 2: Input: nums = [0,0,1,1,1,1,2,3,3] Output: 7, nums = [0,0,1,1,2,3,3,_,_] Explanation: Your function should return k = 7, with the first seven elements of nums being 0, 0, 1, 1, 2, 3 and 3 respectively. It does not matter what you leave beyond the returned k (hence they are underscores). Constraints: 1 <= nums.length <= 3 * 10^4 -10^4 <= nums[i] <= 10^4 nums is sorted in non-decreasing order. Solution","contentType":"doc","description":"Given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. The relative order of the elements should be kept the same","locale":"","route":"/two-pointers/remove-duplicates-from-sorted-array-ii","section":"Two Pointers","title":"80. Remove Duplicates from Sorted Array II","version":""},{"breadcrumb":["Linear Structures","Two Pointers"],"content":"You must do this by modifying the input array in-place with O(1) extra memory. Example 1: Input: s = [\"h\",\"e\",\"l\",\"l\",\"o\"] Output: [\"o\",\"l\",\"l\",\"e\",\"h\"] Example 2: Input: s = [\"H\",\"a\",\"n\",\"n\",\"a\",\"h\"] Output: [\"h\",\"a\",\"n\",\"n\",\"a\",\"H\"] Constraints: 1 <= s.length <= 10^5 s[i] is a printable ascii character. Approach Solution","contentType":"doc","description":"Write a function that reverses a string. The input string is given as an array of characters s","locale":"","route":"/two-pointers/reverse-string","section":"Two Pointers","title":"344. Reverse String","version":""},{"breadcrumb":["Linear Structures","Two Pointers"],"content":"Example 1: Input: nums = [1,2,3,4,5,6,7], k = 3 Output: [5,6,7,1,2,3,4] Explanation: rotate 1 steps to the right: [7,1,2,3,4,5,6] rotate 2 steps to the right: [6,7,1,2,3,4,5] rotate 3 steps to the right: [5,6,7,1,2,3,4] Example 2: Input: nums = [-1,-100,3,99], k = 2 Output: [3,99,-1,-100] Explanation: rotate 1 steps to the right: [99,-1,-100,3] rotate 2 steps to the right: [3,99,-1,-100] Constraints: 1 <= nums.length <= 10^5 -2^31 <= nums[i] <= 2^31 - 1 0 <= k <= 10^5 Try to come up with as many solutions as you can. There are at least three different ways to solve this problem. Could you do it in-place with O(1) extra space? Solution","contentType":"doc","description":"Given an integer array nums, rotate the array to the right by k steps, where k is non-negative","locale":"","route":"/two-pointers/rotate-array","section":"Two Pointers","title":"189. Rotate Array","version":""},{"breadcrumb":["Linear Structures","Two Pointers"],"content":"Squaring each element and sorting the new array is very trivial, could you find an O(n) solution using a different approach? Example 1: Input: nums = [-4,-1,0,3,10] Output: [0,1,9,16,100] Explanation: After squaring, the array becomes [16,1,0,9,100]. After sorting, it becomes [0,1,9,16,100]. Example 2: Input: nums = [-7,-3,2,3,11] Output: [4,9,9,49,121] Constraints: 1 <= nums.length <= 10^4 -10^4 <= nums[i] <= 10^4 nums is sorted in non-decreasing order. Approach Solution","contentType":"doc","description":"Given an integer array nums sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order","locale":"","route":"/two-pointers/squares-of-a-sorted-array","section":"Two Pointers","title":"977. Squares of a Sorted Array","version":""},{"breadcrumb":["Linear Structures","Two Pointers"],"content":"Example 1: Input: height = [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 Explanation: The above elevation map (black section) is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. Example 2: Input: height = [4,2,0,3,2,5] Output: 9 Constraints: n == height.length 1 <= n <= 2 * 10^4 0 <= height[i] <= 10^5 Solution","contentType":"doc","description":"Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining","locale":"","route":"/two-pointers/trapping-rain-water","section":"Two Pointers","title":"42. Trapping Rain Water","version":""},{"breadcrumb":["Linear Structures","Two Pointers"],"content":"Your solution must use only constant extra space. Example 1: Input: numbers = [2,7,11,15], target = 9 Output: [1,2] Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2]. Example 2: Input: numbers = [2,3,4], target = 6 Output: [1,3] Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3]. Example 3: Input: numbers = [-1,0], target = -1 Output: [1,2] Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2]. Constraints: 2 <= numbers.length <= 3 * 10^4 -1000 <= numbers[i] <= 1000 numbers is sorted in non-decreasing order. -1000 <= target <= 1000 The tests are generated such that there is exactly one solution. Approach Solution","contentType":"doc","description":"Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length","locale":"","route":"/two-pointers/two-sum-ii-input-array-is-sorted","section":"Two Pointers","title":"167. Two Sum II - Input Array Is Sorted","version":""},{"breadcrumb":["Linear Structures","Two Pointers"],"content":"Example 1: Input: s = \"A man, a plan, a canal: Panama\" Output: true Explanation: \"amanaplanacanalpanama\" is a palindrome. Example 2: Input: s = \"race a car\" Output: false Explanation: \"raceacar\" is not a palindrome. Example 3: Input: s = \" \" Output: true Explanation: s is an empty string \"\" after removing non-alphanumeric characters. Since an empty string reads the same forward and backward, it is a palindrome. Constraints: 1 <= s.length <= 2 * 10^5 s consists only of printable ASCII characters. Approach Solution","contentType":"doc","description":"A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers","locale":"","route":"/two-pointers/valid-palindrome","section":"Two Pointers","title":"125. Valid Palindrome","version":""},{"breadcrumb":["Relational","Union-Find"],"content":"","contentType":"doc","description":"Union-Find (Disjoint Set Union) is a data structure that maintains a collection of disjoint sets and supports efficiently finding which set an element belongs to and merging two sets.","locale":"","route":"/union-find","section":"Union-Find","title":"Union Find","version":""},{"breadcrumb":["Relational","Union-Find"],"content":"Example 1: Input: n = 5, edges = [[0,1],[1,2],[3,4]] Output: 2 Example 2: Input: n = 5, edges = [[0,1],[1,2],[2,3],[3,4]] Output: 1 Constraints: 1 <= n <= 2000 1 <= edges.length <= 5000 edges[i] = [ai, bi] ai != bi There are no repeated edges. Solution","contentType":"doc","description":"You have a graph of n nodes. You are given an integer n and an array edges where edges[i] = [ai, bi] indicates that there is an edge between ai and bi in the graph","locale":"","route":"/union-find/number-of-connected-components-in-an-undirected-graph","section":"Union-Find","title":"323. Number of Connected Components in an Undirected Graph","version":""},{"breadcrumb":["Relational","Union-Find"],"content":"Example 1: Input: edges = [[1,2],[1,3],[2,3]] Output: [2,3] Example 2: Input: edges = [[1,2],[2,3],[3,4],[1,4],[1,5]] Output: [1,4] Constraints: n == edges.length 3 <= n <= 1000 edges[i].length == 2 1 <= ai < bi <= edges.length ai != bi There are no repeated edges. The given graph is connected. Solution","contentType":"doc","description":"In this problem, a tree is an undirected graph that is connected and has no cycles","locale":"","route":"/union-find/redundant-connection","section":"Union-Find","title":"684. Redundant Connection","version":""}]
diff --git a/apps/docs/src/pages/404.astro b/apps/docs/src/pages/404.astro
new file mode 100644
index 0000000..b227d45
--- /dev/null
+++ b/apps/docs/src/pages/404.astro
@@ -0,0 +1,176 @@
+---
+// Generated by Blume. Do not edit. Override by adding `pages/404.astro`.
+import Icon from "blume/components/Icon.astro";
+import PageLayout from "blume/components/layout/PageLayout.astro";
+import { withMountedBase } from "blume/components/islands/base-path.ts";
+import { mountBasePath } from "blume/core/base-path.ts";
+import data from "blume:data";
+
+export const prerender = true;
+
+const nf = data.ui.notFound;
+
+// The 404 page is an unlocalized route, so its chrome renders in the default
+// locale's dictionary and direction (`data.ui` is the default locale's resolved
+// dictionary), mirroring the catch-all's locale wiring.
+const i18n = data.config.i18n;
+const localeMeta = i18n
+ ? i18n.locales.find((l) => l.code === i18n.defaultLocale)
+ : null;
+const dir = localeMeta?.dir ?? "ltr";
+const htmlLang = i18n ? i18n.defaultLocale : "en";
+
+// Recovery links, so a reader — or an agent that followed a stale URL — can
+// get back on track without guessing: every top-level section, then the
+// machine-readable indexes the build emits (the sitemap only exists with a
+// `deployment.site`; llms.txt only when `agents.llmsTxt` is on). Tabs link to
+// their resolved target when the section has no index page of its own.
+const suggestions = [
+ ...data.navigation.tabs.map((tab) => ({
+ href: withMountedBase(tab.href ?? tab.path),
+ label: tab.label,
+ })),
+ ...(data.config.discovery.sitemap
+ ? [{ href: withMountedBase("/sitemap.xml"), label: nf.sitemap }]
+ : []),
+ ...(data.config.discovery.llmsTxt
+ ? [{ href: withMountedBase("/llms.txt"), label: nf.llms }]
+ : []),
+];
+
+// Every host serves this one page for any missing URL, so a reader who
+// mistyped `/ar/…` would get the default locale's message. The other locales'
+// copy rides along, and the script below swaps it in when the URL's first
+// segment past the site's base names one of them — the message, its language
+// and direction, the tab title, and the home link (to that locale's root).
+// Localized routes sit under both bases (`{deployment.base}/{basePath}/ar/…`),
+// so the script strips the two together, trailing slash included.
+const { basePath } = data.config;
+const siteRoot = withMountedBase(mountBasePath(basePath, "/"));
+const localeBase = siteRoot.endsWith("/") ? siteRoot : siteRoot + "/";
+const localized = Object.fromEntries(
+ (i18n?.locales ?? [])
+ .filter((l) => l.code !== i18n?.defaultLocale && data.uiByLocale[l.code])
+ .map((l) => {
+ const strings = data.uiByLocale[l.code].notFound;
+ return [
+ l.code,
+ {
+ description: strings.description,
+ dir: l.dir ?? "ltr",
+ home: strings.home,
+ homeHref: withMountedBase(mountBasePath(basePath, "/" + l.code)),
+ suggestions: strings.suggestions,
+ title: strings.title,
+ },
+ ];
+ })
+);
+const localizedJson = JSON.stringify(localized).replaceAll("<", "\\u003c");
+---
+
+
+
+
+ {
+ suggestions.length > 0 && (
+
+
+ {nf.suggestions}
+
+
+
+ )
+ }
+
+
+
+
diff --git a/apps/docs/src/pages/404.json.ts b/apps/docs/src/pages/404.json.ts
new file mode 100644
index 0000000..4b42b7e
--- /dev/null
+++ b/apps/docs/src/pages/404.json.ts
@@ -0,0 +1,52 @@
+// Generated by Blume. Do not edit. Override by adding `pages/404.astro`.
+import { problem } from "blume/ai/api/problem.ts";
+import { withMountedBase } from "blume/components/islands/base-path.ts";
+import { absoluteUrl } from "blume/core/site-url.ts";
+import data from "blume:data";
+
+export const prerender = true;
+
+const nf = data.ui.notFound;
+
+// Absolute for internal routes when the site is known; an external tab href
+// passes through untouched.
+const href = (path: string): string => {
+ const based = withMountedBase(path);
+ return data.config.site && based.startsWith("/") && !based.startsWith("//")
+ ? absoluteUrl(data.config.site, based)
+ : based;
+};
+
+// The recovery set of 404.astro: home, every top-level section (a tab links to
+// its resolved target), then the machine-readable indexes that exist.
+const links = [
+ { href: href("/"), label: nf.home },
+ ...data.navigation.tabs.map((tab) => ({
+ href: href(tab.href ?? tab.path),
+ label: tab.label,
+ })),
+ ...(data.config.discovery.sitemap
+ ? [{ href: href("/sitemap.xml"), label: nf.sitemap }]
+ : []),
+ ...(data.config.discovery.llmsTxt
+ ? [{ href: href("/llms.txt"), label: nf.llms }]
+ : []),
+ ...(data.config.discovery.api
+ ? [{ href: href("/openapi.json"), label: nf.api }]
+ : []),
+];
+
+const body = problem({
+ code: "PAGE_NOT_FOUND",
+ detail: nf.description,
+ links,
+ resolution: nf.suggestions + ": " + links.map((link) => link.href).join(", "),
+ status: 404,
+ title: nf.title,
+});
+
+export function GET() {
+ return new Response(JSON.stringify(body, null, 2) + "\n", {
+ headers: { "Content-Type": "application/problem+json; charset=utf-8" },
+ });
+}
diff --git a/apps/docs/src/pages/404.md.ts b/apps/docs/src/pages/404.md.ts
new file mode 100644
index 0000000..5bb5205
--- /dev/null
+++ b/apps/docs/src/pages/404.md.ts
@@ -0,0 +1,57 @@
+// Generated by Blume. Do not edit. Override by adding `pages/404.astro`.
+import { withMountedBase } from "blume/components/islands/base-path.ts";
+import { absoluteUrl } from "blume/core/site-url.ts";
+import data from "blume:data";
+
+export const prerender = true;
+
+const nf = data.ui.notFound;
+
+// Absolute for internal routes when the site is known; an external tab href
+// passes through untouched.
+const href = (path: string): string => {
+ const based = withMountedBase(path);
+ return data.config.site && based.startsWith("/") && !based.startsWith("//")
+ ? absoluteUrl(data.config.site, based)
+ : based;
+};
+
+// The recovery set of 404.astro: home, every top-level section (a tab links to
+// its resolved target), then the machine-readable indexes that exist.
+const links = [
+ { href: href("/"), label: nf.home },
+ ...data.navigation.tabs.map((tab) => ({
+ href: href(tab.href ?? tab.path),
+ label: tab.label,
+ })),
+ ...(data.config.discovery.sitemap
+ ? [{ href: href("/sitemap.xml"), label: nf.sitemap }]
+ : []),
+ ...(data.config.discovery.llmsTxt
+ ? [{ href: href("/llms.txt"), label: nf.llms }]
+ : []),
+ ...(data.config.discovery.api
+ ? [{ href: href("/openapi.json"), label: nf.api }]
+ : []),
+];
+
+const body = [
+ "# " + nf.title,
+ "",
+ nf.description,
+ "",
+ "## " + nf.suggestions,
+ "",
+ ...links.map((link) => "- [" + link.label + "](" + link.href + ")"),
+ "",
+].join("\n");
+
+export function GET() {
+ return new Response(body, {
+ headers: {
+ "Content-Type": "text/markdown; charset=utf-8",
+ // ~4 characters per token; keep in sync with markdownTokenCount.
+ "x-markdown-tokens": String(Math.ceil(body.length / 4)),
+ },
+ });
+}
diff --git a/apps/docs/src/pages/[...slug].astro b/apps/docs/src/pages/[...slug].astro
new file mode 100644
index 0000000..171f604
--- /dev/null
+++ b/apps/docs/src/pages/[...slug].astro
@@ -0,0 +1,492 @@
+---
+// Generated by Blume. Do not edit.
+import { getEntry, render } from "astro:content";
+import { pageModeLayout } from "blume/core/page-modes.ts";
+import type { CollectionKey } from "astro:content";
+import RootLayout from "blume/components/layout/RootLayout.astro";
+import { withBase, withMountedBase } from "blume/components/islands/base-path.ts";
+import { mountBasePath, stripBasePath } from "blume/core/base-path.ts";
+import { routeSetFor, servesRoute } from "blume/core/locale-links.ts";
+import { resolveLocalizable } from "blume/core/localizable.ts";
+import { resolveSlot } from "blume/components/layout/overrides.ts";
+import Accordion from "blume/components/content/Accordion.astro";
+import AccordionItem from "blume/components/content/AccordionItem.astro";
+import ApiEndpoint from "blume/components/content/ApiEndpoint.astro";
+import ApiPlayground from "blume/components/content/ApiPlayground.astro";
+import ApiRail from "blume/components/content/ApiRail.astro";
+import AutoTypeTable from "blume/components/content/AutoTypeTable.astro";
+import Badge from "blume/components/content/Badge.astro";
+import Callout from "blume/components/content/Callout.astro";
+import Card from "blume/components/content/Card.astro";
+import CardGroup from "blume/components/content/CardGroup.astro";
+import Changelog from "blume/components/content/Changelog.astro";
+import CodeBlock from "blume/components/content/CodeBlock.astro";
+import CodeGroup from "blume/components/content/CodeGroup.astro";
+import ColorRoot from "blume/components/content/Color.astro";
+import ColorItem from "blume/components/content/ColorItem.astro";
+import ColorRow from "blume/components/content/ColorRow.astro";
+import Column from "blume/components/content/Column.astro";
+import Columns from "blume/components/content/Columns.astro";
+import Component from "blume/components/content/Component.astro";
+import Diff from "blume/components/content/Diff.astro";
+import Expandable from "blume/components/content/Expandable.astro";
+import FileTree from "blume/components/content/FileTree.astro";
+import Frame from "blume/components/content/Frame.astro";
+import GithubInfo from "blume/components/content/GithubInfo.astro";
+import Panel from "blume/components/content/Panel.astro";
+import ParamField from "blume/components/content/ParamField.astro";
+import Prompt from "blume/components/content/Prompt.astro";
+import RequestExample from "blume/components/content/RequestExample.astro";
+import ResponseExample from "blume/components/content/ResponseExample.astro";
+import ResponseField from "blume/components/content/ResponseField.astro";
+import Step from "blume/components/content/Step.astro";
+import Steps from "blume/components/content/Steps.astro";
+import Tab from "blume/components/content/Tab.astro";
+import Tabs from "blume/components/content/Tabs.astro";
+import Tile from "blume/components/content/Tile.astro";
+import Tooltip from "blume/components/content/Tooltip.astro";
+import TreeRoot from "blume/components/content/Tree.astro";
+import TreeFile from "blume/components/content/TreeFile.astro";
+import TreeFolder from "blume/components/content/TreeFolder.astro";
+import TypeTable from "blume/components/content/TypeTable.astro";
+import View from "blume/components/content/View.astro";
+import Visibility from "blume/components/content/Visibility.astro";
+import YouTube from "blume/components/content/YouTube.astro";
+import Icon from "blume/components/Icon.astro";
+import LocaleLinks from "blume/components/layout/LocaleLinks.astro";
+import NarrationPlayer from "blume/components/layout/NarrationPlayer.astro";
+import ViewSwitcher from "blume/components/content/ViewSwitcher.astro";
+import ApiOverview from "blume/components/openapi/ApiOverview.astro";
+import ApiTagOperations from "blume/components/openapi/ApiTagOperations.astro";
+import Operation from "blume/components/openapi/Operation.astro";
+import { mdxComponents as userMdx, layoutOverrides } from "../generated/components.ts";
+import data from "blume:data";
+
+const Color = Object.assign(ColorRoot, { Item: ColorItem, Row: ColorRow });
+const Tree = Object.assign(TreeRoot, { File: TreeFile, Folder: TreeFolder });
+
+// Docs content is file-based and always prerendered, even in server output
+// (where only endpoints like /api/ask render on demand). Without this, server
+// builds would render this route on demand and ignore getStaticPaths, leaving
+// the entry id undefined.
+export const prerender = true;
+
+const components = {
+ Accordion,
+ AccordionItem,
+ ApiEndpoint,
+ ApiOverview,
+ ApiPlayground,
+ ApiRail,
+ ApiTagOperations,
+ AutoTypeTable,
+ Badge,
+ Callout,
+ Card,
+ CardGroup,
+ Changelog,
+ CodeBlock,
+ CodeGroup,
+ Color,
+ Column,
+ Columns,
+ Component,
+ Diff,
+ Expandable,
+ FileTree,
+ Frame,
+ GithubInfo,
+ Icon,
+ Operation,
+ Panel,
+ ParamField,
+ Prompt,
+ RequestExample,
+ ResponseExample,
+ ResponseField,
+ Step,
+ Steps,
+ Tab,
+ Tabs,
+ Tile,
+ Tooltip,
+ Tree,
+ TypeTable,
+ View,
+ Visibility,
+ YouTube,
+ ...userMdx,
+};
+
+export function getStaticPaths() {
+ return data.routes.map((route) => ({
+ params: { slug: route.path === "/" ? undefined : route.path.slice(1) },
+ props: {
+ alternates: route.alternates,
+ collection: route.collection,
+ editUrl: route.editUrl,
+ entryId: route.entryId,
+ fallback: route.fallback,
+ hreflang: route.hreflang,
+ indexable: route.indexable,
+ lastModified: route.lastModified,
+ published: route.published,
+ locale: route.locale,
+ monolingual: route.monolingual,
+ route: route.path,
+ title: route.title,
+ version: route.version,
+ versionAlternates: route.versionAlternates,
+ },
+ }));
+}
+
+const { entryId, collection, route, title, indexable, editUrl, lastModified, published, locale, alternates, hreflang, fallback, monolingual, version, versionAlternates } = Astro.props;
+const entry = await getEntry(collection as CollectionKey, entryId);
+if (!entry) {
+ return new Response(null, { status: 404 });
+}
+const { Content, headings: allHeadings, remarkPluginFrontmatter } = await render(entry);
+// `[!toc]`-marked headings render on the page but stay out of the table of
+// contents; the heading plugin reports their slugs through the render's
+// frontmatter (see markdown/heading-anchors.ts). Only the plugin's array
+// counts: `frontmatter.extend` can declare the same key, and on a page with
+// no headings that user-supplied value would pass straight through.
+// A page written in `` blocks gets their picker above the content;
+// the views plugin lists them through the render's frontmatter (see
+// markdown/views.ts), and only a list of titled entries counts.
+const viewsRaw = remarkPluginFrontmatter?.blumeViews;
+const views = Array.isArray(viewsRaw)
+ ? viewsRaw.filter((view) => typeof view?.title === "string")
+ : [];
+const tocHiddenRaw = remarkPluginFrontmatter?.__blumeTocHidden;
+const tocHidden = new Set(Array.isArray(tocHiddenRaw) ? tocHiddenRaw : []);
+// A heading holding a `` lists without the badge's text, which the
+// heading plugin reports by slug the same way.
+const tocTextRaw = remarkPluginFrontmatter?.__blumeTocText;
+const tocText = tocTextRaw && typeof tocTextRaw === "object" ? tocTextRaw : {};
+const headings = (
+ tocHidden.size > 0
+ ? allHeadings.filter((heading) => !tocHidden.has(heading.slug))
+ : allHeadings
+).map((heading) =>
+ typeof tocText[heading.slug] === "string"
+ ? { ...heading, text: tocText[heading.slug] }
+ : heading
+);
+// An untitled page's opening `# Heading` is its title, so the title-heading
+// plugin drops it from the body and reports its id here: the title ``
+// below takes it, so links to the heading's anchor still land (see
+// markdown/title-heading.ts).
+const titleIdRaw = remarkPluginFrontmatter?.blumeTitleId;
+const titleId = typeof titleIdRaw === "string" ? titleIdRaw : undefined;
+const frontmatter = entry.data;
+
+const seo = frontmatter.seo;
+const base = data.config.site ? data.config.site.replace(/\/$/, "") : null;
+
+// Percent-encode the route-derived path (the sitemap convention): a Unicode
+// slug (`/api/größe`) is not legal in a raw URI, and crawlers compare
+// canonical against the sitemap's encoded byte-for-byte.
+const ogPath = data.config.og.enabled
+ ? encodeURI(`/og/${route === "/" ? "index" : route.slice(1)}.png`)
+ : null;
+// Root-relative paths carry the deployment base (the page is served under it),
+// and the site too once it's known: `site + base + path`. `seo.image` may be
+// an external URL, which passes through verbatim (mirrors PageLayout). An
+// authored image keeps a base written by hand; the generated card is always
+// mounted under it.
+const absoluteOg = (path: string, based: (path: string) => string) =>
+ path.startsWith("/") ? `${base ?? ""}${based(path)}` : path;
+// The page's own image, else the site's default for its locale.
+const selectedOgImage =
+ seo.image ??
+ (data.config.og.image && resolveLocalizable(data.config.og.image, locale, data.config.i18n?.defaultLocale));
+const ogImage = selectedOgImage
+ ? absoluteOg(selectedOgImage, withBase)
+ : ogPath && absoluteOg(ogPath, withMountedBase);
+// Blume's generated card has known dimensions the layout can declare; an
+// authored image could be any size or format, so it gets none.
+const ogGenerated = !selectedOgImage && Boolean(ogPath);
+
+// X attribution: the site's account, plus a creator the page can claim for
+// itself (a guest post crediting its own author) over the configured default.
+const x = { ...data.config.x, ...(seo.x?.creator ? { creator: seo.x.creator } : {}) };
+
+const basedRoute = withMountedBase(route);
+
+// Locale resolution. With i18n on, pick the active locale's nav + dictionary,
+// build hreflang alternates, and derive the language-switcher targets.
+const i18n = data.config.i18n;
+const localePrefix = (codeArg: string) =>
+ i18n && codeArg === i18n.defaultLocale && i18n.hideDefaultLocalePrefix
+ ? ""
+ : `/${codeArg}`;
+const localizeRoute = (logical: string, codeArg: string) => {
+ const prefix = localePrefix(codeArg);
+ if (!prefix) {
+ return logical;
+ }
+ return logical === "/" ? prefix : `${prefix}${logical}`;
+};
+const stripLocale = (path: string, codeArg: string) => {
+ const prefix = localePrefix(codeArg);
+ return prefix && path.startsWith(prefix) ? path.slice(prefix.length) || "/" : path;
+};
+
+// Version resolution. An archived page renders its snapshot's navigation tree,
+// points its canonical at the latest equivalent (unless configured otherwise),
+// and shows the old-version notice.
+const versionsConfig = data.config.versions;
+const archived = versionsConfig && version
+ ? (versionsConfig.archived.find((v) => v.id === version) ?? null)
+ : null;
+const latestVersionAlt = (versionAlternates ?? []).find((alt) => alt.version === "");
+
+const navigation = version
+ ? (data.navigationByVersion[version]?.[i18n ? locale : ""] ?? data.navigation)
+ : i18n
+ ? (data.navigationByLocale[locale] ?? data.navigation)
+ : data.navigation;
+const ui = i18n ? (data.uiByLocale[locale] ?? data.ui) : data.ui;
+const localeMeta = i18n ? i18n.locales.find((l) => l.code === locale) : null;
+const dir = localeMeta?.dir ?? "ltr";
+const htmlLang = i18n ? locale : "en";
+// A fallback page renders the fallback locale's content, so its text direction
+// follows that language — not the (mirrored) page locale.
+const contentLocale =
+ fallback && i18n?.fallbackLocale ? i18n.fallbackLocale : locale;
+// "Listen to this page", unless the page opts out. The spoken cues are read
+// between the content's own sentences, so they come from the content's
+// language (a fallback page's), while the player's labels follow the page.
+// A `custom` or `frame` page brings its own heading: no title, description,
+// or narration player above its content.
+const pageChrome = pageModeLayout(frontmatter.mode).chrome;
+const narration = pageChrome && frontmatter.narration ? data.config.narration : null;
+const narrationCues = (i18n ? (data.uiByLocale[contentLocale] ?? data.ui) : data.ui).narration;
+const contentDir = i18n
+ ? (i18n.locales.find((l) => l.code === contentLocale)?.dir ?? "ltr")
+ : "ltr";
+// The root route keeps its trailing slash (`https://site/`) so canonical and
+// hreflang URLs byte-match the sitemap's for the home page.
+const absolute = (path: string) => base + withMountedBase(path);
+
+// A fallback page renders the fallback locale's page at this locale's URL, so
+// its canonical names the page it copies, and search engines never rank the
+// copy against the original.
+const fallbackSource =
+ fallback && i18n?.fallbackLocale
+ ? (alternates ?? []).find((alt) => alt.locale === i18n.fallbackLocale)
+ : undefined;
+// The page it copies, when that page is archived too, points on to the latest
+// docs; the copy names that final page directly, the fallback locale's
+// version of the latest one, so the canonical is never a chain.
+const fallbackLatest =
+ fallbackSource && archived && archived.canonical === "latest" && latestVersionAlt
+ ? (data.routes.find((entry) => entry.path === latestVersionAlt.path)?.alternates ?? []).find(
+ (alt) => alt.locale === i18n?.fallbackLocale
+ )
+ : undefined;
+const fallbackTarget = fallbackLatest ?? fallbackSource;
+// An archived page defaults its canonical to the same page in the latest docs
+// when that page still exists — search engines treat the live page as
+// authoritative without deindexing version-only content. A page's own
+// `seo.canonical` always wins, and `canonical: "self"` keeps the default.
+const canonical =
+ seo.canonical ??
+ (fallbackTarget && base
+ ? absolute(fallbackTarget.path)
+ : archived && archived.canonical === "latest" && latestVersionAlt && base
+ ? absolute(latestVersionAlt.path)
+ : base
+ ? `${base}${basedRoute === "/" ? "/" : encodeURI(basedRoute)}`
+ : null);
+const effectiveNoindex = Boolean(seo.noindex) || (archived?.noindex ?? false);
+
+// hreflang names only indexable translations (see `hreflangAlternates`).
+const localeAlternates =
+ i18n && base
+ ? (hreflang ?? []).map((alt) => ({ hreflang: alt.locale, href: absolute(alt.path) }))
+ : [];
+const defaultAlt = i18n ? (hreflang ?? []).find((alt) => alt.locale === i18n.defaultLocale) : null;
+const xDefault = defaultAlt && base ? absolute(defaultAlt.path) : null;
+
+// `route` arrives with `basePath` already applied, so the locale segment it
+// carries sits *after* the base (`/docs/ja/guide`), while `localePrefix` is
+// base-less (`/ja`). Stripping and re-adding a locale therefore happen in
+// base-less space, with the base re-applied at the end — the same
+// `mountBasePath(basePath, localizeRoute(...))` composition the manifest uses to
+// build every real route. Done in based space, `stripLocale` matches nothing
+// and `localizeRoute` prepends a second prefix (`/ja/docs/ja/guide`). Only a
+// switcher entry for a locale with no real translation reaches this fallback:
+// a partially translated hand-written page for its missing locales, or a
+// generated reference (which has no `alternates` at all) for every locale
+// but its own.
+const mountLocalized = (logical: string, codeArg: string) =>
+ mountBasePath(data.config.basePath, localizeRoute(logical, codeArg));
+const logicalRoute = i18n
+ ? stripLocale(stripBasePath(data.config.basePath, route), locale)
+ : route;
+// A page from a one-language source (GitHub Releases) gets no switcher: every
+// other locale would only repeat the same text. A locale with no real
+// translation links the page's fallback copy, which exists only while
+// fallbacks are on; where nothing is served at that URL (`fallbackLocale:
+// null`), the locale is left out rather than linked to a 404.
+const localeSwitch = i18n && !monolingual
+ ? i18n.locales.flatMap((l) => {
+ const alt = (alternates ?? []).find((x) => x.locale === l.code);
+ const href = alt ? alt.path : mountLocalized(logicalRoute, l.code);
+ return alt || servesRoute(routeSetFor(data.routes), href)
+ ? [{
+ code: l.code,
+ current: l.code === locale,
+ dir: l.dir,
+ href,
+ label: l.label,
+ untranslated: !alt,
+ }]
+ : [];
+ })
+ : [];
+
+// Version switcher + old-version notice. The switcher auto-populates from the
+// versions config as a `kind: "version"` selector; a user-declared version
+// selector in `navigation.selectors` suppresses it (theirs renders instead).
+// Fallback version roots compose like real routes — `{basePath}/{locale?}/{id}`
+// (manifest `versionAlternates` paths arrive with the base already applied).
+const versionRootFor = (id: string) => {
+ const logical = id ? `/${id}` : "/";
+ return i18n
+ ? mountLocalized(logical, locale)
+ : mountBasePath(data.config.basePath, logical);
+};
+const samePageSwitch = versionsConfig
+ ? versionsConfig.switcher.redirect === "same-page"
+ : true;
+const userHasVersionSelector = navigation.selectors.some(
+ (selector) => selector.kind === "version"
+);
+const versionSelector =
+ versionsConfig && !userHasVersionSelector
+ ? {
+ items: [
+ {
+ id: "",
+ label: versionsConfig.current.label,
+ tag: versionsConfig.current.badge,
+ },
+ ...versionsConfig.archived.map((v) => ({
+ id: v.id,
+ label: v.label ?? v.id,
+ tag: undefined,
+ })),
+ ].map((entry) => {
+ const alt = (versionAlternates ?? []).find(
+ (a) => a.version === entry.id
+ );
+ return {
+ label: entry.label,
+ path: samePageSwitch && alt ? alt.path : versionRootFor(entry.id),
+ ...(entry.tag ? { tag: entry.tag } : {}),
+ };
+ }),
+ kind: "version" as const,
+ label: ui.versions.switcher,
+ }
+ : null;
+
+const versionNotice =
+ archived && archived.banner !== false
+ ? {
+ latestHref: latestVersionAlt
+ ? latestVersionAlt.path
+ : versionRootFor(""),
+ latestLabel: ui.versions.latest,
+ message:
+ typeof archived.banner === "string"
+ ? archived.banner
+ : ui.versions.notice.replace(
+ "{version}",
+ archived.label ?? archived.id
+ ),
+ }
+ : null;
+
+// The whole page shell is overridable via `layout.Layout`; it receives the same
+// props as the built-in RootLayout, plus the `layout` map for its inner slots.
+const LayoutComponent = resolveSlot(layoutOverrides.Layout, RootLayout);
+---
+
+
+ {pageChrome && {title} }
+ {pageChrome && frontmatter.description && {frontmatter.description}
}
+ {narration && (
+
+ )}
+ {views.length > 1 && }
+
+
+
+
diff --git a/apps/docs/src/pages/[...slug].md.ts b/apps/docs/src/pages/[...slug].md.ts
new file mode 100644
index 0000000..66c9546
--- /dev/null
+++ b/apps/docs/src/pages/[...slug].md.ts
@@ -0,0 +1,24 @@
+// Generated by Blume. Do not edit.
+import raw from "blume:raw-markdown";
+
+export const prerender = true;
+
+export function getStaticPaths() {
+ return Object.keys(raw).map((route) => ({
+ params: { slug: route === "/" ? "index" : route.slice(1) },
+ props: { route },
+ }));
+}
+
+export function GET({ props }: { props: { route: string } }) {
+ const entries = raw as Record;
+ const entry = entries[props.route];
+ const body = entry ? (entry.md ?? entry.mdx ?? "") : "";
+ return new Response(body, {
+ headers: {
+ "Content-Type": "text/markdown; charset=utf-8",
+ // ~4 characters per token; keep in sync with markdownTokenCount.
+ "x-markdown-tokens": String(Math.ceil(body.length / 4)),
+ },
+ });
+}
diff --git a/apps/docs/src/pages/[...slug].mdx.ts b/apps/docs/src/pages/[...slug].mdx.ts
new file mode 100644
index 0000000..97f1290
--- /dev/null
+++ b/apps/docs/src/pages/[...slug].mdx.ts
@@ -0,0 +1,24 @@
+// Generated by Blume. Do not edit.
+import raw from "blume:raw-markdown";
+
+export const prerender = true;
+
+export function getStaticPaths() {
+ return Object.keys(raw).map((route) => ({
+ params: { slug: route === "/" ? "index" : route.slice(1) },
+ props: { route },
+ }));
+}
+
+export function GET({ props }: { props: { route: string } }) {
+ const entries = raw as Record;
+ const entry = entries[props.route];
+ const body = entry ? (entry.mdx ?? "") : "";
+ return new Response(body, {
+ headers: {
+ "Content-Type": "text/markdown; charset=utf-8",
+ // ~4 characters per token; keep in sync with markdownTokenCount.
+ "x-markdown-tokens": String(Math.ceil(body.length / 4)),
+ },
+ });
+}
diff --git a/apps/docs/src/pages/api/docs/navigation.json.ts b/apps/docs/src/pages/api/docs/navigation.json.ts
new file mode 100644
index 0000000..563c72b
--- /dev/null
+++ b/apps/docs/src/pages/api/docs/navigation.json.ts
@@ -0,0 +1,9 @@
+// Generated by Blume. Do not edit.
+import { navigationResponse } from "blume/ai/api/handlers.ts";
+import data from "blume:mcp-data";
+
+export const prerender = true;
+
+export function GET() {
+ return navigationResponse(data);
+}
diff --git a/apps/docs/src/pages/api/docs/pages.json.ts b/apps/docs/src/pages/api/docs/pages.json.ts
new file mode 100644
index 0000000..847c7de
--- /dev/null
+++ b/apps/docs/src/pages/api/docs/pages.json.ts
@@ -0,0 +1,9 @@
+// Generated by Blume. Do not edit.
+import { pagesIndexResponse } from "blume/ai/api/handlers.ts";
+import data from "blume:mcp-data";
+
+export const prerender = true;
+
+export function GET() {
+ return pagesIndexResponse(data);
+}
diff --git a/apps/docs/src/pages/api/docs/pages/[...route].json.ts b/apps/docs/src/pages/api/docs/pages/[...route].json.ts
new file mode 100644
index 0000000..4e8471d
--- /dev/null
+++ b/apps/docs/src/pages/api/docs/pages/[...route].json.ts
@@ -0,0 +1,13 @@
+// Generated by Blume. Do not edit.
+import { pageParams, pageResponse } from "blume/ai/api/handlers.ts";
+import data from "blume:mcp-data";
+
+export const prerender = true;
+
+export function getStaticPaths() {
+ return pageParams(data);
+}
+
+export function GET({ props }: { props: { route: string } }) {
+ return pageResponse(data, props.route);
+}
diff --git a/apps/docs/src/pages/blume-assets/[...asset].ts b/apps/docs/src/pages/blume-assets/[...asset].ts
new file mode 100644
index 0000000..b261c22
--- /dev/null
+++ b/apps/docs/src/pages/blume-assets/[...asset].ts
@@ -0,0 +1,120 @@
+// Generated by Blume. Do not edit.
+import { existsSync } from "node:fs";
+import { readdir, readFile } from "node:fs/promises";
+import { isAbsolute, join, relative, resolve } from "node:path";
+import type { APIRoute } from "astro";
+import assets from "blume:content-assets";
+
+export const prerender = true;
+
+const files = assets as Record;
+const STAGED_DIR = null;
+
+const TYPES: Record = {
+ ".apng": "image/apng",
+ ".avif": "image/avif",
+ ".bmp": "image/bmp",
+ ".gif": "image/gif",
+ ".ico": "image/x-icon",
+ ".jpeg": "image/jpeg",
+ ".jpg": "image/jpeg",
+ ".mov": "video/quicktime",
+ ".mp4": "video/mp4",
+ ".ogv": "video/ogg",
+ ".png": "image/png",
+ ".svg": "image/svg+xml",
+ ".tiff": "image/tiff",
+ ".webm": "video/webm",
+ ".webp": "image/webp",
+};
+
+// The other files a page's links name beside it, typed so the dev server
+// opens them the way a static host would. Anything else downloads.
+const FILE_TYPES: Record = {
+ ".csv": "text/csv; charset=utf-8",
+ ".json": "application/json",
+ ".mp3": "audio/mpeg",
+ ".pdf": "application/pdf",
+ ".txt": "text/plain; charset=utf-8",
+ ".wav": "audio/wav",
+ ".zip": "application/zip",
+};
+
+const extensionOf = (path: string): string =>
+ path.slice(path.lastIndexOf(".")).toLowerCase();
+
+const contentType = (path: string): string =>
+ TYPES[extensionOf(path)] ??
+ FILE_TYPES[extensionOf(path)] ??
+ "application/octet-stream";
+
+// A source downloads only images and videos; anything else in the staged
+// directory (a page an older build saved under its URL's extension) is never
+// published from the docs origin.
+const isMedia = (path: string): boolean => Object.hasOwn(TYPES, extensionOf(path));
+
+const stagedParams = async (): Promise => {
+ if (STAGED_DIR === null || !existsSync(STAGED_DIR)) {
+ return [];
+ }
+ const entries = await readdir(STAGED_DIR, {
+ recursive: true,
+ withFileTypes: true,
+ });
+ return entries
+ .filter((entry) => entry.isFile() && isMedia(entry.name))
+ .map((entry) =>
+ relative(STAGED_DIR, join(entry.parentPath, entry.name)).replaceAll(
+ "\\",
+ "/"
+ )
+ );
+};
+
+export async function getStaticPaths() {
+ const params = [
+ ...Object.keys(files).map((key) => "content/" + key),
+ ...(await stagedParams()),
+ ];
+ return params.map((asset) => ({ params: { asset } }));
+}
+
+const resolveAsset = (asset: string): string | null => {
+ if (asset.startsWith("content/")) {
+ return files[asset.slice("content/".length)] ?? null;
+ }
+ if (STAGED_DIR === null) {
+ return null;
+ }
+ const abs = resolve(STAGED_DIR, asset);
+ // Traversal guard: the dev server renders on demand, so the param is
+ // attacker-controlled there — never step outside the staged directory.
+ // path.relative rather than a string-prefix test: STAGED_DIR is baked in
+ // with forward slashes while resolve() answers in the platform's
+ // separators, so on Windows the prefix test 404'd every legitimate staged
+ // asset — and a bare prefix also admits a sibling directory that merely
+ // shares the name.
+ const rel = relative(STAGED_DIR, abs);
+ if (rel === "" || rel.startsWith("..") || isAbsolute(rel) || !isMedia(rel)) {
+ return null;
+ }
+ return existsSync(abs) ? abs : null;
+};
+
+export const GET: APIRoute = async ({ params }) => {
+ const path = resolveAsset(String(params.asset ?? ""));
+ if (!path) {
+ return new Response(null, { status: 404 });
+ }
+ const body = await readFile(path);
+ const type = contentType(path);
+ // An SVG opened directly is a document that can run script; the sandbox
+ // keeps an uploaded one inert on the docs origin. This endpoint
+ // prerenders, so these headers reach dev only: a build serves the files
+ // with the same headers from its host's rules (see deploy/headers.ts).
+ const headers: Record =
+ type === "image/svg+xml"
+ ? { ...{"Content-Security-Policy":"sandbox","X-Content-Type-Options":"nosniff"}, "Content-Type": type }
+ : { "Content-Type": type };
+ return new Response(new Uint8Array(body), { headers });
+};
diff --git a/apps/docs/src/pages/blume-search.json.ts b/apps/docs/src/pages/blume-search.json.ts
new file mode 100644
index 0000000..42bcd39
--- /dev/null
+++ b/apps/docs/src/pages/blume-search.json.ts
@@ -0,0 +1,10 @@
+// Generated by Blume. Do not edit.
+import documents from "blume:search-index";
+
+export const prerender = true;
+
+export function GET() {
+ return new Response(JSON.stringify(documents), {
+ headers: { "Content-Type": "application/json" },
+ });
+}
diff --git a/apps/docs/src/pages/og/[...slug].png.ts b/apps/docs/src/pages/og/[...slug].png.ts
new file mode 100644
index 0000000..7ac4bf2
--- /dev/null
+++ b/apps/docs/src/pages/og/[...slug].png.ts
@@ -0,0 +1,96 @@
+// Generated by Blume. Do not edit.
+import { cachedOgImage } from "blume/og";
+import type { OgCache, OgFont, OgFontFamilies, OgGoogleFont } from "blume/og";
+import data from "blume:data";
+
+export const prerender = true;
+
+// Custom (non-content) pages opted into a generated card, baked in at build.
+// The annotation keeps the empty-array case from being an implicit any[]
+// (ts(7034)) under a strict tsconfig.
+const customRoutes: { slug: string; title: string }[] = [{"slug":"appendix","title":"Appendix"},{"slug":"changelog","title":"Changelog"},{"slug":"progress","title":"Progress"}];
+
+// Card fonts, resolved at generation time (explicit seo.og.fonts, or derived
+// from theme.fonts). Local entries carry absolute build-machine paths, which
+// is why they are baked into this build-only endpoint instead of the runtime
+// data that pages serialize into HTML.
+const fonts: OgFont[] = [];
+const families: OgFontFamilies | undefined = {"body":"Geist","title":"Geist"};
+// Script fallbacks (Noto families, the configured locales' first). A card
+// fetches them only when its text has a glyph Takumi's built-in font lacks.
+const fallbacks: OgGoogleFont[] = [{"name":"Noto Sans","weight":[400,600]},{"name":"Noto Sans Ethiopic","weight":[400,600]},{"name":"Noto Sans Arabic","weight":[400,600]},{"name":"Noto Sans Bengali","weight":[400,600]},{"name":"Noto Sans Gujarati","weight":[400,600]},{"name":"Noto Sans Hebrew","weight":[400,600]},{"name":"Noto Sans Devanagari","weight":[400,600]},{"name":"Noto Sans Armenian","weight":[400,600]},{"name":"Noto Sans JP","weight":[400,600]},{"name":"Noto Sans Georgian","weight":[400,600]},{"name":"Noto Sans Khmer","weight":[400,600]},{"name":"Noto Sans Kannada","weight":[400,600]},{"name":"Noto Sans KR","weight":[400,600]},{"name":"Noto Sans Lao","weight":[400,600]},{"name":"Noto Sans Malayalam","weight":[400,600]},{"name":"Noto Sans Myanmar","weight":[400,600]},{"name":"Noto Sans Oriya","weight":[400,600]},{"name":"Noto Sans Gurmukhi","weight":[400,600]},{"name":"Noto Sans Sinhala","weight":[400,600]},{"name":"Noto Sans Tamil","weight":[400,600]},{"name":"Noto Sans Telugu","weight":[400,600]},{"name":"Noto Sans Thai","weight":[400,600]},{"name":"Noto Sans SC","weight":[400,600]},{"name":"Noto Sans TC","weight":[400,600]}];
+
+// Rendered cards are kept on disk between builds, keyed by everything that
+// decides their pixels, so a rebuild renders only the cards whose title,
+// description, branding, or fonts changed. The directory is a build-machine
+// path, baked in for the same reason as the local font paths above.
+const cache: OgCache | undefined = undefined;
+
+// A page's own description (its `seo.description`, else `description`) is
+// the card subtitle, so the image says what the page's og:description says.
+// Pages without one fall back to the site-wide subtitle at render time.
+// `seo.og.description: false` hides the subtitle on every card, page text
+// included, which is what switches this off.
+const pageDescriptions = true;
+
+interface CardProps {
+ title: string;
+ description: string | null;
+}
+
+export function getStaticPaths() {
+ const seen = new Set();
+ const paths: { params: { slug: string }; props: CardProps }[] = [];
+ const add = (slug: string, title: string, description: string | null) => {
+ if (seen.has(slug)) {
+ return;
+ }
+ seen.add(slug);
+ paths.push({
+ params: { slug },
+ props: { title, description: pageDescriptions ? description : null },
+ });
+ };
+ // A custom page wins over a content route sharing its path, so add it first.
+ // Its description is unknown at generate time, so it takes the site subtitle.
+ for (const route of customRoutes) {
+ add(route.slug, route.title, null);
+ }
+ for (const route of data.routes) {
+ add(
+ route.path === "/" ? "index" : route.path.slice(1),
+ route.title,
+ route.description
+ );
+ }
+ return paths;
+}
+
+// Footer branding shared by every card. The slug comes from the configured
+// repo rather than the URL, so an Enterprise host reads the same as github.com;
+// the site text (host plus deployment base) is resolved at generate time.
+const repoSlug = data.config.github
+ ? `${data.config.github.owner}/${data.config.github.repo}`
+ : undefined;
+
+export async function GET({ props }: { props: CardProps }) {
+ const png = await cachedOgImage(cache, {
+ accent: data.config.og.palette?.accent ?? data.config.theme.accent.light,
+ brand: data.config.title,
+ description: props.description ?? data.config.og.description,
+ fallbacks,
+ families,
+ fonts,
+ logo: data.config.og.logo,
+ palette: data.config.og.palette,
+ repo: repoSlug,
+ site: data.config.og.site,
+ title: props.title,
+ });
+ return new Response(new Uint8Array(png), {
+ headers: {
+ "Cache-Control": "public, max-age=31536000, immutable",
+ "Content-Type": "image/png",
+ },
+ });
+}
diff --git a/apps/docs/src/pages/openapi.json.ts b/apps/docs/src/pages/openapi.json.ts
new file mode 100644
index 0000000..c9ed0cd
--- /dev/null
+++ b/apps/docs/src/pages/openapi.json.ts
@@ -0,0 +1,803 @@
+// Generated by Blume. Do not edit.
+export const prerender = true;
+
+const payload = {
+ "components": {
+ "schemas": {
+ "JsonRpcRequest": {
+ "description": "A JSON-RPC 2.0 request, as the Model Context Protocol sends.",
+ "properties": {
+ "id": {
+ "description": "Request id (absent on notifications)."
+ },
+ "jsonrpc": {
+ "const": "2.0",
+ "type": "string"
+ },
+ "method": {
+ "description": "The MCP method, e.g. `initialize` or `tools/call`.",
+ "type": "string"
+ },
+ "params": {
+ "description": "Method parameters.",
+ "type": "object"
+ }
+ },
+ "required": [
+ "jsonrpc",
+ "method"
+ ],
+ "type": "object"
+ },
+ "JsonRpcResponse": {
+ "description": "A JSON-RPC 2.0 response: a `result` or an `error`.",
+ "properties": {
+ "error": {
+ "properties": {
+ "code": {
+ "type": "integer"
+ },
+ "data": {},
+ "message": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "code",
+ "message"
+ ],
+ "type": "object"
+ },
+ "id": {},
+ "jsonrpc": {
+ "const": "2.0",
+ "type": "string"
+ },
+ "result": {
+ "type": "object"
+ }
+ },
+ "required": [
+ "jsonrpc"
+ ],
+ "type": "object"
+ },
+ "NavLink": {
+ "properties": {
+ "href": {
+ "description": "Link target — an internal route or an external URL.",
+ "type": "string"
+ },
+ "icon": {
+ "description": "Optional icon name.",
+ "type": "string"
+ },
+ "label": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "href",
+ "label"
+ ],
+ "type": "object"
+ },
+ "NavNode": {
+ "description": "A sidebar entry: a page (linking to its route) or a group holding further nodes.",
+ "oneOf": [
+ {
+ "properties": {
+ "badge": {
+ "type": "string"
+ },
+ "deprecated": {
+ "type": "boolean"
+ },
+ "description": {
+ "type": "string"
+ },
+ "icon": {
+ "type": "string"
+ },
+ "kind": {
+ "const": "page",
+ "type": "string"
+ },
+ "label": {
+ "type": "string"
+ },
+ "pageId": {
+ "description": "The page's stable content id.",
+ "type": "string"
+ },
+ "route": {
+ "description": "The page's route.",
+ "type": "string"
+ }
+ },
+ "required": [
+ "kind",
+ "label",
+ "pageId",
+ "route"
+ ],
+ "type": "object"
+ },
+ {
+ "properties": {
+ "badge": {
+ "type": "string"
+ },
+ "children": {
+ "items": {
+ "$ref": "#/components/schemas/NavNode"
+ },
+ "type": "array"
+ },
+ "collapsed": {
+ "type": "boolean"
+ },
+ "icon": {
+ "type": "string"
+ },
+ "kind": {
+ "const": "group",
+ "type": "string"
+ },
+ "label": {
+ "type": "string"
+ },
+ "path": {
+ "description": "The group's route prefix (not necessarily a page).",
+ "type": "string"
+ },
+ "route": {
+ "description": "The group's index page route, when it has one.",
+ "type": "string"
+ }
+ },
+ "required": [
+ "children",
+ "kind",
+ "label"
+ ],
+ "type": "object"
+ }
+ ]
+ },
+ "NavSelector": {
+ "description": "A top-level partition selector (products, versions, languages).",
+ "properties": {
+ "items": {
+ "items": {
+ "properties": {
+ "description": {
+ "type": "string"
+ },
+ "label": {
+ "type": "string"
+ },
+ "path": {
+ "type": "string"
+ },
+ "tag": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "label",
+ "path"
+ ],
+ "type": "object"
+ },
+ "type": "array"
+ },
+ "kind": {
+ "type": "string"
+ },
+ "label": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "items",
+ "kind",
+ "label"
+ ],
+ "type": "object"
+ },
+ "NavTab": {
+ "properties": {
+ "href": {
+ "description": "The clickable target when it differs from `path` (the section's first page).",
+ "type": "string"
+ },
+ "icon": {
+ "type": "string"
+ },
+ "label": {
+ "type": "string"
+ },
+ "path": {
+ "description": "The tab's section prefix.",
+ "type": "string"
+ }
+ },
+ "required": [
+ "label",
+ "path"
+ ],
+ "type": "object"
+ },
+ "Navigation": {
+ "description": "The docs navigation model: header tabs, the sidebar tree, partition selectors, and pinned links.",
+ "properties": {
+ "actions": {
+ "items": {
+ "$ref": "#/components/schemas/NavLink"
+ },
+ "type": "array"
+ },
+ "cta": {
+ "oneOf": [
+ {
+ "$ref": "#/components/schemas/NavLink"
+ },
+ {
+ "type": "null"
+ }
+ ]
+ },
+ "featured": {
+ "items": {
+ "$ref": "#/components/schemas/NavLink"
+ },
+ "type": "array"
+ },
+ "repoUrl": {
+ "type": [
+ "string",
+ "null"
+ ]
+ },
+ "root": {
+ "description": "The tree root's route (`/`, or a locale/version prefix).",
+ "type": "string"
+ },
+ "selectors": {
+ "items": {
+ "$ref": "#/components/schemas/NavSelector"
+ },
+ "type": "array"
+ },
+ "sidebar": {
+ "items": {
+ "$ref": "#/components/schemas/NavNode"
+ },
+ "type": "array"
+ },
+ "tabs": {
+ "items": {
+ "$ref": "#/components/schemas/NavTab"
+ },
+ "type": "array"
+ }
+ },
+ "required": [
+ "featured",
+ "selectors",
+ "sidebar",
+ "tabs"
+ ],
+ "type": "object"
+ },
+ "Page": {
+ "allOf": [
+ {
+ "$ref": "#/components/schemas/PageSummary"
+ },
+ {
+ "properties": {
+ "markdown": {
+ "description": "The page as agent Markdown: frontmatter included, components downleveled to plain Markdown.",
+ "type": "string"
+ }
+ },
+ "required": [
+ "markdown"
+ ],
+ "type": "object"
+ }
+ ],
+ "description": "A page's index entry plus its full Markdown body."
+ },
+ "PageSummary": {
+ "properties": {
+ "contentType": {
+ "description": "The page's content type (frontmatter `type`).",
+ "type": "string"
+ },
+ "description": {
+ "type": "string"
+ },
+ "facets": {
+ "additionalProperties": {
+ "type": "string"
+ },
+ "description": "Facet values the site declares for the page's content type (`content.types..facets`), key → value.",
+ "type": "object"
+ },
+ "json": {
+ "description": "This page's JSON representation (the `getPage` operation).",
+ "format": "uri-reference",
+ "type": "string"
+ },
+ "lastModified": {
+ "description": "ISO 8601 last-modified date, when known.",
+ "type": [
+ "string",
+ "null"
+ ]
+ },
+ "locale": {
+ "description": "The page's locale code.",
+ "type": "string"
+ },
+ "markdownUrl": {
+ "description": "The page's raw-Markdown mirror (the `getPageMarkdown` operation).",
+ "format": "uri-reference",
+ "type": "string"
+ },
+ "route": {
+ "description": "The page's route (`/guides/install`); the key every other operation takes.",
+ "type": "string"
+ },
+ "title": {
+ "type": "string"
+ },
+ "url": {
+ "description": "Where the rendered page is served.",
+ "format": "uri-reference",
+ "type": "string"
+ },
+ "version": {
+ "description": "Docs version the page belongs to on a versioned site: `\"\"` for the current docs, else an archived version id. Absent on unversioned sites.",
+ "type": "string"
+ }
+ },
+ "required": [
+ "contentType",
+ "json",
+ "lastModified",
+ "locale",
+ "markdownUrl",
+ "route",
+ "title",
+ "url"
+ ],
+ "type": "object"
+ },
+ "PagesIndex": {
+ "properties": {
+ "count": {
+ "type": "integer"
+ },
+ "generator": {
+ "description": "The Blume version that built the site.",
+ "type": "string"
+ },
+ "pages": {
+ "items": {
+ "$ref": "#/components/schemas/PageSummary"
+ },
+ "type": "array"
+ },
+ "site": {
+ "type": [
+ "string",
+ "null"
+ ]
+ }
+ },
+ "required": [
+ "count",
+ "generator",
+ "pages",
+ "site"
+ ],
+ "type": "object"
+ },
+ "Problem": {
+ "description": "RFC 9457 problem details, with a stable code and a resolution hint.",
+ "properties": {
+ "code": {
+ "description": "Stable error code (e.g. `PAGE_NOT_FOUND`).",
+ "type": "string"
+ },
+ "detail": {
+ "description": "Human-readable explanation of this occurrence.",
+ "type": "string"
+ },
+ "instance": {
+ "description": "The request path the problem occurred on.",
+ "type": "string"
+ },
+ "links": {
+ "description": "Recovery links, when there is somewhere useful to go.",
+ "items": {
+ "properties": {
+ "href": {
+ "type": "string"
+ },
+ "label": {
+ "type": "string"
+ }
+ },
+ "required": [
+ "href",
+ "label"
+ ],
+ "type": "object"
+ },
+ "type": "array"
+ },
+ "resolution": {
+ "description": "What to do next.",
+ "type": "string"
+ },
+ "status": {
+ "type": "integer"
+ },
+ "title": {
+ "type": "string"
+ },
+ "type": {
+ "description": "Problem type URI; `about:blank` by default.",
+ "type": "string"
+ }
+ },
+ "required": [
+ "code",
+ "detail",
+ "resolution",
+ "status",
+ "title",
+ "type"
+ ],
+ "type": "object"
+ },
+ "SearchHit": {
+ "properties": {
+ "contentType": {
+ "type": "string"
+ },
+ "excerpt": {
+ "description": "The page description, else the start of its content.",
+ "type": "string"
+ },
+ "facets": {
+ "additionalProperties": {
+ "type": "string"
+ },
+ "description": "Facet values the site declares for the page's content type (`content.types..facets`), key → value.",
+ "type": "object"
+ },
+ "route": {
+ "description": "The page's route; pass it to `getPage`.",
+ "type": "string"
+ },
+ "title": {
+ "type": "string"
+ },
+ "url": {
+ "format": "uri-reference",
+ "type": "string"
+ },
+ "version": {
+ "description": "Docs version the page belongs to on a versioned site: `\"\"` for the current docs, else an archived version id. Absent on unversioned sites.",
+ "type": "string"
+ }
+ },
+ "required": [
+ "excerpt",
+ "route",
+ "title",
+ "url"
+ ],
+ "type": "object"
+ },
+ "SearchResponse": {
+ "properties": {
+ "count": {
+ "type": "integer"
+ },
+ "query": {
+ "type": "string"
+ },
+ "results": {
+ "items": {
+ "$ref": "#/components/schemas/SearchHit"
+ },
+ "type": "array"
+ }
+ },
+ "required": [
+ "count",
+ "query",
+ "results"
+ ],
+ "type": "object"
+ }
+ }
+ },
+ "info": {
+ "description": "Read-only JSON API over the prdlk documentation: Documentation for my LeetCode solutions\n\nEvery operation is public and needs no authentication. Errors are RFC 9457 problem details (`application/problem+json`) with a stable `code`, a `detail`, and a `resolution` hint.",
+ "title": "prdlk API",
+ "version": "2.2.4",
+ "x-generator": "blume@2.2.4"
+ },
+ "openapi": "3.1.0",
+ "paths": {
+ "/api/docs/pages.json": {
+ "get": {
+ "description": "Every documentation page with its route, title, description, content type, locale, facets, and the URLs of its rendered, Markdown, and JSON forms. Unfiltered; on a versioned site every version is listed with its `version`. Use it to enumerate the docs or to find a page when search is too narrow.",
+ "operationId": "listPages",
+ "responses": {
+ "200": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/PagesIndex"
+ }
+ }
+ },
+ "description": "The page index."
+ },
+ "default": {
+ "content": {
+ "application/problem+json": {
+ "schema": {
+ "$ref": "#/components/schemas/Problem"
+ }
+ }
+ },
+ "description": "An error, as problem details."
+ }
+ },
+ "summary": "List every page",
+ "tags": [
+ "Pages"
+ ]
+ }
+ },
+ "/api/docs/pages/{route}.json": {
+ "get": {
+ "description": "A single page as JSON: its index entry plus the page's agent Markdown (frontmatter included, components downleveled to plain Markdown). Take `route` from `listPages` or `searchDocs`.",
+ "operationId": "getPage",
+ "parameters": [
+ {
+ "description": "The page route without its leading slash (`guides/install`), or `index` for the home page. May contain slashes.",
+ "in": "path",
+ "name": "route",
+ "required": true,
+ "schema": {
+ "type": "string"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/Page"
+ }
+ }
+ },
+ "description": "The page."
+ },
+ "404": {
+ "content": {
+ "application/problem+json": {
+ "schema": {
+ "$ref": "#/components/schemas/Problem"
+ }
+ }
+ },
+ "description": "No page has that route."
+ },
+ "default": {
+ "content": {
+ "application/problem+json": {
+ "schema": {
+ "$ref": "#/components/schemas/Problem"
+ }
+ }
+ },
+ "description": "An error, as problem details."
+ }
+ },
+ "summary": "Get a page as JSON",
+ "tags": [
+ "Pages"
+ ]
+ }
+ },
+ "/api/docs/navigation.json": {
+ "get": {
+ "description": "The navigation tree (header tabs and the sidebar hierarchy) as readers see it, for the default locale and the current docs.",
+ "operationId": "getNavigation",
+ "responses": {
+ "200": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "$ref": "#/components/schemas/Navigation"
+ }
+ }
+ },
+ "description": "The navigation tree."
+ },
+ "default": {
+ "content": {
+ "application/problem+json": {
+ "schema": {
+ "$ref": "#/components/schemas/Problem"
+ }
+ }
+ },
+ "description": "An error, as problem details."
+ }
+ },
+ "summary": "Get the navigation tree",
+ "tags": [
+ "Navigation"
+ ]
+ }
+ },
+ "/{route}.md": {
+ "get": {
+ "description": "A page's raw-Markdown mirror: append `.md` to any page URL. Components are downleveled to plain Markdown; `.mdx` serves the source as written. The same body the `getPage` operation carries in its `markdown` field.",
+ "operationId": "getPageMarkdown",
+ "parameters": [
+ {
+ "description": "The page route without its leading slash (`guides/install`), or `index` for the home page. May contain slashes.",
+ "in": "path",
+ "name": "route",
+ "required": true,
+ "schema": {
+ "type": "string"
+ }
+ }
+ ],
+ "responses": {
+ "200": {
+ "content": {
+ "text/markdown": {
+ "schema": {
+ "type": "string"
+ }
+ }
+ },
+ "description": "The page as Markdown."
+ },
+ "404": {
+ "content": {
+ "text/markdown": {
+ "schema": {
+ "type": "string"
+ }
+ }
+ },
+ "description": "No page has that route; the body lists where to look next."
+ }
+ },
+ "summary": "Get a page as Markdown",
+ "tags": [
+ "Markdown"
+ ]
+ }
+ },
+ "/llms.txt": {
+ "get": {
+ "description": "The llms.txt index: the site's summary, when to use it, and every page with a one-line description, grouped by section.",
+ "operationId": "getLlmsTxt",
+ "responses": {
+ "200": {
+ "content": {
+ "text/plain": {
+ "schema": {
+ "type": "string"
+ }
+ }
+ },
+ "description": "The index."
+ }
+ },
+ "summary": "Get llms.txt",
+ "tags": [
+ "Markdown"
+ ]
+ }
+ },
+ "/llms-full.txt": {
+ "get": {
+ "description": "The full Markdown of every current-docs page in one file.",
+ "operationId": "getLlmsFullTxt",
+ "responses": {
+ "200": {
+ "content": {
+ "text/plain": {
+ "schema": {
+ "type": "string"
+ }
+ }
+ },
+ "description": "Every page's Markdown."
+ }
+ },
+ "summary": "Get llms-full.txt",
+ "tags": [
+ "Markdown"
+ ]
+ }
+ },
+ "/agent-readability.json": {
+ "get": {
+ "description": "A manifest indexing every agent-facing artifact the site publishes — this API, the Markdown mirrors, llms.txt, the MCP server, feeds, and the sitemap.",
+ "operationId": "getAgentReadability",
+ "responses": {
+ "200": {
+ "content": {
+ "application/json": {
+ "schema": {
+ "type": "object"
+ }
+ }
+ },
+ "description": "The manifest."
+ }
+ },
+ "summary": "Get the agent-readability manifest",
+ "tags": [
+ "Discovery"
+ ]
+ }
+ }
+ },
+ "security": [],
+ "servers": [
+ {
+ "description": "prdlk",
+ "url": "https://lc.prd.lk"
+ }
+ ],
+ "tags": [
+ {
+ "description": "Enumerate and read documentation pages.",
+ "name": "Pages"
+ },
+ {
+ "description": "How the docs are organized.",
+ "name": "Navigation"
+ },
+ {
+ "description": "Plain-text and Markdown surfaces.",
+ "name": "Markdown"
+ },
+ {
+ "description": "Agent discovery documents.",
+ "name": "Discovery"
+ }
+ ],
+ "externalDocs": {
+ "description": "prdlk documentation",
+ "url": "https://lc.prd.lk/"
+ }
+};
+
+export function GET() {
+ return new Response(JSON.stringify(payload), {
+ headers: { "Content-Type": "application/json" },
+ });
+}