feat: refactored docs structure

This commit is contained in:
Prad Nukala committed 2026-10-05 14:22:28 -04:00
1 parent 31a1fcfdbd
commit 6247915ae5
222 files changed
+6452 -729

No files matched your search

@@ -0,0 +1,48 @@
---
title: '134. Gas Station'
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
topics:
- { name: "Array", slug: "array" }
- { name: "Greedy", slug: "greedy" }
issue: "https://github.com/prdlk/leetcode/issues/176"
sidebar:
label: 'Gas Station'
badge: 'Medium'
---
### 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
```py
class Solution:
def canCompleteCircuit(self, gas: list[int], cost: list[int]) -> int:
start, tank, total = 0, 0, 0
for i in range(len(gas)):
drive = gas[i] - cost[i]
tank = tank + drive
if tank < 0:
tank = 0
start = i + 1
total = total + drive
return start if total >= 0 else -1
```
@@ -0,0 +1,54 @@
---
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" }
issue: "https://github.com/prdlk/leetcode/issues/174"
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:
best, total = nums[0], nums[0]
for i in range(1, len(nums)):
if total < 0:
total = nums[i]
else:
total = total + nums[i]
best = max(best, total)
return best
```
@@ -0,0 +1,45 @@
---
title: '55. Jump Game'
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
topics:
- { name: "Array", slug: "array" }
- { name: "Dynamic Programming", slug: "dynamic-programming" }
- { name: "Greedy", slug: "greedy" }
issue: "https://github.com/prdlk/leetcode/issues/175"
sidebar:
label: 'Jump Game'
badge: 'Medium'
---
### 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
```py
class Solution:
def canJump(self, nums: list[int]) -> bool:
best, last = nums[0], len(nums) - 1
for i in range(last):
if i > best:
return False
else:
pos = nums[i] + i
best = max(best, pos)
return best >= last
```
@@ -0,0 +1,93 @@
---
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
<Steps>
<Step title="Identify the different steps and choices">
- Choices have to be comparable
- must determine if a choice is better than another
</Step>
<Step title="Check if greedy works">
- try finding small cases where greedy is not optimal
- if cases pass, try to deduce why greedy is optimal
</Step>
</Steps>
## 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
:::info
No sort. Walk the list once. Carry one number. At each step, decide: keep it, or drop it.
:::
```python
def walk_and_carry(nums):
carry = start_value
best = start_value
for n in nums:
if carry_is_useless(carry):
carry = fresh_start
carry = update(carry, n)
best = max(best, carry)
return best
```
### Type 2: Sort and Pick
:::info
Used when order doesn't matter, so you pick the order. Meeting rooms, intervals, "most tasks by a deadline."
:::
```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
```
@@ -0,0 +1,3 @@
import { defineMeta } from "blume";
export default defineMeta({ title: "Greedy", order: 23, icon: "coins", display: "page" });