Skip to content

Commit 11bdae6

Browse files
add 1331
1 parent f727cb0 commit 11bdae6

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1331|[Rank Transform of an Array](https://leetcode.com/problems/rank-transform-of-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1331.java) | |Easy||
1112
|1317|[Convert Integer to the Sum of Two No-Zero Integers](https://leetcode.com/problems/convert-integer-to-the-sum-of-two-no-zero-integers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1317.java) | |Easy||
1213
|1305|[All Elements in Two Binary Search Trees](https://leetcode.com/problems/all-elements-in-two-binary-search-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1305.java) | |Medium||
1314
|1304|[Find N Unique Integers Sum up to Zero](https://leetcode.com/problems/find-n-unique-integers-sum-up-to-zero/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1304.java) | |Easy||
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.TreeSet;
6+
7+
/**
8+
* 1331. Rank Transform of an Array
9+
*
10+
* Given an array of integers arr, replace each element with its rank.
11+
* The rank represents how large the element is. The rank has the following rules:
12+
* Rank is an integer starting from 1.
13+
* The larger the element, the larger the rank. If two elements are equal, their rank must be the same.
14+
* Rank should be as small as possible.
15+
*
16+
* Example 1:
17+
* Input: arr = [40,10,20,30]
18+
* Output: [4,1,2,3]
19+
* Explanation: 40 is the largest element. 10 is the smallest. 20 is the second smallest. 30 is the third smallest.
20+
*
21+
* Example 2:
22+
* Input: arr = [100,100,100]
23+
* Output: [1,1,1]
24+
* Explanation: Same elements share the same rank.
25+
*
26+
* Example 3:
27+
* Input: arr = [37,12,28,9,100,56,80,5,12]
28+
* Output: [5,3,4,2,8,6,7,1,3]
29+
*
30+
* Constraints:
31+
* 0 <= arr.length <= 105
32+
* -109 <= arr[i] <= 109
33+
* */
34+
public class _1331 {
35+
public static class Solution1 {
36+
public int[] arrayRankTransform(int[] arr) {
37+
TreeSet<Integer> set = new TreeSet<>();
38+
for (int i : arr) {
39+
set.add(i);
40+
}
41+
Map<Integer, Integer> map = new HashMap<>();
42+
int rank = 1;
43+
for (int num : set) {
44+
map.put(num, rank++);
45+
}
46+
int[] ranks = new int[arr.length];
47+
for (int i = 0; i < arr.length; i++) {
48+
ranks[i] = map.get(arr[i]);
49+
}
50+
return ranks;
51+
}
52+
}
53+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1331;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1331Test {
10+
private static _1331.Solution1 solution1;
11+
private static int[] arr;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _1331.Solution1();
16+
}
17+
18+
@Test
19+
public void test1() {
20+
arr = new int[]{40, 10, 20, 30};
21+
assertArrayEquals(new int[]{4, 1, 2, 3}, solution1.arrayRankTransform(arr));
22+
}
23+
24+
@Test
25+
public void test2() {
26+
arr = new int[]{100, 100, 100};
27+
assertArrayEquals(new int[]{1, 1, 1}, solution1.arrayRankTransform(arr));
28+
}
29+
30+
@Test
31+
public void test3() {
32+
arr = new int[]{-1, -3, 100};
33+
assertArrayEquals(new int[]{2, 1, 3}, solution1.arrayRankTransform(arr));
34+
}
35+
}

0 commit comments

Comments
 (0)