Skip to content

Latest commit

 

History

History
122 lines (98 loc) · 2.78 KB

_1252. Cells with Odd Values in a Matrix.md

File metadata and controls

122 lines (98 loc) · 2.78 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 04, 2024

Last updated : July 01, 2024


Related Topics : Array, Math, Simulation

Acceptance Rate : 79.11 %


Solutions

C

// Swapped to using bools but for some reason it's slower than ints hm
// Memory is also worse oddly

int oddCells(int m, int n, int** indices, int indicesSize, int* indicesColSize) {
    bool** refren = (bool**) malloc(sizeof(bool*) * m);
    for (int i = 0; i < m; i++) {
        refren[i] = (bool*) malloc(sizeof(bool) * n);
        for (int j = 0; j < n; j++) {
            refren[i][j] = false;
        }
    } 

    for (int i = 0; i < indicesSize; i++) {
        // rows
        int row = indices[i][0];
        for (int j = 0; j < n; j++) {
            refren[row][j] = !refren[row][j];
        }

        // cols
        int col = indices[i][1];
        for (int j = 0; j < m; j++) {
            refren[j][col] = !refren[j][col];
        }
    }

    int output = 0;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (refren[i][j]) {
                output++;
            }
        }
    }
    for (int i = 0; i < m; i++) {
        free(refren[i]);
    }
    free(refren);

    return output;
}
// Lot of loops but eh ill swap it around with bools


int oddCells(int m, int n, int** indices, int indicesSize, int* indicesColSize) {

    // reference matrix setup
    int** refren = (int**) malloc(sizeof(int*) * m);
    for (int i = 0; i < m; i++) {
        refren[i] = (int*) malloc(sizeof(int) * n);
        for (int j = 0; j < n; j++) {
            refren[i][j] = 0;
        }
    } 

    // calculations
    for (int i = 0; i < indicesSize; i++) {
        // rows
        int row = indices[i][0];
        for (int j = 0; j < n; j++) {
            refren[row][j]++;
        }

        // cols
        int col = indices[i][1];
        for (int j = 0; j < m; j++) {
            refren[j][col]++;
        }
    }

    // counting odds
    int output = 0;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            if (refren[i][j] % 2 == 1) {
                output++;
            }
        }
    }

    // freeing
    for (int i = 0; i < m; i++) {
        free(refren[i]);
    }
    free(refren);
    
    // output
    return output;
}