Skip to content

Commit c057545

Browse files
committed
2022. Convert 1D Array Into 2D Array: AC
1 parent 93563c2 commit c057545

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/solution/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,3 +1523,4 @@ mod s2016_maximum_difference_between_increasing_elements;
15231523
mod s2017_grid_game;
15241524
mod s2018_check_if_word_can_be_placed_in_crossword;
15251525
mod s2019_the_score_of_students_solving_math_expression;
1526+
mod s2022_convert_1d_array_into_2d_array;
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
* [2022] Convert 1D Array Into 2D Array
3+
*
4+
* You are given a 0-indexed 1-dimensional (1D) integer array original, and two integers, m and n. You are tasked with creating a 2-dimensional (2D) array with m rows and n columns using all the elements from original.
5+
* The elements from indices 0 to n - 1 (inclusive) of original should form the first row of the constructed 2D array, the elements from indices n to 2 * n - 1 (inclusive) should form the second row of the constructed 2D array, and so on.
6+
* Return an m x n 2D array constructed according to the above procedure, or an empty 2D array if it is impossible.
7+
*
8+
* Example 1:
9+
* <img src="https://assets.leetcode.com/uploads/2021/08/26/image-20210826114243-1.png" style="width: 500px; height: 174px;" />
10+
* Input: original = [1,2,3,4], m = 2, n = 2
11+
* Output: [[1,2],[3,4]]
12+
* Explanation: The constructed 2D array should contain 2 rows and 2 columns.
13+
* The first group of n=2 elements in original, [1,2], becomes the first row in the constructed 2D array.
14+
* The second group of n=2 elements in original, [3,4], becomes the second row in the constructed 2D array.
15+
*
16+
* Example 2:
17+
*
18+
* Input: original = [1,2,3], m = 1, n = 3
19+
* Output: [[1,2,3]]
20+
* Explanation: The constructed 2D array should contain 1 row and 3 columns.
21+
* Put all three elements in original into the first row of the constructed 2D array.
22+
*
23+
* Example 3:
24+
*
25+
* Input: original = [1,2], m = 1, n = 1
26+
* Output: []
27+
* Explanation: There are 2 elements in original.
28+
* It is impossible to fit 2 elements in a 1x1 2D array, so return an empty 2D array.
29+
*
30+
*
31+
* Constraints:
32+
*
33+
* 1 <= original.length <= 5 * 10^4
34+
* 1 <= original[i] <= 10^5
35+
* 1 <= m, n <= 4 * 10^4
36+
*
37+
*/
38+
pub struct Solution {}
39+
40+
// problem: https://leetcode.com/problems/convert-1d-array-into-2d-array/
41+
// discuss: https://leetcode.com/problems/convert-1d-array-into-2d-array/discuss/?currentPage=1&orderBy=most_votes&query=
42+
43+
// submission codes start here
44+
45+
impl Solution {
46+
pub fn construct2_d_array(original: Vec<i32>, m: i32, n: i32) -> Vec<Vec<i32>> {
47+
if (m * n) as usize != original.len() {
48+
return Vec::new();
49+
}
50+
51+
let mut result = Vec::with_capacity(m as usize);
52+
53+
for i in 0..m {
54+
let start = (i * n) as usize;
55+
let end = start + n as usize;
56+
result.push(original[start..end].to_vec());
57+
}
58+
59+
result
60+
}
61+
}
62+
63+
// submission codes end
64+
65+
#[cfg(test)]
66+
mod tests {
67+
use super::*;
68+
69+
#[test]
70+
fn test_2022_example_1() {
71+
let original = vec![1, 2, 3, 4];
72+
let m = 2;
73+
let n = 2;
74+
75+
let result = vec![vec![1, 2], vec![3, 4]];
76+
77+
assert_eq!(Solution::construct2_d_array(original, m, n), result);
78+
}
79+
80+
#[test]
81+
fn test_2022_example_2() {
82+
let original = vec![1, 2, 3];
83+
let m = 1;
84+
let n = 3;
85+
86+
let result = vec![vec![1, 2, 3]];
87+
88+
assert_eq!(Solution::construct2_d_array(original, m, n), result);
89+
}
90+
91+
#[test]
92+
fn test_2022_example_3() {
93+
let original = vec![1, 2];
94+
let m = 1;
95+
let n = 1;
96+
97+
let result = vec![];
98+
99+
assert_eq!(Solution::construct2_d_array(original, m, n), result);
100+
}
101+
}

0 commit comments

Comments
 (0)