Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1575
Browse files Browse the repository at this point in the history
No.1575.Count All Possible Routes
  • Loading branch information
yanglbme committed Jul 7, 2022
1 parent 8ef3222 commit 26d7960
Show file tree
Hide file tree
Showing 8 changed files with 379 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
- [矩阵中的最长递增路径](/solution/0300-0399/0329.Longest%20Increasing%20Path%20in%20a%20Matrix/README.md) - `DFS``记忆化搜索`
- [网格图中递增路径的数目](/solution/2300-2399/2328.Number%20of%20Increasing%20Paths%20in%20a%20Grid/README.md) - `DFS``记忆化搜索`
- [翻转游戏 II](/solution/0200-0299/0294.Flip%20Game%20II/README.md) - `DFS``状态压缩``记忆化搜索`
- [统计所有可行路径](/solution/1500-1599/1575.Count%20All%20Possible%20Routes/README.md) - `DFS``记忆化搜索`

<!-- DFS 待补充 -->

Expand Down
1 change: 1 addition & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
- [Longest Increasing Path in a Matrix](/solution/0300-0399/0329.Longest%20Increasing%20Path%20in%20a%20Matrix/README_EN.md) - `DFS``Memoization`
- [Number of Increasing Paths in a Grid](/solution/2300-2399/2328.Number%20of%20Increasing%20Paths%20in%20a%20Grid/README.md) - `DFS``Memoization`
- [Flip Game II](/solution/0200-0299/0294.Flip%20Game%20II/README_EN.md) - `DFS``Bitmask``Memoization`
- [Count All Possible Routes](/solution/1500-1599/1575.Count%20All%20Possible%20Routes/README_EN.md) - `DFS``Memoization`

### 4. Dynamic Programming(DP)

Expand Down
131 changes: 130 additions & 1 deletion solution/1500-1599/1575.Count All Possible Routes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,151 @@

<!-- 这里可写通用的实现逻辑 -->

**方法一:记忆化搜索**

<!-- tabs:start -->

### **Python3**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
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:
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

mod = 10**9 + 7
return dfs(start, fuel)
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
class Solution {
private int[][] f;
private int[] locations;
private int target;
private static 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];
this.locations = locations;
target = finish;
for (int i = 0; i < f.length; ++i) {
Arrays.fill(f[i], -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) {
return 0;
}
int res = i == target ? 1 : 0;
for (int j = 0; j < locations.length; ++j) {
if (j != i) {
int cost = Math.abs(locations[i] - locations[j]);
if (cost <= t) {
res += dfs(j, t - cost);
res %= MOD;
}
}
}
f[i][t] = res;
return res;
}
}
```

### **C++**

```cpp
class Solution {
public:
const int mod = 1e9 + 7;

int countRoutes(vector<int>& locations, int start, int finish, int fuel) {
int n = locations.size();
vector<vector<int>> f(n + 1, vector<int>(fuel + 1, -1));
return dfs(start, fuel, locations, finish, f);
}

int dfs(int i, int t, vector<int>& locations, int target, vector<vector<int>>& 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;
}
};
```
### **Go**
```go
func countRoutes(locations []int, start int, finish int, fuel int) int {
n := len(locations)
f := make([][]int, n+1)
for i := range f {
f[i] = make([]int, fuel+1)
for j := range f[i] {
f[i][j] = -1
}
}
mod := int(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 {
return 0
}
res := 0
if i == finish {
res++
}
for j, v := range locations {
if j != i {
cost := abs(locations[i] - v)
if cost <= t {
res = (res + dfs(j, t-cost)) % mod
}
}
}
f[i][t] = res
return res
}
return dfs(start, fuel)
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
```

### **...**
Expand Down
129 changes: 128 additions & 1 deletion solution/1500-1599/1575.Count All Possible Routes/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,140 @@
### **Python3**

```python

class Solution:
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:
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

mod = 10**9 + 7
return dfs(start, fuel)
```

### **Java**

```java
class Solution {
private int[][] f;
private int[] locations;
private int target;
private static 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];
this.locations = locations;
target = finish;
for (int i = 0; i < f.length; ++i) {
Arrays.fill(f[i], -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) {
return 0;
}
int res = i == target ? 1 : 0;
for (int j = 0; j < locations.length; ++j) {
if (j != i) {
int cost = Math.abs(locations[i] - locations[j]);
if (cost <= t) {
res += dfs(j, t - cost);
res %= MOD;
}
}
}
f[i][t] = res;
return res;
}
}
```

### **C++**

```cpp
class Solution {
public:
const int mod = 1e9 + 7;

int countRoutes(vector<int>& locations, int start, int finish, int fuel) {
int n = locations.size();
vector<vector<int>> f(n + 1, vector<int>(fuel + 1, -1));
return dfs(start, fuel, locations, finish, f);
}

int dfs(int i, int t, vector<int>& locations, int target, vector<vector<int>>& 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;
}
};
```
### **Go**
```go
func countRoutes(locations []int, start int, finish int, fuel int) int {
n := len(locations)
f := make([][]int, n+1)
for i := range f {
f[i] = make([]int, fuel+1)
for j := range f[i] {
f[i][j] = -1
}
}
mod := int(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 {
return 0
}
res := 0
if i == finish {
res++
}
for j, v := range locations {
if j != i {
cost := abs(locations[i] - v)
if cost <= t {
res = (res + dfs(j, t-cost)) % mod
}
}
}
f[i][t] = res
return res
}
return dfs(start, fuel)
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
```

### **...**
Expand Down
24 changes: 24 additions & 0 deletions solution/1500-1599/1575.Count All Possible Routes/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Solution {
public:
const int mod = 1e9 + 7;

int countRoutes(vector<int>& locations, int start, int finish, int fuel) {
int n = locations.size();
vector<vector<int>> f(n + 1, vector<int>(fuel + 1, -1));
return dfs(start, fuel, locations, finish, f);
}

int dfs(int i, int t, vector<int>& locations, int target, vector<vector<int>>& 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;
}
};
42 changes: 42 additions & 0 deletions solution/1500-1599/1575.Count All Possible Routes/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
func countRoutes(locations []int, start int, finish int, fuel int) int {
n := len(locations)
f := make([][]int, n+1)
for i := range f {
f[i] = make([]int, fuel+1)
for j := range f[i] {
f[i][j] = -1
}
}
mod := int(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 {
return 0
}
res := 0
if i == finish {
res++
}
for j, v := range locations {
if j != i {
cost := abs(locations[i] - v)
if cost <= t {
res = (res + dfs(j, t-cost)) % mod
}
}
}
f[i][t] = res
return res
}
return dfs(start, fuel)
}

func abs(x int) int {
if x < 0 {
return -x
}
return x
}
Loading

0 comments on commit 26d7960

Please sign in to comment.