Skip to content

Commit

Permalink
Merge pull request #633 from 0xff-dev/799
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 799
  • Loading branch information
6boris authored Sep 24, 2023
2 parents 99121c9 + 0f9e796 commit cc525f4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 26 deletions.
36 changes: 23 additions & 13 deletions leetcode/701-800/0799.Champagne-Tower/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
# [799.Champagne Tower][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
We stack glasses in a pyramid, where the **first** row has 1 glass, the **second** row has `2` glasses, and so on until the 100<sup>th</sup> row. Each glass holds one cup of champagne.

Then, some champagne is poured into the first glass at the top. When the topmost glass is full, any excess liquid poured will fall equally to the glass immediately to the left and right of it. When those glasses become full, any excess champagne will fall equally to the left and right of those glasses, and so on. (A glass at the bottom row has its excess champagne fall on the floor.)

For example, after one cup of champagne is poured, the top most glass is full. After two cups of champagne are poured, the two glasses on the second row are half full. After three cups of champagne are poured, those two cups become full - there are 3 full glasses total now. After four cups of champagne are poured, the third row has the middle glass half full, and the two outside glasses are a quarter full, as pictured below.

![x](./tower.png)

Now after pouring some non-negative integer cups of champagne, return how full the j<sup>th</sup> glass in the i<sup>th</sup> row is (both `i` and `j` are 0-indexed.)

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: poured = 1, query_row = 1, query_glass = 1
Output: 0.00000
Explanation: We poured 1 cup of champange to the top glass of the tower (which is indexed as (0, 0)). There will be no excess liquid so all the glasses under the top glass will remain empty.
```

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

## 题解

### 思路1
> ...
Champagne Tower
```go
```
Input: poured = 2, query_row = 1, query_glass = 1
Output: 0.50000
Explanation: We poured 2 cups of champange to the top glass of the tower (which is indexed as (0, 0)). There is one cup of excess liquid. The glass indexed as (1, 0) and the glass indexed as (1, 1) will share the excess liquid equally, and each will get half cup of champange.
```

**Example 3:**

```
Input: poured = 100000009, query_row = 33, query_glass = 17
Output: 1.00000
```

## 结语

Expand Down
21 changes: 19 additions & 2 deletions leetcode/701-800/0799.Champagne-Tower/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(poured int, query_row int, query_glass int) float64 {
tower := make([][]float64, 101)
for i := 0; i < 101; i++ {
tower[i] = make([]float64, 101)
}
tower[0][0] = float64(poured)
for row := 1; row <= query_row; row++ {
for col := 0; col < row; col++ {
if tower[row-1][col] > 1.0 {
left := (tower[row-1][col] - 1.0) / 2.0
tower[row][col] += left
tower[row][col+1] += left
}
}
}
if tower[query_row][query_glass] > 1.0 {
return 1.0
}
return tower[query_row][query_glass]
}
23 changes: 12 additions & 11 deletions leetcode/701-800/0799.Champagne-Tower/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,32 @@ import (
func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
name string
q, qr, qg int
expect float64
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", 1, 1, 1, 0},
{"TestCase2", 2, 1, 1, 0.5},
{"TestCase3", 4, 2, 2, 0.25},
{"TestCase4", 100000009, 33, 17, 1.0},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.q, c.qr, c.qg)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v %v",
c.expect, got, c.q, c.qr, c.qg)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Binary file added leetcode/701-800/0799.Champagne-Tower/tower.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cc525f4

Please sign in to comment.