Skip to content

Commit 4c1626c

Browse files
refactor 1122
1 parent 066f232 commit 4c1626c

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Your ideas/fixes/algorithms are more than welcome!
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
3030
|5087|[Letter Tile Possibilities](https://leetcode.com/problems/letter-tile-possibilities/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_5087.java) | O(1) | O(1) | |Medium||
3131
|5083|[Occurrences After Bigram](https://leetcode.com/problems/occurrences-after-bigram/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_5083.java) | O(n) | O(1) | |Easy||
32+
|1122|[Relative Sort Array](https://leetcode.com/problems/relative-sort-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1122.java) | O(n) | O(n) | |Easy||
3233
|1071|[Greatest Common Divisor of Strings](https://leetcode.com/problems/greatest-common-divisor-of-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1071.java) | O(m*n) | O(1) | |Easy||
3334
|1065|[Index Pairs of a String](https://leetcode.com/problems/index-pairs-of-a-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1065.java) | O(nlogn) | O(1) | |Medium||
3435
|1056|[Confusing Number](https://leetcode.com/problems/confusing-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1056.java) | O(n) | O(1) | |Easy||
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.Arrays;
4+
import java.util.TreeMap;
5+
6+
/**
7+
* 1122. Relative Sort Array
8+
*
9+
* Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.
10+
* Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2.
11+
* Elements that don't appear in arr2 should be placed at the end of arr1 in ascending order.
12+
*
13+
* Example 1:
14+
* Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
15+
* Output: [2,2,2,1,4,3,3,9,6,7,19]
16+
*
17+
* Constraints:
18+
* arr1.length, arr2.length <= 1000
19+
* 0 <= arr1[i], arr2[i] <= 1000
20+
* Each arr2[i] is distinct.
21+
* Each arr2[i] is in arr1.
22+
* */
23+
public class _1122 {
24+
public static class Solution1 {
25+
public int[] relativeSortArray(int[] arr1, int[] arr2) {
26+
TreeMap<Integer, Integer> map = new TreeMap<>();
27+
Arrays.stream(arr1).forEach(num -> map.put(num, map.getOrDefault(num, 0) + 1));
28+
int[] result = new int[arr1.length];
29+
int i = 0;
30+
for (int num : arr2) {
31+
int count = map.get(num);
32+
while (count-- > 0) {
33+
result[i++] = num;
34+
}
35+
map.remove(num);
36+
}
37+
for (int key : map.keySet()) {
38+
int count = map.get(key);
39+
while (count-- > 0) {
40+
result[i++] = key;
41+
}
42+
}
43+
return result;
44+
}
45+
}
46+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1122;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _1122Test {
10+
private static _1122.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1122.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertArrayEquals(new int[]{2, 2, 2, 1, 4, 3, 3, 9, 6, 7, 19}, solution1.relativeSortArray(new int[]{2, 3, 1, 3, 2, 4, 6, 7, 9, 2, 19}, new int[]{2, 1, 4, 3, 9, 6}));
20+
}
21+
22+
}

0 commit comments

Comments
 (0)