lolpee69 revised this gist 1 day ago. Go to revision
1 file changed, 67 insertions
loupe.md(file created)
| @@ -0,0 +1,67 @@ | |||
| 1 | + | Code Question | |
| 2 | + | Amazon manages a network of warehouses connected as a weighted tree with network_nodes nodes labeled from 1 to network_nodes. Each edge (u, v) has a positive integer weight network_weight, representing the transfer time between u and v. The distance between two nodes is the sum of weights along their unique path. | |
| 3 | + | Given an integer batch_time, compute for every warehouse c the number of ordered pairs of other warehouses (a, b) such that: | |
| 4 | + | a ≠ b, a ≠ c, b ≠ c | |
| 5 | + | The distances from node a to node b and from node a to node c are divisible by batch_time. | |
| 6 | + | Note: | |
| 7 | + | The path between node a and b is not required to pass through c. Only the distances from a to c and a to b must be divisible by batch_time. | |
| 8 | + | Return an array where each element represents the number of valid ordered pairs for that warehouse. | |
| 9 | + | Additional Notes | |
| 10 | + | In an ordered pair, (a, b) and (b, a) are treated as different pairs. | |
| 11 | + | The warehouse network is a tree (undirected, connected graph with no cycles, no self-loops, and no multiple edges). | |
| 12 | + | Both warehouse labels (nodes) and transfer times (weights) are positive integers. | |
| 13 | + | Example | |
| 14 | + | network_nodes = 4 | |
| 15 | + | network_from = [1, 1, 2] | |
| 16 | + | network_to = [2, 3, 4] | |
| 17 | + | network_weight = [2, 5, 3] | |
| 18 | + | batch_time = 5 | |
| 19 | + | Explanation: | |
| 20 | + | For warehouse 1, valid pairs: (3, 4) and (4, 3) | |
| 21 | + | For warehouse 2, no valid pairs | |
| 22 | + | For warehouse 3, valid pairs: (1, 4) and (4, 1) | |
| 23 | + | For warehouse 4, valid pairs: (1, 3) and (3, 1) | |
| 24 | + | Output: | |
| 25 | + | [2, 0, 2, 2] | |
| 26 | + | Function Description | |
| 27 | + | Complete the function countPackagePairs: | |
| 28 | + | Parameters: | |
| 29 | + | int network_nodes: number of warehouses | |
| 30 | + | int network_from[network_nodes - 1]: one endpoint of each edge | |
| 31 | + | int network_to[network_nodes - 1]: other endpoint of each edge | |
| 32 | + | int network_weight[network_nodes - 1]: weight (distance) of each edge | |
| 33 | + | int batch_time: fixed duration | |
| 34 | + | Constraints | |
| 35 | + | 2 ≤ network_nodes ≤ 1000 | |
| 36 | + | 1 ≤ network_from[i], network_to[i] ≤ n | |
| 37 | + | 1 ≤ network_weight[i] ≤ 10^6 | |
| 38 | + | 1 ≤ batch_time ≤ 1000 | |
| 39 | + | Input Format for Custom Testing | |
| 40 | + | First line: two integers network_nodes and network_nodes - 1 | |
| 41 | + | Next n-1 lines: network_from[i] network_to[i] network_weight[i] | |
| 42 | + | Last line: batch_time | |
| 43 | + | Sample Case 0 | |
| 44 | + | Input: | |
| 45 | + | 4 3 | |
| 46 | + | 1 2 2 | |
| 47 | + | 2 3 2 | |
| 48 | + | 3 4 2 | |
| 49 | + | 2 | |
| 50 | + | Output: | |
| 51 | + | 6 | |
| 52 | + | 6 | |
| 53 | + | 6 | |
| 54 | + | 6 | |
| 55 | + | Explanation: | |
| 56 | + | Each warehouse can connect to every other warehouse → all pairs valid. | |
| 57 | + | Sample Case 1 | |
| 58 | + | Input: | |
| 59 | + | 2 1 | |
| 60 | + | 1 2 5 | |
| 61 | + | 2 | |
| 62 | + | Output: | |
| 63 | + | 0 | |
| 64 | + | 0 | |
| 65 | + | Explanation: | |
| 66 | + | Distance not divisible by batch_time | |
| 67 | + | Also not enough nodes to form (a, b, c) | |
Newer
Older