C# || Max Area of Island – How To Find The Maximum Area Of An Island In A Grid Using C#

The following is a module with functions which demonstrates how to find the maximum area of an island in a grid using C#.

1. Max Area Of Island – Problem Statement

You are given an m x n binary matrix grid. An island is a group of 1‘s (representing land) connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.

The area of an island is the number of cells with a value 1 in the island.

Return the maximum area of an island in grid. If there is no island, return 0.

Example 1:

``` Input: grid = [[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]] Output: 6 Explanation: The answer is not 11, because the island must be connected 4-directionally. ```

Example 2:

``` Input: grid = [[0,0,0,0,0,0,0,0]] Output: 0 ```

2. Max Area Of Island – Solution

The following is a solution which demonstrates how to find the maximum area of an island in a grid.

We want to know the area of each connected shape in the grid, then take the maximum of these.

If we are on a land square and explore every square connected to it 4-directionally (and recursively squares connected to those squares, and so on), then the total number of squares explored will be the area of that connected shape.

To ensure we don’t count squares in a shape more than once, we use ‘seen’ to keep track of squares we haven’t visited before. It will also prevent us from counting the same shape more than once.

``` 2. Max Area Of Island - Solution C# // ============================================================================ // Author: Kenneth Perkins // Date: Jul 28, 2022 // Taken From: http://programmingnotes.org/ // File: Solution.cs // Description: Demonstrates how to find the maximum area in a grid // ============================================================================ public class Solution { public int MaxAreaOfIsland(int[][] grid) { var seen = new bool[grid.Length][]; for (int row = 0; row < seen.Length; ++row) { seen[row] = new bool[grid[row].Length]; } var result = 0; for (int row = 0; row < grid.Length; ++row) { for (int col = 0; col < grid[row].Length; ++col) { result = Math.Max(result, Area(grid, row, col, seen)); } } return result; } private int Area(int[][] grid, int row, int col, bool[][] seen) { if (!IsValidCell(grid, row, col)) { return 0; } if (seen[row][col] || grid[row][col] == 0) { return 0; } seen[row][col] = true; return 1 + // Search Bottom Area(grid, row + 1, col, seen) + // Search Top Area(grid, row - 1, col, seen) + // Search Left Area(grid, row, col - 1, seen) + // Search Right Area(grid, row, col + 1, seen); } private bool IsValidCell(int[][] grid, int row, int col) { return row >= 0 && row < grid.Length && col >= 0 && col < grid[row].Length; } }// http://programmingnotes.org/ 123456789101112131415161718192021222324252627282930313233343536373839404142434445 // ============================================================================//    Author: Kenneth Perkins//    Date:   Jul 28, 2022//    Taken From: http://programmingnotes.org///    File:  Solution.cs//    Description: Demonstrates how to find the maximum area in a grid// ============================================================================public class Solution {    public int MaxAreaOfIsland(int[][] grid) {        var seen = new bool[grid.Length][];        for (int row = 0; row < seen.Length; ++row) {            seen[row] = new bool[grid[row].Length];        }        var result = 0;        for (int row = 0; row < grid.Length; ++row) {            for (int col = 0; col < grid[row].Length; ++col) {                result = Math.Max(result, Area(grid, row, col, seen));            }        }        return result;    }     private int Area(int[][] grid, int row, int col, bool[][] seen) {        if (!IsValidCell(grid, row, col)) {            return 0;        }        if (seen[row][col] || grid[row][col] == 0) {            return 0;        }        seen[row][col] = true;        return 1 +            // Search Bottom            Area(grid, row + 1, col, seen) +            // Search Top            Area(grid, row - 1, col, seen) +            // Search Left            Area(grid, row, col - 1, seen) +            // Search Right            Area(grid, row, col + 1, seen);    }     private bool IsValidCell(int[][] grid, int row, int col) {        return row >= 0 && row < grid.Length && col >= 0 && col < grid[row].Length;    }}// http://programmingnotes.org/ ```

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:

``` 6 0 ```

C# || Binary Tree Right Side View – How To Get Nodes Ordered Top To Bottom C#

The following is a module with functions which demonstrates how to get nodes in a binary tree ordered from top to bottom using C#.

1. Right Side View – Problem Statement

Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

Example 1:

``` Input: root = [1,2,3,null,5,null,4] Output: [1,3,4] ```

Example 2:

``` Input: root = [1,null,3] Output: [1,3] ```

Example 3:

``` Input: root = [] Output: [] ```

2. Right Side View – Solution

The following is a solution which demonstrates how to get right side nodes ordered from top to bottom.

This solution uses Depth First Search level order traversal to explore items at each level, and then adds the last node on every layer.

``` 2. Right Side View - Solution C# // ============================================================================ // Author: Kenneth Perkins // Date: Jul 10, 2022 // Taken From: http://programmingnotes.org/ // File: Solution.cs // Description: Demonstrates how to get nodes ordered from top to bottom // ============================================================================ /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { * this.val = val; * this.left = left; * this.right = right; * } * } */ public class Solution { public IList<int> RightSideView(TreeNode root) { var result = new List<int>(); Traverse(root, 0, result); return result; } public void Traverse(TreeNode node, int currentDepth, List<int> result) { if (node == null) { return; } if (currentDepth == result.Count) { result.Add(node.val); } Traverse(node.right, currentDepth + 1, result); Traverse(node.left, currentDepth + 1, result); } }// http://programmingnotes.org/ 1234567891011121314151617181920212223242526272829303132333435363738 // ============================================================================//    Author: Kenneth Perkins//    Date:   Jul 10, 2022//    Taken From: http://programmingnotes.org///    File:  Solution.cs//    Description: Demonstrates how to get nodes ordered from top to bottom// ============================================================================/** * Definition for a binary tree node. * public class TreeNode { *     public int val; *     public TreeNode left; *     public TreeNode right; *     public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { *         this.val = val; *         this.left = left; *         this.right = right; *     } * } */public class Solution {    public IList<int> RightSideView(TreeNode root) {        var result = new List<int>();        Traverse(root, 0, result);        return result;    }     public void Traverse(TreeNode node, int currentDepth, List<int> result) {        if (node == null) {            return;        }        if (currentDepth == result.Count) {            result.Add(node.val);        }        Traverse(node.right, currentDepth + 1, result);        Traverse(node.left, currentDepth + 1, result);    }}// http://programmingnotes.org/ ```

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:

``` [1,3,4] [1,3] [] ```