Skip to content

fix: update solutions to lc problem: No.53 #3740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions solution/0000-0099/0053.Maximum Subarray/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,23 @@ tags:

### 方法一:动态规划

我们定义 $f[i]$ 表示以元素 $nums[i]$ 为结尾的连续子数组的最大和,初始时 $f[0] = nums[0]$,那么最终我们要求的答案即为 $\max_{0 \leq i < n} f[i]$。
我们定义 $f[i]$ 表示以元素 $\textit{nums}[i]$ 为结尾的连续子数组的最大和,初始时 $f[0] = \textit{nums}[0]$,那么最终我们要求的答案即为 $\max_{0 \leq i < n} f[i]$。

考虑 $f[i]$,其中 $i \geq 1$,它的状态转移方程为:

$$
f[i] = \max \{ f[i - 1] + nums[i], nums[i] \}
f[i] = \max(f[i - 1] + \textit{nums}[i], \textit{nums}[i])
$$

也即:

$$
f[i] = \max \{ f[i - 1], 0 \} + nums[i]
f[i] = \max(f[i - 1], 0) + \textit{nums}[i]
$$

由于 $f[i]$ 只与 $f[i - 1]$ 有关系,因此我们可以只用一个变量 $f$ 来维护对于当前 $f[i]$ 的值是多少,然后进行状态转移即可。答案为 $\max_{0 \leq i < n} f$。

时间复杂度 $O(n)$,其中 $n$ 为数组 $nums$ 的长度。我们只需要遍历一遍数组即可求得答案。空间复杂度 $O(1)$,我们只需要常数空间存放若干变量
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down
14 changes: 7 additions & 7 deletions solution/0000-0099/0053.Maximum Subarray/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,23 @@ tags:

### Solution 1: Dynamic Programming

We define $f[i]$ to represent the maximum sum of the continuous subarray ending with the element $nums[i]$. Initially, $f[0] = nums[0]$. The final answer we are looking for is $\max_{0 \leq i < n} f[i]$.
We define $f[i]$ to represent the maximum sum of a contiguous subarray ending at element $\textit{nums}[i]$. Initially, $f[0] = \textit{nums}[0]$. The final answer we seek is $\max_{0 \leq i < n} f[i]$.

Consider $f[i]$, where $i \geq 1$, its state transition equation is:
Consider $f[i]$ for $i \geq 1$. Its state transition equation is:

$$
f[i] = \max \{ f[i - 1] + nums[i], nums[i] \}
f[i] = \max(f[i - 1] + \textit{nums}[i], \textit{nums}[i])
$$

Which is also:
That is:

$$
f[i] = \max \{ f[i - 1], 0 \} + nums[i]
f[i] = \max(f[i - 1], 0) + \textit{nums}[i]
$$

Since $f[i]$ is only related to $f[i - 1]$, we can use a single variable $f$ to maintain the current value of $f[i]$, and then perform state transition. The answer is $\max_{0 \leq i < n} f$.
Since $f[i]$ is only related to $f[i - 1]$, we can use a single variable $f$ to maintain the current value of $f[i]$ and perform the state transition. The answer is $\max_{0 \leq i < n} f$.

The time complexity is $O(n)$, where $n$ is the length of the array $nums$. We only need to traverse the array once to get the answer. The space complexity is $O(1)$, we only need constant space to store several variables.
The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down
Loading