Skip to content

Commit

Permalink
maximum-subarray/
Browse files Browse the repository at this point in the history
  • Loading branch information
masx200 committed Apr 20, 2022
1 parent bed9145 commit 45f5ac0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/

https://leetcode-cn.com/problems/add-two-numbers/

https://leetcode-cn.com/problems/maximum-subarray/

#### 安装教程

1. 安装`deno`
Expand Down
2 changes: 1 addition & 1 deletion add-two-numbers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ListNode } from "../reverse-linked-list/ListNode.ts";

export default function addTwoNumbers(
l1: ListNode | null,
l2: ListNode | null
l2: ListNode | null,
): ListNode | null {
if (!l1) return l2;
if (!l2) return l1;
Expand Down
10 changes: 10 additions & 0 deletions maximum-subarray/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default function maxSubArray(nums: number[]): number {
let max = nums[0];
const dp = [nums[0]];
for (let i = 1; i < nums.length; i++) {
dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
max = Math.max(max, dp[i]);
}
// const dp=nums.map((v,i,a)=>i===0?nums[0]:Math.max(v,v+(dp[i-1]||0)))
return max;
}

0 comments on commit 45f5ac0

Please sign in to comment.