@xingfeng.unsw said in Simple Java Solution:

Please feel free to correct me if I am wrong.

The requirement is to do the counting in ONE pass. However, in this solution, the data seems to be scanned THREE times: one for checking board[i][j], two for the condition checking of board[i+1][j], board[i][j+1].

I am glad you brought an interesting question that I never thought about. As I am not coming from computer science major (please correct if am wrong), I suspect that the term "one pass" is not a concrete terminology but a sort of descriptive and intuitive phrase accepted by the programming community.

**My perception for what the term means accepted by public:** a single traversal through a data structure with `O(1)`

operations involving traversing index at each member of the data structure. So any `O(1)`

number of accesses of any `board[i'][j']`

at index `(i, j)`

within a single traversal through the `board`

is still publicly accepted as "one pass".

Note that this "common sense" of "one pass" certainly does NOT mean fewer time complexity (i.e., number of operations). For example, to initialize two arrays, the following code is considered as one pass

```
for (i = 0; i < N: ++i) { a[i] = i*i; b[i] = sin(i); }
```

because the initialization of `a[]`

and `b[]`

are independent, so we can do them simultaneously at each index `i`

.

However, if the initialization of each `b[i]`

depends on the entire `a[]`

for some reason, we have to do something like

```
for (int i = 0; i < N; ++i) a[i] = i*i;
for (int i = 0; i < N; ++i) b[i] = a[rand()%N];
```

Now we traversed index range `[0, N)`

twice, but the time complexity is still `O(N)`

.

So this "one pass" term is more about how the operations are "grouped together" with respect to traversal patterns, not referring just a single reference of each member of the traversing structure.