Skip to content

Commit

Permalink
feat: leetcode 面试题 16.17
Browse files Browse the repository at this point in the history
  • Loading branch information
WangYang-Rex committed Jun 8, 2023
1 parent 5cf0469 commit 39c268b
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 75 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<p align="center">
<!-- TOPICS COUNT START -->
<img src="https://img.shields.io/badge/-进度:165-green" alt="进度:165">
<img src="https://img.shields.io/badge/-进度:166-green" alt="进度:166">
<!-- TOPICS COUNT END -->
<a href="./assets/docs/TOPICS.md"><img src="https://img.shields.io/badge/-题库目录-blue" alt="题库目录"></a>
<a href="./assets/docs/CATEGORIES.md"><img src="https://img.shields.io/badge/-题库分类-red" alt="题库分类"></a>
Expand Down
6 changes: 6 additions & 0 deletions assets/data/categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,12 @@
"path": "./problemset/the-masseuse-lcci/README.md",
"difficulty": "简单"
},
{
"id": "面试题 16.17",
"title": "连续数列",
"path": "./problemset/contiguous-sequence-lcci/README.md",
"difficulty": "简单"
},
{
"id": "剑指 Offer 47",
"title": "礼物的最大价值",
Expand Down
10 changes: 10 additions & 0 deletions assets/data/topics.json
Original file line number Diff line number Diff line change
Expand Up @@ -1639,6 +1639,16 @@
"url": "https://leetcode.cn/problems/maximum-lcci/",
"path": "./problemset/maximum-lcci/README.md"
},
{
"id": "面试题 16.17",
"title": {
"cn": "连续数列",
"en": "contiguous-sequence-lcci"
},
"difficulty": "简单",
"url": "https://leetcode.cn/problems/contiguous-sequence-lcci/",
"path": "./problemset/contiguous-sequence-lcci/README.md"
},
{
"id": "剑指 Offer 47",
"title": {
Expand Down
1 change: 1 addition & 0 deletions assets/docs/CATEGORIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@
| 剑指 Offer II 088. [爬楼梯的最少成本](../../problemset/gzcjip/README.md) | 简单 |
| 剑指 Offer II 003. [前 n 个数字二进制中 1 的个数](../../problemset/w3tcbm/README.md) | 简单 |
| 面试题 17.16. [按摩师](../../problemset/the-masseuse-lcci/README.md) | 简单 |
| 面试题 16.17. [连续数列](../../problemset/contiguous-sequence-lcci/README.md) | 简单 |
| 剑指 Offer 47. [礼物的最大价值](../../problemset/li-wu-de-zui-da-jie-zhi-lcof/README.md) | 中等 |

## 双指针
Expand Down
2 changes: 2 additions & 0 deletions assets/docs/TOPICS.md
Original file line number Diff line number Diff line change
Expand Up @@ -328,4 +328,6 @@

[面试题 16.07. 最大数值](../../problemset/maximum-lcci/README.md)

[面试题 16.17. 连续数列](../../problemset/contiguous-sequence-lcci/README.md)

[剑指 Offer 47. 礼物的最大价值](../../problemset/li-wu-de-zui-da-jie-zhi-lcof/README.md)
16 changes: 16 additions & 0 deletions problemset/contiguous-sequence-lcci/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 连续数列

> 难度:简单
>
> https://leetcode.cn/problems/contiguous-sequence-lcci/
## 题目

给定一个整数数组,找出总和最大的连续数列,并返回总和。

### 示例:
```
输入: [-2,1,-3,4,-1,2,1,-5,4]
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
```
17 changes: 17 additions & 0 deletions problemset/contiguous-sequence-lcci/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, expect, it } from 'vitest'
import { maxSubArray } from '.'

describe('连续数列', () => {
describe('', () => {
testCase(maxSubArray)
})
})

function testCase(fn: (nums: number[]) => number) {
it.each([
// test cases
[[-2, 1, -3, 4, -1, 2, 1, -5, 4], 6],
])('示例%#', (nums, expected) => {
expect(fn(nums)).toBe(expected)
})
}
9 changes: 9 additions & 0 deletions problemset/contiguous-sequence-lcci/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function maxSubArray(nums: number[]): number {
// [-2, 1, -3, 4, -1, 2, 1, -5, 4]
let pre = 0; let maxAns = nums[0];
nums.forEach((x) => {
pre = Math.max(pre + x, x);
maxAns = Math.max(maxAns, pre);
});
return maxAns;
}
45 changes: 0 additions & 45 deletions problemset/qing-wa-tiao-tai-jie-wen-ti-lcof/README.md

This file was deleted.

19 changes: 0 additions & 19 deletions problemset/qing-wa-tiao-tai-jie-wen-ti-lcof/index.spec.ts

This file was deleted.

10 changes: 0 additions & 10 deletions problemset/qing-wa-tiao-tai-jie-wen-ti-lcof/index.ts

This file was deleted.

0 comments on commit 39c268b

Please sign in to comment.