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.
1views = [
2 [1, 3,
The Python implementation makes use of sets to hold unique author_id
s where the author is the viewer. This approach ensures that duplicates are automatically eliminated in an O(1) time complexity for insertions.