📊알고리즘, 문제풀이/📈문제풀이 (PS) 136

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

[알고리즘][Python] LeetCode 24 Swap Nodes in Pairs 문제 풀이

문제 출처 : leetcode.com/problems/swap-nodes-in-pairs/ Swap Nodes in Pairs - 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 책의 풀이를 참고했습니다. ) 풀이..

[알고리즘][Python] LeetCode 2 Add Two Numbers 문제 풀이

문제 출처 : leetcode.com/problems/add-two-numbers/ Add Two Numbers - 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개의 링크드 리스트에 있는 수를 더해서 다시 링크드 리스트로 반환하는 문제이다. 문제 풀이 : 링크드 리스트를 읽어 드려 합을 더하고 다시 링크드 리스트로 만드는 과정을 거치면 문제를 풀 수 있다. 풀이 코드 class ListNode: def __init__(self, val ..

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

문제 출처 : leetcode.com/problems/reverse-linked-list/ Reverse 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 문제 해석 : 주어진 링크드 리스트를 뒤집어서 반환하는 문제이다. 문제 풀이 : 주어진 링크드 리스트를 하나씩 탐색하면서 새로운 링크드리스트에 연결시켜주어 반환하면 된다. 풀이 코드 class ListNode: val = 0 next = None def __init__(self, val ..

[알고리즘][Python] LeetCode 234 Palindrome Linked List 문제 풀이

문제 출처 : leetcode.com/problems/palindrome-linked-list/ Palindrome 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 문제 해석 : 링크드 리스트를 이용한 팰린드롬 문제이다. 문제 풀이 : 링크드 리스트 개념을 이해하고 있으면 val를 모두 리스트에 저장하여 팰린드롬을 검사하면 쉽게 풀 수 있는 문제이다. 풀이 코드 class ListNode: def __init__(self, val = 0,..

[알고리즘][Python] 백준 15683 감시 문제 풀이

문제 출처 : www.acmicpc.net/problem/15683 15683번: 감시 스타트링크의 사무실은 1×1크기의 정사각형으로 나누어져 있는 N×M 크기의 직사각형으로 나타낼 수 있다. 사무실에는 총 K개의 CCTV가 설치되어져 있는데, CCTV는 5가지 종류가 있다. 각 CCTV가 감 www.acmicpc.net 문제 해석 : 여러 종류의 카메라를 90도씩 회전하면서 각자 다른 방향을 동시에 감시할 수 있다. 이때 사각지대가 최소가 되도록 하면 사각지대의 갯수를 구하는 문제이다. 문제 풀이 : 문제에서 요구하는 방법을 차근차근 따르면서 재귀, 스택 형식으로 코드를 작성하면 풀수 있다. 풀이 코드 dx = [-1,0,1,0] dy = [0,1,0,-1] def see(arr, i, j, dir_l..

[알고리즘][Python] LeetCode 121 Best Time to Buy and Sell Stock 문제 풀이

문제 출처 : leetcode.com/problems/best-time-to-buy-and-sell-stock/ Best Time to Buy and Sell Stock - 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 문제 해석 : 시간에 흐름에 따라 생성된 배열은 각 시간에 흐름에 따른 주식의 가격을 의미한다. 이때 최대의 이득을 구하여라 문제 풀이 : 모든 경우의 수를 검사할 때는 시간 복잡도가 O(N^2)이기 때문에 문제를 풀이할 수 없다. 따라서 O(N..

[알고리즘][Python] LeetCode 238 Product of Array Except Self 문제 풀이

문제 출처 : leetcode.com/problems/product-of-array-except-self/ Product of Array Except Self - 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 문제 해석 : 배열에서 자기 자신을 제외한 모든 수들의 곱을 구하는 문제이다. 다만 나누기 연산을 활용하지 않아야 하면 시간 복잡도 O(n)에 풀 수 있도록 해야한다. 문제 풀이 : 배열 두개를 활용해서 앞에서부터의 곱 뒤에서 부터의 곱을 미리 계산해 놓은..