Skip to content

Commit 70bc41c

Browse files
committed
update: 53
1 parent b2a48d7 commit 70bc41c

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
4040
| 46 | [Permutations](https://leetcode.com/problems/permutations/) | [JavaScript](./src/permutations/res.js) | Medium |
4141
| 48 | [Rotate Image](https://leetcode.com/problems/rotate-image/) | [JavaScript](./src/rotate-image/res.js) | Medium |
4242
| 49 | [Group Anagrams](https://leetcode.com/problems/anagrams/) | [JavaScript](./src/anagrams/res.js) | Medium |
43-
| 53 | [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) | [JavaScript](./src/maximum-subarray/res.js) | Easy |
43+
| 53 | [Maximum Subarray](https://leetcode.com/problems/maximum-subarray/) | [JavaScript](./src/maximum-subarray/res.js) · [TypeScript](./src/maximum-subarray/res.ts) | Easy |
4444
| 54 | [Spiral Matrix](https://leetcode.com/problems/spiral-matrix/) | [JavaScript](./src/spiral-matrix/res.js) | Medium |
4545
| 55 | [Jump Game](https://leetcode.com/problems/jump-game/) <sup>*</sup> | [JavaScript](./src/jump-game/res.js) | Medium |
4646
| 56 | [Merge Intervals](https://leetcode.com/problems/merge-intervals/) | [JavaScript](./src/merge-intervals/res.js) | Medium |

src/maximum-subarray/res.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
function maxSubArray(nums: number[]): number {
2+
if (nums.length === 0) {
3+
return 0;
4+
}
5+
6+
if (nums.length === 1) {
7+
return nums[0];
8+
}
9+
10+
const maxI = [nums[0]];
11+
nums.forEach((num, i) => {
12+
if (!i) {
13+
return ;
14+
}
15+
16+
maxI[i] = Math.max(maxI[i-1]+num, num);
17+
})
18+
19+
return Math.max(...maxI);
20+
};
21+
22+
function Status(lSum: number, rSum: number, mSum: number, iSum: number) {
23+
return {
24+
lSum,
25+
rSum,
26+
mSum,
27+
iSum,
28+
};
29+
}
30+
31+
function getSum(nums: number[], start: number, end: number) {
32+
if (start === end) {
33+
return Status(nums[start], nums[start], nums[start], nums[start]);
34+
}
35+
36+
const mIndex = Math.floor((start + end) / 2);
37+
const leftSum = getSum(nums, start, mIndex);
38+
const rightSum = getSum(nums, mIndex+1, end);
39+
40+
return Status(
41+
Math.max(leftSum.lSum, leftSum.iSum + rightSum.lSum),
42+
Math.max(rightSum.rSum, rightSum.iSum + leftSum.rSum),
43+
Math.max(leftSum.mSum, rightSum.mSum, leftSum.rSum + rightSum.lSum),
44+
leftSum.iSum+rightSum.iSum
45+
)
46+
47+
}
48+
49+
/**
50+
* 线段树
51+
* @param nums
52+
*/
53+
function maxSubArray2(nums: number[]): number {
54+
return getSum(nums, 0, nums.length-1).mSum;
55+
};

src/minimum-depth-of-binary-tree/res.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function minDepth(root: TreeNode | null): number {
1616
if (!root) {
1717
return 0;
1818
}
19-
19+
2020
if (!root.left && !root.right) {
2121
return 1;
2222
}

0 commit comments

Comments
 (0)