|
| 1 | +--- |
| 2 | +id: convert-an-array-into-a-2d-array-with-conditions |
| 3 | +title: Convert an Array Into a 2D Array With Conditions |
| 4 | +sidebar_label: 2610 Convert an Array Into a 2D Array With Conditions |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Hash Table |
| 8 | + - LeetCode |
| 9 | + - C++ |
| 10 | +description: "This is a solution to the Convert an Array Into a 2D Array With Conditions problem on LeetCode." |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem Description |
| 14 | + |
| 15 | +You are given an integer array nums. You need to create a 2D array from nums satisfying the following conditions: |
| 16 | + |
| 17 | +- The 2D array should contain only the elements of the array nums. |
| 18 | +- Each row in the 2D array contains distinct integers. |
| 19 | +- The number of rows in the 2D array should be minimal. |
| 20 | +Return the resulting array. If there are multiple answers, return any of them. |
| 21 | + |
| 22 | +Note that the 2D array can have a different number of elements on each row. |
| 23 | + |
| 24 | +### Examples |
| 25 | + |
| 26 | +**Example 1:** |
| 27 | + |
| 28 | +``` |
| 29 | +
|
| 30 | +Input: nums = [1,2] |
| 31 | +Output: 2 |
| 32 | +``` |
| 33 | + |
| 34 | +**Example 2:** |
| 35 | + |
| 36 | +``` |
| 37 | +Input: nums = [1,3,4,1,2,3,1] |
| 38 | +Output: [[1,3,4,2],[1,3],[1]] |
| 39 | +``` |
| 40 | + |
| 41 | +**Example 2:** |
| 42 | + |
| 43 | +``` |
| 44 | +Input: nums = [1,2,3,4] |
| 45 | +Output: [[4,3,2,1]] |
| 46 | +``` |
| 47 | + |
| 48 | +### Constraints |
| 49 | + |
| 50 | +- $1 \leq \text{nums.length} \leq 200$. |
| 51 | +- $1 \leq \text{nums[i]} \leq \text{nums.length}$. |
| 52 | + |
| 53 | +### Approach |
| 54 | + |
| 55 | +To solve this problem(convert an array into a 2d array with conditions)we simply have to use an unordered map which will keep track if we can insert a particular number in the row. we will process the elements in the array one by one in any order and only create a new row in the matrix when we cannot put it into the existing rows. |
| 56 | + |
| 57 | +#### Code in C++ |
| 58 | + |
| 59 | +```cpp |
| 60 | +class Solution { |
| 61 | +public: |
| 62 | + vector<vector<int>> findMatrix(vector<int>& nums) { |
| 63 | + vector<vector<int>>a; |
| 64 | + unordered_map<int,int>b; |
| 65 | + int r=0; |
| 66 | + for(int i=0;i<nums.size();i++){ |
| 67 | + b[nums[i]]++; |
| 68 | + if(b[nums[i]]>r){ |
| 69 | + r++; |
| 70 | + a.push_back({nums[i]}); |
| 71 | + } |
| 72 | + else{ |
| 73 | + a[b[nums[i]]-1].push_back(nums[i]); |
| 74 | + } |
| 75 | + } |
| 76 | + return a; |
| 77 | + } |
| 78 | +}; |
| 79 | +``` |
| 80 | +
|
| 81 | +#### Code in Java |
| 82 | +
|
| 83 | +```java |
| 84 | +class Solution { |
| 85 | +
|
| 86 | + public List<List<Integer>> findMatrix(int[] nums) { |
| 87 | + HashMap<Integer, Integer> map = new HashMap(); |
| 88 | + for (int n : nums) { |
| 89 | + map.put(n, map.getOrDefault(n, 0) + 1); |
| 90 | + } |
| 91 | +
|
| 92 | + List<List<Integer>> ll = new ArrayList(); |
| 93 | + for (Map.Entry<Integer, Integer> entry : map.entrySet()) { |
| 94 | + int key = entry.getKey(); |
| 95 | + int val = entry.getValue(); |
| 96 | + for (int i = ll.size(); i < val; i++) ll.add(new ArrayList()); |
| 97 | + int i = 0; |
| 98 | + while (val-- > 0) { |
| 99 | + ll.get(i++).add(key); |
| 100 | + } |
| 101 | + } |
| 102 | +
|
| 103 | + return ll; |
| 104 | + } |
| 105 | +} |
| 106 | +
|
| 107 | +``` |
| 108 | + |
| 109 | + |
0 commit comments