codekofi
← All problems

Problem 101

Medium

1 · Worked examples

0 / 3

Type an input and write what you think is the output . Each problem is converted to Web Assembly, so any possible input will show the corresponding output. Only correct predictions count.

solution()
returns

2 · Which problem is it?

Locked until you have predicted 3 outputs correctly.

The accepted solution

1vector<vector<int>> solution(vector<vector<int>> grid) {
2
3 if (grid.empty() || grid[0].empty()) {
4 return grid;
5 }
6
7 int rows = grid.size();
8 int cols = grid[0].size();
9 bool firstCol = false;
10
11 for (int r = 0; r < rows; r++) {
12
13 if (grid[r][0] == 0) {
14 firstCol = true;
15 }
16
17 for (int c = 1; c < cols; c++) {
18
19 if (grid[r][c] == 0) {
20 grid[r][0] = 0;
21 grid[0][c] = 0;
22 }
23 }
24 }
25
26 for (int r = rows - 1; r >= 0; r--) {
27
28 for (int c = cols - 1; c >= 1; c--) {
29
30 if (grid[r][0] == 0 || grid[0][c] == 0) {
31 grid[r][c] = 0;
32 }
33 }
34
35 if (firstCol) {
36 grid[r][0] = 0;
37 }
38 }
39
40 return grid;
41}

Names have been stripped. The signature is the only clue you get for free. Compiled as C++20 with the standard headers and using namespace std; already in scope.