Skip to content

Commit

Permalink
feat: leetcode 682
Browse files Browse the repository at this point in the history
  • Loading branch information
王洋 committed Jul 29, 2024
1 parent adbe5df commit f4a6c33
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 1 deletion.
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/-进度:182-green" alt="进度:182">
<img src="https://img.shields.io/badge/-进度:183-green" alt="进度:183">
<!-- 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
12 changes: 12 additions & 0 deletions assets/data/categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,12 @@
"path": "./problemset/move-zeroes/README.md",
"difficulty": "简单"
},
{
"id": "682",
"title": "棒球比赛",
"path": "./problemset/baseball-game/README.md",
"difficulty": "简单"
},
{
"id": "1144",
"title": "递减元素使数组呈锯齿状",
Expand Down Expand Up @@ -1311,6 +1317,12 @@
"path": "./problemset/number-complement/README.md",
"difficulty": "简单"
},
{
"id": "682",
"title": "棒球比赛",
"path": "./problemset/baseball-game/README.md",
"difficulty": "简单"
},
{
"id": "1015",
"title": "可被 K 整除的最小整数",
Expand Down
10 changes: 10 additions & 0 deletions assets/data/topics.json
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,16 @@
"url": "https://leetcode.cn/problems/valid-palindrome-ii/",
"path": "./problemset/valid-palindrome-ii/README.md"
},
{
"id": "682",
"title": {
"cn": "棒球比赛",
"en": "baseball-game"
},
"difficulty": "简单",
"url": "https://leetcode.cn/problems/baseball-game/description/?envType=daily-question&envId=2024-07-29",
"path": "./problemset/baseball-game/README.md"
},
{
"id": "746",
"title": {
Expand Down
2 changes: 2 additions & 0 deletions assets/docs/CATEGORIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
| 167. [两数之和 II - 输入有序数组](../../problemset/two-sum-ii-input-array-is-sorted/README.md) | 中等 |
| 215. [数组中的第K个最大元素](../../problemset/kth-largest-element-in-an-array/README.md) | 中等 |
| 283. [移动零](../../problemset/move-zeroes/README.md) | 简单 |
| 682. [棒球比赛](../../problemset/baseball-game/README.md) | 简单 |
| 1144. [递减元素使数组呈锯齿状](../../problemset/decrease-elements-to-make-array-zigzag/README.md) | 中等 |
| 1487. [保证文件名唯一](../../problemset/making-file-names-unique/README.md) | 中等 |
| 2341. [数组能形成多少数对](../../problemset/maximum-number-of-pairs-in-array/README.md) | 简单 |
Expand Down Expand Up @@ -268,6 +269,7 @@
| 447. [回旋镖的数量](../../problemset/number-of-boomerangs/README.md) | 中等 |
| 470. [用 Rand7() 实现 Rand10()](../../problemset/implement-rand10-using-rand7/README.md) | 中等 |
| 476. [数字的补数](../../problemset/number-complement/README.md) | 简单 |
| 682. [棒球比赛](../../problemset/baseball-game/README.md) | 简单 |
| 1015. [可被 K 整除的最小整数](../../problemset/smallest-integer-divisible-by-k/README.md) | 中等 |
| 1016. [子串能表示从 1 到 N 数字的二进制串](../../problemset/binary-string-with-substrings-representing-1-to-n/README.md) | 中等 |
| 1072. [按列翻转得到最大值等行数](../../problemset/flip-columns-for-maximum-number-of-equal-rows/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 @@ -258,6 +258,8 @@

[680. 验证回文串 II](../../problemset/valid-palindrome-ii/README.md)

[682. 棒球比赛](../../problemset/baseball-game/README.md)

[746. 使用最小花费爬楼梯](../../problemset/min-cost-climbing-stairs/README.md)

[877. 石子游戏](../../problemset/stone-game/README.md)
Expand Down
55 changes: 55 additions & 0 deletions problemset/baseball-game/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# 棒球比赛

> 难度:简单
>
> https://leetcode.cn/problems/baseball-game/description/?envType=daily-question&envId=2024-07-29
## 题目

你现在是一场采用特殊赛制棒球比赛的记录员。这场比赛由若干回合组成,过去几回合的得分可能会影响以后几回合的得分。

比赛开始时,记录是空白的。你会得到一个记录操作的字符串列表 `ops`,其中 `ops[i]` 是你需要记录的第 `i` 项操作,`ops` 遵循下述规则:

- 整数 `x` - 表示本回合新获得分数 `x`
- `"+"` - 表示本回合新获得的得分是前两次得分的总和。题目数据保证记录此操作时前面总是存在两个有效的分数。
- `"D"` - 表示本回合新获得的得分是前一次得分的两倍。题目数据保证记录此操作时前面总是存在一个有效的分数。
- `"C"` - 表示前一次得分无效,将其从记录中移除。题目数据保证记录此操作时前面总是存在一个有效的分数。

请你返回记录中所有得分的总和。



### 示例 1:
```
输入:ops = ["5","2","C","D","+"]
输出:30
解释:
"5" - 记录加 5 ,记录现在是 [5]
"2" - 记录加 2 ,记录现在是 [5, 2]
"C" - 使前一次得分的记录无效并将其移除,记录现在是 [5].
"D" - 记录加 2 * 5 = 10 ,记录现在是 [5, 10].
"+" - 记录加 5 + 10 = 15 ,记录现在是 [5, 10, 15].
所有得分的总和 5 + 10 + 15 = 30
```

### 示例 2:
```
输入:ops = ["5","-2","4","C","D","9","+","+"]
输出:27
解释:
"5" - 记录加 5 ,记录现在是 [5]
"-2" - 记录加 -2 ,记录现在是 [5, -2]
"4" - 记录加 4 ,记录现在是 [5, -2, 4]
"C" - 使前一次得分的记录无效并将其移除,记录现在是 [5, -2]
"D" - 记录加 2 * -2 = -4 ,记录现在是 [5, -2, -4]
"9" - 记录加 9 ,记录现在是 [5, -2, -4, 9]
"+" - 记录加 -4 + 9 = 5 ,记录现在是 [5, -2, -4, 9, 5]
"+" - 记录加 9 + 5 = 14 ,记录现在是 [5, -2, -4, 9, 5, 14]
所有得分的总和 5 + -2 + -4 + 9 + 5 + 14 = 27
```

### 示例 3:
```
输入:ops = ["1"]
输出:1
```
15 changes: 15 additions & 0 deletions problemset/baseball-game/index.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from typing import List

class Solution:
def calPoints(self, operations: List[str]) -> int:
stack = []
for op in operations:
if op == 'C':
stack.pop()
elif op == 'D':
stack.append(stack[-1] * 2)
elif op == '+':
stack.append(stack[-1] + stack[-2])
else:
stack.append(int(op))
return sum(stack)
17 changes: 17 additions & 0 deletions problemset/baseball-game/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { describe, expect, it } from 'vitest'
import { calPoints } from '.'

describe('棒球比赛', () => {
testCase(calPoints)
})

function testCase(fn: (operations: string[]) => number) {
it.each([
// test cases
[['5', '2', 'C', 'D', '+'], 30],
[['5', '-2', '4', 'C', 'D', '9', '+', '+'], 27],
[['1'], 1],
])('示例%#', (operations, expected) => {
expect(fn(operations)).toBe(expected)
})
}
26 changes: 26 additions & 0 deletions problemset/baseball-game/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export function calPoints(operations: string[]): number {
if (operations.length === 0) {
return 0;
}
const arr: number[] = [];
let res = 0;
while (operations.length > 0) {
const x = operations.shift() as string;
if (x === '+') {
arr.push(arr[arr.length - 1] + arr[arr.length - 2])
res += arr[arr.length - 1];
} else if (x === 'D') {
arr.push(arr[arr.length - 1] * 2)
res += arr[arr.length - 1];
} else if (x === 'C') {
res -= arr[arr.length - 1];
arr.pop();
} else {
arr.push(parseInt(x))
res += arr[arr.length - 1];
}
}

return res;
// return arr.reduce((prev, curr) => prev + curr, 0);
}

0 comments on commit f4a6c33

Please sign in to comment.