FastPrepRestaurant Vote Leaders

Restaurant Vote Leaders

ZipRecruiter logoZipRecruiter● EasyFULLTIMENEW GRADPHONE SCREENONSITE INTERVIEW
Learn

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 weighted is false, every cell contributes 1 point to its restaurant.
  • When weighted is true and a row has m columns, the restaurant at index j receives m - j points. Each row therefore assigns weights m, 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 1 and 50 characters.
  • The total number of cells is at most 100000.
  • If rankings is rectangular, every row has the same number of columns.

More ZipRecruiter problems

See ZipRecruiter hiring insights
public String[] restaurantVoteLeaders(String[][] rankings, boolean weighted) {
    // Write your solution here.
}
rankings[["a","b","c"],["a","c","d"]]
weightedfalse
expected["a", "c"]
Checking account…