Skip to content

Commit 861a593

Browse files
committedApr 20, 2025
Add solution #1575
1 parent 7e951d3 commit 861a593

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,378 LeetCode solutions in JavaScript
1+
# 1,379 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1202,6 +1202,7 @@
12021202
1572|[Matrix Diagonal Sum](./solutions/1572-matrix-diagonal-sum.js)|Easy|
12031203
1573|[Number of Ways to Split a String](./solutions/1573-number-of-ways-to-split-a-string.js)|Medium|
12041204
1574|[Shortest Subarray to be Removed to Make Array Sorted](./solutions/1574-shortest-subarray-to-be-removed-to-make-array-sorted.js)|Medium|
1205+
1575|[Count All Possible Routes](./solutions/1575-count-all-possible-routes.js)|Hard|
12051206
1576|[Replace All ?'s to Avoid Consecutive Repeating Characters](./solutions/1576-replace-all-s-to-avoid-consecutive-repeating-characters.js)|Medium|
12061207
1598|[Crawler Log Folder](./solutions/1598-crawler-log-folder.js)|Easy|
12071208
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* 1575. Count All Possible Routes
3+
* https://leetcode.com/problems/count-all-possible-routes/
4+
* Difficulty: Hard
5+
*
6+
* You are given an array of distinct positive integers locations where locations[i] represents
7+
* the position of city i. You are also given integers start, finish and fuel representing the
8+
* starting city, ending city, and the initial amount of fuel you have, respectively.
9+
*
10+
* At each step, if you are at city i, you can pick any city j such that j != i and
11+
* 0 <= j < locations.length and move to city j. Moving from city i to city j reduces the
12+
* amount of fuel you have by |locations[i] - locations[j]|. Please notice that |x| denotes
13+
* the absolute value of x.
14+
*
15+
* Notice that fuel cannot become negative at any point in time, and that you are allowed to visit
16+
* any city more than once (including start and finish).
17+
*
18+
* Return the count of all possible routes from start to finish. Since the answer may be too large,
19+
* return it modulo 109 + 7.
20+
*/
21+
22+
/**
23+
* @param {number[]} locations
24+
* @param {number} start
25+
* @param {number} finish
26+
* @param {number} fuel
27+
* @return {number}
28+
*/
29+
var countRoutes = function(locations, start, finish, fuel) {
30+
const MOD = 1e9 + 7;
31+
const memo = new Array(locations.length).fill().map(() => new Array(fuel + 1).fill(-1));
32+
33+
function calculateRoutes(currentCity, remainingFuel) {
34+
if (remainingFuel < 0) return 0;
35+
if (memo[currentCity][remainingFuel] !== -1) return memo[currentCity][remainingFuel];
36+
37+
let routes = currentCity === finish ? 1 : 0;
38+
39+
for (let nextCity = 0; nextCity < locations.length; nextCity++) {
40+
if (nextCity !== currentCity) {
41+
const fuelCost = Math.abs(locations[currentCity] - locations[nextCity]);
42+
routes = (routes + calculateRoutes(nextCity, remainingFuel - fuelCost)) % MOD;
43+
}
44+
}
45+
46+
memo[currentCity][remainingFuel] = routes;
47+
return routes;
48+
}
49+
50+
return calculateRoutes(start, fuel);
51+
};

0 commit comments

Comments
 (0)
Please sign in to comment.