분류 전체보기 202

[알고리즘][Python] 백준 17940 지하철 문제 풀이

문제 출처 : www.acmicpc.net/problem/17940 17940번: 지하철 대학원생인 형욱이는 연구실에 출근할 때 주로 지하철을 이용한다. 지하철은 A와 B, 두 개의 회사에서 운영하고 있다. 두 회사는 경쟁사 관계로 사람들이 상대 회사의 지하철을 이용하는 것을 매 www.acmicpc.net 문제 해석 : 최소의 환승과 시간을 기준으로 목적지까지 갈 때 환승 횟수와 시간을 구하는 문제이다. 문제 풀이 : 환승에는 높은 가중치를 두고 다익스트라 알고리즘으로 풀이하면 된다. 3개의 변수를 가지고 다익스트라를 실행할 경우에는 메모리 초과가 워셜 플루이드 알고리즘으로 풀이시 시간 초과가 발생한다. 풀이 코드 import sys, heapq input = sys.stdin.readline def ..

[알고리즘][Python] LeetCode 20 Valid Parentheses 문제 풀이

문제 출처 : leetcode.com/problems/valid-parentheses/ Valid Parentheses - 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 문제 해석 : 괄호가 제대로 구성되어 있는지 검사하는 문제이다. 문제 풀이 : 시작 괄호와 끝 괄호의 짝을 매칭해가면서 검사하면 된다. 풀이 코드 class Solution: def isValid(self, s: str) -> bool: stack = [] a = { ')': '(', ']': ..

[알고리즘][Python] LeetCode 92 Reverse Linked List 2 문제 풀이

문제 출처 : leetcode.com/problems/reverse-linked-list-ii/ Reverse Linked List 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 문제 해석 : 시작점과 끝점이 주어졌을 때 그 구간을 뒤집어서 반화하는 문제이다. 문제 풀이 : 시작점과 끝점 앞과 뒤를 저장해 놓고 그 사이를 뒤집어서 반환하면 된다. ( 이 문제의 풀이는 Python Algorithm Interview 책을 참고 했습니다. ) 풀이 코드 c..

[알고리즘][Python] LeetCode 328 Odd Even Linked List 문제 풀이

문제 출처 : leetcode.com/problems/odd-even-linked-list/ Odd Even Linked List - 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 문제 해석 : 홀수번째 리스트 끼리, 짝수번째 리스트끼리 묶어서 다시 링크드 리스트를 반환하는 문제이다. 문제 풀이 : 문제에 제약 조건으로 상수 공간을 사용하라고 했기 때문에 하나씩 건너뛰면서 모두 저장하는 방식이 아닌 순차적으로 논리적인 연결을 수행해야 한다. ( 이 문제의 풀이는..