From 32dadf59289abbb8f4369bc9e845f78e7a4852ec Mon Sep 17 00:00:00 2001 From: Prad Nukala Date: Tue, 29 Sep 2026 13:22:07 -0400 Subject: [PATCH] docs(docs): add Maximum Subarray problem page and redirect entry --- .../greedy/53-maximum-subarray.mdx | 43 +++++++++++++ .../(05-decision-space)/greedy/index.mdx | 64 +++++++++++++++++++ .../(05-decision-space)/greedy/meta.ts | 3 + apps/docs/redirects.ts | 1 + 4 files changed, 111 insertions(+) create mode 100644 apps/docs/content/(05-decision-space)/greedy/53-maximum-subarray.mdx create mode 100644 apps/docs/content/(05-decision-space)/greedy/meta.ts diff --git a/apps/docs/content/(05-decision-space)/greedy/53-maximum-subarray.mdx b/apps/docs/content/(05-decision-space)/greedy/53-maximum-subarray.mdx new file mode 100644 index 0000000..cb6649a --- /dev/null +++ b/apps/docs/content/(05-decision-space)/greedy/53-maximum-subarray.mdx @@ -0,0 +1,43 @@ +--- +title: '53. Maximum Subarray' +description: Given an integer array nums, find the subarray with the largest sum, and return its sum +icon: dot +topics: + - { name: "Array", slug: "array" } + - { name: "Divide and Conquer", slug: "divide-and-conquer" } + - { name: "Dynamic Programming", slug: "dynamic-programming" } +sidebar: + label: 'Maximum Subarray' + badge: 'Medium' +--- + +::::warning +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 + +```py +class Solution: + def maxSubArray(self, nums: list[int]) -> int: +``` diff --git a/apps/docs/content/(05-decision-space)/greedy/index.mdx b/apps/docs/content/(05-decision-space)/greedy/index.mdx index b70f80b..f616ca1 100644 --- a/apps/docs/content/(05-decision-space)/greedy/index.mdx +++ b/apps/docs/content/(05-decision-space)/greedy/index.mdx @@ -3,3 +3,67 @@ title: Greedy description: 'The best local choice is made at each step and is not changed later.' hidden: true --- + +## 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 + +1. Return the minimum number of swaps required to make s1 equal to s2. +2. Return the minimum number of steps to make s and t anagrams of each other. +3. 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 + + + + - Choices have to be comparable + - must determine if a choice is better than another + + + - 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 + +```python +def greedy(items): + # 1. Sort by the greedy key (deadline, end time, size, ratio, ...) + items.sort(key=greedy_key) + result = 0 + state = initial_state() + for item in items: + # 2. Take the item only if the locally best choice is valid + if is_feasible(item, state): + result += take(item, state) + # 3. Correctness relies on an exchange argument, not just passing tests + return result +``` diff --git a/apps/docs/content/(05-decision-space)/greedy/meta.ts b/apps/docs/content/(05-decision-space)/greedy/meta.ts new file mode 100644 index 0000000..9b97e42 --- /dev/null +++ b/apps/docs/content/(05-decision-space)/greedy/meta.ts @@ -0,0 +1,3 @@ +import { defineMeta } from "blume"; + +export default defineMeta({ title: "Greedy", order: 23, icon: "coins", display: "group" }); diff --git a/apps/docs/redirects.ts b/apps/docs/redirects.ts index fc48264..2963927 100644 --- a/apps/docs/redirects.ts +++ b/apps/docs/redirects.ts @@ -15,6 +15,7 @@ export default [ { from: "/46", to: "/backtracking/permutations" }, { from: "/48", to: "/matrix-index-math/rotate-image" }, { from: "/49", to: "/hash-table/group-anagrams" }, + { from: "/53", to: "/greedy/maximum-subarray" }, { from: "/54", to: "/matrix-index-math/spiral-matrix" }, { from: "/62", to: "/multi-d-grid-dp/unique-paths" }, { from: "/64", to: "/multi-d-grid-dp/minimum-path-sum" },