Similar Problems

Similar Problems not available

Count Artifacts That Can Be Extracted - Leetcode Solution

Companies:

LeetCode:  Count Artifacts That Can Be Extracted Leetcode Solution

Difficulty: Medium

Topics: hash-table array simulation  

Problem Statement:

You are given a matrix of size m x n representing a field of artifacts. Each artifact can be extracted if it is surrounded by 4 adjacent artifacts (left, right, up, down) of the same type. A cell is considered adjacent to another cell if they share a side (i.e no diagonal adjacency). Return the number of artifacts that can be extracted.

Solution:

Approach:

  • Create a helper function called dfs that iterates through the field.
  • In the helper function, implement a recursive depth-first-search algorithm that will find all adjacent artifacts of the same type and mark them as visited.
  • To count the number of artifacts that can be extracted in the main function countArtifacts, iterate through the matrix and call dfs for each unvisited cell. If there are at least 4 artifacts of the same type that are adjacent to the current cell that are also unvisited, increment the number of artifacts that can be extracted.

Algorithm:

  • Define a function called dfs that will be used to traverse the field.
  • Iterate through the field, if a cell is unvisited and has at least one unvisited neighbor of the same type, call the dfs function on the cell.
  • Implement a depth-first search algorithm in the dfs function. At each cell, check if it is of the same type as the previous cell and if it is unvisited. If it is, mark it as visited and recursively call dfs on its neighbors.
  • Define a function called countArtifacts that will iterate through the matrix and call dfs for each unvisited cell. If the dfs function returns True, increment the number of artifacts that can be extracted.

Pseudocode:

function dfs(field, visited, row, col, artifact_type)
    if the current cell is visited or out of bounds or not the same type as artifact_type
        return False 
    mark the current cell as visited
    for each adjacent cell (up, down, left, right)
        if the adjacent cell is unvisited and of the same type
            recursively call dfs on the adjacent cell
    return True 

function countArtifacts(field)
    initialize visited matrix
    initialize number of artifacts to 0
    iterate through the field
        if the cell is unvisited and has at least one unvisited neighbor of the same type
            if dfs(field, visited, row, col, field[row][col]) is True
                increment the number of artifacts
    return the number of artifacts 

Time Complexity:

The dfs function will be called at most once for each cell in the field, so the time complexity of this algorithm is O(n * m).

Space Complexity:

The visited matrix has the same size as the field matrix, so the space complexity is O(n * m).

Example:

Input: field = [ [1, 1, 2, 2], [1, 1, 2, 2], [2, 2, 1, 1], [2, 2, 1, 1] ]

Output: 4

Explanation: Each artifact is surrounded by adjacent artifacts of the same type, so all 4 of them can be extracted.

Count Artifacts That Can Be Extracted Solution Code

1