Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9e12f7c

Browse files
✨feat: Add 1672、2069 & Modify 902
1 parent 664fbf1 commit 9e12f7c

File tree

5 files changed

+220
-0
lines changed

5 files changed

+220
-0
lines changed

‎Index/模拟.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
| [1614. 括号的最大嵌套深度](https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-nesting-depth-of-the-parentheses/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-pf5d/) | 简单 | 🤩🤩🤩🤩🤩 |
117117
| [1629. 按键持续时间最长的键](https://leetcode-cn.com/problems/slowest-key/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/slowest-key/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-zjwb/) | 简单 | 🤩🤩🤩🤩🤩 |
118118
| [1646. 获取生成数组中的最大值](https://leetcode-cn.com/problems/get-maximum-in-generated-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/get-maximum-in-generated-array/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-sj53/) | 简单 | 🤩🤩🤩🤩 |
119+
| [1672. 最富有客户的资产总量](https://leetcode-cn.com/problems/richest-customer-wealth/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/richest-customer-wealth/solution/by-ac_oier-ai19/) | 简单 | 🤩🤩🤩🤩 |
119120
| [1688. 比赛中的配对次数](https://leetcode-cn.com/problems/count-of-matches-in-tournament/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-of-matches-in-tournament/solution/gong-shui-san-xie-jian-dan-nao-jin-ji-zh-cx7a/) | 简单 | 🤩🤩🤩🤩🤩 |
120121
| [1706. 球会落何处](https://leetcode-cn.com/problems/where-will-the-ball-fall/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/where-will-the-ball-fall/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-jz6f/) | 中等 | 🤩🤩🤩🤩 |
121122
| [1716. 计算力扣银行的钱](https://leetcode-cn.com/problems/calculate-money-in-leetcode-bank/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/calculate-money-in-leetcode-bank/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-ifit/) | 简单 | 🤩🤩🤩🤩 |
@@ -139,5 +140,6 @@
139140
| [2038. 如果相邻两个颜色均相同则删除当前颜色](https://leetcode-cn.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/solution/gong-shui-san-xie-nao-jin-ji-zhuan-wan-y-a8xm/) | 中等 | 🤩🤩 |
140141
| [2043. 简易银行系统](https://leetcode-cn.com/problems/simple-bank-system/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/simple-bank-system/solution/by-ac_oier-9pqi/) | 中等 | 🤩🤩🤩🤩 |
141142
| [2047. 句子中的有效单词数](https://leetcode-cn.com/problems/number-of-valid-words-in-a-sentence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-valid-words-in-a-sentence/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-5pcz/) | 简单 | 🤩🤩🤩🤩 |
143+
| [2069. 模拟行走机器人 II](https://leetcode-cn.com/problems/walking-robot-simulation-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/walking-robot-simulation-ii/solution/by-ac_oier-6zib/) | 中等 | 🤩🤩🤩🤩 |
142144
| [面试题 10.02. 变位词组](https://leetcode-cn.com/problems/group-anagrams-lcci/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/group-anagrams-lcci/solution/gong-shui-san-xie-tong-ji-bian-wei-ci-de-0iqe/) | 中等 | 🤩🤩🤩🤩 |
143145

‎Index/脑筋急转弯.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
| [419. 甲板上的战舰](https://leetcode-cn.com/problems/battleships-in-a-board/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/battleships-in-a-board/solution/gong-shui-san-xie-ji-chong-sao-miao-xian-trmc/) | 中等 | 🤩🤩🤩🤩 |
55
| [423. 从英文中重建数字](https://leetcode-cn.com/problems/reconstruct-original-digits-from-english/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reconstruct-original-digits-from-english/solution/gong-shui-san-xie-nao-jin-ji-zhuan-wan-m-vg7a/) | 中等 | 🤩🤩🤩🤩 |
66
| [2038. 如果相邻两个颜色均相同则删除当前颜色](https://leetcode-cn.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color/solution/gong-shui-san-xie-nao-jin-ji-zhuan-wan-y-a8xm/) | 中等 | 🤩🤩🤩🤩🤩 |
7+
| [2069. 模拟行走机器人 II](https://leetcode-cn.com/problems/walking-robot-simulation-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/walking-robot-simulation-ii/solution/by-ac_oier-6zib/) | 中等 | 🤩🤩🤩🤩 |
78

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1672. 最富有客户的资产总量](https://leetcode-cn.com/problems/richest-customer-wealth/solution/by-ac_oier-ai19/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
给你一个 $m * n$ 的整数网格 `accounts`,其中 $accounts[i][j]$ 是第 $i$ 位客户在第 $j$ 家银行托管的资产数量。返回最富有客户所拥有的 资产总量 。
10+
11+
客户的 资产总量 就是他们在各家银行托管的资产数量之和。最富有客户就是 资产总量 最大的客户。
12+
13+
示例 1:
14+
```
15+
输入:accounts = [[1,2,3],[3,2,1]]
16+
17+
输出:6
18+
19+
解释:
20+
第 1 位客户的资产总量 = 1 + 2 + 3 = 6
21+
第 2 位客户的资产总量 = 3 + 2 + 1 = 6
22+
两位客户都是最富有的,资产总量都是 6 ,所以返回 6 。
23+
```
24+
示例 2:
25+
```
26+
输入:accounts = [[1,5],[7,3],[3,5]]
27+
28+
输出:10
29+
30+
解释:
31+
第 1 位客户的资产总量 = 6
32+
第 2 位客户的资产总量 = 10
33+
第 3 位客户的资产总量 = 8
34+
第 2 位客户是最富有的,资产总量是 10
35+
```
36+
示例 3:
37+
```
38+
输入:accounts = [[2,8,7],[7,1,3],[1,9,5]]
39+
40+
输出:17
41+
```
42+
43+
提示:
44+
* $m == accounts.length$
45+
* $n == accounts[i].length$
46+
* 1ドル <= m, n <= 50$
47+
* 1ドル <= accounts[i][j] <= 100$
48+
49+
---
50+
51+
### 模拟
52+
53+
根据题意进行模拟即可。
54+
55+
```Java
56+
class Solution {
57+
public int maximumWealth(int[][] accounts) {
58+
int m = accounts.length, n = accounts[0].length, ans = 0;
59+
for (int i = 0; i < m; i++) {
60+
int cur = 0;
61+
for (int j = 0; j < n; j++) cur += accounts[i][j];
62+
ans = Math.max(ans, cur);
63+
}
64+
return ans;
65+
}
66+
}
67+
```
68+
* 时间复杂度:$O(m * n)$
69+
* 空间复杂度:$O(1)$
70+
71+
---
72+
73+
### 最后
74+
75+
这是我们「刷穿 LeetCode」系列文章的第 `No.1672` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
76+
77+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
78+
79+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
80+
81+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
82+
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[2069. 模拟行走机器人 II](https://leetcode-cn.com/problems/walking-robot-simulation-ii/solution/by-ac_oier-6zib/)** ,难度为 **中等**
4+
5+
Tag : 「模拟」、「脑筋急转弯」
6+
7+
8+
9+
给你一个在 `XY` 平面上的 `width x height` 的网格图,左下角 的格子为 $(0, 0),ドル右上角的格子为 $(width - 1, height - 1)$ 。网格图中相邻格子为四个基本方向之一(`"North"`,`"East"`,`"South"``"West"`)。一个机器人初始在格子 $(0, 0)$ ,方向为 `"East"`
10+
11+
机器人可以根据指令移动指定的步数。每一步,它可以执行以下操作。
12+
13+
1. 沿着当前方向尝试往前一步 。
14+
2. 如果机器人下一步将到达的格子超出了边界,机器人会逆时针转 90 度,然后再尝试往前一步。
15+
16+
如果机器人完成了指令要求的移动步数,它将停止移动并等待下一个指令。
17+
18+
请你实现 `Robot` 类:
19+
* `Robot(int width, int height)` 初始化一个 `width x height` 的网格图,机器人初始在 $(0, 0),ドル方向朝 `"East"`
20+
* `void move(int num)` 给机器人下达前进 `num` 步的指令。
21+
* `int[] getPos()` 返回机器人当前所处的格子位置,用一个长度为 2ドル$ 的数组 $[x, y]$ 表示。
22+
* `String getDir()` 返回当前机器人的朝向,为 `"North"`,`"East"`,`"South"` 或者 `"West"`
23+
24+
示例 1:
25+
![](https://assets.leetcode.com/uploads/2021/10/09/example-1.png)
26+
```
27+
输入:
28+
["Robot", "move", "move", "getPos", "getDir", "move", "move", "move", "getPos", "getDir"]
29+
[[6, 3], [2], [2], [], [], [2], [1], [4], [], []]
30+
31+
输出:
32+
[null, null, null, [4, 0], "East", null, null, null, [1, 2], "West"]
33+
34+
解释:
35+
Robot robot = new Robot(6, 3); // 初始化网格图,机器人在 (0, 0) ,朝东。
36+
robot.move(2); // 机器人朝东移动 2 步,到达 (2, 0) ,并朝东。
37+
robot.move(2); // 机器人朝东移动 2 步,到达 (4, 0) ,并朝东。
38+
robot.getPos(); // 返回 [4, 0]
39+
robot.getDir(); // 返回 "East"
40+
robot.move(2); // 朝东移动 1 步到达 (5, 0) ,并朝东。
41+
// 下一步继续往东移动将出界,所以逆时针转变方向朝北。
42+
// 然后,往北移动 1 步到达 (5, 1) ,并朝北。
43+
robot.move(1); // 朝北移动 1 步到达 (5, 2) ,并朝 北 (不是朝西)。
44+
robot.move(4); // 下一步继续往北移动将出界,所以逆时针转变方向朝西。
45+
// 然后,移动 4 步到 (1, 2) ,并朝西。
46+
robot.getPos(); // 返回 [1, 2]
47+
robot.getDir(); // 返回 "West"
48+
```
49+
50+
提示:
51+
* 2ドル <= width, height <= 100$
52+
* 1ドル <= num <= 10^5$
53+
* `move`,`getPos``getDir` 总共调用次数不超过 10ドル^4$ 次。
54+
55+
---
56+
57+
### 模拟
58+
59+
根据题目给定的移动规则可知,机器人总是在外圈移动(共上下左右四条),而移动方向分为四类:
60+
61+
![image.png](https://pic.leetcode-cn.com/1649906046-cdlyAL-image.png)
62+
63+
当行走步数为 $mod = 2 * (w - 1) + 2 * (h - 1)$ 的整数倍时,会回到起始位置,因此我们可以通过维护一个变量 `loc` 来记录行走的总步数,并且每次将 `loc``mod` 进行取模来得到有效步数。
64+
65+
在回答 `getPos``getDir` 询问时,根据当前 `loc` 进行分情况讨论(见注释)。
66+
67+
另外还有一个小细节:根据题意,如果当前处于 $(0, 0)$ 位置,并且没有移动过,方向为 `East`,若移动过,方向则为 `South`,这可以通过一个变量 `moved` 来进行特判处理。
68+
69+
代码:
70+
```Java
71+
class Robot {
72+
String[] ss = new String[]{"East", "North", "West", "South"};
73+
int w, h, loc; // loc: 有效(取模后)移动步数
74+
boolean moved; // 记录是否经过移动,用于特判 (0,0) 的方向
75+
public Robot(int width, int height) {
76+
w = width; h = height;
77+
}
78+
public void step(int num) {
79+
moved = true;
80+
loc += num;
81+
loc %= 2 * (w - 1) + 2 * (h - 1);
82+
}
83+
public int[] getPos() {
84+
int[] info = move();
85+
return new int[]{info[0], info[1]};
86+
}
87+
public String getDir() {
88+
int[] info = move();
89+
int x = info[0], y = info[1], dir = info[2];
90+
// 特殊处理当前在 (0,0) 的情况,当未移动过方向为 East,移动过方向为 South
91+
if (x == 0 && y == 0) return moved ? ss[3] : ss[0];
92+
return ss[dir];
93+
}
94+
int[] move() {
95+
if (loc <= w - 1) {
96+
// 当移动步数范围在 [0,w-1] 时,所在位置为外圈的下方,方向为 East
97+
return new int[]{loc, 0, 0};
98+
} else if (loc <= (w - 1) + (h - 1)) {
99+
// 当移动步数范围在 [w,(w-1)+(h-1)] 时,所在位置为外圈的右方,方向为 North
100+
return new int[]{w - 1, loc - (w - 1), 1};
101+
} else if (loc <= 2 * (w - 1) + (h - 1)) {
102+
// 当移动步数范围在 [(w-1)+(h-1)+1,2*(w-1)+(h-1)] 时,所在位置为外圈的上方,方向为 West
103+
return new int[]{(w - 1) - (loc - ((w - 1) + (h - 1))), h - 1, 2};
104+
} else {
105+
// 当移动步数范围在 [2*(w-1)+(h-1)+1,2*(w-1)+2*(h-1)] 时,所在位置为外圈的左方,方向为 South
106+
return new int[]{0, (h - 1) - (loc - (2 * (w - 1) + (h - 1))), 3};
107+
}
108+
}
109+
}
110+
```
111+
* 时间复杂度:$O(1)$
112+
* 空间复杂度:$O(1)$
113+
114+
---
115+
116+
### 最后
117+
118+
这是我们「刷穿 LeetCode」系列文章的第 `No.2169` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
119+
120+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
121+
122+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
123+
124+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
125+

‎LeetCode/901-910/902. 最大为 N 的数字组合(困难).md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,16 @@ class Solution {
129129

130130
---
131131

132+
### 总结
133+
134+
数位 DP 的难度取决于「限制条件」的多少,而 LC 上仅有的几道数位 DP 题目限制条件都很少,且不需要引入额外的数据结构来记录状态,因此都属于数位 DP 的入门难度(LC 难度均为 Hard)。
135+
136+
几乎所有的数位 DP 问题都可以归纳到上述的解法 :「将问题抽象为求解一个 $[0, x]$ / $[1, x]$ 范围方案数的方法」->「对方案数统计根据 位数 来分情况讨论:数位相等的情况 + 数位不等情况」->「统计数位相等的方案数时,需要按位处理,并根据限制条件做逻辑;统计数位不等的方案数时,通常要做一些预处理,然后配合乘法原理直接算得」。
137+
138+
在还没卷到数位 DP 烂大街的现在,掌握此类求解方式单一,普遍定位为「困难」的数位 DP 类型,还是极具性价比的。
139+
140+
---
141+
132142
### 最后
133143

134144
这是我们「刷穿 LeetCode」系列文章的第 `No.902` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /