#3808

Find Emotionally Consistent Users

Medium
Hash MapArray
LeetCode ↗

Approaches

Brute ForceOptimal
Complexity Comparison
Brute ForceOptimal Solution
Time
O(n²)
O(n)
Space
O(1)
O(n)
💡

Intuition

Time O(n)Space O(n)

Use a single pass to count reactions and determine consistency, leveraging hash maps for efficient counting.

⚙️

Algorithm

3 steps
  1. 1Step 1: Count each user's reactions and the number of distinct content items.
  2. 2Step 2: Calculate the maximum reaction count and check if it meets the 60% threshold.
  3. 3Step 3: Return users who meet the criteria, sorted by reaction ratio and user ID.
solution.py1 lines
1SELECT user_id, ROUND(MAX(count) / total_reactions * 100, 2) AS reaction_ratio FROM (SELECT user_id, reaction, COUNT(*) AS count, COUNT(DISTINCT content_id) AS total_reactions FROM reactions GROUP BY user_id, reaction) AS user_reactions WHERE total_reactions >= 5 GROUP BY user_id HAVING MAX(count) / total_reactions >= 0.6 ORDER BY reaction_ratio DESC, user_id ASC;

Complexity note: This complexity is linear due to processing each reaction once and using hash maps for counting.

  • 1Users must react to at least 5 different contents.
  • 2Consistency is defined by the majority reaction type.

Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.