Minimum Time to Visit All Houses - Problem

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, house i is connected to house i + 1 via a road with length forward[i] meters. Additionally, house n - 1 is connected back to house 0 via a road with length forward[n - 1] meters, completing the circle.
  • For all 1 <= i <= n - 1, house i is connected to house i - 1 via a road with length backward[i] meters. Additionally, house 0 is connected back to house n - 1 via a road with length backward[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.

Input & Output

Example 1 — Basic Circular Path
$ Input: forward = [3,1,2,4], backward = [5,2,1,3], queries = [1,2,0]
Output: 5
💡 Note: Start at house 0. Go to house 1 (min(3,5)=3), then to house 2 (min(1,2)=1), then to house 0 (min(6,1)=1). Total: 3+1+1=5
Example 2 — Same House Query
$ Input: forward = [2,3], backward = [1,4], queries = [0,1,0]
Output: 3
💡 Note: Start at house 0. Stay at house 0 (0 time), go to house 1 (min(2,4)=2), return to house 0 (min(3,1)=1). Total: 0+2+1=3
Example 3 — Larger Circle
$ Input: forward = [1,2,3,4,5], backward = [6,5,4,3,2], queries = [2,4,1]
Output: 8
💡 Note: Start at house 0. Go to house 2 (min(3,9)=3), then to house 4 (min(9,6)=6), then to house 1 (min(9,1)=1). Total: 3+6+1=10

Constraints

  • 1 ≤ forward.length = backward.length = n ≤ 1000
  • 1 ≤ forward[i], backward[i] ≤ 1000
  • 1 ≤ queries.length ≤ 1000
  • 0 ≤ queries[i] < n

Visualization

Tap to expand
Minimum Time to Visit All Houses INPUT H0 H1 H2 H3 f:3 f:1 f:2 f:4 b:5 b:2 b:1 b:3 forward = [3,1,2,4] backward = [5,2,1,3] queries = [1,2,0] Visit Order: Start H0 --> H1 --> H2 --> H0 Forward Backward ALGORITHM STEPS 1 Precompute Prefix Sums Build forward/backward prefix 2 Query 1: H0 to H1 Forward: 3, Backward: 5+2+1=8 min(3, 8) = 3 3 Query 2: H1 to H2 Forward: 1, Backward: 5+3+2=10 min(1, 10) = 1 4 Query 3: H2 to H0 Forward: 2+4=6, Backward: 1+2=3 min(6, 3) = --> use backward Distance Calculation H2-->H3: backward[2]=1 H3-->H0: backward[3]=3 backward total = 1+3 = 4 min(6, 4) = 4 (backward) FINAL RESULT H0 4 H1 1 H2 2 H3 +3 +1 +1 +3 Total Time Calculation H0 --> H1: 3 (forward) H1 --> H2: 1 (forward) H2 --> H0: 4 (backward) Output: 11 3 + 1 + 4 + 3 = 11 [OK] Key Insight: For each query, compare forward and backward path distances using prefix sums. In a circular arrangement, the optimal path is always min(forward_distance, backward_distance). Precomputing prefix sums allows O(1) distance calculation per query, achieving O(n + q) total time complexity. TutorialsPoint - Minimum Time to Visit All Houses | Optimal Solution
Asked in
Google 15 Amazon 12 Meta 8
8.5K Views
Medium Frequency
~25 min Avg. Time
342 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen