From a7450d6ab649f909fb8a9c06f15681e61f756996 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 16 Aug 2023 19:35:26 +0800 Subject: [PATCH] feat: add solutions to lcp problems: No.01~03 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * No.01.猜数字 * No.02.分式化简 * No.03.机器人大冒险 --- .../README.md" | 43 ++++- .../Solution.cpp" | 16 +- .../Solution.go" | 9 +- .../Solution.java" | 18 +- .../Solution.js" | 4 +- .../Solution.py" | 6 +- .../Solution.ts" | 9 + .../README.md" | 98 +++++++--- .../Solution.cpp" | 28 +-- .../Solution.go" | 17 +- .../Solution.java" | 35 ++-- .../Solution.js" | 11 +- .../Solution.py" | 20 +- .../Solution.ts" | 15 ++ .../README.md" | 179 +++++++++++++++++- .../Solution.cpp" | 30 +++ .../Solution.go" | 35 ++++ .../Solution.java" | 29 +++ .../Solution.py" | 21 ++ .../Solution.ts" | 35 ++++ .../README.md | 2 +- .../README_EN.md | 2 +- .../Solution.cpp | 34 ++-- 23 files changed, 579 insertions(+), 117 deletions(-) create mode 100644 "lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.ts" create mode 100644 "lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.ts" create mode 100644 "lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.cpp" create mode 100644 "lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.go" create mode 100644 "lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.java" create mode 100644 "lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.py" create mode 100644 "lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.ts" diff --git "a/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/README.md" "b/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/README.md" index 067b148f09fef..d18069f6c96f1 100644 --- "a/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/README.md" +++ "b/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/README.md" @@ -39,6 +39,12 @@ +**方法一:遍历** + +我们同时遍历两个数组,如果对应位置的元素相等,那么答案加一。 + +时间复杂度 $O(n)$,其中 $n$ 是数组的长度,本题中 $n=3$。空间复杂度 $O(1)$。 + ### **Python3** @@ -48,7 +54,7 @@ ```python class Solution: def game(self, guess: List[int], answer: List[int]) -> int: - return sum(1 for i in range(3) if guess[i] == answer[i]) + return sum(a == b for a, b in zip(guess, answer)) ``` ### **Java** @@ -60,7 +66,9 @@ class Solution { public int game(int[] guess, int[] answer) { int ans = 0; for (int i = 0; i < 3; ++i) { - ans += guess[i] == answer[i] ? 1 : 0; + if (guess[i] == answer[i]) { + ++ans; + } } return ans; } @@ -74,7 +82,9 @@ class Solution { public: int game(vector& guess, vector& answer) { int ans = 0; - for (int i = 0; i < 3; ++i) ans += guess[i] == answer[i]; + for (int i = 0; i < 3; ++i) { + ans += guess[i] == answer[i]; + } return ans; } }; @@ -83,14 +93,27 @@ public: ### **Go** ```go -func game(guess []int, answer []int) int { - ans := 0 - for i := 0; i < 3; i++ { - if guess[i] == answer[i] { +func game(guess []int, answer []int) (ans int) { + for i, a := range guess { + if a == answer[i] { ans++ } } - return ans + return +} +``` + +### **TypeScript** + +```ts +function game(guess: number[], answer: number[]): number { + let ans = 0; + for (let i = 0; i < 3; ++i) { + if (guess[i] === answer[i]) { + ++ans; + } + } + return ans; } ``` @@ -105,7 +128,9 @@ func game(guess []int, answer []int) int { var game = function (guess, answer) { let ans = 0; for (let i = 0; i < 3; ++i) { - ans += guess[i] === answer[i]; + if (guess[i] === answer[i]) { + ++ans; + } } return ans; }; diff --git "a/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.cpp" "b/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.cpp" index 91e6c0785c247..dcf00c84e6402 100644 --- "a/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.cpp" +++ "b/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.cpp" @@ -1,8 +1,10 @@ -class Solution { -public: - int game(vector& guess, vector& answer) { - int ans = 0; - for (int i = 0; i < 3; ++i) ans += guess[i] == answer[i]; - return ans; - } +class Solution { +public: + int game(vector& guess, vector& answer) { + int ans = 0; + for (int i = 0; i < 3; ++i) { + ans += guess[i] == answer[i]; + } + return ans; + } }; \ No newline at end of file diff --git "a/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.go" "b/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.go" index 803c3c38c2a86..1d277e283d040 100644 --- "a/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.go" +++ "b/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.go" @@ -1,9 +1,8 @@ -func game(guess []int, answer []int) int { - ans := 0 - for i := 0; i < 3; i++ { - if guess[i] == answer[i] { +func game(guess []int, answer []int) (ans int) { + for i, a := range guess { + if a == answer[i] { ans++ } } - return ans + return } \ No newline at end of file diff --git "a/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.java" "b/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.java" index 97820cb943248..56b2cf34a3892 100644 --- "a/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.java" +++ "b/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.java" @@ -1,9 +1,11 @@ -class Solution { - public int game(int[] guess, int[] answer) { - int ans = 0; - for (int i = 0; i < 3; ++i) { - ans += guess[i] == answer[i] ? 1 : 0; - } - return ans; - } +class Solution { + public int game(int[] guess, int[] answer) { + int ans = 0; + for (int i = 0; i < 3; ++i) { + if (guess[i] == answer[i]) { + ++ans; + } + } + return ans; + } } \ No newline at end of file diff --git "a/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.js" "b/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.js" index fd026bdfa6d2e..f6f8275494c1f 100644 --- "a/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.js" +++ "b/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.js" @@ -6,7 +6,9 @@ var game = function (guess, answer) { let ans = 0; for (let i = 0; i < 3; ++i) { - ans += guess[i] === answer[i]; + if (guess[i] === answer[i]) { + ++ans; + } } return ans; }; diff --git "a/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.py" "b/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.py" index d62264b680788..c737679c8f8c2 100644 --- "a/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.py" +++ "b/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.py" @@ -1,3 +1,3 @@ -class Solution: - def game(self, guess: List[int], answer: List[int]) -> int: - return sum(1 for i in range(3) if guess[i] == answer[i]) +class Solution: + def game(self, guess: List[int], answer: List[int]) -> int: + return sum(a == b for a, b in zip(guess, answer)) diff --git "a/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.ts" "b/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.ts" new file mode 100644 index 0000000000000..8a9dd50b44625 --- /dev/null +++ "b/lcp/LCP 01. \347\214\234\346\225\260\345\255\227/Solution.ts" @@ -0,0 +1,9 @@ +function game(guess: number[], answer: number[]): number { + let ans = 0; + for (let i = 0; i < 3; ++i) { + if (guess[i] === answer[i]) { + ++ans; + } + } + return ans; +} diff --git "a/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/README.md" "b/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/README.md" index a8823d0e76728..45dd820b0d381 100644 --- "a/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/README.md" +++ "b/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/README.md" @@ -45,6 +45,18 @@ +**方法一:DFS + 数学** + +我们设计一个函数 $dfs(i)$,表示从下标 $i$ 开始到最后一个元素的连分数的值,那么答案就是 $dfs(0)$。 + +函数 $dfs(i)$ 的执行逻辑如下: + +如果 $i = n - 1$,只有一个元素,那么它的值就是 $cont[i]$,分母为 $1$,返回 $[cont[i], 1]$。 + +否则,我们递归调用 $dfs(i + 1)$,记返回值为 $[a, b]$,那么 $dfs(i)= 1 + \frac{1}{\frac{a}{b}}$,即 $dfs(i) = \frac{a \times cont[i] + b}{a}$,分子为 $x = a \times cont[i] + b$,分母为 $y = a$,我们求出 $x$ 和 $y$ 的最大公约数 $g$,最终返回 $[\frac{x}{g}, \frac{y}{g}]$。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 $cont$ 的长度。 + ### **Python3** @@ -54,13 +66,15 @@ ```python class Solution: def fraction(self, cont: List[int]) -> List[int]: - def dfs(cont): - if len(cont) == 1: - return [cont[0], 1] - a, b = dfs(cont[1:]) - return [a * cont[0] + b, a] - - return dfs(cont) + def dfs(i: int) -> List[int]: + if i == len(cont) - 1: + return [cont[i], 1] + a, b = dfs(i + 1) + x, y = a * cont[i] + b, a + g = gcd(x, y) + return [x // g, y // g] + + return dfs(0) ``` ### **Java** @@ -69,17 +83,26 @@ class Solution: ```java class Solution { + private int[] cont; + public int[] fraction(int[] cont) { - return dfs(cont, 0); + this.cont = cont; + return dfs(0); } - private int[] dfs(int[] cont, int i) { + private int[] dfs(int i) { if (i == cont.length - 1) { return new int[] {cont[i], 1}; } - int[] ans = dfs(cont, i + 1); - int a = ans[0], b = ans[1]; - return new int[] {a * cont[i] + b, a}; + int[] next = dfs(i + 1); + int a = next[0], b = next[1]; + int x = a * cont[i] + b, y = a; + int g = gcd(x, y); + return new int[] {x / g, y / g}; + } + + private int gcd(int a, int b) { + return b == 0 ? a : gcd(b, a % b); } } ``` @@ -90,14 +113,18 @@ class Solution { class Solution { public: vector fraction(vector& cont) { - return dfs(cont, 0); - } - - vector dfs(vector& cont, int i) { - if (i == cont.size() - 1) return {cont[i], 1}; - vector ans = dfs(cont, i + 1); - int a = ans[0], b = ans[1]; - return {a * cont[i] + b, a}; + function(int)> dfs = [&](int i) { + if (i == cont.size() - 1) { + return vector{cont[i], 1}; + } + vector next = dfs(i + 1); + int a = next[0], b = next[1]; + int x = a * cont[i] + b; + int y = a; + int g = __gcd(x, y); + return vector{x / g, y / g}; + }; + return dfs(0); } }; ``` @@ -119,6 +146,26 @@ func fraction(cont []int) []int { } ``` +### **TypeScript** + +```ts +function fraction(cont: number[]): number[] { + const dfs = (i: number): number[] => { + if (i === cont.length - 1) { + return [cont[i], 1]; + } + const [a, b] = dfs(i + 1); + const [x, y] = [a * cont[i] + b, a]; + const g = gcd(x, y); + return [x / g, y / g]; + }; + const gcd = (a: number, b: number): number => { + return b === 0 ? a : gcd(b, a % b); + }; + return dfs(0); +} +``` + ### **JavaScript** ```js @@ -127,13 +174,18 @@ func fraction(cont []int) []int { * @return {number[]} */ var fraction = function (cont) { - function dfs(i) { + const dfs = i => { if (i === cont.length - 1) { return [cont[i], 1]; } const [a, b] = dfs(i + 1); - return [a * cont[i] + b, a]; - } + const [x, y] = [a * cont[i] + b, a]; + const g = gcd(x, y); + return [Math.floor(x / g), Math.floor(y / g)]; + }; + const gcd = (a, b) => { + return b === 0 ? a : gcd(b, a % b); + }; return dfs(0); }; ``` diff --git "a/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.cpp" "b/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.cpp" index 6e79e166dec69..b4cae3afa1d4b 100644 --- "a/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.cpp" +++ "b/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.cpp" @@ -1,13 +1,17 @@ -class Solution { -public: - vector fraction(vector& cont) { - return dfs(cont, 0); - } - - vector dfs(vector& cont, int i) { - if (i == cont.size() - 1) return {cont[i], 1}; - vector ans = dfs(cont, i + 1); - int a = ans[0], b = ans[1]; - return {a * cont[i] + b, a}; - } +class Solution { +public: + vector fraction(vector& cont) { + function(int)> dfs = [&](int i) { + if (i == cont.size() - 1) { + return vector{cont[i], 1}; + } + vector next = dfs(i + 1); + int a = next[0], b = next[1]; + int x = a * cont[i] + b; + int y = a; + int g = __gcd(x, y); + return vector{x / g, y / g}; + }; + return dfs(0); + } }; \ No newline at end of file diff --git "a/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.go" "b/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.go" index 34a0fa07288c0..be51efda2bc31 100644 --- "a/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.go" +++ "b/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.go" @@ -1,12 +1,21 @@ func fraction(cont []int) []int { - var dfs func(i int) []int + var dfs func(int) []int dfs = func(i int) []int { if i == len(cont)-1 { return []int{cont[i], 1} } - ans := dfs(i + 1) - a, b := ans[0], ans[1] - return []int{a*cont[i] + b, a} + next := dfs(i + 1) + a, b := next[0], next[1] + x, y := a*cont[i]+b, a + g := gcd(x, y) + return []int{x / g, y / g} } return dfs(0) +} + +func gcd(a, b int) int { + if b == 0 { + return a + } + return gcd(b, a%b) } \ No newline at end of file diff --git "a/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.java" "b/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.java" index dfead94bf2818..3c17f0fc0e908 100644 --- "a/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.java" +++ "b/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.java" @@ -1,14 +1,23 @@ -class Solution { - public int[] fraction(int[] cont) { - return dfs(cont, 0); - } - - private int[] dfs(int[] cont, int i) { - if (i == cont.length - 1) { - return new int[] {cont[i], 1}; - } - int[] ans = dfs(cont, i + 1); - int a = ans[0], b = ans[1]; - return new int[] {a * cont[i] + b, a}; - } +class Solution { + private int[] cont; + + public int[] fraction(int[] cont) { + this.cont = cont; + return dfs(0); + } + + private int[] dfs(int i) { + if (i == cont.length - 1) { + return new int[] {cont[i], 1}; + } + int[] next = dfs(i + 1); + int a = next[0], b = next[1]; + int x = a * cont[i] + b, y = a; + int g = gcd(x, y); + return new int[] {x / g, y / g}; + } + + private int gcd(int a, int b) { + return b == 0 ? a : gcd(b, a % b); + } } \ No newline at end of file diff --git "a/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.js" "b/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.js" index bd9a7ee86b9bd..62e073bdb4d2b 100644 --- "a/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.js" +++ "b/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.js" @@ -3,12 +3,17 @@ * @return {number[]} */ var fraction = function (cont) { - function dfs(i) { + const dfs = i => { if (i === cont.length - 1) { return [cont[i], 1]; } const [a, b] = dfs(i + 1); - return [a * cont[i] + b, a]; - } + const [x, y] = [a * cont[i] + b, a]; + const g = gcd(x, y); + return [Math.floor(x / g), Math.floor(y / g)]; + }; + const gcd = (a, b) => { + return b === 0 ? a : gcd(b, a % b); + }; return dfs(0); }; diff --git "a/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.py" "b/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.py" index df8070fd81257..1aaf142565c4e 100644 --- "a/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.py" +++ "b/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.py" @@ -1,9 +1,11 @@ -class Solution: - def fraction(self, cont: List[int]) -> List[int]: - def dfs(cont): - if len(cont) == 1: - return [cont[0], 1] - a, b = dfs(cont[1:]) - return [a * cont[0] + b, a] - - return dfs(cont) +class Solution: + def fraction(self, cont: List[int]) -> List[int]: + def dfs(i: int) -> List[int]: + if i == len(cont) - 1: + return [cont[i], 1] + a, b = dfs(i + 1) + x, y = a * cont[i] + b, a + g = gcd(x, y) + return [x // g, y // g] + + return dfs(0) diff --git "a/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.ts" "b/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.ts" new file mode 100644 index 0000000000000..7e8c2effb418f --- /dev/null +++ "b/lcp/LCP 02. \345\210\206\345\274\217\345\214\226\347\256\200/Solution.ts" @@ -0,0 +1,15 @@ +function fraction(cont: number[]): number[] { + const dfs = (i: number): number[] => { + if (i === cont.length - 1) { + return [cont[i], 1]; + } + const [a, b] = dfs(i + 1); + const [x, y] = [a * cont[i] + b, a]; + const g = gcd(x, y); + return [x / g, y / g]; + }; + const gcd = (a: number, b: number): number => { + return b === 0 ? a : gcd(b, a % b); + }; + return dfs(0); +} diff --git "a/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/README.md" "b/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/README.md" index d5bdcf8215b4d..11bc41d7c660a 100644 --- "a/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/README.md" +++ "b/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/README.md" @@ -51,6 +51,20 @@ +**方法一:哈希表** + +我们用哈希表 $vis$ 记录机器人在一轮指令执行完毕后所能到达的所有位置。初始时,机器人位于原点 $(0, 0)$,因此 $vis$ 中只包含一个元素 $(0, 0)$。随后我们遍历指令 $command$ 中的每个字符 $c$,更新机器人的位置,加入哈希表 $vis$ 中。记第一轮执行完毕后,机器人所在的位置为 $(i, j)$。 + +如果机器人重复执行多轮指令,那么实际上机器人的位置是在 $vis$ 中的所有位置的线性组合。我们将 $(x, y)$ 分别减去 $k$ 倍 $(i, j)$ 的偏移量,其中 $k = \min(\lfloor x / i \rfloor, \lfloor y / j \rfloor)$,如果 $(x, y)$ 不在 $vis$ 中, 说明机器人不可能到达 $(x, y)$,返回 `false`。 + +接下来,我们只需要判断机器人有没有经过障碍点即可。 + +我们遍历所有障碍点 $(a, b)$,如果 $a \gt x$ 或者 $b \gt y$,说明机器人不会经过该障碍点,跳过即可。否则,我们将 $(x, y)$ 分别减去 $k$ 倍 $(i, j)$ 的偏移量,其中 $k = \min(\lfloor a / i \rfloor, \lfloor b / j \rfloor)$,如果 $(a, b)$ 在 $vis$ 中,说明机器人会经过该障碍点,返回 `false`。 + +否则,遍历结束后,返回 `true`。 + +时间复杂度 $O(m + n)$,空间复杂度 $O(m)$。其中 $m$ 和 $n$ 分别是指令 $command$ 和障碍数组 $obstacles$ 的长度。 + ### **Python3** @@ -58,7 +72,27 @@ ```python - +class Solution: + def robot(self, command: str, obstacles: List[List[int]], x: int, y: int) -> bool: + vis = {(0, 0)} + i = j = 0 + for c in command: + match c: + case "U": + j += 1 + case "R": + i += 1 + vis.add((i, j)) + k = min(x // i, y // j) + if (x - k * i, y - k * j) not in vis: + return False + for a, b in obstacles: + if a > x or b > y: + continue + k = min(a // i, b // j) + if (a - k * i, b - k * j) in vis: + return False + return True ``` ### **Java** @@ -66,7 +100,150 @@ ```java +class Solution { + public boolean robot(String command, int[][] obstacles, int x, int y) { + Set> vis = new HashSet<>(); + int i = 0, j = 0; + vis.add(List.of(i, j)); + for (char c : command.toCharArray()) { + if (c == 'U') { + ++j; + } else { + ++i; + } + vis.add(List.of(i, j)); + } + int k = Math.min(x / i, y / j); + if (!vis.contains(List.of(x - k * i, y - k * j))) { + return false; + } + for (int[] ob : obstacles) { + if (ob[0] > x || ob[1] > y) { + continue; + } + k = Math.min(ob[0] / i, ob[1] / j); + if (vis.contains(List.of(ob[0] - k * i, ob[1] - k * j))) { + return false; + } + } + return true; + } +} +``` + +### **C++** + +```cpp +class Solution { +public: + bool robot(string command, vector>& obstacles, int x, int y) { + set> vis; + int i = 0, j = 0; + vis.insert({i, j}); + for (char c : command) { + if (c == 'U') { + ++j; + } else { + ++i; + } + vis.insert({i, j}); + } + int k = min(x / i, y / j); + if (!vis.count({x - k * i, y - k * j})) { + return false; + } + for (auto& ob : obstacles) { + if (ob[0] > x || ob[1] > y) { + continue; + } + k = min(ob[0] / i, ob[1] / j); + if (vis.count({ob[0] - k * i, ob[1] - k * j})) { + return false; + } + } + return true; + } +}; +``` + +### **Go** + +```go +func robot(command string, obstacles [][]int, x int, y int) bool { + type pair struct{ i, j int } + vis := map[pair]bool{} + i, j := 0, 0 + vis[pair{0, 0}] = true + for _, c := range command { + if c == 'U' { + j++ + } else { + i++ + } + vis[pair{i, j}] = true + } + k := min(x/i, y/j) + if !vis[pair{x - k*i, y - k*j}] { + return false + } + for _, ob := range obstacles { + if ob[0] > x || ob[1] > y { + continue + } + k := min(ob[0]/i, ob[1]/j) + if vis[pair{ob[0] - k*i, ob[1] - k*j}] { + return false + } + } + return true +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} +``` +### **TypeScript** + +```ts +function robot( + command: string, + obstacles: number[][], + x: number, + y: number, +): boolean { + const f = (i: number, j: number): number => { + return i * (10 ** 9 + 1) + j; + }; + const vis: Set = new Set(); + vis.add(f(0, 0)); + let [i, j] = [0, 0]; + for (const c of command) { + if (c === 'U') { + ++j; + } else { + ++i; + } + vis.add(f(i, j)); + } + const k = Math.min(Math.floor(x / i), Math.floor(y / j)); + if (!vis.has(f(x - k * i, y - k * j))) { + return false; + } + for (const [a, b] of obstacles) { + if (a > x || b > y) { + continue; + } + const k = Math.min(Math.floor(a / i), Math.floor(b / j)); + if (vis.has(f(a - k * i, b - k * j))) { + return false; + } + } + return true; +} ``` ### **...** diff --git "a/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.cpp" "b/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.cpp" new file mode 100644 index 0000000000000..eae04fdb9a7e2 --- /dev/null +++ "b/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.cpp" @@ -0,0 +1,30 @@ +class Solution { +public: + bool robot(string command, vector>& obstacles, int x, int y) { + set> vis; + int i = 0, j = 0; + vis.insert({i, j}); + for (char c : command) { + if (c == 'U') { + ++j; + } else { + ++i; + } + vis.insert({i, j}); + } + int k = min(x / i, y / j); + if (!vis.count({x - k * i, y - k * j})) { + return false; + } + for (auto& ob : obstacles) { + if (ob[0] > x || ob[1] > y) { + continue; + } + k = min(ob[0] / i, ob[1] / j); + if (vis.count({ob[0] - k * i, ob[1] - k * j})) { + return false; + } + } + return true; + } +}; \ No newline at end of file diff --git "a/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.go" "b/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.go" new file mode 100644 index 0000000000000..f68afbb2205ab --- /dev/null +++ "b/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.go" @@ -0,0 +1,35 @@ +func robot(command string, obstacles [][]int, x int, y int) bool { + type pair struct{ i, j int } + vis := map[pair]bool{} + i, j := 0, 0 + vis[pair{0, 0}] = true + for _, c := range command { + if c == 'U' { + j++ + } else { + i++ + } + vis[pair{i, j}] = true + } + k := min(x/i, y/j) + if !vis[pair{x - k*i, y - k*j}] { + return false + } + for _, ob := range obstacles { + if ob[0] > x || ob[1] > y { + continue + } + k := min(ob[0]/i, ob[1]/j) + if vis[pair{ob[0] - k*i, ob[1] - k*j}] { + return false + } + } + return true +} + +func min(a, b int) int { + if a < b { + return a + } + return b +} \ No newline at end of file diff --git "a/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.java" "b/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.java" new file mode 100644 index 0000000000000..fadd7ed076b7d --- /dev/null +++ "b/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.java" @@ -0,0 +1,29 @@ +class Solution { + public boolean robot(String command, int[][] obstacles, int x, int y) { + Set> vis = new HashSet<>(); + int i = 0, j = 0; + vis.add(List.of(i, j)); + for (char c : command.toCharArray()) { + if (c == 'U') { + ++j; + } else { + ++i; + } + vis.add(List.of(i, j)); + } + int k = Math.min(x / i, y / j); + if (!vis.contains(List.of(x - k * i, y - k * j))) { + return false; + } + for (int[] ob : obstacles) { + if (ob[0] > x || ob[1] > y) { + continue; + } + k = Math.min(ob[0] / i, ob[1] / j); + if (vis.contains(List.of(ob[0] - k * i, ob[1] - k * j))) { + return false; + } + } + return true; + } +} \ No newline at end of file diff --git "a/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.py" "b/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.py" new file mode 100644 index 0000000000000..52c35abcea86f --- /dev/null +++ "b/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.py" @@ -0,0 +1,21 @@ +class Solution: + def robot(self, command: str, obstacles: List[List[int]], x: int, y: int) -> bool: + vis = {(0, 0)} + i = j = 0 + for c in command: + match c: + case "U": + j += 1 + case "R": + i += 1 + vis.add((i, j)) + k = min(x // i, y // j) + if (x - k * i, y - k * j) not in vis: + return False + for a, b in obstacles: + if a > x or b > y: + continue + k = min(a // i, b // j) + if (a - k * i, b - k * j) in vis: + return False + return True diff --git "a/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.ts" "b/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.ts" new file mode 100644 index 0000000000000..ea1db31793722 --- /dev/null +++ "b/lcp/LCP 03. \346\234\272\345\231\250\344\272\272\345\244\247\345\206\222\351\231\251/Solution.ts" @@ -0,0 +1,35 @@ +function robot( + command: string, + obstacles: number[][], + x: number, + y: number, +): boolean { + const f = (i: number, j: number): number => { + return i * (10 ** 9 + 1) + j; + }; + const vis: Set = new Set(); + vis.add(f(0, 0)); + let [i, j] = [0, 0]; + for (const c of command) { + if (c === 'U') { + ++j; + } else { + ++i; + } + vis.add(f(i, j)); + } + const k = Math.min(Math.floor(x / i), Math.floor(y / j)); + if (!vis.has(f(x - k * i, y - k * j))) { + return false; + } + for (const [a, b] of obstacles) { + if (a > x || b > y) { + continue; + } + const k = Math.min(Math.floor(a / i), Math.floor(b / j)); + if (vis.has(f(a - k * i, b - k * j))) { + return false; + } + } + return true; +} diff --git a/solution/2600-2699/2682.Find the Losers of the Circular Game/README.md b/solution/2600-2699/2682.Find the Losers of the Circular Game/README.md index 3e4ad3edbcb71..deeb1c5ac70cd 100644 --- a/solution/2600-2699/2682.Find the Losers of the Circular Game/README.md +++ b/solution/2600-2699/2682.Find the Losers of the Circular Game/README.md @@ -127,7 +127,7 @@ public: i = (i + p * k) % n; } vector ans; - for (int i = 0, j = 0; i < n; ++i) { + for (int i = 0; i < n; ++i) { if (!vis[i]) { ans.push_back(i + 1); } diff --git a/solution/2600-2699/2682.Find the Losers of the Circular Game/README_EN.md b/solution/2600-2699/2682.Find the Losers of the Circular Game/README_EN.md index 9a8831be2e97a..c7e36b7babe5b 100644 --- a/solution/2600-2699/2682.Find the Losers of the Circular Game/README_EN.md +++ b/solution/2600-2699/2682.Find the Losers of the Circular Game/README_EN.md @@ -108,7 +108,7 @@ public: i = (i + p * k) % n; } vector ans; - for (int i = 0, j = 0; i < n; ++i) { + for (int i = 0; i < n; ++i) { if (!vis[i]) { ans.push_back(i + 1); } diff --git a/solution/2600-2699/2682.Find the Losers of the Circular Game/Solution.cpp b/solution/2600-2699/2682.Find the Losers of the Circular Game/Solution.cpp index b211b85060be1..aeb1c2649629e 100644 --- a/solution/2600-2699/2682.Find the Losers of the Circular Game/Solution.cpp +++ b/solution/2600-2699/2682.Find the Losers of the Circular Game/Solution.cpp @@ -1,18 +1,18 @@ -class Solution { -public: - vector circularGameLosers(int n, int k) { - bool vis[n]; - memset(vis, false, sizeof(vis)); - for (int i = 0, p = 1; !vis[i]; ++p) { - vis[i] = true; - i = (i + p * k) % n; - } - vector ans; - for (int i = 0, j = 0; i < n; ++i) { - if (!vis[i]) { - ans.push_back(i + 1); - } - } - return ans; - } +class Solution { +public: + vector circularGameLosers(int n, int k) { + bool vis[n]; + memset(vis, false, sizeof(vis)); + for (int i = 0, p = 1; !vis[i]; ++p) { + vis[i] = true; + i = (i + p * k) % n; + } + vector ans; + for (int i = 0; i < n; ++i) { + if (!vis[i]) { + ans.push_back(i + 1); + } + } + return ans; + } }; \ No newline at end of file