This problem looks busy at first, but the accepted solution is built around one steady invariant. For 463. Island Perimeter, the solution in this repository is mainly a graph traversal solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. 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: graph traversal.
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 islandPerimeter.
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:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
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) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 304 ms, faster than 13.00% of C++ online submissions for Island Perimeter.
02//Memory Usage: 97.6 MB, less than 16.54% of C++ online submissions for Island Perimeter.
03class Solution {
04public:
05 int islandPerimeter(vector<vector<int>>& grid) {
06 int m = grid.size();
07 if(m == 0) return 0;
08 int n = grid[0].size();
09 if(n == 0) return 0;
10
11 vector<vector<int>> dirs = {
12 {1,0},
13 {-1,0},
14 {0,1},
15 {0,-1}};
16
17 int ans = 0;
18
19 for(int i = 0; i < m; ++i){
20 for(int j = 0; j < n; ++j){
21 if(grid[i][j]){
22 ans += 4;
23 for(vector<int>& dir : dirs){
24 int ni = i + dir[0];
25 int nj = j + dir[1];
26 if(ni >= 0 && ni < m && nj >= 0 && nj < n && grid[ni][nj]){
27 --ans;
28 }
29 }
30 }
31 }
32 }
33
34 return ans;
35 }
36};
37
38//math
39//https://leetcode.com/problems/island-perimeter/discuss/95001/clear-and-easy-java-solution
40//Runtime: 228 ms, faster than 29.18% of C++ online submissions for Island Perimeter.
41//Memory Usage: 96 MB, less than 97.84% of C++ online submissions for Island Perimeter.
42class Solution {
43public:
44 int islandPerimeter(vector<vector<int>>& grid) {
45 int m = grid.size();
46 if(m == 0) return 0;
47 int n = grid[0].size();
48 if(n == 0) return 0;
49
50 int islands = 0;
51 int sharedEdges = 0;
52
53 for(int i = 0; i < m; ++i){
54 for(int j = 0; j < n; ++j){
55 if(grid[i][j]){
56 ++islands;
57 /*
58 count shared edges,
59 note that we only look down and right
60 */
61 if(i+1 < m && grid[i+1][j]) ++sharedEdges;
62 if(j+1 < n && grid[i][j+1]) ++sharedEdges;
63 }
64 }
65 }
66
67 return islands*4 - sharedEdges*2;
68 }
69};
Cost