Top Articles by Comment Count
Learn this problemProblem statement
You are given four inputs that describe a finite collection of article records:
titles[i]is the article's primary title, or an empty string when it is missing.fallbackTitles[i]is its fallback title, or an empty string when it is missing.commentCounts[i]is its number of comments, or-1when the count is missing.limitis the maximum number of titles to return.
For each record, use titles[i] when it is nonempty; otherwise use fallbackTitles[i]. Ignore a record when both title fields are empty. Treat a missing comment count as 0.
Sort the remaining records by comment count in descending order. Break ties by display title in ascending ASCII lexicographic order. Return the first limit display titles, or all usable titles when fewer than limit remain.
Function
topArticles(titles: String[], fallbackTitles: String[], commentCounts: int[], limit: int) → String[]Examples
Example 1
titles = ["First","","Third"]fallbackTitles = ["","Second Story",""]commentCounts = [10,12,12]limit = 2return = ["Second Story","Third"]Second Story and Third both have 12 comments, so their titles break the tie.
Example 2
titles = ["Alpha","Beta",""]fallbackTitles = ["","","Gamma"]commentCounts = [-1,0,5]limit = 5return = ["Gamma","Alpha","Beta"]The missing count for Alpha becomes 0. Alpha then comes before Beta by title.
Example 3
titles = ["","B","C",""]fallbackTitles = ["","","","A"]commentCounts = [100,3,3,3]limit = 2return = ["A","B"]The first record is ignored because it has no usable title, even though its count is largest. The other records tie at 3 comments and are ordered by title.
Constraints
1 <= titles.length = fallbackTitles.length = commentCounts.length <= 1000001 <= limit <= 100000- Each title is empty or has length from
1to100. - Nonempty titles contain only ASCII letters, digits, and single spaces between words.
-1 <= commentCounts[i] <= 1000000000;-1is the only missing-count marker.