clueless coding // TODO: be smarter

LeetCode Lonely Pixel I: CPP Solution



TL;DR Code Solution


int findLonelyPixel(vector<vector<char>>& picture) {
    int result = 0;

    if (picture.size() > 0 && picture[0].size() > 0) {
        vector<int> rows(picture.size(), 0);
        vector<int> cols(picture[0].size(), 0);
        for (int i = 0; i < picture.size(); i++) {
            for (int j = 0; j < picture[0].size(); j++) {
                if (picture[i][j] == 'B') {
                    rows[i]++;
                    cols[j]++;
                }
            }
        }

        for (int i = 0; i < picture.size(); i++) {
            for (int j = 0; j < picture[0].size(); j++) {
                if (picture[i][j] == 'B') {
                    result += (rows[i] == 1 && cols[j] == 1);
                }
            }
        }
    }

    return result;
}