LeetCode Entry
1331. Rank Transform of an Array
Rank of items in an array
1331. Rank Transform of an Array easy substack youtube
https://dmitrysamoylenko.com/leetcode/

Join me on Telegram
https://t.me/leetcode_daily_unstoppable/1418
Problem TLDR
Rank of items in an array
Intuition
Sort. Dedup. Binary search each number.
Approach
- we can use toSortedMap().run { … headSet(it).size } but leetcode give TLE, still O(nlogn)
- Rust: use itertools and collect tuples to the hashmap
Complexity
-
Time complexity: \(O(nlogn)\)
-
Space complexity: \(O(n)\)
Code
fun arrayRankTransform(a: IntArray) =
a.toSet().sorted().run { a.map { binarySearch(it) + 1 } }
6
Comments