wherehows
Home
  • 분류 전체보기 (35)
    • 알고리즘 (31)
      • 코드트리 (0)
      • 백준 (1)
      • 리트코드 (21)
      • 프로그래머스 (9)
Home
  • 분류 전체보기 (35)
    • 알고리즘 (31)
      • 코드트리 (0)
      • 백준 (1)
      • 리트코드 (21)
      • 프로그래머스 (9)
블로그 내 검색

wherehows

  • 알고리즘/리트코드

    1277. Count Square Submatrices with All Ones

    2021. 5. 23.

    by. 창고

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

     

    Count Square Submatrices with All Ones - LeetCode

    Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

    leetcode.com

    행렬에서 정사각형 개수 찾기

     

    1. Brute Force

    둘레를 따라서 탐색하기 = 대각선으로 탐색하기

    오른쪽 아래 방향으로 진행

    /**

     * @param {number[][]} matrix

     * @return {number}

     */

    var countSquares = function (matrix) {

        let row = matrix[0].length;

        let col = matrix.length;

        let count = 0;

     

        for (let y = 0; y < col; y++) {

            for (let x = 0; x < row; x++) {

                outerLoop: for (let i = 0; y + i < col && x + i < row; i++, count++) {

                    for (let j = 0; j <= i; j++) {

                        if (matrix[y + i][x + j] === 0 || matrix[y + j][x + i] === 0) break outerLoop;

                    }

                }

            }

        }

        return count;

    };

     

    2. 개선

    특정 위치에서 만들 수 있는 사각형의 개수를 누적하여 기록

     

     

     

     

     

    https://leetcode.com/problems/count-square-submatrices-with-all-ones/discuss/442184/JavaScript-Easy-to-understand-2-solutions

    '알고리즘 > 리트코드' 카테고리의 다른 글

    210. Course Schedule II  (0) 2021.05.24
    207. Course Schedule  (0) 2021.05.23
    152. Maximum Product Subarray  (0) 2021.05.23
    53. Maximum Subarray  (0) 2021.05.23
    121. Best Time to Buy and Sell Stock  (0) 2021.05.23

    댓글

    관련글

    • 210. Course Schedule II 2021.05.24
    • 207. Course Schedule 2021.05.23
    • 152. Maximum Product Subarray 2021.05.23
    • 53. Maximum Subarray 2021.05.23
    맨 위로
전체 글 보기
Tistory 로그인
Tistory 로그아웃
로그아웃 글쓰기 관리

Today

Total

Powered by ⓒ Kakao Corp.

Designed by Nana
블로그 이미지
창고

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.