diff --git a/solution/0700-0799/0753.Cracking the Safe/README.md b/solution/0700-0799/0753.Cracking the Safe/README.md index bcbac1c0841b2..53ce315a7410e 100644 --- a/solution/0700-0799/0753.Cracking the Safe/README.md +++ b/solution/0700-0799/0753.Cracking the Safe/README.md @@ -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(); + const ans: string[] = []; + + dfs(0); + ans.push('0'.repeat(n - 1)); + return ans.join(''); +} +``` + diff --git a/solution/0700-0799/0753.Cracking the Safe/README_EN.md b/solution/0700-0799/0753.Cracking the Safe/README_EN.md index b5a4511d44ef4..2cbc024c3ea89 100644 --- a/solution/0700-0799/0753.Cracking the Safe/README_EN.md +++ b/solution/0700-0799/0753.Cracking the Safe/README_EN.md @@ -76,7 +76,13 @@ Thus "01100" will unlock the safe. "10011", and "11001& -### 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)$. @@ -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(); + const ans: string[] = []; + + dfs(0); + ans.push('0'.repeat(n - 1)); + return ans.join(''); +} +``` + diff --git a/solution/0700-0799/0753.Cracking the Safe/Solution.ts b/solution/0700-0799/0753.Cracking the Safe/Solution.ts new file mode 100644 index 0000000000000..2d7f3214c6db0 --- /dev/null +++ b/solution/0700-0799/0753.Cracking the Safe/Solution.ts @@ -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(); + const ans: string[] = []; + + dfs(0); + ans.push('0'.repeat(n - 1)); + return ans.join(''); +} diff --git a/solution/0700-0799/0754.Reach a Number/README.md b/solution/0700-0799/0754.Reach a Number/README.md index b274110d34683..4680c8600a357 100644 --- a/solution/0700-0799/0754.Reach a Number/README.md +++ b/solution/0700-0799/0754.Reach a Number/README.md @@ -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)$。 diff --git a/solution/0700-0799/0754.Reach a Number/README_EN.md b/solution/0700-0799/0754.Reach a Number/README_EN.md index cf6ec0472a6ac..2d8c341239e78 100644 --- a/solution/0700-0799/0754.Reach a Number/README_EN.md +++ b/solution/0700-0799/0754.Reach a Number/README_EN.md @@ -64,7 +64,17 @@ On the 2nd move, we step from 1 to 3 (2 steps). -### 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)$. diff --git a/solution/0700-0799/0755.Pour Water/README_EN.md b/solution/0700-0799/0755.Pour Water/README_EN.md index 83b1a995f6135..3f4b1a356d75a 100644 --- a/solution/0700-0799/0755.Pour Water/README_EN.md +++ b/solution/0700-0799/0755.Pour Water/README_EN.md @@ -80,7 +80,11 @@ Finally, the fourth droplet falls at index k = 3. Since moving left would not ev -### 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.