Rank Transform of an Array - Easy - LeetCode
💻 coding

Rank Transform of an Array - Easy - LeetCode

2 min read 382 words
2 min read
ShareWhatsAppPost on X
  • 1The rank transform replaces each element in an array with its rank based on size, starting from 1.
  • 2Equal elements in the array share the same rank, ensuring ranks are minimized.
  • 3The solution has a time complexity of O(n) and a space complexity of O(n).

AI-generated summary · May not capture all nuances

Key Insight
AskGif

"The rank transform replaces each element in an array with its rank based on size, starting from 1."

Rank Transform of an Array - Easy - LeetCode

Given an array of integers arr, replace each element with its rank.

The rank represents how large the element is. The rank has the following rules:

Rank is an integer starting from 1.

The larger the element, the larger the rank. If two elements are equal, their rank must be the same.

Rank should be as small as possible.

Example 1:

Input: arr = [40,10,20,30]

Output: [4,1,2,3]

Explanation: 40 is the largest element. 10 is the smallest. 20 is the second smallest. 30 is the third smallest.

Example 2:

Input: arr = [100,100,100]

Output: [1,1,1]

Explanation: Same elements share the same rank.

Example 3:

Input: arr = [37,12,28,9,100,56,80,5,12]

Output: [5,3,4,2,8,6,7,1,3]

Constraints:

0 <= arr.length <= 105

-109 <= arr[i] <= 109

Solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LeetCode.AskGif.Easy.Array
{
 public class ArrayRankTransformSoln
 {
 public int[] ArrayRankTransform(int[] arr)
 {
 var map = new Dictionary<int, int>();
 var mapRank = new Dictionary<int, int>();
 for (int i = 0; i < arr.Length; i++)
 {
 if (map.ContainsKey(arr[i]))
 {
 map[arr[i]]++;
 }
 else
 {
 map.Add(arr[i], 1);
 }
 }

 var sorted = map.OrderBy(x => x.Key);
 var rank = 1;
 foreach (var item in sorted)
 {
 mapRank.Add(item.Key, rank);
 rank++;
 }

 var res = new int[arr.Length];
 for (int i = 0; i < arr.Length; i++)
 {
 res[i] = mapRank[arr[i]];
 }

 return res;
 }
 }
}

Time Complexity: O(n)

Space Complexity: O(n)

Unit Tests:

using LeetCode.AskGif.Easy.Array;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Text;

namespace CodingUnitTest.Easy.Array
{
 [TestClass]
 public class ArrayRankTransformSolnTests
 {
 [TestMethod]
 public void ArrayRankTransformSoln_First()
 {
 var arr = new int[] { 40, 10, 20, 30 };
 var output = new int[]{ 4, 1, 2, 3 };
 var res = new ArrayRankTransformSoln().ArrayRankTransform(arr);

 AreEqual(res, output);
 }

 [TestMethod]
 public void ArrayRankTransformSoln_Second()
 {
 var arr = new int[] { 100, 100, 100 };
 var output = new int[] { 1, 1, 1 };
 var res = new ArrayRankTransformSoln().ArrayRankTransform(arr);

 AreEqual(res, output);
 }

 [TestMethod]
 public void ArrayRankTransformSoln_Third()
 {
 var arr = new int[] { 37, 12, 28, 9, 100, 56, 80, 5, 12 };
 var output = new int[] { 5, 3, 4, 2, 8, 6, 7, 1, 3 };
 var res = new ArrayRankTransformSoln().ArrayRankTransform(arr);

 AreEqual(res, output);
 }

 private void AreEqual(int[] res, int[] output)
 {
 Assert.AreEqual(res.Length, output.Length);
 for (int i = 0; i < res.Length; i++)
 {
 Assert.AreEqual(res[i], output[i]);
 }
 }
 }
}

Enjoyed this article?

Share it with someone who'd find it useful.

ShareWhatsAppPost on X

AskGif

Published on 8 June 2020 · 2 min read · 382 words

Part of AskGif Blog · coding

You might also like