Skip to content

Commit b6f9dda

Browse files
committed
Added 2 solutions
1 parent 292033d commit b6f9dda

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

Easy/Relative Sort Array.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
public int[] relativeSortArray(int[] A1, int[] A2) {
3+
Map<Integer, Integer> a2Map = new LinkedHashMap<>();
4+
Map<Integer, Integer> a1Map = new HashMap<>();
5+
int idx = 0;
6+
for (int num : A2) {
7+
if (!a2Map.containsKey(num)) {
8+
a2Map.put(num, idx++);
9+
}
10+
}
11+
12+
for (int num : A1) {
13+
a1Map.put(num, a1Map.getOrDefault(num, 0) + 1);
14+
}
15+
16+
int i = 0;
17+
for (Integer key : a2Map.keySet()) {
18+
int count = a1Map.getOrDefault(key, 0);
19+
while (count-- > 0) {
20+
A1[i++] = key;
21+
}
22+
23+
a1Map.remove(key);
24+
}
25+
26+
List<Integer> leftOver = new ArrayList<>();
27+
for (Integer key : a1Map.keySet()) {
28+
int count = a1Map.get(key);
29+
while (count-- > 0) {
30+
leftOver.add(key);
31+
}
32+
}
33+
34+
Collections.sort(leftOver);
35+
for (int num : leftOver) {
36+
A1[i++] = num;
37+
}
38+
39+
return A1;
40+
}
41+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public boolean canDivideIntoSubsequences(int[] nums, int K) {
3+
int maxCount = 1;
4+
int count = 1;
5+
int idx = 1;
6+
int n = nums.length;
7+
while (idx < n) {
8+
if (nums[idx] == nums[idx - 1]) {
9+
count++;
10+
}
11+
else {
12+
maxCount = Math.max(maxCount, count);
13+
count = 1;
14+
}
15+
16+
idx++;
17+
}
18+
19+
maxCount = Math.max(maxCount, count);
20+
21+
return nums.length >= maxCount * K;
22+
}
23+
}

0 commit comments

Comments
 (0)