A good way into this one is to ask: what do we need to remember so we never redo work blindly? For 1582. Special Positions in a Binary Matrix, the solution in this repository is mainly a straightforward implementation solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: straightforward implementation.
The notes already sitting in the source point us in the right direction:
- time: O(N^3)
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are numSpecial, rows.
Guide
Why?
The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.
- The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Read the setup variables first.
- Follow the main loop or recursive helper next.
- Watch where invalid states get skipped.
- Check which value survives to the return statement.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(N^2)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 44 ms, faster than 100.00% of C++ online submissions for Special Positions in a Binary Matrix.
02//Memory Usage: 13 MB, less than 33.33% of C++ online submissions for Special Positions in a Binary Matrix.
03//time: O(N^3)
04class Solution {
05public:
06 int numSpecial(vector<vector<int>>& mat) {
07 int m = mat.size();
08 int n = mat[0].size();
09
10 int ans = 0;
11
12 for(int i = 0; i < m; ++i){
13 for(int j = 0; j < n; ++j){
14 if(mat[i][j] == 1){
15 int rcount = 0;
16 for(int ii = 0; ii < m; ++ii){
17 if(mat[ii][j] == 1){
18 ++rcount;
19 if(rcount > 1) break;
20 }
21 }
22
23 int ccount = 0;
24 for(int jj = 0; jj < n; ++jj){
25 if(mat[i][jj] == 1){
26 ++ccount;
27 if(ccount > 1) break;
28 }
29 }
30
31 if(rcount == 1 && ccount == 1){
32 ++ans;
33 }
34 }
35 }
36 }
37
38 return ans;
39 }
40};
41
42//two pass, O(N^2)
43//Runtime: 44 ms, faster than 100.00% of C++ online submissions for Special Positions in a Binary Matrix.
44//Memory Usage: 13.2 MB, less than 33.33% of C++ online submissions for Special Positions in a Binary Matrix.
45//https://leetcode.com/problems/special-positions-in-a-binary-matrix/discuss/843949/C%2B%2B-2-passes
46//time: O(N^2)
47class Solution {
48public:
49 int numSpecial(vector<vector<int>>& mat) {
50 int m = mat.size();
51 int n = mat[0].size();
52
53 vector<int> rows(m), cols(n);
54
55 for(int i = 0; i < m; ++i){
56 for(int j = 0; j < n; ++j){
57 if(mat[i][j] == 1){
58 ++rows[i];
59 ++cols[j];
60 }
61 }
62 }
63
64 int ans = 0;
65
66 for(int i = 0; i < m; ++i){
67 for(int j = 0; j < n; ++j){
68 if(mat[i][j] == 1 && rows[i] == 1 && cols[j] == 1){
69 ++ans;
70 }
71 }
72 }
73
74 return ans;
75 }
76};
Cost