Skip to content

Commit d94511b

Browse files
2610 medium java good array list hashtable
You are given an integer array nums. You need to create a 2D array from nums satisfying the following conditions: The 2D array should contain only the elements of the array nums. Each row in the 2D array contains distinct integers. The number of rows in the 2D array should be minimal. Return the resulting array. If there are multiple answers, return any of them. Note that the 2D array can have a different number of elements on each row. Example 1: Input: nums = [1,3,4,1,2,3,1] Output: [[1,3,4,2],[1,3],[1]] Explanation: We can create a 2D array that contains the following rows: - 1,3,4,2 - 1,3 - 1 All elements of nums were used, and each row of the 2D array contains distinct integers, so it is a valid answer. It can be shown that we cannot have less than 3 rows in a valid array. Example 2: Input: nums = [1,2,3,4] Output: [[4,3,2,1]] Explanation: All elements of the array are distinct, so we can keep all of them in the first row of the 2D array. Constraints: 1 <= nums.length <= 200 1 <= nums[i] <= nums.length
1 parent 1c966be commit d94511b

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Approach 1
2+
// Initialize an empty 2D list called list to store the sub-lists.
3+
// Initialize a variable count to keep track of the number of elements processed.
4+
// Use a while loop to iterate until all the elements in the nums array are processed.
5+
// Inside the loop, initialize a new sub-list ans to store unique integers from nums array.
6+
// For each element nums[i], if its not already present in the ans list, add it to ans, set nums[i] to zero (to mark it as processed), and increment count.
7+
// On reaching the end of nums array, add the ans list to the 2D list.
8+
// Continue this process until all elements of nums are processed.
9+
10+
11+
// class Solution {
12+
// public List<List<Integer>> findMatrix(int[] nums) {
13+
// List<List<Integer>> list=new ArrayList<>();
14+
// int count=0;
15+
// while(count<nums.length){
16+
// ArrayList<Integer> ans=new ArrayList<>();
17+
// for(int i=0;i<nums.length;i++){
18+
// if(nums[i]!=0 && !ans.contains(nums[i])){
19+
// ans.add(nums[i]);
20+
// nums[i]=0;
21+
// count++;
22+
// }
23+
// }
24+
// list.add(ans);
25+
// }
26+
// return list;
27+
// }
28+
// }
29+
30+
31+
// This is a more optimized approach to achieve the same result.
32+
// Initialize an empty 2D list called list to store the sub-lists.
33+
// Initialize an integer array count of size nums.length + 1 to keep track of the count of each unique integer encountered in nums.
34+
// Iterate through each element i in the array nums.
35+
// Check if the count of the current integer i is equal to the size of the list. If it is, this implies that we have to add a new empty sub-list to list.
36+
// Add the current integer i to the appropriate sub-list in list, determined by its count stored in the count array.
37+
// Continue this process for all elements in the nums array.
38+
class Solution {
39+
public List<List<Integer>> findMatrix(int[] nums) {
40+
List<List<Integer>> list=new ArrayList<>();
41+
int[] count=new int[nums.length+1];
42+
for(int i:nums){
43+
if(count[i]==list.size()){
44+
list.add(new ArrayList<>());
45+
}
46+
list.get(count[i]).add(i);
47+
count[i]++;
48+
}
49+
return list;
50+
}
51+
}

0 commit comments

Comments
 (0)