Problem · Array

Top Three Companies by Profit

Learn this problem
EasyCresta logoCrestaFULLTIMEOA

Problem statement

You are given unique company names in companyNames. For each index i, dailyProfits[i] contains that company's integer profit values over several days.

Compute each company's total profit and return the names of the companies with the three highest totals. Order the result by total profit descending. If totals are equal, order those companies by name lexicographically ascending. If fewer than three companies are provided, return every company in that order.

Function

topThreeCompanies(companyNames: String[], dailyProfits: long[][]) → String[]

Examples

Example 1

companyNames = ["Orion","Nova","Atlas","Zen"]dailyProfits = [[3,5],[10,-2],[4,4],[1,1]]return = ["Atlas","Nova","Orion"]

Atlas, Nova, and Orion each total 8. Their equal totals are resolved lexicographically, so Atlas comes before Nova, then Orion.

Example 2

companyNames = ["Beta","Alpha"]dailyProfits = [[-5],[-1,0]]return = ["Alpha","Beta"]

Alpha totals -1 and Beta totals -5. Because only two companies are provided, both are returned.

Example 3

companyNames = ["A","B","C","D","E"]dailyProfits = [[9],[-2,20],[5,5],[100,-100],[8]]return = ["B","C","A"]

The totals are 9, 18, 10, 0, 8. Therefore B, C, and A occupy the first three ranks.

Constraints

  • 1 <= companyNames.length == dailyProfits.length <= 10^5.
  • Every company name is unique, has length from 1 through 30, and contains only English letters.
  • The total number of entries across dailyProfits is at most 10^5.
  • -10^9 <= dailyProfits[i][j] <= 10^9.
  • Use 64-bit arithmetic for company totals.

More Cresta problems

drafts saved locally
public String[] topThreeCompanies(String[] companyNames, long[][] dailyProfits) {
  // write your code here
}
companyNames["Orion","Nova","Atlas","Zen"]
dailyProfits[[3,5],[10,-2],[4,4],[1,1]]
expected["Atlas", "Nova", "Orion"]
checking account