Tap here to switch tabs
Problem
Submissions

Problem: Minimum Time to Visit All Houses

Try to solve the Minimum Time to Visit All Houses problem.

Statement

There are nn houses numbered from 00 to n−1n - 1 arranged in a circle in clockwise order.

You start at house 00 at time 00. You are given two 00 indexed integer arrays forward and backward, each of length nn, and an integer array queries.

For each house ii (where 0≤i<n0 \leq i < n):

  • Traveling clockwise from house ii to house (i+1) mod n(i + 1) \bmod n takes forward[i] seconds.

  • Traveling counterclockwise from house ii to house (i−1+n) mod n(i - 1 + n) \bmod n takes backward[i] seconds.

You need to visit the houses in the sequence given by queries. For each consecutive destination, you can choose to travel either clockwise or counterclockwise along the circle to minimize the travel time.

Return the minimum total time required to visit all houses in the specified order.

Constraints:

  • 2≤n≤1052 \leq n \leq 10^5

  • n==n == forward.length ==== backward.length

  • 1≤1 \leq forward[i], backward[i] ≤105\leq 10^5

  • 1≤1 \leq queries.length ≤105\leq 10^5

  • 0≤0 \leq queries[i] <n< n

  • queries[i] ≠\neq queries[i + 1]

  • queries[0] ≠0\neq 0

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Time to Visit All Houses

Try to solve the Minimum Time to Visit All Houses problem.

Statement

There are nn houses numbered from 00 to n−1n - 1 arranged in a circle in clockwise order.

You start at house 00 at time 00. You are given two 00 indexed integer arrays forward and backward, each of length nn, and an integer array queries.

For each house ii (where 0≤i<n0 \leq i < n):

  • Traveling clockwise from house ii to house (i+1) mod n(i + 1) \bmod n takes forward[i] seconds.

  • Traveling counterclockwise from house ii to house (i−1+n) mod n(i - 1 + n) \bmod n takes backward[i] seconds.

You need to visit the houses in the sequence given by queries. For each consecutive destination, you can choose to travel either clockwise or counterclockwise along the circle to minimize the travel time.

Return the minimum total time required to visit all houses in the specified order.

Constraints:

  • 2≤n≤1052 \leq n \leq 10^5

  • n==n == forward.length ==== backward.length

  • 1≤1 \leq forward[i], backward[i] ≤105\leq 10^5

  • 1≤1 \leq queries.length ≤105\leq 10^5

  • 0≤0 \leq queries[i] <n< n

  • queries[i] ≠\neq queries[i + 1]

  • queries[0] ≠0\neq 0