[Addepar] Grad Software Engineer OA

There is a weighted undirected graph with N nodes and M edges. The stress level of a path between two nodes is defined as the weight of the edge with the maximum value present in the path. Given a source node "source" and a destination node "destination", find a path with the minimum stress level. If no such path exists, report -1.

Example

Consider the following example below, where source = 1 and destination = 3.

There are two paths from node 1 to node 3.

  1. The max weighted edge/stress level in path 1 → 2 → 3 is 200.
  2. The max weighted edge/stress level in path 1 → 4 → 3 is 20.

Return 20, the lower stress level from the second path.


Function Description

Complete the function leastStressfulPath in the editor below.

def leastStressfulPath(graph_nodes, graph_from, graph_to, graph_weight, source, destination):

Parameters

  • graph_nodes: number of nodes
  • graph_from: one end node for an edge
  • graph_to: one end node for an edge
  • graph_weight: the weights of the edges
  • source: the source node
  • destination: the destination node

Returns

  • int: the value of the least stressful path.
  • If no path exists, return -1.

Note

  • Even though edge endpoints are called graph_from[i] and graph_to[i], this is an undirected graph.

Constraints

  • 1 ≤ graph_nodes ≤ 10⁵
  • 1 ≤ |graph_from| = |graph_to| = |graph_weight| ≤ 10⁵
  • 1 ≤ graph_from[i], graph_to[i], source, destination ≤ graph_nodes
  • 0 ≤ graph_weight[i] ≤ 10⁹

Input Format for Custom Testing

  1. The first line contains two space-separated integers graph_nodes and m, the number of nodes and the number of edges.
  2. Each of the following m lines contains three space-separated integers a, b, w, meaning there is an edge connecting the nodes a and b of weight w.
  3. The next line contains a single integer, denoting the index of the "source" node.
  4. The next line contains a single integer, denoting the index of the "destination" node.

Sample Case 0

Input

5 6
1 2 10
1 4 3
4 3 2
3 2 5
4 5 4
5 3 6
1
3

Output

3

Explanation

There are three paths from node 1 to node 3:

  1. 1 → 2 → 3: Edges have weights 10 and 5, the max weighted edge is 10.
  2. 1 → 4 → 3: Edges have weights 3 and 2, the max weighted edge is 3.
  3. 1 → 5 → 3: Edges have weights 4 and 6, the max weighted edge is 6.

The minimum stress level is 3 from path 1 → 4 → 3.


Sample Case 1

Input

3 1
1 2 10
1
3

Output

-1

Explanation

There is no path from node 1 to node 3, so return -1.


我们长期稳定承接各大科技公司如TikTok、Google、Amazon等的OA笔试代写服务,确保满分通过。如有需求,请随时联系我们。

We consistently provide professional online assessment services for major tech companies like TikTok, Google, and Amazon, guaranteeing perfect scores. Feel free to contact us if you're interested.

Leave a Reply

Your email address will not be published. Required fields are marked *