Eat Study Love

먹고 공부하고 사랑하라

Coding_Practice

Convert 1D Array Into 2D Array[E,Array,Matrix,Simulation]

eatplaylove 2024. 7. 26. 23:03

https://leetcode.com/problems/convert-1d-array-into-2d-array/description/

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.

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.

Return an m x n 2D array constructed according to the above procedure, or an empty 2D array if it is impossible.

 

Example 1:

Input: original = [1,2,3,4], m = 2, n = 2
Output: [[1,2],[3,4]]
Explanation: The constructed 2D array should contain 2 rows and 2 columns.
The first group of n=2 elements in original, [1,2], becomes the first row in the constructed 2D array.
The second group of n=2 elements in original, [3,4], becomes the second row in the constructed 2D array.

Example 2:

Input: original = [1,2,3], m = 1, n = 3
Output: [[1,2,3]]
Explanation: The constructed 2D array should contain 1 row and 3 columns.
Put all three elements in original into the first row of the constructed 2D array.

Example 3:

Input: original = [1,2], m = 1, n = 1
Output: []
Explanation: There are 2 elements in original.
It is impossible to fit 2 elements in a 1x1 2D array, so return an empty 2D array.

 

Constraints:

  • 1 <= original.length <= 5 * 104
  • 1 <= original[i] <= 105
  • 1 <= m, n <= 4 * 104

1.Python

 

Easy 난이도인데 개떡같이 풀었다.

 

이게 맞나 싶다~~

class Solution:
    def construct2DArray(self, original: list[int], m: int, n: int) -> list[list[int]]:
        if len(original) != m*n : return []
        ans = [[] for _ in range(m)]
        cut = len(original)/m
        cut2 = len(original)/m
        cnt = 0
        for i in range(len(original)):
            if i == cut :
                cnt+=1
                cut+=cut2
            ans[cnt].append(original[i])
                
        return ans
class Solution:
    def construct2DArray(self, original: list[int], m: int, n: int) -> list[list[int]]:
        if len(original) != m*n : return []
        ans = []

        for i in range(m):
            row = original[i*n:(i+1)*n]
            ans.append(row)

        return ans

알고보니 겁나리 간단한 문제였다. 슬라이싱이면 끝나는데,,

 

C, C++하다보면 사고가 퇴화하는 거 같다.

 

2. C

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** construct2DArray(int* original, int originalSize, int m, int n, int* returnSize, int** returnColumnSizes) {
    // Check if it's possible to form a 2D array of size m x n
    if (originalSize != m * n) {
        *returnSize = 0;
        *returnColumnSizes = NULL;
        return NULL;
    }
    
    // Allocate memory for the 2D array
    int** result = (int**)malloc(m * sizeof(int*));
    // Allocate memory for the column sizes array
    *returnColumnSizes = (int*)malloc(m * sizeof(int));
    
    for (int i = 0; i < m; ++i) {
        result[i] = (int*)malloc(n * sizeof(int));
        (*returnColumnSizes)[i] = n;
        for (int j = 0; j < n; ++j) {
            result[i][j] = original[i * n + j];
        }
    }
    
    *returnSize = m;
    return result;
}

 

C는 내 영역을 벗어났다.

이게 뭐야 parameter를 몇개를 쳐 받으시니 ㅠㅠ

 

 

3. C++

 

class Solution {
public:
    vector<vector<int>> construct2DArray(vector<int>& original, int m, int n) {
        if(original.size()!=m*n) return vector<vector<int>>{};

        vector<vector<int>> vec(m);

        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                vec[i].push_back(original[j+n*i]);
            }
        }
        return vec;
    }
};

그래도 뭐 끙차 끙차 코딩을 해내긴 했다.

 

에요~ 죽겄따!!

 

2D Array는 n만큼 original을 잘라서 넣는 것이 POINT다!