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

[알고리즘][Python] LeetCode 561 Array Partition 1 문제 풀이

문제 출처 : leetcode.com/problems/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 문제 해석 : 주어진 배열에서 2개씩 짝을 지을때 그 최솟값의 합이 가장 큰 경우를 구하는 문제이다. 문제 풀이 : 정렬을 통해서 간단하게 풀이할 수 있다. 정렬되었을 때 가장 큰 값을 최솟값으로 하는 방법은 0번째 2번째 4번째 .... 2n번쨰 수를 선택해서 이를 더해 주는 것이다. 가능한 ..

[알고리즘][Python] LeetCode 15 3-Sum 문제 풀이

문제 출처 : leetcode.com/problems/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이 되는 조합을 모든 찾아 반환하는 문제이다. 문제 풀이 : 브루트포스로 풀이를 진행하면 시간 복잡도에 의해서 정답 처리를 받지 못한다. 따라서 2개의 수를 더하는 것과 같이하면서 그 사이에 수를 선택하는 방법으로 풀이 할 수 있다. ( 아래 풀이는 "파이썬 알고리즘 인터뷰"를 참고 했습니다...

[알고리즘][Python] LeetCode 42 Trapping Rain Water 문제 풀이

문제 출처 : leetcode.com/problems/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 문제 해석 : 빗물이 고이는 총량을 계산하는 문제이다. 문제는 직관적이지만 다양한 접근을 활용할 수 있다. 문제 풀이 : 아래 풀이는 O(n^2)의 브루트포스 풀이법으로 각 원소마다 양쪽의 최댓값을 구해서 작은 부분과의 차를 전체 합에 더해 주는 방식이다. 하지만 이 풀이는 시간제한이 빡..

[알고리즘][Python] LeetCode 1 Two Sum 문제 풀이

문제 출처 : leetcode.com/problems/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 문제 해석 : 배열안에 두 개의 수를 더했을 때 target과 같은 인덱스들을 반환하는 문제이다. 문제 풀이 : 여기서는 가장 쉬운 방법인 브루트포스로 풀이 하였다. 가능한 다른 풀이 : 풀이에 따르면 in을 사용한 방법, 딕셔너리를 활용하는 방법 등이 있다. 자세한 다양한 풀이법을 알고 싶다면 leetcode 페이지의 solut..

[알고리즘][Python] LeetCode 5 Longest Palindromic Substring 문제 풀이

문제 출처 : leetcode.com/problems/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 문제 해석 : 문자열 속에서 가장 긴 팰런드롬을 찾아서 반환하는 문제이다. 문제 풀이 : 이 문제는 투 포인터를 활용하여 풀이 할 수 있다. --> ( 이 문제의 풀이는 파이썬 알고리즘 인터뷰 책의 풀이를 참고 했습니다. ) 풀이 코드 class Sol..

[알고리즘][Python] LeetCode 49 Group Anagrams 문제 풀이

문제 출처 : 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 문제 해석 : 문자열 배열을 입력으로 받고 애너그램 단위로 이를 묶어서 반환하는 문제이다. 문제 풀이 : sort를 함으로 갯수와 문자구성이 동일한지 알 수 있다. 이를 딕셔너리로 정리하여 한 배열에 담아서 반환하면 된다. 풀이 코드 from typing import List class Solution: ..

[알고리즘][Python] LeetCode 819 Most Common Word 문제 풀이

문제 출처 : 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 문제 해석 : 차단된 단어를 제외하고 문장에서 가장 많이 나온 단어를 찾는 것이다. 문제 풀이 : 단어 이외에 다른 문자들도 섞여 있기 때문에 정규표현식을 이용해서 전처리를 진행한 후에 Counter를 사용해서 이를 해결 하면 된다. 풀이 코드 from typing import List import..

[알고리즘][Python] LeetCode 937 Reorder Data in Log Files 문제 풀이

문제 출처 : 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 문제 해석 : 로그 파일을 정해진 규칙 대로 재 정렬하는 문제이다. 문제 풀이 : 파이썬 기본 내장 Sort 함수의 정렬 방법을 알면 쉽게 풀 수 있다. 풀이 코드 from typing import List class Solution: def reorderLogFil..

[알고리즘][Python] LeetCode 344 Reverse String 문제 풀이

문제 출처 : leetcode.com/problems/reverse-string/ Reverse String - 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 문제 해석 : 문자 배열을 뒤집어라 문제 풀이 : 다양한 방법으로 풀 수 있지만 가장 간단하게 built-in 함수를 이용했다. 가능한 다른 풀이 : index를 이용해서 직접 변경해 줄 수 있다. 풀이 코드 class Solution: def reverseString(self, s) -> None: s...

[알고리즘][Python] LeetCode 125 Valid Palindrome 문제 풀이

문제 출처 : leetcode.com/problems/valid-palindrome/ Valid Palindrome - 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 isPalindrome..