Update solution 0874

This commit is contained in:
YDZ
2021-05-08 16:29:09 +08:00
parent a10cebc033
commit 76753e8ca6
11 changed files with 251 additions and 98 deletions

View File

@@ -126,16 +126,16 @@
| | Easy | Medium | Hard | Total |
|:--------:|:--------:|:--------:|:--------:|:--------:|
|Optimizing|36|37|18|91|
|Optimizing|35|37|18|90|
|Accepted|**279**|**370**|**108**|**757**|
|Total|484|978|391|1853|
|Perfection Rate|87.1%|90.0%|83.3%|88.0%|
|Perfection Rate|87.5%|90.0%|83.3%|88.1%|
|Completion Rate|57.6%|37.8%|27.6%|40.9%|
|------------|----------------------------|----------------------------|----------------------------|----------------------------|
## 二. 目录
以下已经收录了 666 道题的题解,还有 11 道题在尝试优化到 beats 100%
以下已经收录了 667 道题的题解,还有 11 道题在尝试优化到 beats 100%
| No. | Title | Solution | Acceptance | Difficulty | Frequency |
|:--------:|:--------------------------------------------------------------|:--------:|:--------:|:--------:|:--------:|
@@ -210,7 +210,7 @@
|0069|Sqrt(x)|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0069.Sqrt(x))|35.6%|Easy||
|0070|Climbing Stairs|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0070.Climbing-Stairs)|48.9%|Easy||
|0071|Simplify Path|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0071.Simplify-Path)|35.1%|Medium||
|0072|Edit Distance||47.3%|Hard||
|0072|Edit Distance||47.4%|Hard||
|0073|Set Matrix Zeroes|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0073.Set-Matrix-Zeroes)|44.8%|Medium||
|0074|Search a 2D Matrix|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0074.Search-a-2D-Matrix)|38.3%|Medium||
|0075|Sort Colors|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0075.Sort-Colors)|50.1%|Medium||
@@ -319,7 +319,7 @@
|0178|Rank Scores||51.5%|Medium||
|0179|Largest Number|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0179.Largest-Number)|31.0%|Medium||
|0180|Consecutive Numbers||42.9%|Medium||
|0181|Employees Earning More Than Their Managers||61.4%|Easy||
|0181|Employees Earning More Than Their Managers||61.5%|Easy||
|0182|Duplicate Emails||65.3%|Easy||
|0183|Customers Who Never Order||57.9%|Easy||
|0184|Department Highest Salary||41.3%|Medium||
@@ -376,7 +376,7 @@
|0235|Lowest Common Ancestor of a Binary Search Tree|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0235.Lowest-Common-Ancestor-of-a-Binary-Search-Tree)|52.4%|Easy||
|0236|Lowest Common Ancestor of a Binary Tree|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0236.Lowest-Common-Ancestor-of-a-Binary-Tree)|49.7%|Medium||
|0237|Delete Node in a Linked List|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0237.Delete-Node-in-a-Linked-List)|67.8%|Easy||
|0238|Product of Array Except Self||61.4%|Medium||
|0238|Product of Array Except Self||61.3%|Medium||
|0239|Sliding Window Maximum|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0239.Sliding-Window-Maximum)|44.9%|Hard||
|0240|Search a 2D Matrix II|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0240.Search-a-2D-Matrix-II)|45.5%|Medium||
|0241|Different Ways to Add Parentheses||57.8%|Medium||
@@ -980,7 +980,7 @@
|0839|Similar String Groups|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0839.Similar-String-Groups)|41.9%|Hard||
|0840|Magic Squares In Grid||37.9%|Medium||
|0841|Keys and Rooms|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0841.Keys-and-Rooms)|66.6%|Medium||
|0842|Split Array into Fibonacci Sequence|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0842.Split-Array-into-Fibonacci-Sequence)|37.1%|Medium||
|0842|Split Array into Fibonacci Sequence|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0842.Split-Array-into-Fibonacci-Sequence)|37.0%|Medium||
|0843|Guess the Word||46.3%|Hard||
|0844|Backspace String Compare|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0844.Backspace-String-Compare)|47.2%|Easy||
|0845|Longest Mountain in Array|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0845.Longest-Mountain-in-Array)|38.8%|Medium||
@@ -1012,7 +1012,7 @@
|0871|Minimum Number of Refueling Stops||32.6%|Hard||
|0872|Leaf-Similar Trees|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0872.Leaf-Similar-Trees)|64.5%|Easy||
|0873|Length of Longest Fibonacci Subsequence||48.2%|Medium||
|0874|Walking Robot Simulation||36.8%|Easy||
|0874|Walking Robot Simulation|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0874.Walking-Robot-Simulation)|36.8%|Easy||
|0875|Koko Eating Bananas|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0875.Koko-Eating-Bananas)|53.5%|Medium||
|0876|Middle of the Linked List|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0876.Middle-of-the-Linked-List)|69.3%|Easy||
|0877|Stone Game||67.2%|Medium||
@@ -1044,7 +1044,7 @@
|0903|Valid Permutations for DI Sequence||54.4%|Hard||
|0904|Fruit Into Baskets|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0904.Fruit-Into-Baskets)|43.0%|Medium||
|0905|Sort Array By Parity||75.0%|Easy||
|0906|Super Palindromes||32.7%|Hard||
|0906|Super Palindromes||33.2%|Hard||
|0907|Sum of Subarray Minimums|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0907.Sum-of-Subarray-Minimums)|33.1%|Medium||
|0908|Smallest Range I||66.4%|Easy||
|0909|Snakes and Ladders||39.3%|Medium||
@@ -1183,7 +1183,7 @@
|1042|Flower Planting With No Adjacent||48.8%|Medium||
|1043|Partition Array for Maximum Sum||67.7%|Medium||
|1044|Longest Duplicate Substring||31.3%|Hard||
|1045|Customers Who Bought All Products||68.3%|Medium||
|1045|Customers Who Bought All Products||68.2%|Medium||
|1046|Last Stone Weight||62.4%|Easy||
|1047|Remove All Adjacent Duplicates In String|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1047.Remove-All-Adjacent-Duplicates-In-String)|71.5%|Easy||
|1048|Longest String Chain||55.7%|Medium||
@@ -1346,7 +1346,7 @@
|1205|Monthly Transactions II||45.7%|Medium||
|1206|Design Skiplist||59.0%|Hard||
|1207|Unique Number of Occurrences|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1207.Unique-Number-of-Occurrences)|72.0%|Easy||
|1208|Get Equal Substrings Within Budget|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1208.Get-Equal-Substrings-Within-Budget)|44.3%|Medium||
|1208|Get Equal Substrings Within Budget|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1208.Get-Equal-Substrings-Within-Budget)|44.4%|Medium||
|1209|Remove All Adjacent Duplicates in String II|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1209.Remove-All-Adjacent-Duplicates-in-String-II)|57.6%|Medium||
|1210|Minimum Moves to Reach Target with Rotations||46.7%|Hard||
|1211|Queries Quality and Percentage||70.2%|Easy||
@@ -1589,7 +1589,7 @@
|1448|Count Good Nodes in Binary Tree||71.7%|Medium||
|1449|Form Largest Integer With Digits That Add up to Target||44.6%|Hard||
|1450|Number of Students Doing Homework at a Given Time||77.0%|Easy||
|1451|Rearrange Words in a Sentence||60.1%|Medium||
|1451|Rearrange Words in a Sentence||60.0%|Medium||
|1452|People Whose List of Favorite Companies Is Not a Subset of Another List||55.4%|Medium||
|1453|Maximum Number of Darts Inside of a Circular Dartboard||35.8%|Hard||
|1454|Active Users||38.8%|Medium||
@@ -1635,7 +1635,7 @@
|1494|Parallel Courses II||30.9%|Hard||
|1495|Friendly Movies Streamed Last Month||51.1%|Easy||
|1496|Path Crossing||55.2%|Easy||
|1497|Check If Array Pairs Are Divisible by k||40.3%|Medium||
|1497|Check If Array Pairs Are Divisible by k||40.2%|Medium||
|1498|Number of Subsequences That Satisfy the Given Sum Condition||39.1%|Medium||
|1499|Max Value of Equation||45.4%|Hard||
|1500|Design a File Sharing System||46.7%|Medium||
@@ -1690,19 +1690,19 @@
|1549|The Most Recent Orders for Each Product||67.4%|Medium||
|1550|Three Consecutive Odds||64.5%|Easy||
|1551|Minimum Operations to Make Array Equal|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1551.Minimum-Operations-to-Make-Array-Equal)|80.7%|Medium||
|1552|Magnetic Force Between Two Balls||49.5%|Medium||
|1552|Magnetic Force Between Two Balls||49.6%|Medium||
|1553|Minimum Number of Days to Eat N Oranges||30.1%|Hard||
|1554|Strings Differ by One Character||64.3%|Medium||
|1555|Bank Account Summary||52.8%|Medium||
|1556|Thousand Separator||57.0%|Easy||
|1557|Minimum Number of Vertices to Reach All Nodes||75.9%|Medium||
|1558|Minimum Numbers of Function Calls to Make Target Array||63.3%|Medium||
|1559|Detect Cycles in 2D Grid||44.6%|Hard||
|1559|Detect Cycles in 2D Grid||44.7%|Hard||
|1560|Most Visited Sector in a Circular Track||56.9%|Easy||
|1561|Maximum Number of Coins You Can Get||77.2%|Medium||
|1562|Find Latest Group of Size M||39.8%|Medium||
|1563|Stone Game V||39.9%|Hard||
|1564|Put Boxes Into the Warehouse I||65.6%|Medium||
|1564|Put Boxes Into the Warehouse I||65.9%|Medium||
|1565|Unique Orders and Customers Per Month||82.9%|Easy||
|1566|Detect Pattern of Length M Repeated K or More Times||42.7%|Easy||
|1567|Maximum Length of Subarray With Positive Product||37.2%|Medium||
@@ -1726,7 +1726,7 @@
|1585|Check If String Is Transformable With Substring Sort Operations||48.6%|Hard||
|1586|Binary Search Tree Iterator II||66.7%|Medium||
|1587|Bank Account Summary II||90.0%|Easy||
|1588|Sum of All Odd Length Subarrays||81.7%|Easy||
|1588|Sum of All Odd Length Subarrays||81.8%|Easy||
|1589|Maximum Sum Obtained of Any Permutation||35.1%|Medium||
|1590|Make Sum Divisible by P||26.9%|Medium||
|1591|Strange Printer II||55.7%|Hard||
@@ -1738,7 +1738,7 @@
|1597|Build Binary Expression Tree From Infix Expression||59.2%|Hard||
|1598|Crawler Log Folder||63.8%|Easy||
|1599|Maximum Profit of Operating a Centennial Wheel||43.7%|Medium||
|1600|Throne Inheritance||60.8%|Medium||
|1600|Throne Inheritance||60.9%|Medium||
|1601|Maximum Number of Achievable Transfer Requests||48.0%|Hard||
|1602|Find Nearest Right Node in Binary Tree||73.4%|Medium||
|1603|Design Parking System|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1603.Design-Parking-System)|86.4%|Easy||
@@ -1747,7 +1747,7 @@
|1606|Find Servers That Handled Most Number of Requests||37.6%|Hard||
|1607|Sellers With No Sales||55.2%|Easy||
|1608|Special Array With X Elements Greater Than or Equal X|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1608.Special-Array-With-X-Elements-Greater-Than-or-Equal-X)|61.3%|Easy||
|1609|Even Odd Tree||52.1%|Medium||
|1609|Even Odd Tree||52.2%|Medium||
|1610|Maximum Number of Visible Points||31.5%|Hard||
|1611|Minimum One Bit Operations to Make Integers Zero||58.0%|Hard||
|1612|Check If Two Expression Trees are Equivalent||69.9%|Medium||
@@ -1863,7 +1863,7 @@
|1722|Minimize Hamming Distance After Swap Operations||54.2%|Medium||
|1723|Find Minimum Time to Finish All Jobs||43.8%|Hard||
|1724|Checking Existence of Edge Length Limited Paths II||57.8%|Hard||
|1725|Number Of Rectangles That Can Form The Largest Square|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1725.Number-Of-Rectangles-That-Can-Form-The-Largest-Square)|78.1%|Easy||
|1725|Number Of Rectangles That Can Form The Largest Square|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1725.Number-Of-Rectangles-That-Can-Form-The-Largest-Square)|78.2%|Easy||
|1726|Tuple with Same Product||57.4%|Medium||
|1727|Largest Submatrix With Rearrangements||59.0%|Medium||
|1728|Cat and Mouse II||41.1%|Hard||
@@ -1883,7 +1883,7 @@
|1742|Maximum Number of Balls in a Box|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1742.Maximum-Number-of-Balls-in-a-Box)|73.4%|Easy||
|1743|Restore the Array From Adjacent Pairs||63.8%|Medium||
|1744|Can You Eat Your Favorite Candy on Your Favorite Day?||30.8%|Medium||
|1745|Palindrome Partitioning IV||49.4%|Hard||
|1745|Palindrome Partitioning IV||49.5%|Hard||
|1746|Maximum Subarray Sum After One Operation||61.9%|Medium||
|1747|Leetflex Banned Accounts||68.9%|Medium||
|1748|Sum of Unique Elements|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1748.Sum-of-Unique-Elements)|74.5%|Easy||
@@ -1914,7 +1914,7 @@
|1773|Count Items Matching a Rule||84.6%|Easy||
|1774|Closest Dessert Cost||57.3%|Medium||
|1775|Equal Sum Arrays With Minimum Number of Operations||50.7%|Medium||
|1776|Car Fleet II||47.8%|Hard||
|1776|Car Fleet II||47.9%|Hard||
|1777|Product's Price for Each Store||86.4%|Easy||
|1778|Shortest Path in a Hidden Grid||45.3%|Medium||
|1779|Find Nearest Point That Has the Same X or Y Coordinate||66.8%|Easy||
@@ -1952,7 +1952,7 @@
|1811|Find Interview Candidates||68.2%|Medium||
|1812|Determine Color of a Chessboard Square||78.1%|Easy||
|1813|Sentence Similarity III||43.0%|Medium||
|1814|Count Nice Pairs in an Array||38.9%|Medium||
|1814|Count Nice Pairs in an Array||39.0%|Medium||
|1815|Maximum Number of Groups Getting Fresh Donuts||38.9%|Hard||
|1816|Truncate Sentence||79.3%|Easy||
|1817|Finding the Users Active Minutes||78.4%|Medium||
@@ -1961,7 +1961,7 @@
|1820|Maximum Number of Accepted Invitations||49.2%|Medium||
|1821|Find Customers With Positive Revenue this Year||89.5%|Easy||
|1822|Sign of the Product of an Array||78.5%|Easy||
|1823|Find the Winner of the Circular Game||72.2%|Medium||
|1823|Find the Winner of the Circular Game||72.1%|Medium||
|1824|Minimum Sideway Jumps||58.3%|Medium||
|1825|Finding MK Average||31.2%|Hard||
|1826|Faulty Sensor||58.9%|Easy||
@@ -1988,9 +1988,9 @@
|1847|Closest Room||35.2%|Hard||
|1848|Minimum Distance to the Target Element||62.2%|Easy||
|1849|Splitting a String Into Descending Consecutive Values||34.7%|Medium||
|1850|Minimum Adjacent Swaps to Reach the Kth Smallest Number||61.9%|Medium||
|1850|Minimum Adjacent Swaps to Reach the Kth Smallest Number||62.0%|Medium||
|1851|Minimum Interval to Include Each Query||40.2%|Hard||
|1852|Distinct Numbers in Each Subarray||79.5%|Medium||
|1852|Distinct Numbers in Each Subarray||79.6%|Medium||
|1853|Convert Date Format||91.1%|Easy||
|------------|-------------------------------------------------------|-------| ----------------| ---------------|-------------|

View File

@@ -1,19 +1,19 @@
# [875. Koko Eating Bananas](https://leetcode.com/problems/walking-robot-simulation/)
# [874. Walking Robot Simulation](https://leetcode.com/problems/walking-robot-simulation/)
## 题目
A robot on an infinite XY-plane starts at point `(0, 0)` and faces north. The robot can receive one of three possible types of `commands`:
A robot on an infinite XY-plane starts at point `(0, 0)` and faces north. The robot can receive one of three possible types of `commands`:
- `-2`: turn left `90` degrees,
- `-1`: turn right `90` degrees, or
- `1 <= k <= 9`: move forward `k` units.
- `2`: turn left `90` degrees,
- `1`: turn right `90` degrees, or
- `1 <= k <= 9`: move forward `k` units.
Some of the grid squares are `obstacles`. The `ith` obstacle is at grid point `obstacles[i] = (xi, yi)`.
Some of the grid squares are `obstacles`. The `ith` obstacle is at grid point `obstacles[i] = (xi, yi)`.
If the robot would try to move onto them, the robot stays on the previous grid square instead (but still continues following the rest of the route.)
Return *the maximum Euclidean distance that the robot will be from the origin **squared** (i.e. if the distance is* `5`*, return* `25`*)*.
Return *the maximum Euclidean distance that the robot will be from the origin **squared** (i.e. if the distance is* `5`*, return* `25`*)*.
**Note:**
@@ -22,8 +22,6 @@ Return *the maximum Euclidean distance that the robot will be from the origin **
- South means -Y direction.
- West means -X direction.
**Example 1:**
```
@@ -34,6 +32,7 @@ Explanation: The robot starts at (0, 0):
2. Turn right.
3. Move east 3 units to (3, 4).
The furthest point away from the origin is (3, 4), which is 32 + 42 = 25 units away.
```
**Example 2:**
@@ -48,77 +47,89 @@ Explanation: The robot starts at (0, 0):
4. Turn left.
5. Move north 4 units to (1, 8).
The furthest point away from the origin is (1, 8), which is 12 + 82 = 65 units away.
```
**Constraints:**
- `1 <= commands.length <= 104`
- `commands[i]` is one of the values in the list `[-2,-1,1,2,3,4,5,6,7,8,9]`.
- `commands[i]` is one of the values in the list `[-2,-1,1,2,3,4,5,6,7,8,9]`.
- `0 <= obstacles.length <= 104`
- `-3 * 104 <= xi, yi <= 3 * 104`
- The answer is guaranteed to be less than `231`.
- `3 * 104 <= xi, yi <= 3 * 104`
- The answer is guaranteed to be less than `231`.
## 题目大意
机器人在一个无限大小的 XY 网格平面上行走,从点 (0, 0) 处开始出发,面向北方。该机器人可以接收以下三种类型的命令 commands
机器人在一个无限大小的 XY 网格平面上行走,从点 (0, 0) 处开始出发,面向北方。该机器人可以接收以下三种类型的命令 commands
- 2 向左转 90 度
- -1 :向右转 90 度
- 1 <= x <= 9 向前移动 x 个单位长度
在网格上有一些格子被视为障碍物 obstacles 。第 i 个障碍物位于网格点 obstacles[i] = (xi, yi) 。机器人无法走到障碍物上,它将会停留在障碍物的前一个网格方块上,但仍然可以继续尝试进行该路线的其余部分。返回从原点到机器人所有经过的路径点(坐标为整数)的最大欧式距离的平方。(即,如果距离为 5 ,则返回 25
-2 :向左转 90 度
-1 :向右转 90 度
1 <= x <= 9 :向前移动 x 个单位长度
在网格上有一些格子被视为障碍物 obstacles 。第 i 个障碍物位于网格点 obstacles[i] = (xi, yi) 。
机器人无法走到障碍物上,它将会停留在障碍物的前一个网格方块上,但仍然可以继续尝试进行该路线的其余部分。
返回从原点到机器人所有经过的路径点(坐标为整数)的最大欧式距离的平方。(即,如果距离为 5 ,则返回 25
示例 1
输入commands = [4,-1,3], obstacles = []
输出25
解释:
机器人开始位于 (0, 0)
1. 向北移动 4 个单位,到达 (0, 4)
2. 右转
3. 向东移动 3 个单位,到达 (3, 4)
距离原点最远的是 (3, 4) ,距离为 32 + 42 = 25
示例 2
输入commands = [4,-1,4,-2,4], obstacles = [[2,4]]
输出65
解释:机器人开始位于 (0, 0)
1. 向北移动 4 个单位,到达 (0, 4)
2. 右转
3. 向东移动 1 个单位,然后被位于 (2, 4) 的障碍物阻挡,机器人停在 (1, 4)
4. 左转
5. 向北走 4 个单位,到达 (1, 8)
距离原点最远的是 (1, 8) ,距离为 12 + 82 = 65
提示:
- `1 <= commands.length <= 104`
- `commands[i]` is one of the values in the list `[-2,-1,1,2,3,4,5,6,7,8,9]`.
- `0 <= obstacles.length <= 104`
- `-3 * 104 <= xi, yi <= 3 * 104`
- The answer is guaranteed to be less than `231`.
注意:
- 北表示 +Y 方向。
- 东表示 +X 方向。
- 南表示 -Y 方向。
- 西表示 -X 方向。
## 解题思路
这个题的难点在于,怎么用编程语言去描述机器人的行为
可以用以下数据结构表达机器人的行为:
- 这个题的难点在于,怎么用编程语言去描述机器人的行为,可以用以下数据结构表达机器人的行为:
```go
direct:= 0 // direct表示机器人移动方向0 1 2 3 4 (北东南西),默认朝北
x, y := 0, 0 // 表示当前机器人所在横纵坐标位置,默认为(0,0)
directX := []int{0, 1, 0, -1}
directY := []int{1, 0, -1, 0}
// 组合directX directY和direct表示机器人往某一个方向移动
nextX := x + directX[direct]
nextY := y + directY[direct]
```
其他代码按照题意翻译即可
## 代码
```go
direct:= 0 // direct表示机器人移动方向0 1 2 3 4 (北东南西),默认朝北
x, y := 0, 0 // 表示当前机器人所在横纵坐标位置,默认为(0,0)
directX := []int{0, 1, 0, -1}
directY := []int{1, 0, -1, 0}
// 组合directX directY和direct表示机器人往某一个方向移动
nextX := x + directX[direct]
nextY := y + directY[direct]
其他代码按照题意翻译即可
package leetcode
func robotSim(commands []int, obstacles [][]int) int {
m := make(map[[2]int]struct{})
for _, v := range obstacles {
if len(v) != 0 {
m[[2]int{v[0], v[1]}] = struct{}{}
}
}
directX := []int{0, 1, 0, -1}
directY := []int{1, 0, -1, 0}
direct, x, y := 0, 0, 0
result := 0
for _, c := range commands {
if c == -2 {
direct = (direct + 3) % 4
continue
}
if c == -1 {
direct = (direct + 1) % 4
continue
}
for ; c > 0; c-- {
nextX := x + directX[direct]
nextY := y + directY[direct]
if _, ok := m[[2]int{nextX, nextY}]; ok {
break
}
tmpResult := nextX*nextX + nextY*nextY
if tmpResult > result {
result = tmpResult
}
x = nextX
y = nextY
}
}
return result
}
```

View File

@@ -65,5 +65,5 @@ func dfsLeaf(root *TreeNode, leaf *[]int) {
----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0870.Advantage-Shuffle/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0875.Koko-Eating-Bananas/">下一页➡️</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0874.Walking-Robot-Simulation/">下一页➡️</a></p>
</div>

View File

@@ -0,0 +1,142 @@
# [874. Walking Robot Simulation](https://leetcode.com/problems/walking-robot-simulation/)
## 题目
A robot on an infinite XY-plane starts at point `(0, 0)` and faces north. The robot can receive one of three possible types of `commands`:
- `2`: turn left `90` degrees,
- `1`: turn right `90` degrees, or
- `1 <= k <= 9`: move forward `k` units.
Some of the grid squares are `obstacles`. The `ith` obstacle is at grid point `obstacles[i] = (xi, yi)`.
If the robot would try to move onto them, the robot stays on the previous grid square instead (but still continues following the rest of the route.)
Return *the maximum Euclidean distance that the robot will be from the origin **squared** (i.e. if the distance is* `5`*, return* `25`*)*.
**Note:**
- North means +Y direction.
- East means +X direction.
- South means -Y direction.
- West means -X direction.
**Example 1:**
```
Input: commands = [4,-1,3], obstacles = []
Output: 25
Explanation: The robot starts at (0, 0):
1. Move north 4 units to (0, 4).
2. Turn right.
3. Move east 3 units to (3, 4).
The furthest point away from the origin is (3, 4), which is 32 + 42 = 25 units away.
```
**Example 2:**
```
Input: commands = [4,-1,4,-2,4], obstacles = [[2,4]]
Output: 65
Explanation: The robot starts at (0, 0):
1. Move north 4 units to (0, 4).
2. Turn right.
3. Move east 1 unit and get blocked by the obstacle at (2, 4), robot is at (1, 4).
4. Turn left.
5. Move north 4 units to (1, 8).
The furthest point away from the origin is (1, 8), which is 12 + 82 = 65 units away.
```
**Constraints:**
- `1 <= commands.length <= 104`
- `commands[i]` is one of the values in the list `[-2,-1,1,2,3,4,5,6,7,8,9]`.
- `0 <= obstacles.length <= 104`
- `3 * 104 <= xi, yi <= 3 * 104`
- The answer is guaranteed to be less than `231`.
## 题目大意
机器人在一个无限大小的 XY 网格平面上行走,从点 (0, 0) 处开始出发,面向北方。该机器人可以接收以下三种类型的命令 commands
- 2 向左转 90 度
- -1 :向右转 90 度
- 1 <= x <= 9 向前移动 x 个单位长度
在网格上有一些格子被视为障碍物 obstacles 。第 i 个障碍物位于网格点 obstacles[i] = (xi, yi) 。机器人无法走到障碍物上,它将会停留在障碍物的前一个网格方块上,但仍然可以继续尝试进行该路线的其余部分。返回从原点到机器人所有经过的路径点(坐标为整数)的最大欧式距离的平方。(即,如果距离为 5 ,则返回 25
注意:
- 北表示 +Y 方向。
- 东表示 +X 方向。
- 南表示 -Y 方向。
- 西表示 -X 方向。
## 解题思路
- 这个题的难点在于,怎么用编程语言去描述机器人的行为,可以用以下数据结构表达机器人的行为:
```go
direct:= 0 // direct表示机器人移动方向0 1 2 3 4 (北东南西),默认朝北
x, y := 0, 0 // 表示当前机器人所在横纵坐标位置,默认为(0,0)
directX := []int{0, 1, 0, -1}
directY := []int{1, 0, -1, 0}
// 组合directX directY和direct表示机器人往某一个方向移动
nextX := x + directX[direct]
nextY := y + directY[direct]
```
其他代码按照题意翻译即可
## 代码
```go
package leetcode
func robotSim(commands []int, obstacles [][]int) int {
m := make(map[[2]int]struct{})
for _, v := range obstacles {
if len(v) != 0 {
m[[2]int{v[0], v[1]}] = struct{}{}
}
}
directX := []int{0, 1, 0, -1}
directY := []int{1, 0, -1, 0}
direct, x, y := 0, 0, 0
result := 0
for _, c := range commands {
if c == -2 {
direct = (direct + 3) % 4
continue
}
if c == -1 {
direct = (direct + 1) % 4
continue
}
for ; c > 0; c-- {
nextX := x + directX[direct]
nextY := y + directY[direct]
if _, ok := m[[2]int{nextX, nextY}]; ok {
break
}
tmpResult := nextX*nextX + nextY*nextY
if tmpResult > result {
result = tmpResult
}
x = nextX
y = nextY
}
}
return result
}
```
----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0872.Leaf-Similar-Trees/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0875.Koko-Eating-Bananas/">下一页➡️</a></p>
</div>

View File

@@ -103,6 +103,6 @@ func maxInArr(xs []int) int {
----------------------------------------------
<div style="display: flex;justify-content: space-between;align-items: center;">
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0872.Leaf-Similar-Trees/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0874.Walking-Robot-Simulation/">⬅️上一页</a></p>
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0800~0899/0876.Middle-of-the-Linked-List/">下一页➡️</a></p>
</div>

View File

@@ -133,7 +133,7 @@ weight: 1
|1185|Day of the Week|[Go]({{< relref "/ChapterFour/1100~1199/1185.Day-of-the-Week.md" >}})|Easy||||61.0%|
|1200|Minimum Absolute Difference|[Go]({{< relref "/ChapterFour/1200~1299/1200.Minimum-Absolute-Difference.md" >}})|Easy||||67.1%|
|1202|Smallest String With Swaps|[Go]({{< relref "/ChapterFour/1200~1299/1202.Smallest-String-With-Swaps.md" >}})|Medium||||49.1%|
|1208|Get Equal Substrings Within Budget|[Go]({{< relref "/ChapterFour/1200~1299/1208.Get-Equal-Substrings-Within-Budget.md" >}})|Medium||||44.3%|
|1208|Get Equal Substrings Within Budget|[Go]({{< relref "/ChapterFour/1200~1299/1208.Get-Equal-Substrings-Within-Budget.md" >}})|Medium||||44.4%|
|1217|Minimum Cost to Move Chips to The Same Position|[Go]({{< relref "/ChapterFour/1200~1299/1217.Minimum-Cost-to-Move-Chips-to-The-Same-Position.md" >}})|Easy||||70.8%|
|1232|Check If It Is a Straight Line|[Go]({{< relref "/ChapterFour/1200~1299/1232.Check-If-It-Is-a-Straight-Line.md" >}})|Easy||||43.1%|
|1252|Cells with Odd Values in a Matrix|[Go]({{< relref "/ChapterFour/1200~1299/1252.Cells-with-Odd-Values-in-a-Matrix.md" >}})|Easy||||78.6%|

View File

@@ -16,7 +16,7 @@ weight: 7
|0062|Unique Paths|[Go]({{< relref "/ChapterFour/0001~0099/0062.Unique-Paths.md" >}})|Medium| O(n^2)| O(n^2)||56.5%|
|0063|Unique Paths II|[Go]({{< relref "/ChapterFour/0001~0099/0063.Unique-Paths-II.md" >}})|Medium| O(n^2)| O(n^2)||35.9%|
|0064|Minimum Path Sum|[Go]({{< relref "/ChapterFour/0001~0099/0064.Minimum-Path-Sum.md" >}})|Medium| O(n^2)| O(n^2)||56.7%|
|0070|Climbing Stairs|[Go]({{< relref "/ChapterFour/0001~0099/0070.Climbing-Stairs.md" >}})|Easy| O(n)| O(n)||48.8%|
|0070|Climbing Stairs|[Go]({{< relref "/ChapterFour/0001~0099/0070.Climbing-Stairs.md" >}})|Easy| O(n)| O(n)||48.9%|
|0091|Decode Ways|[Go]({{< relref "/ChapterFour/0001~0099/0091.Decode-Ways.md" >}})|Medium| O(n)| O(n)||27.1%|
|0095|Unique Binary Search Trees II|[Go]({{< relref "/ChapterFour/0001~0099/0095.Unique-Binary-Search-Trees-II.md" >}})|Medium||||43.3%|
|0096|Unique Binary Search Trees|[Go]({{< relref "/ChapterFour/0001~0099/0096.Unique-Binary-Search-Trees.md" >}})|Medium| O(n)| O(n)||54.8%|

View File

@@ -58,7 +58,7 @@ weight: 12
|0775|Global and Local Inversions|[Go]({{< relref "/ChapterFour/0700~0799/0775.Global-and-Local-Inversions.md" >}})|Medium||||46.2%|
|0781|Rabbits in Forest|[Go]({{< relref "/ChapterFour/0700~0799/0781.Rabbits-in-Forest.md" >}})|Medium||||55.9%|
|0812|Largest Triangle Area|[Go]({{< relref "/ChapterFour/0800~0899/0812.Largest-Triangle-Area.md" >}})|Easy||||59.2%|
|0836|Rectangle Overlap|[Go]({{< relref "/ChapterFour/0800~0899/0836.Rectangle-Overlap.md" >}})|Easy||||43.4%|
|0836|Rectangle Overlap|[Go]({{< relref "/ChapterFour/0800~0899/0836.Rectangle-Overlap.md" >}})|Easy||||43.3%|
|0869|Reordered Power of 2|[Go]({{< relref "/ChapterFour/0800~0899/0869.Reordered-Power-of-2.md" >}})|Medium||||61.2%|
|0878|Nth Magical Number|[Go]({{< relref "/ChapterFour/0800~0899/0878.Nth-Magical-Number.md" >}})|Hard||||28.9%|
|0885|Spiral Matrix III|[Go]({{< relref "/ChapterFour/0800~0899/0885.Spiral-Matrix-III.md" >}})|Medium| O(n^2)| O(1)||71.2%|

View File

@@ -43,7 +43,7 @@ weight: 17
|1040|Moving Stones Until Consecutive II|[Go]({{< relref "/ChapterFour/1000~1099/1040.Moving-Stones-Until-Consecutive-II.md" >}})|Medium| O(n log n)| O(1) |❤️|54.4%|
|1052|Grumpy Bookstore Owner|[Go]({{< relref "/ChapterFour/1000~1099/1052.Grumpy-Bookstore-Owner.md" >}})|Medium| O(n log n)| O(1) ||56.0%|
|1074|Number of Submatrices That Sum to Target|[Go]({{< relref "/ChapterFour/1000~1099/1074.Number-of-Submatrices-That-Sum-to-Target.md" >}})|Hard| O(n^3)| O(n) |❤️|65.0%|
|1208|Get Equal Substrings Within Budget|[Go]({{< relref "/ChapterFour/1200~1299/1208.Get-Equal-Substrings-Within-Budget.md" >}})|Medium||||44.3%|
|1208|Get Equal Substrings Within Budget|[Go]({{< relref "/ChapterFour/1200~1299/1208.Get-Equal-Substrings-Within-Budget.md" >}})|Medium||||44.4%|
|1423|Maximum Points You Can Obtain from Cards|[Go]({{< relref "/ChapterFour/1400~1499/1423.Maximum-Points-You-Can-Obtain-from-Cards.md" >}})|Medium||||47.1%|
|1438|Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit|[Go]({{< relref "/ChapterFour/1400~1499/1438.Longest-Continuous-Subarray-With-Absolute-Diff-Less-Than-or-Equal-to-Limit.md" >}})|Medium||||44.5%|
|1658|Minimum Operations to Reduce X to Zero|[Go]({{< relref "/ChapterFour/1600~1699/1658.Minimum-Operations-to-Reduce-X-to-Zero.md" >}})|Medium||||33.3%|