407. Help Article Suggestions
Asked in
Help Article Suggestions

A support website contains n help articles with IDs from 0 to n - 1.

relatedArticles.get(i) is a comma-separated string containing the IDs of articles related to article i. An empty string means that no related article is listed.

Relatedness is undirected and transitive. If article a is related to article b, they belong to the same topic group. If articles a and b belong to the same group, and articles b and c belong to the same group, then all three articles belong to one topic group.

Each article also has a helpfulness score. When a user opens an article, return up to k other articles from the same topic group.

Order suggestions by helpfulness score in descending order. If two articles have the same score, place the article with the smaller ID first. The opened article must not be included.

Constructor

HelpArticleSuggestions

HelpArticleSuggestions( List<String> relatedArticles, List<Integer> helpfulScores )

Parameters

  • relatedArticles: A list where relatedArticles.get(i) contains comma-separated IDs of articles related to article i.
  • helpfulScores: A list where helpfulScores.get(i) is the helpfulness score of article i.

Method

getSuggestions

List<Integer> getSuggestions(int articleId, int k)

Parameters

  • articleId: The ID of the article opened by the user.
  • k: The maximum number of suggestions to return.

Returns

Return up to k other article IDs from the same topic group, ordered by helpfulness score in descending order and then by article ID in ascending order.

Return all available suggestions if the topic group contains fewer than k other articles. Return an empty list if no other article belongs to the same group.

Constraints

  • 1 ≤ relatedArticles.size() ≤ 100,000
  • helpfulScores.size() == relatedArticles.size()
  • Each string in relatedArticles is either empty or contains comma-separated article IDs without spaces.
  • Every listed article ID is between 0 and relatedArticles.size() - 1.
  • An article is not listed as related to itself.
  • The same article ID does not appear more than once in one related article string.
  • The total number of article IDs across all related article strings does not exceed 200,000.
  • 0 ≤ helpfulScores.get(i) ≤ 1,000,000,000
  • 0 ≤ articleId < relatedArticles.size()
  • 1 ≤ k ≤ relatedArticles.size()
  • At most 100,000 calls will be made to getSuggestions.
  • No parameter value will be null.

Examples

Example 1

HelpArticleSuggestions( relatedArticles = List.of("1,2", "0", "4", "", "2"), helpfulScores = List.of(25, 80, 65, 95, 70) )

getSuggestions(articleId = 0, k = 3)

Output: List.of(1, 4, 2)

Articles 0, 1, 2, and 4 belong to one topic group. After excluding article 0, the remaining articles are ordered by their helpfulness scores.

getSuggestions(articleId = 4, k = 2)

Output: List.of(1, 2)

Articles 1 and 2 have the two highest scores among the other articles in article 4's topic group.

getSuggestions(articleId = 3, k = 4)

Output: List.of()

Article 3 does not belong to a topic group containing any other article.

Example 2

HelpArticleSuggestions( relatedArticles = List.of("2", "3", "", "4", "", "4"), helpfulScores = List.of(100, 60, 100, 90, 75, 90) )

getSuggestions(articleId = 1, k = 3)

Output: List.of(3, 5, 4)

Articles 1, 3, 4, and 5 belong to one topic group. Articles 3 and 5 have equal scores, so the smaller ID appears first.

getSuggestions(articleId = 2, k = 2)

Output: List.of(0)

Article 0 is the only other article in article 2's topic group.

getSuggestions(articleId = 5, k = 2)

Output: List.of(3, 4)

After excluding article 5, articles 3 and 4 have the highest helpfulness scores in the group.



Please use Laptop/Desktop or any other large screen to add/edit code.