From f4a6c33ccabd631f03b760aff576ee8c9bd2d2bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=B4=8B?= Date: Mon, 29 Jul 2024 19:19:44 +0800 Subject: [PATCH] feat: leetcode 682 --- README.md | 2 +- assets/data/categories.json | 12 ++++++ assets/data/topics.json | 10 +++++ assets/docs/CATEGORIES.md | 2 + assets/docs/TOPICS.md | 2 + problemset/baseball-game/README.md | 55 ++++++++++++++++++++++++++ problemset/baseball-game/index.py | 15 +++++++ problemset/baseball-game/index.spec.ts | 17 ++++++++ problemset/baseball-game/index.ts | 26 ++++++++++++ 9 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 problemset/baseball-game/README.md create mode 100644 problemset/baseball-game/index.py create mode 100644 problemset/baseball-game/index.spec.ts create mode 100644 problemset/baseball-game/index.ts diff --git a/README.md b/README.md index 4af871f..410cdbf 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@

-进度:182 +进度:183 题库目录 题库分类 diff --git a/assets/data/categories.json b/assets/data/categories.json index 72be66c..dad7013 100644 --- a/assets/data/categories.json +++ b/assets/data/categories.json @@ -294,6 +294,12 @@ "path": "./problemset/move-zeroes/README.md", "difficulty": "简单" }, + { + "id": "682", + "title": "棒球比赛", + "path": "./problemset/baseball-game/README.md", + "difficulty": "简单" + }, { "id": "1144", "title": "递减元素使数组呈锯齿状", @@ -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 整除的最小整数", diff --git a/assets/data/topics.json b/assets/data/topics.json index b85db6f..5097a43 100644 --- a/assets/data/topics.json +++ b/assets/data/topics.json @@ -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": { diff --git a/assets/docs/CATEGORIES.md b/assets/docs/CATEGORIES.md index 6e4be61..1dd222f 100644 --- a/assets/docs/CATEGORIES.md +++ b/assets/docs/CATEGORIES.md @@ -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) | 简单 | @@ -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) | 中等 | diff --git a/assets/docs/TOPICS.md b/assets/docs/TOPICS.md index 52e0e9b..793b8f7 100644 --- a/assets/docs/TOPICS.md +++ b/assets/docs/TOPICS.md @@ -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) diff --git a/problemset/baseball-game/README.md b/problemset/baseball-game/README.md new file mode 100644 index 0000000..404bdda --- /dev/null +++ b/problemset/baseball-game/README.md @@ -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 +``` \ No newline at end of file diff --git a/problemset/baseball-game/index.py b/problemset/baseball-game/index.py new file mode 100644 index 0000000..c6cc25f --- /dev/null +++ b/problemset/baseball-game/index.py @@ -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) \ No newline at end of file diff --git a/problemset/baseball-game/index.spec.ts b/problemset/baseball-game/index.spec.ts new file mode 100644 index 0000000..65ef411 --- /dev/null +++ b/problemset/baseball-game/index.spec.ts @@ -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) + }) +} diff --git a/problemset/baseball-game/index.ts b/problemset/baseball-game/index.ts new file mode 100644 index 0000000..49aee2d --- /dev/null +++ b/problemset/baseball-game/index.ts @@ -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); +}