본문 바로가기

알고리즘/LeetCode

Running Sum of 1d Array (defaultdict) LeetCode의 Running Sum of 1d Array 문제다. Running Sum of 1d Array - 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, 2, 3, 4] 같은 배열이 주어지면 첫 번째 요소까지의 합, 두 번째 요소까지의 합, 세 번째 요소까지의 합, 네 번째 요소까지의 합을 구해서 반환하는 문제다. 즉 [1, 1+2, 1+2+3, 1+2+3+4]를 계산하는 것인데 단순히 생각하면 그냥 매 요소마다 반복문으로 현재 요소까지의 합을 .. 더보기
Number of Students Unable to Eat Lunch (deque) LeetCode의 Number of Students Unable to Eat Lunch 문제다. Number of Students Unable to Eat Lunch - 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 별로 어렵지 않은 문제지만 collections 모듈의 Counter 클래스를 활용하는 풀이가 있어 적어본다. 문제는 다음과 같다. 학생들은 선호하는 샌드위치 모양이 있다. 이는 각각 0, 1로 나타낸다. 학생들을 위해 준비된 샌드위치들이 있다. 이.. 더보기
Array Partition I (Pythonic way) LeetCode의 Array Partition I 문제다. Array Partition I - 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 딱히 어려울 게 없는 문제지만 책에서 아주 인상적인 풀이를 제공했기 때문에 기록해두려고 한다. 문제 자체는 다음과 같이 간단하다. 2n개의 정수 목록이 주어진다. 정수들을 n개의 한 쌍으로 분리한다. 각 쌍의 최솟값의 합이 나타낼 수 있는 최댓값을 구하라. 쉽게 말해서 [1, 3, 2, 4]가 주어질 때 (1, 3), (2.. 더보기
3Sum (Two Pointers) LeetCode의 3Sum 문제다. 3Sum - 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 주어진 수 중에서 3개를 골라 합이 0인 조합을 반환하는 문제인데 중복된 조합은 반환하지 않아야 한다. 정수 배열은 10000개 까지 주어질 수 있기 때문에 최적화되지 않은 코드의 경우 시간 초과가 발생할 수 있다. 실제로 첫 번째로 구현한 아래 코드는 28번째 테스트 케이스에서 시간 초과 에러가 발생했다. class Solution: def threeSum(self,.. 더보기
Trapping Rain Water (Two Pointers) LeetCode의 Trapping Rain Water 문제다. Trapping Rain Water - 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 처음으로 건드려보는 Hard 난이도의 문제다. 사실 Medium 문제도 혼자 힘으로 풀기 버거워서 Hard 난이도는 손대지 않고 있었는데 책에서 풀이를 알려주기 때문에 이를 공부하는 셈 치고 풀어보았다. 사실 직접 푼 것도 아니고 해설을 읽고 분석하는 정도지만 나중에 비슷한 문제가 나왔을 때 활용할 수 있을 것이다... 더보기
Two Sum (Dictionary) LeetCode의 첫 번째 문제인 Two Sum이다. Two Sum - 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, 2, 3, 4, 5]가 있고 6을 만들어야 할 때 [0, 4]를 반환하면 되는 것이다. 문제에서는 배열 내에 목푯값을 만들 수 있는 최소한 한 쌍의 정수가 존재한다고.. 더보기
Longest Palindromic Substring LeetCode의 Longest Palindromic Substring 문제다. Longest Palindromic Substring - 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 제목 그대로 가장 긴 회문 문자열을 찾는 문제인데 이전에 풀어봤던 Valid Palindrome 문제에서 언급했듯이 Palindrome이란 회문으로 거꾸로 뒤집어도 동일한 문자열을 의미한다. 굳이 더 설명하진 않고 그러면 문자열에서 어떻게 회문을 탐색할 수 있는지 알아보자. 책에서.. 더보기
Path Sum (Recursive) LeetCode의 Binary Tree 튜토리얼 두 번째 섹션의 마지막 문제인 Path Sum이다. Account Login - 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 트리의 루트 노드부터 자식 노드까지 탐색하면서 한 경로의 노드 값들을 합해서 특정 값을 만들 수 있는지 확인하는 문제다. 노드의 값과 합해서 만들어야 하는 값들은 음수 또는 양수 둘 다 될 수 있기 때문에 말단 노드까지 합해가면서 특정 값이 만들어졌는지 확인해야 한다. 그래서 재귀적으로 이.. 더보기