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 110e86a

Browse files
feat: add solutions to lc problem: No.0753 (doocs#3939)
No.0753.Cracking the Safe
1 parent 112c723 commit 110e86a

File tree

6 files changed

+100
-7
lines changed

6 files changed

+100
-7
lines changed

‎solution/0700-0799/0753.Cracking the Safe/README.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,32 @@ func crackSafe(n int, k int) string {
189189
}
190190
```
191191

192+
#### TypeScript
193+
194+
```ts
195+
function crackSafe(n: number, k: number): string {
196+
function dfs(u: number): void {
197+
for (let x = 0; x < k; x++) {
198+
const e = u * 10 + x;
199+
if (!vis.has(e)) {
200+
vis.add(e);
201+
const v = e % mod;
202+
dfs(v);
203+
ans.push(x.toString());
204+
}
205+
}
206+
}
207+
208+
const mod = Math.pow(10, n - 1);
209+
const vis = new Set<number>();
210+
const ans: string[] = [];
211+
212+
dfs(0);
213+
ans.push('0'.repeat(n - 1));
214+
return ans.join('');
215+
}
216+
```
217+
192218
<!-- tabs:end -->
193219

194220
<!-- solution:end -->

‎solution/0700-0799/0753.Cracking the Safe/README_EN.md‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,13 @@ Thus &quot;01100&quot; will unlock the safe. &quot;10011&quot;, and &quot;11001&
7676

7777
<!-- solution:start -->
7878

79-
### Solution 1
79+
### Solution 1: Eulerian Circuit
80+
81+
We can construct a directed graph based on the description in the problem: each point is considered as a length $n-1$ $k$-string, and each edge carries a character from 0ドル$ to $k-1$. If there is a directed edge $e$ from point $u$ to point $v,ドル and the character carried by $e$ is $c,ドル then the last $k-1$ characters of $u+c$ form the string $v$. At this point, the edge $u+c$ represents a password of length $n$.
82+
83+
In this directed graph, there are $k^{n-1}$ points, each point has $k$ outgoing edges and $k$ incoming edges. Therefore, this directed graph has an Eulerian circuit, and the path traversed by the Eulerian circuit is the answer to the problem.
84+
85+
The time complexity is $O(k^n),ドル and the space complexity is $O(k^n)$.
8086

8187
<!-- tabs:start -->
8288

@@ -181,6 +187,32 @@ func crackSafe(n int, k int) string {
181187
}
182188
```
183189

190+
#### TypeScript
191+
192+
```ts
193+
function crackSafe(n: number, k: number): string {
194+
function dfs(u: number): void {
195+
for (let x = 0; x < k; x++) {
196+
const e = u * 10 + x;
197+
if (!vis.has(e)) {
198+
vis.add(e);
199+
const v = e % mod;
200+
dfs(v);
201+
ans.push(x.toString());
202+
}
203+
}
204+
}
205+
206+
const mod = Math.pow(10, n - 1);
207+
const vis = new Set<number>();
208+
const ans: string[] = [];
209+
210+
dfs(0);
211+
ans.push('0'.repeat(n - 1));
212+
return ans.join('');
213+
}
214+
```
215+
184216
<!-- tabs:end -->
185217

186218
<!-- solution:end -->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function crackSafe(n: number, k: number): string {
2+
function dfs(u: number): void {
3+
for (let x = 0; x < k; x++) {
4+
const e = u * 10 + x;
5+
if (!vis.has(e)) {
6+
vis.add(e);
7+
const v = e % mod;
8+
dfs(v);
9+
ans.push(x.toString());
10+
}
11+
}
12+
}
13+
14+
const mod = Math.pow(10, n - 1);
15+
const vis = new Set<number>();
16+
const ans: string[] = [];
17+
18+
dfs(0);
19+
ans.push('0'.repeat(n - 1));
20+
return ans.join('');
21+
}

‎solution/0700-0799/0754.Reach a Number/README.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ tags:
6868

6969
### 方法一:数学分析
7070

71-
由于对称性,每次可以选择向左或向右移动,因此,我们可以将 $target$ 统一取绝对值。
71+
由于对称性,每次可以选择向左或向右移动,因此,我们可以将 $\textit{target}$ 统一取绝对值。
7272

7373
定义 $s$ 表示当前所处的位置,用变量 $k$ 记录移动的次数。初始时 $s$ 和 $k$ 均为 0ドル$。
7474

75-
我们将 $s$ 一直循环累加,直到满足 $s\ge target$ 并且 $(s-target)\mod 2 = 0,ドル此时的移动次数 $k$ 就是答案,直接返回。
75+
我们将 $s$ 一直循环累加,直到满足 $s\ge \textit{target}$ 并且 $(s-\textit{target}) \bmod 2 = 0,ドル此时的移动次数 $k$ 就是答案,直接返回。
7676

77-
为什么?因为如果 $s\ge target$ 且 $(s-target)\mod 2 = 0,ドル我们只需要把前面 $\frac{s-target}{2}$ 这个正整数变为负数,就能使得 $s$ 与 $target$ 相等。正整数变负数的过程,实际上是将移动的方向改变,但实际移动次数仍然不变。
77+
为什么?因为如果 $s\ge \textit{target}$ 且 $(s-\textit{target})\mod 2 = 0,ドル我们只需要把前面 $\frac{s-\textit{target}}{2}$ 这个正整数变为负数,就能使得 $s$ 与 $\textit{target}$ 相等。正整数变负数的过程,实际上是将移动的方向改变,但实际移动次数仍然不变。
7878

79-
时间复杂度 $O(\sqrt{\left | target \right | }),ドル空间复杂度 $O(1)$。
79+
时间复杂度 $O(\sqrt{\left | \textit{target} \right | }),ドル空间复杂度 $O(1)$。
8080

8181
<!-- tabs:start -->
8282

‎solution/0700-0799/0754.Reach a Number/README_EN.md‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,17 @@ On the 2<sup>nd</sup> move, we step from 1 to 3 (2 steps).
6464

6565
<!-- solution:start -->
6666

67-
### Solution 1
67+
### Solution 1: Mathematical Analysis
68+
69+
Due to symmetry, each time we can choose to move left or right, so we can take the absolute value of $\textit{target}$.
70+
71+
Define $s$ as the current position, and use the variable $k$ to record the number of moves. Initially, both $s$ and $k$ are 0ドル$.
72+
73+
We keep adding to $s$ in a loop until $s \ge \textit{target}$ and $(s - \textit{target}) \bmod 2 = 0$. At this point, the number of moves $k$ is the answer, and we return it directly.
74+
75+
Why? Because if $s \ge \textit{target}$ and $(s - \textit{target}) \bmod 2 = 0,ドル we only need to change the sign of the positive integer $\frac{s - \textit{target}}{2}$ to negative, so that $s$ equals $\textit{target}$. Changing the sign of a positive integer essentially means changing the direction of the move, but the actual number of moves remains the same.
76+
77+
The time complexity is $O(\sqrt{\left | \textit{target} \right | }),ドル and the space complexity is $O(1)$.
6878

6979
<!-- tabs:start -->
7080

‎solution/0700-0799/0755.Pour Water/README_EN.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ Finally, the fourth droplet falls at index k = 3. Since moving left would not ev
8080

8181
<!-- solution:start -->
8282

83-
### Solution 1
83+
### Solution 1: Simulation
84+
85+
We can simulate the process of each unit of water dropping. Each time a drop falls, we first try to move left. If it can move to a lower height, it moves to the lowest height; if it cannot move to a lower height, we try to move right. If it can move to a lower height, it moves to the lowest height; if it cannot move to a lower height, it rises at the current position.
86+
87+
The time complexity is $O(v \times n),ドル and the space complexity is $O(1),ドル where $v$ and $n$ are the number of water drops and the length of the height array, respectively.
8488

8589
<!-- tabs:start -->
8690

0 commit comments

Comments
(0)

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