In this video, I explain LeetCode 1331 – Rank Transform of an Array in C++ with two efficient approaches, along with their intuition, implementation, and time complexity.
📌 Approaches Covered
✅ Approach 1: Ordered Map (std::map)
Store unique elements in sorted order
Assign ranks while traversing the map
Replace each element with its corresponding rank
✅ Approach 2: Sorting + Unordered Map (sort + unordered_map)
Create a copy of the array and sort it
Assign ranks only to unique elements
Use an unordered_map for O(1) average lookups
Replace the original array with its ranks
⏱️ Time Complexity
Approach 1 (Ordered Map)
Time: O(n log n)
Space: O(n)
Approach 2 (Sorting + Unordered Map)
Time: O(n log n)
Space: O(n)
If you’re preparing for coding interviews, placements, or LeetCode contests, this problem is a great example of choosing the right data structure for efficient ranking and coordinate compression.
👍 If you found the explanation helpful, don’t forget to Like, Share, and Subscribe for more DSA and LeetCode solutions in C++.
#LeetCode #DSA #CPP #CPlusPlus #CodingInterview #Programming #Algorithms #DataStructures #CompetitiveProgramming #LeetCode1331 #Array #Sorting #Map #UnorderedMap #PlacementPreparation