Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0753
Browse files Browse the repository at this point in the history
No.0753.Cracking the Safe
  • Loading branch information
yanglbme committed Jan 9, 2025
1 parent 112c723 commit 69f20da
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 7 deletions.
26 changes: 26 additions & 0 deletions solution/0700-0799/0753.Cracking the Safe/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,32 @@ func crackSafe(n int, k int) string {
}
```

#### TypeScript

```ts
function crackSafe(n: number, k: number): string {
function dfs(u: number): void {
for (let x = 0; x < k; x++) {
const e = u * 10 + x;
if (!vis.has(e)) {
vis.add(e);
const v = e % mod;
dfs(v);
ans.push(x.toString());
}
}
}

const mod = Math.pow(10, n - 1);
const vis = new Set<number>();
const ans: string[] = [];

dfs(0);
ans.push('0'.repeat(n - 1));
return ans.join('');
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
34 changes: 33 additions & 1 deletion solution/0700-0799/0753.Cracking the Safe/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ Thus &quot;01100&quot; will unlock the safe. &quot;10011&quot;, and &quot;11001&

<!-- solution:start -->

### Solution 1
### Solution 1: Eulerian Circuit

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$.

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.

The time complexity is $O(k^n)$, and the space complexity is $O(k^n)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -181,6 +187,32 @@ func crackSafe(n int, k int) string {
}
```

#### TypeScript

```ts
function crackSafe(n: number, k: number): string {
function dfs(u: number): void {
for (let x = 0; x < k; x++) {
const e = u * 10 + x;
if (!vis.has(e)) {
vis.add(e);
const v = e % mod;
dfs(v);
ans.push(x.toString());
}
}
}

const mod = Math.pow(10, n - 1);
const vis = new Set<number>();
const ans: string[] = [];

dfs(0);
ans.push('0'.repeat(n - 1));
return ans.join('');
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
21 changes: 21 additions & 0 deletions solution/0700-0799/0753.Cracking the Safe/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function crackSafe(n: number, k: number): string {
function dfs(u: number): void {
for (let x = 0; x < k; x++) {
const e = u * 10 + x;
if (!vis.has(e)) {
vis.add(e);
const v = e % mod;
dfs(v);
ans.push(x.toString());
}
}
}

const mod = Math.pow(10, n - 1);
const vis = new Set<number>();
const ans: string[] = [];

dfs(0);
ans.push('0'.repeat(n - 1));
return ans.join('');
}
8 changes: 4 additions & 4 deletions solution/0700-0799/0754.Reach a Number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ tags:

### 方法一:数学分析

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

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

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

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

时间复杂度 $O(\sqrt{\left | target \right | })$,空间复杂度 $O(1)$。
时间复杂度 $O(\sqrt{\left | \textit{target} \right | })$,空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down
12 changes: 11 additions & 1 deletion solution/0700-0799/0754.Reach a Number/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,17 @@ On the 2<sup>nd</sup> move, we step from 1 to 3 (2 steps).

<!-- solution:start -->

### Solution 1
### Solution 1: Mathematical Analysis

Due to symmetry, each time we can choose to move left or right, so we can take the absolute value of $\textit{target}$.

Define $s$ as the current position, and use the variable $k$ to record the number of moves. Initially, both $s$ and $k$ are $0$.

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.

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.

The time complexity is $O(\sqrt{\left | \textit{target} \right | })$, and the space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down
6 changes: 5 additions & 1 deletion solution/0700-0799/0755.Pour Water/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ Finally, the fourth droplet falls at index k = 3. Since moving left would not ev

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

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.

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.

<!-- tabs:start -->

Expand Down

0 comments on commit 69f20da

Please sign in to comment.