FastPrepShortest Pin Path Through Topics

Shortest Pin Path Through Topics

Pinterest logoPinterest● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Each row in topics lists the pins belonging to one topic. Treat every topic as an undirected clique: any two distinct pins in the same topic are connected by one unit-cost edge. Duplicate pin names inside a row have no additional effect.

Return the minimum number of pin-to-pin edges from start to destination. Return 0 when they are equal. Return -1 when either endpoint is absent or the destination is unreachable.

Function

minimumPinSteps(topics: String[][], start: String, destination: String) → int

Examples

Example 1

topics = [["California","New York"],["New York","Cantonese cuisine"]]start = "California"destination = "Cantonese cuisine"return = 2

California connects to New York, then to Cantonese cuisine.

Example 2

topics = [["a","b","c"]]start = "a"destination = "c"return = 1

Pins in one topic are directly adjacent.

Constraints

  • 0 <= topics.length <= 500.
  • The total number of pin occurrences is at most 5000.
  • Pin names are nonempty.

More Pinterest problems

See Pinterest hiring insights
public int minimumPinSteps(String[][] topics, String start, String destination) {
  // write your code here
}
topics[["California","New York"],["New York","Cantonese cuisine"]]
start"California"
destination"Cantonese cuisine"
expected2
Checking account…