본문 바로가기

알고리즘/LeetCode

Binary Tree Preorder Traversal (Stack) LeetCode의 Binary Tree 튜토리얼을 시작하게 됐다. 그중 첫 번째 문제인 트리의 Pre-order 탐색을 구현하는 문제인 Binary Tree Preorder Traversal이다. Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 문제는 간단하다. 이전에 풀었던 Binary Tree Inorder Traversal (Stack)과 비슷하게.. 더보기
Valid Palindrome (Deque) 드디어 파이썬 알고리즘 인터뷰 책을 좀 읽어보게 됐다. LeetCode에서 튜토리얼을 진행할 때는 자바로 했지만 프로그래머스나 혼자 풀 때는 파이썬 언어를 사용하고자 한다. 책에서 소개하는 문제들을 먼저 풀어보고 다른 사람들의 풀이나 책에 나온 내용들을 기반으로 개선방안을 학습하며 포스팅할 예정이다. 이번 문제는 LeetCode의 Easy 문제인 Valid Palindrome이다. Valid Palindrome - 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 .. 더보기
Keys and Rooms (BFS) 이번 문제는 LeetCode의 Queue, Stack 튜토리얼의 마지막 문제인 Keys and Rooms다. 문제의 조건은 다음과 같다. N개의 방이 주어지고 0번 방에서부터 시작한다. 이 방은 List 타입으로 주어진다. 각 방에는 다른 방으로 갈 수 있는 열쇠가 있다. 이 열쇠들은 List 타입으로 주어진다. 0번 방에서부터 시작하여 열쇠들을 이용하여 모든 방에 방문할 수 있는지 여부를 확인하라. 방 이동에 제약은 없으며 0번 방을 제외한 모든 방은 방문한 적이 없는 상태다. 각 방과 방에 들어있는 열쇠는 리스트로 주어지는데 예를 들어 [[1], [2], [3], []]처럼 주어진다면 이는 총 4개의 방이 있으며 0번 방에는 1번 열쇠가, 1번 방에는 2번 열쇠가, 2번 방에는 3번 열쇠가 들어있는 .. 더보기
01 Matrix (BFS) LeetCode의 Queue, Stack 튜토리얼의 마지막 섹션의 5번째 문제인 01 Matrix다. Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 문제는 다음과 같다. 0과 1로 이루어진 2차원 배열이 주어진다. 인접한 두 셀의 거리는 1이다. 각 셀은 상하좌우로만 인접하고 있다. 각 셀에서 가장 가까운 0셀까지의 거리를 2차원 배열로 반환하라. 문.. 더보기
Flood Fill (DFS) LeetCode의 Queue, Stack 튜토리얼의 마지막 섹션의 세 번째 문제인 Flood Fill이다. Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 문제는 간단하다. 2차원 배열로 주어진 이미지 픽셀 값에 대하여 특정 위치의 좌표와 새로운 값을 주면 해당 좌표부터 시작해서 동서남북 4방향으로 탐색하며 시작 좌표와 동일한 값을 가진 픽셀을 새로운 .. 더보기
Decode String (Stack) LeetCode의 Queue, Stack 튜토리얼의 마지막 섹션의 세 번째 문제인 Decode String이다. Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 일정한 규칙으로 주어진 문자열을 해독하는 것이 목적인데 문제의 규칙은 다음과 같다. 문자열에 k[encoded_string] 포맷이 있다면 encoded_string을 k번 반복해서 이어 붙인 .. 더보기
Implement Stack using Queues LeetCode의 Queue, Stack 튜토리얼에서 마지막 섹션의 두 번째 문제인 Implement Stack using Queues다. Explore - LeetCode LeetCode Explore is the best place for everyone to start practicing and learning on LeetCode. No matter if you are a beginner or a master, there are always new topics waiting for you to explore. leetcode.com 이번 문제에서는 지난 문제(스택을 이용한 큐 구현)처럼 큐를 이용하여 스택을 구현하는 것을 목표로 하고 있다. 역시 큐 자료구조에서 사용할 수 있는 연산(push, p.. 더보기
Implement Queue using Stacks 이번 문제는 LeetCode의 Queue, Stack 튜토리얼에서 마지막 섹션의 첫 번째 문제인 Implement Queue using Stacks다. 이전에 학습할 때 큐와 스택의 가장 큰 차이점은 선입선출이냐 선입후출이냐의 차이였다. 즉 먼저 삽입된 요소가 먼저 삭제되거나 가장 나중에 삭제되는 완전 반대의 역할을 수행하고 있는데 스택을 이용하여 반대의 역할을 하는 큐를 구현할 수 있을까? 결론적으로는 가능하다. 하지만 추가적인 메모리 공간이 필요한데 일단 문제에서 요구하는 것은 다음과 같다. 구현된 큐는 push, pop, peek, empty 네 가지 연산을 수행할 수 있어야 한다. 큐를 구현할 때는 Stack 자료구조에서 지원하는 연산(push, pop, size, empty 등)만 사용해야 한다.. 더보기