forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.1575
No.1575.Count All Possible Routes
- Loading branch information
Showing
8 changed files
with
379 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
solution/1500-1599/1575.Count All Possible Routes/Solution.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
solution/1500-1599/1575.Count All Possible Routes/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.