Skip to content

Commit

Permalink
finish algorithm presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
wujunwei committed Jan 10, 2021
1 parent cebd5ad commit 3146e26
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
64 changes: 63 additions & 1 deletion content/game/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ outputs = ["Reveal"]
margin = 0.02
transition = "slide"
transition_speed = "slow"
theme="league"
theme="beige"
+++

<h3 class="fragment fade-up">我们来玩个游戏</h3>
Expand All @@ -16,6 +16,68 @@ theme="league"

---


A和B用几堆石子在做游戏。 游戏以谁手中的石子最多来决出胜负。石子的总数是奇数,所以没有平局。
A和B轮流进行,A先开始。 每回合,玩家从行的开始或结束处取走整堆石头。 直到取完为止,此时手中石子最多的玩家获胜。
假设A和B都发挥出最佳水平,当A赢得比赛时返回true,否则返回false。

```
输入:[5,3,4,5]
输出:true
解释:
A先开始,只能拿前 5 颗或后 5 颗石子 。
假设他取了前 5 颗,这一行就变成了 [3,4,5] 。
如果B拿走前 3 颗,那么剩下的是 [4,5],A拿走后 5 颗赢得 10 分。
如果B拿走后 5 颗,那么剩下的是 [3,4],A拿走后 4 颗赢得 9 分。
这表明,取前 5 颗石子对A来说是一个胜利的举动,所以我们返回 true 。
```
---

### answer 1

$$
dp[i][j]=max(piles[i]−dp[i+1][j],piles[j]−dp[i][j−1])
$$

```go
package main
func stoneGame(piles []int) bool {
length := len(piles)
dp := make([][]int, length)
for i := 0; i < length; i++ {
dp[i] = make([]int, length)
dp[i][i] = piles[i]
}
for i := length - 2; i >= 0; i-- {
for j := i + 1; j < length; j++ {
dp[i][j] = max(piles[i] - dp[i+1][j], piles[j] - dp[i][j-1])
}
}
return dp[0][length-1] > 0
}

func max(x, y int) int {
if x > y {
return x
}
return y
}
```

---

### answer 2

![](/images/stone.png)

---

```go
package main
func stoneGame(piles []int) bool {
return true
}
```
#### [返回](/#/7)


8 changes: 7 additions & 1 deletion content/others/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ func majorityElement(nums []int) int {
---

### answer 1
暴力(Brute Force)

暴力(Brute Force)最直接的方法 - 沿着字符换逐步移动滑动窗口,将窗口内的子串与 needle 字符串比较。
![](/images/bf.png)

---

```go
package main
func strStr(haystack string, needle string) int {
Expand Down Expand Up @@ -228,6 +233,7 @@ func strStr(haystack string, needle string) int {

### KMP (Boyer-Moore、Sunday)

![](/images/substrings.png)

---

Expand Down
Binary file added static/images/bf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/images/stone.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/images/substrings.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 3146e26

Please sign in to comment.