알고리즘 91

[프로그래머스] 2 x n 타일링 문제 풀이와 코드

https://school.programmers.co.kr/learn/courses/30/lessons/12900?language=python3# 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr [풀이방법] 타일 크기는 2x1이기 때문에 결국 가로길이인 n에서 1과 2를 조합한 경우의 수를 구하는 문제나 마찬가지이다. n=0인 경우는 타일을 배치할 수는 없지만 경우의 수로 따졌을 때는 타일이 0개 배치된 한 가지 방법이 있다. n=1인 경우는 타일을 세로로 배치하는 한 가지 방법밖에 없다. n=2는 가로와 세로 배치가 모두 가능한데, 세로배치 같은 경우는 ..

알고리즘 2023.01.10

LeetCode 2258. Escape the Spreading Fire (BFS, Dijkstra)

https://leetcode.com/problems/escape-the-spreading-fire/ Escape the Spreading Fire - 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초마다 사방으로 퍼지는데 불에 타지 않고 도착지까지 간다고 할 수 있을 때, 시작위치에서 최대로 머무를 수 있는 시간을 구하는 문제이다. 크게 두 단계로 나눠서 진행해야 한다. 1단계는 불이 근원지부터 모든 셀에 퍼지는데 걸리는 시간을 grid에 기록하고 2단..

알고리즘 2022.06.22

LeetCode LCA문제: 1143. Longest Common Subsequence (top-down, bottom-up)

https://leetcode.com/problems/longest-common-subsequence/ Longest Common Subsequence - 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 알고리즘의 전형적인 LCA문제 그 자체이다. 이 문제는 top-down 방식과 bottom-up 방식 두 가지로 풀어보았다. class Solution: def longestCommonSubsequence(self, text1: str, text2: str) ->..

알고리즘 2022.06.01

LeetCode BFS문제: 909. Snakes and Ladders

https://leetcode.com/problems/snakes-and-ladders/ Snakes and Ladders - 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. ladder 또는 snake를 만났을 때 해당 위치로 바로 이동하는 게 하나의 과정이라는 것 먼저 좌표를 구하는 함수를 따로 만들어서 현재 칸 번호를 주면 row, col을 되돌려주도록..

알고리즘 2022.05.31

LeetCode 다익스트라 알고리즘, DFS 문제: 778. Swim in Rising Water

https://leetcode.com/problems/swim-in-rising-water/ Swim in Rising 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 이 문제는 DFS문제인데 다익스트라 알고리즘을 사용해서 풀 수도 있다. 파이썬은 heapq 메서드가 있어서 우선순위 큐를 구현하기 쉽기 때문에 파이썬으로는 다익스트라 알고리즘을 적용해서 풀었고 자바스크립트는 DFS로 풀었다. [다익스트라] 다익스트라 알고리즘은 이 문제처럼 각 지점 간..

알고리즘 2022.05.26

LeetCode DFS문제: 1202. Smallest String With Swaps(Sorting, DFS, 자바스크립트 이중배열)

https://leetcode.com/problems/smallest-string-with-swaps/ Smallest String With Swaps - 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 정렬과 DFS가 같이 혼합된 문제이다. 문제의 핵심은 pairs에서 swap 할 수 있는 모든 인덱스들을 묶어서 swap 하는 데에 있다. 만약 swap 할 수 있는 인덱스가 (1,2) (2,3) 두 가지라면 결국 (1,2,3)끼리는 어느 위치로든 swap 할 수..

알고리즘 2022.05.25

LeetCode BFS문제: 207. Course Schedule (파이썬 딕셔너리, 자바스크립트 map(value가 배열))

https://leetcode.com/problems/course-schedule/ Course Schedule - 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 prerequisites의 [0]의 값을 갖기 위해서는 [1]값을 먼저 얻어야 하는 BFS문제이다. 전형적인 BFS문제대로 풀었다. ① [0]의 값을 키로 딕셔너리(python) 혹은 맵(JS)를 만들어서 해당 값을 얻기 위해 먼저 얻어야 하는 목록들을 value로 저장해둔다. ② numCourses-..

알고리즘 2022.05.24

[LeetCode] 로봇이 체리 먹는 문제: 1463. Cherry Pickup II

https://leetcode.com/problems/cherry-pickup-ii/ Cherry Pickup II - 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 grid에서 경로를 선택하는 문제는 몇 번 풀었는데 이 문제는 경로를 선택하는 주체가 둘이다. 방법을 조금 고민하다 보면 두 로봇은 다음 row로 넘어갈 때 동시에 넘어가야 한다는 결론이 나온다. bottom-up방식을 선택했는데 여기에는 로봇이 하나인 경우처럼 선택할 수 있는 루트의 합계를 누적하..

알고리즘 2022.04.14

[LeetCode] 딕셔너리 알파벳 빈도수: 1255. Maximum Score Words Formed by Letters

https://leetcode.com/problems/maximum-score-words-formed-by-letters/ Maximum Score Words Formed by Letters - 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.Count()를 사용해서 letters에 있는 알파벳들의 개수를 파악한다. 이를 count라고 할 때 words의 문자열을 확인할 때마다 count의 개수를 줄여나가야 한다. words의 문자열을 취해서..

알고리즘 2022.04.14

[LeetCode] Edit_distance 알고리즘 문제: 72. Edit Distance

https://leetcode.com/problems/edit-distance/ Edit Distance - 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 이 문제는 edit distance 알고리즘의 전형적인 예시이다. 한 문자열을 다른 문자열로 변환하는 데 필요한 최소 작업 수를 계산하는 문제이다. 예전에 푼 문제와 비슷한 듯하면서도 다른다. 2021.12.02 - [알고리즘] - [Python] 리트코드 583. Delete Operation for Two..

알고리즘 2022.04.14