2610. Convert an Array Into a 2D Array With Conditions LeetCode Solution
In this guide, you will get 2610. Convert an Array Into a 2D Array With Conditions LeetCode Solution with the best time and space complexity. The solution to Convert an Array Into a D Array With Conditions problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Convert an Array Into a D Array With Conditions solution in C++
- Convert an Array Into a D Array With Conditions solution in Java
- Convert an Array Into a D Array With Conditions solution in Python
- Additional Resources
Problem Statement of Convert an Array Into a D Array With Conditions
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
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2610. Convert an Array Into a 2D Array With Conditions LeetCode Solution in C++
class Solution {
public:
vector<vector<int>> findMatrix(vector<int>& nums) {
// The number of rows we need equals the maximum frequency.
vector<vector<int>> ans;
vector<int> count(nums.size() + 1);
for (const int num : nums) {
// Construct `ans` on demand.
if (++count[num] > ans.size())
ans.push_back({});
ans[count[num] - 1].push_back(num);
}
return ans;
}
};
/* code provided by PROGIEZ */
2610. Convert an Array Into a 2D Array With Conditions LeetCode Solution in Java
class Solution {
public List<List<Integer>> findMatrix(int[] nums) {
// The number of rows we need equals the maximum frequency.
List<List<Integer>> ans = new ArrayList<>();
int[] count = new int[nums.length + 1];
for (final int num : nums) {
// Construct `ans` on demand.
if (++count[num] > ans.size())
ans.add(new ArrayList<>());
ans.get(count[num] - 1).add(num);
}
return ans;
}
}
// code provided by PROGIEZ
2610. Convert an Array Into a 2D Array With Conditions LeetCode Solution in Python
class Solution:
def findMatrix(self, nums: list[int]) -> list[list[int]]:
# The number of rows we need equals the maximum frequency.
ans = []
count = [0] * (len(nums) + 1)
for num in nums:
count[num] += 1
# Construct `ans` on demand.
if count[num] > len(ans):
ans.append([])
ans[count[num] - 1].append(num)
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.