알고리즘 15

2024 10 27 1277. Count Square Submatrices with All Ones

문제Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.요약1만으로 체워진 사각형 갯수 찾기풀이누적합으로 풀면 쉬울것 같았다.행으로 누적합을 구한 매트릭스에서 다시 열로 다 더해서 누적합을 2*2 매트릭스를 만들면정사각형의 변 길이를 s라고 하였을때 [r][c],[r-s+1][c],[r][c-s+1],[r-s+1][c-s+1]이 만드는 사각형 내부의 값의 총 합은sum = prefix[r][c] - prefix[r-s][c] -prefix[r][c] + prefix[r-s][c-s]가 된다.코드class Solution { public int countSquares(int[][] matrix)..

알고리즘 2024.10.27

2024.10.23 2641. Cousins in Binary Tree II

문제Given the root of a binary tree, replace the value of each node in the tree with the sum of all its cousins' values.Two nodes of a binary tree are cousins if they have the same depth with different parents.Return the root of the modified tree.Note that the depth of a node is the number of edges in the path from the root node to it.요약트리 문제이다. 노드수의 제약이 10000개 밖에 되지않아 충분히 전탐으로 될꺼라고 생각했다.풀이한번 돌면서 ..

알고리즘 2024.10.23

2024.10.22 2583. Kth Largest Sum in a Binary Tree (Java)

문제You are given the root of a binary tree and a positive integer k.The level sum in the tree is the sum of the values of the nodes that are on the same level.Return the kth largest level sum in the tree (not necessarily distinct). If there are fewer than k levels in the tree, return -1.Note that two nodes are on the same level if they have the same distance from the root.요약같은 depth에서 총 합의 크기가 k번..

알고리즘 2024.10.22

2024.10.22 LeetCode 1593. Split a String Into the Max Number of Unique Substrings

Given a string `s`, return _the maximum number of unique substrings that the given string can be split into_.You can split string `s` into any list of **non-empty substrings**, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are **unique**.A **substring** is a contiguous sequence of characters within a string.풀이 시간..

알고리즘 2024.10.22