Zero to Hero
article thumbnail
239. Sliding Window Maximum
Algorithm 2021. 8. 14. 18:39

Sliding Window Maximum - 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 슬라이딩 윈도 사이즈 k가 주어진다. 슬라이딩 윈도를 수행하면서 윈도 내 가장 큰 정수를 list로 저장해서 반환하는 문제다. 예시는 다음과 같다. Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3 Output: [3,3,5,5,6,7] Explanation: Window position Max --------------- ----- [1 3..

article thumbnail
297. Serialize and Deserialize Binary Tree
Algorithm 2021. 8. 12. 12:41

Serialize and Deserialize Binary Tree - 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 이진트리가 주어진다. 이진트리를 직렬화(serialize)해 문자열로 반환하고, 반환된 문자열을 역직렬화(deserialize)해서 이진트리를 복원하는 메서드를 작성하는 문제다. 직렬화 및 역직렬화 방법에 제한은 없다. 1. 초기 접근법 처음에 나는 이 문제를 전위(pre-order), 중위(in-order) 순회 정보를 가지고 트리를 복구하는 ..

article thumbnail
654. Maximum Binary Tree
Algorithm 2021. 8. 10. 11:01

Maximum Binary Tree - 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 중복된 값이 없는 정수로 구성된 list가 주어진다. 이 list를 이용해서 아래 조건에 부합하는 tree를 만들어 root를 반환하는 문제다. 1. Create a root node whose value is the maximum value in nums. 2. Recursively build the left subtree on the subarray prefix to the..

article thumbnail
1551. Minimum Operations to Make Array Equal
Algorithm 2021. 8. 8. 10:07

Minimum Operations to Make Array Equal - 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 정수가 N이 주어진다. 1부터 시작하는 홀수 N개의 배열 arr에 대해서 아래 연산 법을 사용할 수 있다. 연산 법 len를 arr의 길이, 0 int: return n * n // 4 이 문제는 수의 성질을 이용하면 O(1)로 해결할 수 있는 문제다. 홀수인 경우 n = 7이라면 arr = [1, 3, 5, 7, 9, 11, 13]이다. 결국..

article thumbnail
1329. Sort the Matrix Diagonally
Algorithm 2021. 8. 7. 08:43

Sort the Matrix Diagonally - 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 2차원 배열이 주어진다. 배열을 우측 하단 대각성 방향으로 오름차순 정렬한 뒤 반환하는 문제다. 1. 구현 from typing import * class Solution: def diagonalSort(self, mat: List[List[int]]) -> List[List[int]]: if not mat: return None height, width = len..

article thumbnail
N*N, N**2, pow(N, 2), math.pow(N,2)
Programming 2021. 8. 6. 10:08

이전에 재밌는 문제를 풀었다. 관련 내용은 아래 포스팅 참고. 1828. Queries on Number of Points Inside a Circle Queries on Number of Points Inside a Circle - 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. l.. doljae.tistory.com 사실 이 문제는 문제 자체가 주는 의미도 있지만, Python을 알고리즘 문제 해결에 사용하는 사람들은 한 번쯤은 생각해볼 부분이 있다. 바로 거듭제곱 연산이다. Py..