본문 바로가기

알고리즘/파이썬 알고리즘 인터뷰

(10)
[LeetCode] 77. Combinations (그래프) 문제 https://leetcode.com/problems/combinations/ Combinations - 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, n]의 범위를 가지는 숫자들에서 k개의 조합을 구하자. 내 풀이 1 (백트래킹) 순열과는 비슷하지만, 파라미터의 'start'를 for문에서 사용했다는 점이 다르다. 시간 : 712ms class Solution: def combine(self, n: int, k: int) -> List[List[i..
[LeetCode] 46. Permutations (그래프) 문제 https://leetcode.com/problems/permutations/ Permutations - 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 - 백트래킹 순열을 만들기 위해 check 배열을 사용하여 순서를 지정해주었다. 시간 : 39ms class Solution: def permute(self, nums: List[int]) -> List[List[int]]: ans, tmp = [], [] check ..
[LeetCode] 17. Letter Combinations of a Phone Number (그래프) 문제 https://leetcode.com/problems/letter-combinations-of-a-phone-number/ Letter Combinations of a Phone Number - 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에서 9까지의 숫자를 포함한 문자열이 주어진다. 각 숫자에는 문자가 매핑된다. 전화번호로 조합 가능한 모든 문자를 출력하자. 풀이 1 - 중복 조합 product 두 개 이상의 리스트의 모든 조합을 구할 때 사용하는 ..
[LeetCode] 200. Number of Islands (그래프) 문제 https://leetcode.com/problems/number-of-islands/ Number of Islands - 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"이 땅이고, "0"이 바다이다. 섬은 "1"로 이루어지고, "0"에 둘러싸여 있다. 섬의 개수를 구하자. DFS 풀이 몇 년 전에 그래프 열심히 풀었었던 기억을 되살려 풀어보았다. 내가 알고 있는 풀이와 책의 풀이가 좀 다르다. DFS 버전과 BFS 버전을 만들었다. from coll..
슬라이딩 윈도우 슬라이딩 윈도우(sliding window)란 고정 사이즈의 윈도우가 이동하면서 윈도우 내에 있는 데이터를 이용해 문제를 풀이하는 알고리즘을 말한다. 슬라이딩 윈도우는 투 포인터와 함께 알고리즘 풀이에 매우 유용하게 사용된다. 투 포인터와 비슷하지만 이와 구분하기 위해 일반적으로 고정 사이즈 윈도우를 사용하는 경우를 슬라이딩 윈도우로 따로 구분하기도 한다. 또한 주로 정렬된 배열을 대상으로 하는 투 포인터와 달리 슬라이딩 윈도우는 정렬 여부에 관계없이 활용된다는 차이가 있다. 슬라이딩 윈도우에서 윈도우 사이즈는 고정이며, 좌 또는 우 한쪽 방향으로만 이동한다. 이름 정렬 여부 윈도우 사이즈 이동 투 포인터 대부분 O 가변 좌우 포인터 양방향 슬라이딩 윈도우 X 고정 좌 또는 우 단방향 때로는 투 포인터와..
[LeetCode] 49. Group Anagrams (문자열 조작) 문제 https://leetcode.com/problems/group-anagrams/ Group Anagrams - 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 애너그램끼리 함께 묶어보자. 정답에서 애너그램과 단어의 순서는 상관하지 않는다. 애너그램 그룹은 같은 문자를 가졌지만 다른 의미를 가진다. 내 풀이 시간 : 156ms 1. 같은 애너그램 그룹에 속하는지 파악하기 위해 strs 안의 단어를 정렬하였다. 2. 정렬했을 때 같은 단어들의 조합으로 되어있으..
[LeetCode] 819. Most Common Word (문자열 조작) 문제 https://leetcode.com/problems/most-common-word/ Most Common Word - 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 문자열(paragraph)과 금지 단어 목록(banned)을 입력으로 받는다. 문자열 중에서 금지 단어가 아닌 가장 자주 사용된 단어를 소문자로 반환하자. 내 풀이 시간 : 50ms 처음에 정규식을 사용하지 않고 문자열 처리 함수인 split() 만으로 해결하려고 했더니 잘 안되었다. 그래서 ..
[LeetCode] 937. Reorder Data in Log Files (문자열 조작) 문제 https://leetcode.com/problems/reorder-data-in-log-files/ Reorder Data in Log Files - 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 logs 배열을 입력으로 받는다. 각 로그는 단어의 집합으로 이루어지는데 각 단어들은 공백으로 구분된다. 그리고 첫 번째 단어는 '식별자'라고 부른다. 로그는 두 가지 종류로 나뉜다. 1. Letter-logs : 식별자를 제외한 모든 단어들은 영어 소문자로 구..