-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimalarray.java
42 lines (33 loc) · 1.11 KB
/
minimalarray.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
public List<List<Integer>> findMatrix(int[] nums) {
HashMap<Integer, Integer> map = new HashMap<>();
List<List<Integer>> out = new ArrayList<>();
out.add(new ArrayList<>());
int max = 0;
for(int i=0; i < nums.length; i++){
if(map.containsKey(nums[i])){
map.put(nums[i], map.get(nums[i])+1);
max = Math.max(max, map.get(nums[i]));
}
else{
map.put(nums[i], map.getOrDefault(nums[i],0)+1);
out.get(0).add(nums[i]);
}
}
int k = 0;
while(k < max){
List<Integer> p1 = new ArrayList<>();
for(int i=0; i < out.get(0).size(); i++){
int ele = out.get(0).get(i);
if(map.containsKey(ele) && map.get(ele) != 1)
{
p1.add(ele);
map.put(ele, map.get(ele)-1);
}
}
if(p1.size() > 0) out.add(p1);
k++;
}
return out;
}
}