2022. Convert 1D Array

This is my solution to the LeetCode exercise 2022. Convert 1D Array Into 2D Array. It's an easy array problem that can look more difficult than it actually is.

I tend to overthink and and try to do more than what actually needs to be done when attempting to solve leetcode type problems while in reality the answer may be simpler than what I'm thinking.

Do no overthink it, look for simplicity.
js
/**
* @param {number[]} original
* @param {number} m
* @param {number} n
* @return {number[][]}
*/
var construct2DArray = function (original, m, n) {
if (m * n !== original.length) return []
const res = []
for (let i = 0; i < original.length; i += n) {
const rows = original.slice(i, i + n)
res.push(rows)
}
return res
}
all posts

©2025 Carla Montano | All Rights reserved