Restaurant Vote Leaders
Problem statement
You are given a two-dimensional array of restaurant names, rankings, and a boolean weighted.
Every row is one list of restaurant votes. If the rows do not all have the same length, return an empty array.
- When
weightedisfalse, every cell contributes1point to its restaurant. - When
weightedistrueand a row hasmcolumns, the restaurant at indexjreceivesm - jpoints. Each row therefore assigns weightsm, m - 1, ..., 1.
Return every restaurant whose total score is maximal, in ascending lexicographic order.
Every cell contributes independently, including repeated appearances of the same restaurant within one row. If rankings is empty or its rows contain no names, return an empty array.
Function
restaurantVoteLeaders(rankings: String[][], weighted: boolean) → String[]Examples
Example 1
rankings = [["a","b","c"],["a","c","d"]]weighted = falsereturn = ["a","c"]Restaurants a and c each appear twice. They share the maximum score, so both are returned in lexicographic order.
Example 2
rankings = [["a","b","c"],["a","c","d"]]weighted = truereturn = ["a"]Each row assigns weights 3, 2, and 1. Restaurant a receives 6 points, more than any other restaurant.
Example 3
rankings = [["a","b"],["a"]]weighted = falsereturn = []The rows have different lengths, so the required result is an empty array.
Constraints
0 <= rankings.length <= 1000- Every restaurant name contains between
1and50characters. - The total number of cells is at most
100000. - If
rankingsis rectangular, every row has the same number of columns.