diff --git a/solution/1000-1099/1032.Stream of Characters/README.md b/solution/1000-1099/1032.Stream of Characters/README.md index 46c72c20a9b88..e21deb5ead56b 100644 --- a/solution/1000-1099/1032.Stream of Characters/README.md +++ b/solution/1000-1099/1032.Stream of Characters/README.md @@ -147,7 +147,7 @@ class Trie { public boolean query(StringBuilder s) { Trie node = this; - for (int i = s.length() - 1, j = 0; i >= 0 && j < 201; --i, ++j) { + for (int i = s.length() - 1; i >= 0; --i) { int idx = s.charAt(i) - 'a'; if (node.children[idx] == null) { return false; @@ -211,7 +211,7 @@ public: bool search(string& w) { Trie* node = this; - for (int i = w.size() - 1, j = 0; ~i && j < 201; --i, ++j) { + for (int i = w.size() - 1; ~i; --i) { int idx = w[i] - 'a'; if (!node->children[idx]) { return false; @@ -231,7 +231,7 @@ public: string s; StreamChecker(vector& words) { - for (auto& w : words) { + for (auto&& w : words) { trie->insert(w); } } @@ -275,7 +275,7 @@ func (this *Trie) Insert(word string) { func (this *Trie) Search(word []byte) bool { node := this - for i, j := len(word)-1, 0; i >= 0 && j < 201; i, j = i-1, j+1 { + for i := len(word) - 1; i >= 0; i-- { idx := word[i] - 'a' if node.children[idx] == nil { return false diff --git a/solution/1000-1099/1032.Stream of Characters/README_EN.md b/solution/1000-1099/1032.Stream of Characters/README_EN.md index a671f1bc333c0..a47ca4eb8cbc0 100644 --- a/solution/1000-1099/1032.Stream of Characters/README_EN.md +++ b/solution/1000-1099/1032.Stream of Characters/README_EN.md @@ -124,7 +124,7 @@ class Trie { public boolean query(StringBuilder s) { Trie node = this; - for (int i = s.length() - 1, j = 0; i >= 0 && j < 201; --i, ++j) { + for (int i = s.length() - 1; i >= 0; --i) { int idx = s.charAt(i) - 'a'; if (node.children[idx] == null) { return false; @@ -188,7 +188,7 @@ public: bool search(string& w) { Trie* node = this; - for (int i = w.size() - 1, j = 0; ~i && j < 201; --i, ++j) { + for (int i = w.size() - 1; ~i; --i) { int idx = w[i] - 'a'; if (!node->children[idx]) { return false; @@ -208,7 +208,7 @@ public: string s; StreamChecker(vector& words) { - for (auto& w : words) { + for (auto&& w : words) { trie->insert(w); } } @@ -252,7 +252,7 @@ func (this *Trie) Insert(word string) { func (this *Trie) Search(word []byte) bool { node := this - for i, j := len(word)-1, 0; i >= 0 && j < 201; i, j = i-1, j+1 { + for i := len(word) - 1; i >= 0; i-- { idx := word[i] - 'a' if node.children[idx] == nil { return false diff --git a/solution/1000-1099/1032.Stream of Characters/Solution.cpp b/solution/1000-1099/1032.Stream of Characters/Solution.cpp index 2bcd06bd18cb4..e6eb258dab88b 100644 --- a/solution/1000-1099/1032.Stream of Characters/Solution.cpp +++ b/solution/1000-1099/1032.Stream of Characters/Solution.cpp @@ -22,7 +22,7 @@ class Trie { bool search(string& w) { Trie* node = this; - for (int i = w.size() - 1, j = 0; ~i && j < 201; --i, ++j) { + for (int i = w.size() - 1; ~i; --i) { int idx = w[i] - 'a'; if (!node->children[idx]) { return false; @@ -42,7 +42,7 @@ class StreamChecker { string s; StreamChecker(vector& words) { - for (auto& w : words) { + for (auto&& w : words) { trie->insert(w); } } diff --git a/solution/1000-1099/1032.Stream of Characters/Solution.go b/solution/1000-1099/1032.Stream of Characters/Solution.go index c8bf6ba865744..1e1b070dd7e05 100644 --- a/solution/1000-1099/1032.Stream of Characters/Solution.go +++ b/solution/1000-1099/1032.Stream of Characters/Solution.go @@ -21,7 +21,7 @@ func (this *Trie) Insert(word string) { func (this *Trie) Search(word []byte) bool { node := this - for i, j := len(word)-1, 0; i >= 0 && j < 201; i, j = i-1, j+1 { + for i := len(word) - 1; i >= 0; i-- { idx := word[i] - 'a' if node.children[idx] == nil { return false diff --git a/solution/1000-1099/1032.Stream of Characters/Solution.java b/solution/1000-1099/1032.Stream of Characters/Solution.java index 66db1d3c0ad49..b5443dc6be904 100644 --- a/solution/1000-1099/1032.Stream of Characters/Solution.java +++ b/solution/1000-1099/1032.Stream of Characters/Solution.java @@ -16,7 +16,7 @@ public void insert(String w) { public boolean query(StringBuilder s) { Trie node = this; - for (int i = s.length() - 1, j = 0; i >= 0 && j < 201; --i, ++j) { + for (int i = s.length() - 1; i >= 0; --i) { int idx = s.charAt(i) - 'a'; if (node.children[idx] == null) { return false; diff --git a/solution/1000-1099/1032.Stream of Characters/Solution.py b/solution/1000-1099/1032.Stream of Characters/Solution.py index ef978585efaf1..0b0e741f6cace 100644 --- a/solution/1000-1099/1032.Stream of Characters/Solution.py +++ b/solution/1000-1099/1032.Stream of Characters/Solution.py @@ -25,7 +25,6 @@ def search(self, w: List[str]) -> bool: class StreamChecker: - def __init__(self, words: List[str]): self.trie = Trie() self.cs = [] @@ -35,7 +34,8 @@ def __init__(self, words: List[str]): def query(self, letter: str) -> bool: self.cs.append(letter) - return self.trie.search(self.cs[-self.limit:]) + return self.trie.search(self.cs[-self.limit :]) + # Your StreamChecker object will be instantiated and called as such: # obj = StreamChecker(words) diff --git a/solution/1000-1099/1034.Coloring A Border/README.md b/solution/1000-1099/1034.Coloring A Border/README.md index 25cb30bb0a7f6..6edf86b03d626 100644 --- a/solution/1000-1099/1034.Coloring A Border/README.md +++ b/solution/1000-1099/1034.Coloring A Border/README.md @@ -210,6 +210,42 @@ func colorBorder(grid [][]int, row int, col int, color int) [][]int { } ``` +### **TypeScript** + +```ts +function colorBorder( + grid: number[][], + row: number, + col: number, + color: number, +): number[][] { + const m = grid.length; + const n = grid[0].length; + const vis = new Array(m).fill(0).map(() => new Array(n).fill(false)); + const dirs = [-1, 0, 1, 0, -1]; + const dfs = (i: number, j: number, c: number) => { + vis[i][j] = true; + for (let k = 0; k < 4; ++k) { + const x = i + dirs[k]; + const y = j + dirs[k + 1]; + if (x >= 0 && x < m && y >= 0 && y < n) { + if (!vis[x][y]) { + if (grid[x][y] == c) { + dfs(x, y, c); + } else { + grid[i][j] = color; + } + } + } else { + grid[i][j] = color; + } + } + }; + dfs(row, col, grid[row][col]); + return grid; +} +``` + ### **...** ``` diff --git a/solution/1000-1099/1034.Coloring A Border/README_EN.md b/solution/1000-1099/1034.Coloring A Border/README_EN.md index a1edb629d8f3e..546d0d918d88d 100644 --- a/solution/1000-1099/1034.Coloring A Border/README_EN.md +++ b/solution/1000-1099/1034.Coloring A Border/README_EN.md @@ -176,6 +176,42 @@ func colorBorder(grid [][]int, row int, col int, color int) [][]int { } ``` +### **TypeScript** + +```ts +function colorBorder( + grid: number[][], + row: number, + col: number, + color: number, +): number[][] { + const m = grid.length; + const n = grid[0].length; + const vis = new Array(m).fill(0).map(() => new Array(n).fill(false)); + const dirs = [-1, 0, 1, 0, -1]; + const dfs = (i: number, j: number, c: number) => { + vis[i][j] = true; + for (let k = 0; k < 4; ++k) { + const x = i + dirs[k]; + const y = j + dirs[k + 1]; + if (x >= 0 && x < m && y >= 0 && y < n) { + if (!vis[x][y]) { + if (grid[x][y] == c) { + dfs(x, y, c); + } else { + grid[i][j] = color; + } + } + } else { + grid[i][j] = color; + } + } + }; + dfs(row, col, grid[row][col]); + return grid; +} +``` + ### **...** ``` diff --git a/solution/1000-1099/1034.Coloring A Border/Solution.ts b/solution/1000-1099/1034.Coloring A Border/Solution.ts new file mode 100644 index 0000000000000..539e5b05125c6 --- /dev/null +++ b/solution/1000-1099/1034.Coloring A Border/Solution.ts @@ -0,0 +1,31 @@ +function colorBorder( + grid: number[][], + row: number, + col: number, + color: number, +): number[][] { + const m = grid.length; + const n = grid[0].length; + const vis = new Array(m).fill(0).map(() => new Array(n).fill(false)); + const dirs = [-1, 0, 1, 0, -1]; + const dfs = (i: number, j: number, c: number) => { + vis[i][j] = true; + for (let k = 0; k < 4; ++k) { + const x = i + dirs[k]; + const y = j + dirs[k + 1]; + if (x >= 0 && x < m && y >= 0 && y < n) { + if (!vis[x][y]) { + if (grid[x][y] == c) { + dfs(x, y, c); + } else { + grid[i][j] = color; + } + } + } else { + grid[i][j] = color; + } + } + }; + dfs(row, col, grid[row][col]); + return grid; +} diff --git a/solution/1500-1599/1575.Count All Possible Routes/README.md b/solution/1500-1599/1575.Count All Possible Routes/README.md index d5d9dd83bbe57..d1ac135d0a874 100644 --- a/solution/1500-1599/1575.Count All Possible Routes/README.md +++ b/solution/1500-1599/1575.Count All Possible Routes/README.md @@ -68,6 +68,19 @@ **方法一:记忆化搜索** +我们设计一个函数 $dfs(i, k)$,表示从城市 $i$ 出发,剩余汽油量为 $k$ 时,到达目的地 $finish$ 的路径数。那么答案就是 $dfs(start, fuel)$。 + +函数 $dfs(i, k)$ 的计算过程如下: + +- 如果 $k \lt 0$ 或者 $|locations[i] - locations[finish]| \gt k$,那么返回 $0$。 +- 如果 $i = finish$,那么答案路径数初始时为 $1$,否则为 $0$。 +- 然后,我们遍历所有城市 $j$,如果 $j \ne i$,那么我们可以从城市 $i$ 移动到城市 $j$,此时剩余汽油量为 $k - |locations[i] - locations[j]|$,那么我们可以将答案路径数加上 $dfs(j, k - |locations[i] - locations[j]|)$。 +- 最后,我们返回答案路径数。 + +为了避免重复计算,我们可以使用记忆化搜索。 + +时间复杂度 $O(n^2 \times fuel)$,空间复杂度 $O(n \times fuel)$。其中 $n$ 为城市数量。 + ### **Python3** @@ -80,15 +93,16 @@ class Solution: self, locations: List[int], start: int, finish: int, fuel: int ) -> int: @cache - def dfs(i, t): - if abs(locations[i] - locations[finish]) > t: + def dfs(i: int, k: int) -> int: + if k < 0 or abs(locations[i] - locations[finish] > k): return 0 - res = int(i == finish) - for j, v in enumerate(locations): - if j != i: - if (cost := abs(locations[i] - v)) <= t: - res += dfs(j, t - cost) - return res % mod + ans = int(i == finish) + ans += sum( + dfs(j, k - abs(locations[i] - x)) + for j, x in enumerate(locations) + if j != i + ) + return ans % mod mod = 10**9 + 7 return dfs(start, fuel) @@ -100,41 +114,34 @@ class Solution: ```java class Solution { - private int[][] f; private int[] locations; - private int target; - private static final int MOD = (int) 1e9 + 7; + private int finish; + private int n; + private Integer[][] f; + private final int mod = (int) 1e9 + 7; public int countRoutes(int[] locations, int start, int finish, int fuel) { - int n = locations.length; - f = new int[n + 1][fuel + 1]; + n = locations.length; this.locations = locations; - target = finish; - for (int i = 0; i < f.length; ++i) { - Arrays.fill(f[i], -1); - } + this.finish = finish; + f = new Integer[n][fuel + 1]; return dfs(start, fuel); } - private int dfs(int i, int t) { - if (f[i][t] != -1) { - return f[i][t]; - } - if (Math.abs(locations[i] - locations[target]) > t) { + private int dfs(int i, int k) { + if (k < 0 || Math.abs(locations[i] - locations[finish]) > k) { return 0; } - int res = i == target ? 1 : 0; - for (int j = 0; j < locations.length; ++j) { + if (f[i][k] != null) { + return f[i][k]; + } + int ans = i == finish ? 1 : 0; + for (int j = 0; j < n; ++j) { if (j != i) { - int cost = Math.abs(locations[i] - locations[j]); - if (cost <= t) { - res += dfs(j, t - cost); - res %= MOD; - } + ans = (ans + dfs(j, k - Math.abs(locations[i] - locations[j]))) % mod; } } - f[i][t] = res; - return res; + return f[i][k] = ans; } } ``` @@ -144,25 +151,27 @@ class Solution { ```cpp class Solution { public: - const int mod = 1e9 + 7; - int countRoutes(vector& locations, int start, int finish, int fuel) { int n = locations.size(); - vector> f(n + 1, vector(fuel + 1, -1)); - return dfs(start, fuel, locations, finish, f); - } - - int dfs(int i, int t, vector& locations, int target, vector>& f) { - if (f[i][t] != -1) return f[i][t]; - if (abs(locations[i] - locations[target]) > t) return 0; - int res = i == target; - for (int j = 0; j < locations.size(); ++j) { - if (j == i) continue; - int cost = abs(locations[i] - locations[j]); - if (cost <= t) res = (res + dfs(j, t - cost, locations, target, f)) % mod; - } - f[i][t] = res; - return res; + int f[n][fuel + 1]; + memset(f, -1, sizeof(f)); + const int mod = 1e9 + 7; + function dfs = [&](int i, int k) -> int { + if (k < 0 || abs(locations[i] - locations[finish]) > k) { + return 0; + } + if (f[i][k] != -1) { + return f[i][k]; + } + int ans = i == finish; + for (int j = 0; j < n; ++j) { + if (j != i) { + ans = (ans + dfs(j, k - abs(locations[i] - locations[j]))) % mod; + } + } + return f[i][k] = ans; + }; + return dfs(start, fuel); } }; ``` @@ -172,36 +181,32 @@ public: ```go func countRoutes(locations []int, start int, finish int, fuel int) int { n := len(locations) - f := make([][]int, n+1) + f := make([][]int, n) for i := range f { f[i] = make([]int, fuel+1) for j := range f[i] { f[i][j] = -1 } } - mod := int(1e9) + 7 + const mod = 1e9 + 7 var dfs func(int, int) int - dfs = func(i, t int) int { - if f[i][t] != -1 { - return f[i][t] - } - if abs(locations[i]-locations[finish]) > t { + dfs = func(i, k int) (ans int) { + if k < 0 || abs(locations[i]-locations[finish]) > k { return 0 } - res := 0 + if f[i][k] != -1 { + return f[i][k] + } if i == finish { - res++ + ans = 1 } - for j, v := range locations { + for j, x := range locations { if j != i { - cost := abs(locations[i] - v) - if cost <= t { - res = (res + dfs(j, t-cost)) % mod - } + ans = (ans + dfs(j, k-abs(locations[i]-x))) % mod } } - f[i][t] = res - return res + f[i][k] = ans + return } return dfs(start, fuel) } @@ -214,6 +219,38 @@ func abs(x int) int { } ``` +### **TypeScript** + +```ts +function countRoutes( + locations: number[], + start: number, + finish: number, + fuel: number, +): number { + const n = locations.length; + const f = Array.from({ length: n }, () => Array(fuel + 1).fill(-1)); + const mod = 1e9 + 7; + const dfs = (i: number, k: number): number => { + if (k < 0 || Math.abs(locations[i] - locations[finish]) > k) { + return 0; + } + if (f[i][k] !== -1) { + return f[i][k]; + } + let ans = i === finish ? 1 : 0; + for (let j = 0; j < n; ++j) { + if (j != i) { + const x = Math.abs(locations[i] - locations[j]); + ans = (ans + dfs(j, k - x)) % mod; + } + } + return (f[i][k] = ans); + }; + return dfs(start, fuel); +} +``` + ### **...** ``` diff --git a/solution/1500-1599/1575.Count All Possible Routes/README_EN.md b/solution/1500-1599/1575.Count All Possible Routes/README_EN.md index eb5df7408b58a..5c2d84c443dc7 100644 --- a/solution/1500-1599/1575.Count All Possible Routes/README_EN.md +++ b/solution/1500-1599/1575.Count All Possible Routes/README_EN.md @@ -69,15 +69,16 @@ class Solution: self, locations: List[int], start: int, finish: int, fuel: int ) -> int: @cache - def dfs(i, t): - if abs(locations[i] - locations[finish]) > t: + def dfs(i: int, k: int) -> int: + if k < 0 or abs(locations[i] - locations[finish] > k): return 0 - res = int(i == finish) - for j, v in enumerate(locations): - if j != i: - if (cost := abs(locations[i] - v)) <= t: - res += dfs(j, t - cost) - return res % mod + ans = int(i == finish) + ans += sum( + dfs(j, k - abs(locations[i] - x)) + for j, x in enumerate(locations) + if j != i + ) + return ans % mod mod = 10**9 + 7 return dfs(start, fuel) @@ -87,41 +88,34 @@ class Solution: ```java class Solution { - private int[][] f; private int[] locations; - private int target; - private static final int MOD = (int) 1e9 + 7; + private int finish; + private int n; + private Integer[][] f; + private final int mod = (int) 1e9 + 7; public int countRoutes(int[] locations, int start, int finish, int fuel) { - int n = locations.length; - f = new int[n + 1][fuel + 1]; + n = locations.length; this.locations = locations; - target = finish; - for (int i = 0; i < f.length; ++i) { - Arrays.fill(f[i], -1); - } + this.finish = finish; + f = new Integer[n][fuel + 1]; return dfs(start, fuel); } - private int dfs(int i, int t) { - if (f[i][t] != -1) { - return f[i][t]; - } - if (Math.abs(locations[i] - locations[target]) > t) { + private int dfs(int i, int k) { + if (k < 0 || Math.abs(locations[i] - locations[finish]) > k) { return 0; } - int res = i == target ? 1 : 0; - for (int j = 0; j < locations.length; ++j) { + if (f[i][k] != null) { + return f[i][k]; + } + int ans = i == finish ? 1 : 0; + for (int j = 0; j < n; ++j) { if (j != i) { - int cost = Math.abs(locations[i] - locations[j]); - if (cost <= t) { - res += dfs(j, t - cost); - res %= MOD; - } + ans = (ans + dfs(j, k - Math.abs(locations[i] - locations[j]))) % mod; } } - f[i][t] = res; - return res; + return f[i][k] = ans; } } ``` @@ -131,25 +125,27 @@ class Solution { ```cpp class Solution { public: - const int mod = 1e9 + 7; - int countRoutes(vector& locations, int start, int finish, int fuel) { int n = locations.size(); - vector> f(n + 1, vector(fuel + 1, -1)); - return dfs(start, fuel, locations, finish, f); - } - - int dfs(int i, int t, vector& locations, int target, vector>& f) { - if (f[i][t] != -1) return f[i][t]; - if (abs(locations[i] - locations[target]) > t) return 0; - int res = i == target; - for (int j = 0; j < locations.size(); ++j) { - if (j == i) continue; - int cost = abs(locations[i] - locations[j]); - if (cost <= t) res = (res + dfs(j, t - cost, locations, target, f)) % mod; - } - f[i][t] = res; - return res; + int f[n][fuel + 1]; + memset(f, -1, sizeof(f)); + const int mod = 1e9 + 7; + function dfs = [&](int i, int k) -> int { + if (k < 0 || abs(locations[i] - locations[finish]) > k) { + return 0; + } + if (f[i][k] != -1) { + return f[i][k]; + } + int ans = i == finish; + for (int j = 0; j < n; ++j) { + if (j != i) { + ans = (ans + dfs(j, k - abs(locations[i] - locations[j]))) % mod; + } + } + return f[i][k] = ans; + }; + return dfs(start, fuel); } }; ``` @@ -159,36 +155,32 @@ public: ```go func countRoutes(locations []int, start int, finish int, fuel int) int { n := len(locations) - f := make([][]int, n+1) + f := make([][]int, n) for i := range f { f[i] = make([]int, fuel+1) for j := range f[i] { f[i][j] = -1 } } - mod := int(1e9) + 7 + const mod = 1e9 + 7 var dfs func(int, int) int - dfs = func(i, t int) int { - if f[i][t] != -1 { - return f[i][t] - } - if abs(locations[i]-locations[finish]) > t { + dfs = func(i, k int) (ans int) { + if k < 0 || abs(locations[i]-locations[finish]) > k { return 0 } - res := 0 + if f[i][k] != -1 { + return f[i][k] + } if i == finish { - res++ + ans = 1 } - for j, v := range locations { + for j, x := range locations { if j != i { - cost := abs(locations[i] - v) - if cost <= t { - res = (res + dfs(j, t-cost)) % mod - } + ans = (ans + dfs(j, k-abs(locations[i]-x))) % mod } } - f[i][t] = res - return res + f[i][k] = ans + return } return dfs(start, fuel) } @@ -201,6 +193,38 @@ func abs(x int) int { } ``` +### **TypeScript** + +```ts +function countRoutes( + locations: number[], + start: number, + finish: number, + fuel: number, +): number { + const n = locations.length; + const f = Array.from({ length: n }, () => Array(fuel + 1).fill(-1)); + const mod = 1e9 + 7; + const dfs = (i: number, k: number): number => { + if (k < 0 || Math.abs(locations[i] - locations[finish]) > k) { + return 0; + } + if (f[i][k] !== -1) { + return f[i][k]; + } + let ans = i === finish ? 1 : 0; + for (let j = 0; j < n; ++j) { + if (j != i) { + const x = Math.abs(locations[i] - locations[j]); + ans = (ans + dfs(j, k - x)) % mod; + } + } + return (f[i][k] = ans); + }; + return dfs(start, fuel); +} +``` + ### **...** ``` diff --git a/solution/1500-1599/1575.Count All Possible Routes/Solution.cpp b/solution/1500-1599/1575.Count All Possible Routes/Solution.cpp index c9b2b0e485f91..330dcca75efec 100644 --- a/solution/1500-1599/1575.Count All Possible Routes/Solution.cpp +++ b/solution/1500-1599/1575.Count All Possible Routes/Solution.cpp @@ -1,23 +1,25 @@ class Solution { public: - const int mod = 1e9 + 7; - int countRoutes(vector& locations, int start, int finish, int fuel) { int n = locations.size(); - vector> f(n + 1, vector(fuel + 1, -1)); - return dfs(start, fuel, locations, finish, f); - } - - int dfs(int i, int t, vector& locations, int target, vector>& f) { - if (f[i][t] != -1) return f[i][t]; - if (abs(locations[i] - locations[target]) > t) return 0; - int res = i == target; - for (int j = 0; j < locations.size(); ++j) { - if (j == i) continue; - int cost = abs(locations[i] - locations[j]); - if (cost <= t) res = (res + dfs(j, t - cost, locations, target, f)) % mod; - } - f[i][t] = res; - return res; + int f[n][fuel + 1]; + memset(f, -1, sizeof(f)); + const int mod = 1e9 + 7; + function dfs = [&](int i, int k) -> int { + if (k < 0 || abs(locations[i] - locations[finish]) > k) { + return 0; + } + if (f[i][k] != -1) { + return f[i][k]; + } + int ans = i == finish; + for (int j = 0; j < n; ++j) { + if (j != i) { + ans = (ans + dfs(j, k - abs(locations[i] - locations[j]))) % mod; + } + } + return f[i][k] = ans; + }; + return dfs(start, fuel); } }; \ No newline at end of file diff --git a/solution/1500-1599/1575.Count All Possible Routes/Solution.go b/solution/1500-1599/1575.Count All Possible Routes/Solution.go index 8b911b8f51ccc..088e2d24c5fb9 100644 --- a/solution/1500-1599/1575.Count All Possible Routes/Solution.go +++ b/solution/1500-1599/1575.Count All Possible Routes/Solution.go @@ -1,35 +1,31 @@ func countRoutes(locations []int, start int, finish int, fuel int) int { n := len(locations) - f := make([][]int, n+1) + f := make([][]int, n) for i := range f { f[i] = make([]int, fuel+1) for j := range f[i] { f[i][j] = -1 } } - mod := int(1e9) + 7 + const mod = 1e9 + 7 var dfs func(int, int) int - dfs = func(i, t int) int { - if f[i][t] != -1 { - return f[i][t] - } - if abs(locations[i]-locations[finish]) > t { + dfs = func(i, k int) (ans int) { + if k < 0 || abs(locations[i]-locations[finish]) > k { return 0 } - res := 0 + if f[i][k] != -1 { + return f[i][k] + } if i == finish { - res++ + ans = 1 } - for j, v := range locations { + for j, x := range locations { if j != i { - cost := abs(locations[i] - v) - if cost <= t { - res = (res + dfs(j, t-cost)) % mod - } + ans = (ans + dfs(j, k-abs(locations[i]-x))) % mod } } - f[i][t] = res - return res + f[i][k] = ans + return } return dfs(start, fuel) } diff --git a/solution/1500-1599/1575.Count All Possible Routes/Solution.java b/solution/1500-1599/1575.Count All Possible Routes/Solution.java index 08bb1f50b978f..a4c08980527a8 100644 --- a/solution/1500-1599/1575.Count All Possible Routes/Solution.java +++ b/solution/1500-1599/1575.Count All Possible Routes/Solution.java @@ -1,38 +1,31 @@ class Solution { - private int[][] f; private int[] locations; - private int target; - private static final int MOD = (int) 1e9 + 7; + private int finish; + private int n; + private Integer[][] f; + private final int mod = (int) 1e9 + 7; public int countRoutes(int[] locations, int start, int finish, int fuel) { - int n = locations.length; - f = new int[n + 1][fuel + 1]; + n = locations.length; this.locations = locations; - target = finish; - for (int i = 0; i < f.length; ++i) { - Arrays.fill(f[i], -1); - } + this.finish = finish; + f = new Integer[n][fuel + 1]; return dfs(start, fuel); } - private int dfs(int i, int t) { - if (f[i][t] != -1) { - return f[i][t]; - } - if (Math.abs(locations[i] - locations[target]) > t) { + private int dfs(int i, int k) { + if (k < 0 || Math.abs(locations[i] - locations[finish]) > k) { return 0; } - int res = i == target ? 1 : 0; - for (int j = 0; j < locations.length; ++j) { + if (f[i][k] != null) { + return f[i][k]; + } + int ans = i == finish ? 1 : 0; + for (int j = 0; j < n; ++j) { if (j != i) { - int cost = Math.abs(locations[i] - locations[j]); - if (cost <= t) { - res += dfs(j, t - cost); - res %= MOD; - } + ans = (ans + dfs(j, k - Math.abs(locations[i] - locations[j]))) % mod; } } - f[i][t] = res; - return res; + return f[i][k] = ans; } } \ No newline at end of file diff --git a/solution/1500-1599/1575.Count All Possible Routes/Solution.py b/solution/1500-1599/1575.Count All Possible Routes/Solution.py index 40f7adf5f43b5..646260b1daa0f 100644 --- a/solution/1500-1599/1575.Count All Possible Routes/Solution.py +++ b/solution/1500-1599/1575.Count All Possible Routes/Solution.py @@ -3,15 +3,16 @@ def countRoutes( self, locations: List[int], start: int, finish: int, fuel: int ) -> int: @cache - def dfs(i, t): - if abs(locations[i] - locations[finish]) > t: + def dfs(i: int, k: int) -> int: + if k < 0 or abs(locations[i] - locations[finish] > k): return 0 - res = int(i == finish) - for j, v in enumerate(locations): - if j != i: - if (cost := abs(locations[i] - v)) <= t: - res += dfs(j, t - cost) - return res % mod + ans = int(i == finish) + ans += sum( + dfs(j, k - abs(locations[i] - x)) + for j, x in enumerate(locations) + if j != i + ) + return ans % mod mod = 10**9 + 7 return dfs(start, fuel) diff --git a/solution/1500-1599/1575.Count All Possible Routes/Solution.ts b/solution/1500-1599/1575.Count All Possible Routes/Solution.ts new file mode 100644 index 0000000000000..eaed3233a2479 --- /dev/null +++ b/solution/1500-1599/1575.Count All Possible Routes/Solution.ts @@ -0,0 +1,27 @@ +function countRoutes( + locations: number[], + start: number, + finish: number, + fuel: number, +): number { + const n = locations.length; + const f = Array.from({ length: n }, () => Array(fuel + 1).fill(-1)); + const mod = 1e9 + 7; + const dfs = (i: number, k: number): number => { + if (k < 0 || Math.abs(locations[i] - locations[finish]) > k) { + return 0; + } + if (f[i][k] !== -1) { + return f[i][k]; + } + let ans = i === finish ? 1 : 0; + for (let j = 0; j < n; ++j) { + if (j != i) { + const x = Math.abs(locations[i] - locations[j]); + ans = (ans + dfs(j, k - x)) % mod; + } + } + return (f[i][k] = ans); + }; + return dfs(start, fuel); +} diff --git a/solution/1700-1799/1760.Minimum Limit of Balls in a Bag/README_EN.md b/solution/1700-1799/1760.Minimum Limit of Balls in a Bag/README_EN.md index c1482b7c67db1..a9ff29d3ee201 100644 --- a/solution/1700-1799/1760.Minimum Limit of Balls in a Bag/README_EN.md +++ b/solution/1700-1799/1760.Minimum Limit of Balls in a Bag/README_EN.md @@ -10,12 +10,10 @@
  • Take any bag of balls and divide it into two new bags with a positive number of balls. -
    • For example, a bag of 5 balls can become two new bags of 1 and 4 balls, or two new bags of 2 and 3 balls.
  • -

Your penalty is the maximum number of balls in a bag. You want to minimize your penalty after the operations.