Tap here to switch tabs
Problem
Submissions

Problem: Shortest Distance to Target Color

Try to solve the Shortest Distance to Target Color problem.

Statement

You are given an array of integers colors, where each element represents a color, and a 2extD2 ext{D} array queries where each query is in the form [i, c].

For each query [i, c], find the shortest distance between index i and any index jj such that colors[j] == c. The distance between index i and index jj is given by ∣i−j∣|i - j|. If the color c does not exist in colors, the answer for that query is −1-1.

Return an array of integers representing the answers to all queries in the given order.

Constraints:

  • 1≤1 \leq colors.length ≤5imes104\leq 5 imes 10^4

  • 1≤1 \leq colors[i] ≤3\leq 3

  • 1≤1 \leq queries.length ≤5imes104\leq 5 imes 10^4

  • queries[i].length =2= 2

  • 0≤0 \leq queries[i][0] << colors.length

  • 1≤1 \leq queries[i][1] ≤3\leq 3

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Shortest Distance to Target Color

Try to solve the Shortest Distance to Target Color problem.

Statement

You are given an array of integers colors, where each element represents a color, and a 2extD2 ext{D} array queries where each query is in the form [i, c].

For each query [i, c], find the shortest distance between index i and any index jj such that colors[j] == c. The distance between index i and index jj is given by ∣i−j∣|i - j|. If the color c does not exist in colors, the answer for that query is −1-1.

Return an array of integers representing the answers to all queries in the given order.

Constraints:

  • 1≤1 \leq colors.length ≤5imes104\leq 5 imes 10^4

  • 1≤1 \leq colors[i] ≤3\leq 3

  • 1≤1 \leq queries.length ≤5imes104\leq 5 imes 10^4

  • queries[i].length =2= 2

  • 0≤0 \leq queries[i][0] << colors.length

  • 1≤1 \leq queries[i][1] ≤3\leq 3