
Sponsored
Sponsored
An efficient way to solve this problem is by performing a self-join on the Views table where the author_id is equal to the viewer_id. This will help in identifying rows where authors viewed their own articles. After identifying, we need to select distinct author IDs and return them in ascending order.
Time Complexity: O(n log n) - due to sorting the result.
Space Complexity: O(n) - storing distinct author IDs.
1SELECT DISTINCT V1.author_id as id FROM Views V1 WHERE V1.author_id = V1.viewer_id ORDER BY V1.author_id;The solution involves selecting distinct authors whose author_id matches the viewer_id. The query returns all such instances, selects distinct ids, and orders them as required.
An alternative implementation can employ the use of a data structure such as a set to track those authors that viewed their own articles. We iterate over the Views table and whenever the author_id equates viewer_id, we insert it into the set. Finally, we convert this set into a sorted list of distinct author IDs.
Time Complexity: O(n log n) - due to sorting the set elements.
Space Complexity: O(n) - to store the unique authors in memory.
1let views = [
2 { article_id:
This JavaScript implementation uses a Set object to gather unique author IDs. It iterates through the views array, checks for self-viewed articles, and adds those author IDs to the set which is eventually converted to a sorted array and printed.