Python Solution with Explanation


  • 0
    J

    I'm new to programming but this is what I got. The program works but I'm not sure what time and space complexity is. All I know is that this code is comprehensible to me. Each land is given 4 points and points are negated based on the presence of any land around it

    def island(matrix):
        count = 0
        for y in range(len(matrix)):
            for x in range(len(matrix[0])):
                if matrix[y][x]==1:
                    count += score1(y,x,matrix)
        return count
    
    def score1(y,x,matrix):
        '''land is a coordinate (y,x)'''
        negate = 0
        around = [(y-1,x), (y+1,x), (y,x-1), (y,x+1)]
        # [top, bottom, left, right]
        for (i,j) in around:
            if 0<=i<len(matrix) and 0<=j<len(matrix[0]) and matrix[i][j]:
                negate += 1
        return 4-negate
    

  • 0
    L

    @Juan-the-Doggo said in Python Solution with Explanation:

    presence

    cool, did you konw the time and space complexity now?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.