2
\$\begingroup\$

https://leetcode.com/problems/number-of-enclaves/

Given a 2D array A, each cell is 0 (representing sea) or 1 (representing land)

A move consists of walking from one land square 4-directionally to another land square, or off the boundary of the grid.

Return the number of land squares in the grid for which we cannot walk off the boundary of the grid in any number of moves.

Example 1:

Input: [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]] Output: 3 Explanation: There are three 1s that are enclosed by 0s, and one 1 that isn't enclosed because its on the boundary. Example 2:

Input: [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]] Output: 0 Explanation: All 1s are either on the boundary or can reach the boundary.

Note:

1 <= A.length <= 500 1 <= A[i].length <= 500 0 <= A[i][j] <= 1 All rows have the same size.

please review for performance.

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace GraphsQuestions
{
    /// <summary>
    /// https://leetcode.com/problems/number-of-enclaves
    /// </summary>
    [TestClass]
    public class NumberOfEnclavesTest
    {

        [TestMethod]
        public void FirstTest()
        {

            int[][] A =
            {
                new[] { 0, 0, 0, 0 },
                new[] { 1, 0, 1, 0 },
                new[] { 0, 1, 1, 0 },
                new[] { 0, 0, 0, 0 }
            };
            int expected = 3;
            Assert.AreEqual(expected, NumEnclaves(A));

        }

        [TestMethod]
        public void FailedTest()
        {

            int[][] A =
            {
                new[] { 0, 1, 1, 0 },
                new[] { 0, 0, 1, 0 },
                new[] { 0, 0, 1, 0 },
                new[] { 0, 0, 0, 0 }
            };
            int expected = 0;
            Assert.AreEqual(expected,NumEnclaves(A));

        }

        public int NumEnclaves(int[][] A)
        {
            int count = 0;
            for (int row = 1; row < A.Length - 1; row++)
            {
                for (int col = 1; col < A[0].Length - 1; col++)
                {
                    if (A[row][col] != 0)
                    {
                        bool isDone = false;
                        int res = DFS(row, col, A, ref isDone);
                        if (!isDone)
                        {
                            count += res;
                        }
                    }
                }
            }
            return count;
        }

        private int DFS(int row, int col, int[][] A, ref bool isDone)
        {
            if (row <= 0 || col <= 0 || row >= A.Length-1 || col >= A[0].Length-1 )
            {
                if (A[row][col] == 1)
                {
                    isDone = true;
                }
                return 0;
            }

            if (A[row][col] == 0)
            {
                return 0;
            }

            int count = 1;
            A[row][col] = 0;
            count += DFS(row, col - 1, A,ref isDone);
            count += DFS(row, col + 1, A,ref isDone);
            count += DFS(row - 1, col, A,ref isDone);
            count += DFS(row + 1, col, A,ref isDone);
            if (isDone)
            {
                count = 0;
            }
            return count;
        }
    }
}
\$\endgroup\$
5
  • \$\begingroup\$ This seems pretty extreme for an interview question. 😱 \$\endgroup\$
    – Taco
    Commented Oct 26, 2020 at 22:42
  • 1
    \$\begingroup\$ @Emma I suppose it depends on the type of work you'll be doing too though. I think the hardest I've had was to write a method that provided a single point from an nth order Bezier curve based on time, which after landing that position and working there for 4 years, I still think it was overkill. I'm very happy that they let me Google the mathematics for it or I would've never reached the correct answer. \$\endgroup\$
    – Taco
    Commented Oct 26, 2020 at 22:56
  • 2
    \$\begingroup\$ @Taco this a medium question. once you practice more and more of those at leetcode.com or any other site. you can see the patterns. \$\endgroup\$
    – Gilad
    Commented Oct 27, 2020 at 6:50
  • \$\begingroup\$ I wouldn't doubt it to be honest. I've been in the industry for a decade now, and have been on both sides of the interview process many times at this point, just seemed pretty overkill-ish to me lol \$\endgroup\$
    – Taco
    Commented Oct 27, 2020 at 11:23
  • 1
    \$\begingroup\$ @Taco this is google/amazon/microsoft type of questions \$\endgroup\$
    – Gilad
    Commented Oct 27, 2020 at 11:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.