You are given direct ownership relationships between companies. Each relationship states that one company owns a percentage of another company.
Find the total percentage of targetCompany owned by ownerCompany, including both direct and indirect ownership.
CompanyOwnershipAnalyzer
public double getOwnershipPercentage(List<String> ownerships, String ownerCompany, String targetCompany)
ownerships contains the direct ownership relationships.ownerCompany is the company whose ownership is being calculated.targetCompany is the company being owned.targetCompany owned by ownerCompany.Every relationship uses the format "ownerCompany,ownedCompany,percentage".
ownerCompany directly owns percentage percent of ownedCompany."atlas,beacon,40" means that atlas directly owns 40% of beacon.100% of itself.p1, p2, ..., pk, its contribution is 100 * (p1 / 100) * (p2 / 100) * ... * (pk / 100).ownerCompany to targetCompany.0.0 when no ownership path exists.ownerships list must not be modified.1 ≤ ownerships.size() ≤ 100,0001 ≤ companyId.length() ≤ 200 < percentage ≤ 100100.ownerCompany and targetCompany appear in the supplied relationships.0.000000001.ownerships, its elements, ownerCompany, and targetCompany are never null.getOwnershipPercentage( ownerships = List.of("atlas,beacon,40", "beacon,delta,50", "atlas,cobalt,25", "cobalt,delta,20", "atlas,delta,10"), ownerCompany = "atlas", targetCompany = "delta")
Output: 35.0
The direct ownership is 10%. The paths through beacon and cobalt contribute 40% * 50% = 20% and 25% * 20% = 5%. The total is 35%.
getOwnershipPercentage( ownerships = List.of("atlas,beacon,40", "beacon,delta,50", "atlas,cobalt,25", "cobalt,delta,20", "atlas,delta,10"), ownerCompany = "delta", targetCompany = "atlas")
Output: 0.0
There is no ownership path from delta to atlas.
getOwnershipPercentage( ownerships = List.of("atlas,beacon,40", "beacon,delta,50"), ownerCompany = "beacon", targetCompany = "beacon")
Output: 100.0
A company owns 100% of itself.