Skip to content

Instantly share code, notes, and snippets.

@mykeels
Last active November 1, 2018 12:15
Show Gist options
  • Save mykeels/83138cfabb278c700648d649ed49bc51 to your computer and use it in GitHub Desktop.
Save mykeels/83138cfabb278c700648d649ed49bc51 to your computer and use it in GitHub Desktop.
Given the root node of a tree, write a function that calculates the minimal Sales Path cost in the tree

The car manufacturer Honda holds their distribution system in the form of a tree (not necessarily binary). The root is the company itself, and every node in the tree represents a car distributor that receives cars from the parent node and ships them to its children nodes. The leaf nodes are car dealerships that sell cars direct to consumers. In addition, every node holds an integer that is the cost of shipping a car to it.

Take for example the tree below:

honda-sales-tree

See SampleTree.json

A path from Honda's factory to a car dealership, which is a path from the root to a leaf in the tree, is called a Sales Path. The cost of a Sales Path is the sum of the costs for every node in the path. For example, in the tree above one Sales Path is 0→3→0→10, and its cost is 13 (0+3+0+10).

Honda wishes to find the minimal Sales Path cost in its distribution tree. Given a node rootNode, write a function getCheapestCost that calculates the minimal Sales Path cost in the tree.

Implement your function in the most efficient manner and analyze its time and space complexities.


For example:

Given the rootNode of the tree in diagram above

Your function would return:

7 since it's the minimal Sales Path cost (there are actually two Sales Paths in the tree whose cost is 7: 0→6→1 and 0→3→2→1→1)

Constraints:

[time limit] 5000ms

[input] Node rootNode
    0 ≤ rootNode.cost ≤ 100000

[output] integer
import java.io.*;
import java.util.*;
class SampleGetCheapestPath {
static class Node {
int cost;
Node[] children;
Node parent;
Node(int cost) {
this.cost = cost;
children = null;
parent = null;
}
}
static class SalesPath {
int getCheapestCost(Node rootNode) {
// your code goes here
}
}
/*********************************************
* Driver program to test above method *
*********************************************/
public static void main(String[] args) {
}
}
{
"cost": 0,
"children": [
{
"cost": 5,
"children": [
{
"cost": 4
}
]
},
{
"cost": 3,
"children": [
{
"cost": 2,
"children": [
{
"cost": 1,
"children": [
{
"cost": 1
}
]
}
]
},
{
"cost": 0,
"children": [
{
"cost": 10
}
]
}
]
},
{
"cost": 6,
"children": [
{
"cost": 1
},
{
"cost": 5
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment