|
| 1 | +## 2019 力扣杯全国秋季编程大赛 |
| 2 | + |
| 3 | +[点击前往](https://leetcode-cn.com/contest/season/2019-fall/) |
| 4 | + |
| 5 | +### 1. 猜数字 |
| 6 | + |
| 7 | +[原题链接](https://leetcode-cn.com/contest/season/2019-fall/problems/guess-numbers/) |
| 8 | + |
| 9 | +#### 思路 |
| 10 | + |
| 11 | +```python |
| 12 | +class Solution: |
| 13 | + def game(self, guess: List[int], answer: List[int]) -> int: |
| 14 | + res = 0 |
| 15 | + for i in range(3): |
| 16 | + if guess[i] == answer[i]: |
| 17 | + res += 1 |
| 18 | + return res |
| 19 | +``` |
| 20 | + |
| 21 | +### 2. 分式化简 |
| 22 | + |
| 23 | +[原题链接](https://leetcode-cn.com/contest/season/2019-fall/problems/deep-dark-fraction/) |
| 24 | + |
| 25 | +#### 思路 |
| 26 | + |
| 27 | +是一个不断“取反、相加”的循环。 |
| 28 | + |
| 29 | +```python |
| 30 | +class Solution: |
| 31 | + def fraction(self, cont: List[int]) -> List[int]: |
| 32 | + length = len(cont) |
| 33 | + res = [cont[length - 1], 1] |
| 34 | + for i in range(length - 2, -1, -1): |
| 35 | + # 取反 |
| 36 | + cur = cont[i] |
| 37 | + left = cur * res[0] |
| 38 | + res = [res[1] + left, res[0]] |
| 39 | + |
| 40 | + return res |
| 41 | +``` |
| 42 | + |
| 43 | +### 3. 机器人大冒险 |
| 44 | + |
| 45 | +[原题链接](https://leetcode-cn.com/contest/season/2019-fall/problems/programmable-robot/) |
| 46 | + |
| 47 | +#### 思路 |
| 48 | + |
| 49 | +先记录一波数据: |
| 50 | + |
| 51 | +1. 把执行一次命令能走到的点都记录下来,记为 `path` |
| 52 | +2. 把命令中 `R` 的次数记作 `px`,`U` 的次数记作 `py` |
| 53 | + |
| 54 | +那么拿到一个点 `(x, y)` 时,求出需要循环执行命令的次数: |
| 55 | + |
| 56 | +``` |
| 57 | +r = min(x // px, y // py) |
| 58 | +``` |
| 59 | + |
| 60 | +循环 `r` 次命令后剩余的步数为:`(x - r * px, y - r * py)`。若 `(x - r * px, y - r * py)` 在 `path` 中,则说明可以走到点 `(x, y)`。 |
| 61 | + |
| 62 | +```python |
| 63 | +class Solution: |
| 64 | + def robot(self, command: str, obstacles: List[List[int]], x: int, y: int) -> bool: |
| 65 | + path = set() |
| 66 | + path.add((0, 0)) |
| 67 | + |
| 68 | + px = 0 |
| 69 | + py = 0 |
| 70 | + for c in command: |
| 71 | + if c == "R": |
| 72 | + px += 1 |
| 73 | + else: |
| 74 | + py += 1 |
| 75 | + path.add((px, py)) |
| 76 | + |
| 77 | + def can_arrive(x, y): |
| 78 | + x_round = x // px |
| 79 | + y_round = y // py |
| 80 | + r = min(x_round, y_round) |
| 81 | + lx = x - r * px |
| 82 | + ly = y - r * py |
| 83 | + if (lx, ly) in path: |
| 84 | + return True |
| 85 | + else: |
| 86 | + return False |
| 87 | + |
| 88 | + if not can_arrive(x, y): |
| 89 | + return False |
| 90 | + |
| 91 | + for ob in obstacles: |
| 92 | + if ob[0] > x or ob[1] > y: |
| 93 | + continue |
| 94 | + if can_arrive(ob[0], ob[1]): |
| 95 | + return False |
| 96 | + |
| 97 | + return True |
| 98 | +``` |
0 commit comments