2610. Convert an Array Into a 2D Array With Conditions Medium
1/**
2 * [2610] Convert an Array Into a 2D Array With Conditions
3 *
4 * You are given an integer array nums. You need to create a 2D array from nums satisfying the following conditions:
5 *
6 * The 2D array should contain only the elements of the array nums.
7 * Each row in the 2D array contains distinct integers.
8 * The number of rows in the 2D array should be minimal.
9 *
10 * Return the resulting array. If there are multiple answers, return any of them.
11 * Note that the 2D array can have a different number of elements on each row.
12 *
13 * <strong class="example">Example 1:
14 *
15 * Input: nums = [1,3,4,1,2,3,1]
16 * Output: [[1,3,4,2],[1,3],[1]]
17 * Explanation: We can create a 2D array that contains the following rows:
18 * - 1,3,4,2
19 * - 1,3
20 * - 1
21 * All elements of nums were used, and each row of the 2D array contains distinct integers, so it is a valid answer.
22 * It can be shown that we cannot have less than 3 rows in a valid array.
23 * <strong class="example">Example 2:
24 *
25 * Input: nums = [1,2,3,4]
26 * Output: [[4,3,2,1]]
27 * Explanation: All elements of the array are distinct, so we can keep all of them in the first row of the 2D array.
28 *
29 *
30 * Constraints:
31 *
32 * 1 <= nums.length <= 200
33 * 1 <= nums[i] <= nums.length
34 *
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions/
39// discuss: https://leetcode.com/problems/convert-an-array-into-a-2d-array-with-conditions/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44 pub fn find_matrix(nums: Vec<i32>) -> Vec<Vec<i32>> {
45 vec![]
46 }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53 use super::*;
54
55 #[test]
56 fn test_2610() {
57 }
58}
59
Back
© 2025 bowen.ge All Rights Reserved.