Skip to content

Commit 6e6aa9d

Browse files
committed
1575 solved.
1 parent 439a688 commit 6e6aa9d

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.25)
2+
project(cpp_1575)
3+
4+
set(CMAKE_CXX_STANDARD 17)
5+
6+
add_executable(cpp_1575 main.cpp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/// Source : https://leetcode.com/problems/count-all-possible-routes/description/
2+
/// Author : liuyubobobo
3+
/// Time : 2023-06-25
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// Memoization
12+
/// Time Complexity: O(n^2 * fuel)
13+
/// Space Complexity: O(n * fuel)
14+
class Solution {
15+
16+
private:
17+
long long MOD = 1e9 + 7;
18+
19+
public:
20+
int countRoutes(vector<int>& locations, int start, int finish, int fuel) {
21+
22+
int n = locations.size();
23+
vector<vector<long long>> dp(n, vector<long long>(fuel + 1, -1));
24+
return dfs(n, locations, finish, start, fuel, dp);
25+
}
26+
27+
private:
28+
long long dfs(int n, const vector<int>& locations, int finish, int cur, int fuel,
29+
vector<vector<long long>>& dp){
30+
31+
if(fuel < 0) return 0;
32+
if(dp[cur][fuel] != -1) return dp[cur][fuel];
33+
34+
long long res = 0;
35+
if(cur == finish) res = 1;
36+
for(int i = 0; i < n; i ++){
37+
if(i == cur) continue;
38+
res += dfs(n, locations, finish, i, fuel - abs(locations[i] - locations[cur]), dp);
39+
}
40+
return dp[cur][fuel] = res % MOD;
41+
}
42+
};
43+
44+
45+
int main() {
46+
47+
return 0;
48+
}

readme.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1475,7 +1475,7 @@ email: [[email protected]](mailto:[email protected])
14751475
| 1572 | [Matrix Diagonal Sum](https://leetcode.com/problems/matrix-diagonal-sum/) | [] | [C++](1501-2000/1572-Matrix-Diagonal-Sum/cpp-1572/) | | |
14761476
| | | | | | |
14771477
| 1574 | [Shortest Subarray to be Removed to Make Array Sorted](https://leetcode.com/problems/shortest-subarray-to-be-removed-to-make-array-sorted/description/) | [] | [C++](1501-2000/1574-Shortest-Subarray-to-be-Removed-to-Make-Array-Sorted/cpp-1574/) | | |
1478-
| | | | | | |
1478+
| 1575 | [Count All Possible Routes](https://leetcode.com/problems/count-all-possible-routes/description/) | [] | [C++](1501-2000/1575-Count-All-Possible-Routes/cpp-1575/) | | |
14791479
| 1576 | [Replace All ?'s to Avoid Consecutive Repeating Characters](https://leetcode.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/) | [] | [C++](1501-2000/1576-Replace-All-Question-Marks-to-Avoid-Consecutive-Repeating-Characters/cpp-1576/) | | |
14801480
| 1577 | [Number of Ways Where Square of Number Is Equal to Product of Two Numbers](https://leetcode.com/problems/number-of-ways-where-square-of-number-is-equal-to-product-of-two-numbers/) | [] | [C++](1501-2000/1577-Number-of-Ways-Where-Square-of-Number-Is-Equal-to-Product-of-Two-Numbers/cpp-1577/) | | |
14811481
| 1578 | [Minimum Deletion Cost to Avoid Repeating Letters](https://leetcode.com/problems/minimum-deletion-cost-to-avoid-repeating-letters/) | [] | [C++](1501-2000/1578-Minimum-Deletion-Cost-to-Avoid-Repeating-Letters/cpp-1578/) | | |

0 commit comments

Comments
 (0)