Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add solution and test-cases for problem 682
  • Loading branch information
0xff-dev committed Apr 10, 2022
commit bc5e712d19e017be5aec22f0baa61df36b26f640
53 changes: 40 additions & 13 deletions leetcode/601-700/0682.Baseball-Game/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,55 @@
# [682.Baseball Game][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You are keeping score for a baseball game with strange rules. The game consists of several rounds, where the scores of past rounds may affect future rounds' scores.

At the beginning of the game, you start with an empty record. You are given a list of strings `ops`, where `ops[i]` is the i<sup>th</sup> operation you must apply to the record and is one of the following:

1. An integer `x` - Record a new score of `x`.
2. `"+"` - Record a new score that is the sum of the previous two scores. It is guaranteed there will always be two previous scores.
3. `"D"` - Record a new score that is double the previous score. It is guaranteed there will always be a previous score.
4. `"C"` - Invalidate the previous score, removing it from the record. It is guaranteed there will always be a previous score.

Return the sum of all the scores on the record.


**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: ops = ["5","2","C","D","+"]
Output: 30
Explanation:
"5" - Add 5 to the record, record is now [5].
"2" - Add 2 to the record, record is now [5, 2].
"C" - Invalidate and remove the previous score, record is now [5].
"D" - Add 2 * 5 = 10 to the record, record is now [5, 10].
"+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15].
The total sum is 5 + 10 + 15 = 30.
```

## 题意
> ...
**Example 2:**

## 题解

### 思路1
> ...
Baseball Game
```go
```
Input: ops = ["5","-2","4","C","D","9","+","+"]
Output: 27
Explanation:
"5" - Add 5 to the record, record is now [5].
"-2" - Add -2 to the record, record is now [5, -2].
"4" - Add 4 to the record, record is now [5, -2, 4].
"C" - Invalidate and remove the previous score, record is now [5, -2].
"D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4].
"9" - Add 9 to the record, record is now [5, -2, -4, 9].
"+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5].
"+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14].
The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
```

**Example 3:**

```
Input: ops = ["1"]
Output: 1
```

## 结语

Expand Down
31 changes: 29 additions & 2 deletions leetcode/601-700/0682.Baseball-Game/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
package Solution

func Solution(x bool) bool {
return x
import "strconv"

func Solution(ops []string) int {
ans := 0

points := make([]int, len(ops))
idx := 0
for i := 0; i < len(ops); i, idx = i+1, idx+1 {
op := ops[i]
if op == "C" {
idx -= 2
continue
}
if op == "D" {
points[idx] = points[idx-1] * 2
continue
}

if op == "+" {
points[idx] = points[idx-1] + points[idx-2]
continue
}
_t, _ := strconv.Atoi(op)
points[idx] = _t
}
for i := 0; i < idx; i++ {
ans += points[i]
}
return ans
}
10 changes: 5 additions & 5 deletions leetcode/601-700/0682.Baseball-Game/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs []string
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []string{"5", "2", "C", "D", "+"}, 30},
{"TestCase2", []string{"5", "-2", "4", "C", "D", "9", "+", "+"}, 27},
{"TestCase3", []string{"1"}, 1},
}

// 开始测试
Expand Down