mirror of
https://github.com/prdlk/leetcode.git
synced 2026-11-02 13:12:44 +00:00
docs(docs): add Maximum Subarray problem page and redirect entry
This commit is contained in:
1 parent
5b6a33088c
commit
32dadf5928
4 files changed
+111
No files matched your search
@@ -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:
|
||||
```
|
||||
@@ -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
|
||||
|
||||
<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
|
||||
|
||||
```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: "group" });
|
||||
@@ -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" },
|
||||
|
||||
Reference in new issue
Block a user