-
Notifications
You must be signed in to change notification settings - Fork 0
/
Array_conversion.java
34 lines (31 loc) · 1.07 KB
/
Array_conversion.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
// leetcode - 2610. Convert an Array Into a 2D Array With Conditions
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Array_conversion {
public static void main(String[] args) {
int []num = {1,3,4,1,2,3,1};
List<List<Integer>> dynamicList = new ArrayList<>();
dynamicList = findMatrix(num);
for (List<Integer> innerList : dynamicList) {
for (Integer value : innerList) {
System.out.print(value + " ");
}
System.out.println();
}
}
public static List<List<Integer>> findMatrix(int[] nums) {
Map<Integer, Integer> countMap = new HashMap<>();
List<List<Integer>> dynamicList = new ArrayList<>();
for(int num : nums){
int row = countMap.getOrDefault(num, 0);
if(dynamicList.size() == row){
dynamicList.add(new ArrayList<>());
}
dynamicList.get(row).add(num);
countMap.put(num, row + 1);
}
return dynamicList;
}
}