3540. Minimum Time to Visit All Houses π
Description
You are given two integer arrays forward
and backward
, both of size n
. You are also given another integer array queries
.
There are n
houses arranged in a circle. The houses are connected via roads in a special arrangement:
- For all
0 <= i <= n - 2
, housei
is connected to housei + 1
via a road with lengthforward[i]
meters. Additionally, housen - 1
is connected back to house 0 via a road with lengthforward[n - 1]
meters, completing the circle. - For all
1 <= i <= n - 1
, housei
is connected to housei - 1
via a road with lengthbackward[i]
meters. Additionally, house 0 is connected back to housen - 1
via a road with lengthbackward[0]
meters, completing the circle.
You can walk at a pace of one meter per second. Starting from house 0, find the minimum time taken to visit each house in the order specified by queries
.
Return the minimum total time taken to visit the houses.
Example 1:
Input: forward = [1,4,4], backward = [4,1,2], queries = [1,2,0,2]
Output: 12
Explanation:
The path followed is 0(0) → 1(1) →βββββββ 2(5) → 1(7) →βββββββ 0(8) → 2(12)
.
Note: The notation used is node(total time)
, →
represents forward road, and →
represents backward road.
Example 2:
Input: forward = [1,1,1,1], backward = [2,2,2,2], queries = [1,2,3,0]
Output: 4
Explanation:
The path travelled is 0 →βββββββ 1 →βββββββ 2 →βββββββ 3 → 0
. Each step is in the forward direction and requires 1 second.
Constraints:
2 <= n <= 105
n == forward.length == backward.length
1 <= forward[i], backward[i] <= 105
1 <= queries.length <= 105
0 <= queries[i] < n
queries[i] != queries[i + 1]
queries[0]
is not 0.
Solutions
Solution 1
1 |
|
1 |
|
1 |
|
1 |
|