Skip to content

Commit 2e04ca9

Browse files
committed
0053-maximum-subarray.md Reworded.
1 parent 476cb1d commit 2e04ca9

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

0053-maximum-subarray.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# 53. Maximum Subarray
2-
LeetCode problem: [53. Maximum Subarray](https://leetcode.com/problems/maximum-subarray/)
2+
LeetCode problem: [53. Maximum Subarray](https://leetcode.com/problems/maximum-subarray/){:target="_blank"}
33

44
## Problem
55
> Given an integer array nums, find the subarray with the largest sum, and return its sum.
@@ -12,19 +12,21 @@ Explanation: The subarray [4,-1,2,1] has the largest sum 6.
1212
```
1313

1414
## Thoughts
15+
* This problem can be solved by using `greedy algorithm`, but here we will use another way.
1516
* Imagine the size of nums is `i`, let us consider if the same question is applied to the `subarray` of `nums` from index `0` to `i - 1`.
1617
* The answer is `yes`. Then let us think if the `i - 1`'s answer could impact the answer of `i`.
1718
* The answer is still `yes`. What would be the impact?
18-
* For index `i`,
19+
* For `nums[i]`,
1920
if the `previous sum` is negative, we can discard it;
2021
if the `previous sum` is positive, we can add it to the `current sum`.
21-
* So we can use dynamic programming to solve the problem, but we should use the `current sum` instead of the `largest sum` in the `dp` array because `largest sum` is recorded in the `dp` array.
22+
* So we can use dynamic programming to solve the problem.
2223

2324
### Common steps in dynamic programming
2425
These five steps are a pattern for solving dynamic programming problems.
2526

2627
1. Define the `dp` array
2728
* `dp[i]` represents the `current sum` at index `i`.
29+
* We should not define `dp[i]` as the `largest sum` because `largest sum` is recorded in the `current sum` array.
2830
2. Determine the `dp` array's recurrence formula
2931
* `dp[i] = max(nums[i], dp[i - 1] + nums[i])`.
3032
3. Determine the `dp` array's initial value

0 commit comments

Comments
 (0)