동까의 코딩

[99클럽] 20일차 TIL 본문

문제 풀이/99클럽

[99클럽] 20일차 TIL

동까의 코딩 2024. 6. 9. 20:47
반응형

https://leetcode.com/problems/count-square-submatrices-with-all-ones/submissions/1282783267/

 

class Solution(object):
    def countSquares(self, matrix):
        if len(matrix) == 0 or len(matrix[0]) == 0:
            return 0
            
        rows, cols = len(matrix), len(matrix[0])
        dp = [[0 for _ in range(cols)] for _ in range(rows)]
        ans = 0
        for i in range(rows):
            for j in range(cols):
                if matrix[i][j] == 1:
                    if i == 0 or j == 0:
                        dp[i][j] = 1
                    else:
                        dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j], dp[i][j - 1]) + 1
                    ans += dp[i][j]
        return ans

 

코드를 참조해서 풀이를 작성하였습니다.

내일 다시 풀어볼 예정입니다.

반응형

'문제 풀이 > 99클럽' 카테고리의 다른 글

[99클럽] 22일차 TIL  (0) 2024.06.11
[99클럽] 21일차 TIL  (0) 2024.06.10
[99클럽] 19일차 TIL  (0) 2024.06.08
[99클럽] 18일차 TIL  (0) 2024.06.07
[99클럽] 17일차 TIL  (0) 2024.06.06