Solution in C
this was difficult lol\n\n# Intuition\nit\'s slightly similar to yesterday\'s problem but i still had to bang my head around to wrap all that and actually under
this was **difficult** lol\n\n# Intuition\nit\'s slightly similar to yesterday\'s problem but i still had to bang my head around to wrap all that and actually understand the problem. even though i could see think and come up with a solution on paper i knew this was going to be really difficult to code especially in C.\n\n# Approach\nthanks to neetcode for his approach with topological sort to identify cycles and also gpt for solving out of bound and minor\nissues\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nbool dfs(int src, int k, int** adj, int* sizes, bool* visit, bool* path, int* order, int* orderSize) {\n    if (src < 1 || src > k) return true;\n    if (path[src]) return false;\n    if (visit[src]) return true;\n    \n    visit[src] = true;\n    path[src] = true;\n    \n    for (int i = 0; i < sizes[src]; i++) {\n        int nei = adj[src][i];\n        if (nei < 1 || nei > k) continue;\n        if (!dfs(nei, k, adj, sizes, visit, path, order, orderSize)) {\n            return false;\n        }\n    }\n    \n    path[src] = false;\n    order[*orderSize] = src;\n    (*orderSize)++;\n    return true;\n}\n\nint* tsort(int k, int** conditions, int conditionsSize, int* conditionsColSize) {\n    int** adj = (int**)malloc((k + 1) * sizeof(int*));\n    if (!adj) return NULL;\n    \n    int* sizes = (int*)calloc(k + 1, sizeof(int));\n    if (!sizes) {\n        return NULL;\n    }\n    \n    for (int i = 0; i <= k; i++) {\n        adj[i] = (int*)malloc((k + 1) * sizeof(int));\n\n    }\n    \n    for (int i = 0; i < conditionsSize; i++) {\n        int src = conditions[i][0], dst = conditions[i][1];\n        if (src >= 1 && src <= k && dst >= 1 && dst <= k && sizes[src] < k) {\n            adj[src][sizes[src]++] = dst;\n        }\n    }\n    \n    bool* visit = (bool*)calloc(k + 1, sizeof(bool));\n    bool* path = (bool*)calloc(k + 1, sizeof(bool));\n    int* order = (int*)malloc((k + 1) * sizeof(int));\n    if (!visit || !path || !order) {\n        \n        return NULL;\n    }\n    \n    int orderSize = 0;\n    bool hasCycle = false;\n    \n    for (int src = 1; src <= k; src++) {\n        if (!dfs(src, k, adj, sizes, visit, path, order, &orderSize)) {\n            hasCycle = true;\n            break;\n        }\n    }\n    \n    int* result = NULL;\n    if (!hasCycle) {\n        result = (int*)malloc(k * sizeof(int));\n        if (result) {\n            for (int i = 0; i < k; i++) {\n                result[i] = order[k - 1 - i];\n            }\n        }\n    }\n\n    \n    return result;\n}\n\nint** buildMatrix(int k, int** rowConditions, int rowConditionsSize, int* rowConditionsColSize, \n                  int** colConditions, int colConditionsSize, int* colConditionsColSize, \n                  int* returnSize, int** returnColumnSizes) {\n    if (k <= 0 || k > 400) {\n        *returnSize = 0;\n        *returnColumnSizes = NULL;\n        return NULL;\n    }\n    \n    int* rows = tsort(k, rowConditions, rowConditionsSize, rowConditionsColSize);\n    int* cols = tsort(k, colConditions, colConditionsSize, colConditionsColSize);\n    \n    if (!rows || !cols) {\n        *returnSize = 0;\n        *returnColumnSizes = NULL;\n \n        return NULL;\n    }\n    \n    int* val_rows = (int*)malloc((k + 1) * sizeof(int));\n    int* val_cols = (int*)malloc((k + 1) * sizeof(int));\n    if (!val_rows || !val_cols) {\n\n        *returnSize = 0;\n        *returnColumnSizes = NULL;\n        return NULL;\n    }\n    \n    for (int i = 0; i < k; i++) {\n        val_rows[rows[i]] = i;\n        val_cols[cols[i]] = i;\n    }\n    \n    int** output = (int**)malloc(k * sizeof(int*));\n    *returnColumnSizes = (int*)malloc(k * sizeof(int));\n    if (!output || !*returnColumnSizes) {\n        *returnSize = 0;\n        *returnColumnSizes = NULL;\n        return NULL;\n    }\n    \n    for (int i = 0; i < k; i++) {\n        output[i] = (int*)calloc(k, sizeof(int));\n        if (!output[i]) {\n\n            *returnSize = 0;\n            *returnColumnSizes = NULL;\n            return NULL;\n        }\n        (*returnColumnSizes)[i] = k;\n    }\n    \n    for (int num = 1; num <= k; num++) {\n        int i = val_rows[num], j = val_cols[num];\n        output[i][j] = num;\n    }\n    \n    *returnSize = k;\n\n    return output;\n}\n```
['C']