diff --git a/solution/0000-0099/0053.Maximum Subarray/README.md b/solution/0000-0099/0053.Maximum Subarray/README.md index a2cdc37fc575c..68072c67bcf7f 100644 --- a/solution/0000-0099/0053.Maximum Subarray/README.md +++ b/solution/0000-0099/0053.Maximum Subarray/README.md @@ -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)$。 diff --git a/solution/0000-0099/0053.Maximum Subarray/README_EN.md b/solution/0000-0099/0053.Maximum Subarray/README_EN.md index 81e71ba9450e3..2e184cf82c3cd 100644 --- a/solution/0000-0099/0053.Maximum Subarray/README_EN.md +++ b/solution/0000-0099/0053.Maximum Subarray/README_EN.md @@ -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)$.