Skip to content

Latest commit

 

History

History
87 lines (62 loc) · 3.44 KB

022.md

File metadata and controls

87 lines (62 loc) · 3.44 KB

Difficulty: 🟡 Medium

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.

Examples:

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

Solutions

O(n^2) solution in Python3

class Solution:
    def findMatrix(self, nums: List[int]) -> List[List[int]]:
        counter = Counter(nums)

        result = []
        for _ in range(max(counter.values())):
            array = []
            for num in counter.keys():
                if counter[num] > 0:
                    array.append(num)
                    counter[num] -= 1
            result.append(array)
        return result

The given solution uses a counter to count the occurrences of each number in the nums array. It then creates a 2D array by iteratively selecting distinct numbers from the counter until all occurrences of each number have been used.

The algorithm works as follows:

  1. Create a counter to count the occurrences of each number in the nums array.
  2. Initialize an empty result list to store the rows of the 2D array.
  3. Iterate until all occurrences of each number have been used:
    • Create an empty array to represent a row in the 2D array.
    • Iterate through each distinct number in the counter:
      • If the counter value for the number is greater than 0, append the number to the row array and decrement the counter value by 1.
    • Append the row array to the result list.
  4. Return the resulting 2D array.

Analysis of Time and Space Complexity

The time complexity of this algorithm is O(n^2), where n is the length of the nums array. The algorithm iterates over the counter and builds the rows of the 2D array.

The space complexity of the algorithm is O(n), where n is the length of the nums array. The algorithm uses additional space to store the counter and the resulting 2D array.

Summary

The given solution constructs a 2D array from the nums array by satisfying the conditions mentioned in the problem statement. It uses a counter to count the occurrences of each number and builds the rows of the 2D array by iteratively selecting distinct numbers from the counter. The algorithm has a time complexity of O(n^2) and a space complexity of O(n), making it an efficient solution for the problem at hand.

NB: If you want to get community points please suggest solutions in other languages as merge requests.