본문 바로가기

알고리즘

(9)
[알고리즘] 배열-배열 파티션 I - Jungyu Ko 문제 출처 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 문제 설명 n개의 페어를 이용한 min(a, b)의 합으로 만들 수 있는 가장 큰 수를 출력하라. 문제 풀이 class Solution: def arrayPairSum(self, nums: List[int]) -> int: sorted_nums = sorted(nums) # 입력 배열을 정렬 ans = 0 # 정답 ans 초기값 설정 for i in range(0, le..
[알고리즘] 배열-세 수의 합 - Jungyu Ko 문제 출처 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 문제 설명 배열을 입력받아 합으로 0을 만들 수 있는 3개의 엘리먼트를 출력하라. 문제 풀이 브루트포스 방법으로 문제를 풀면 시간초과 발생... ### 브루트포스 1 class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: ans = [] # 정답 nums = sorted(nums) # sorting! for i in ra..
[알고리즘] 배열-빗물 트래핑 - Jungyu Ko 문제 출처 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 문제 설명 높이를 입력받아 비 온 후 얼마나 많은 물이 쌓일 수 있는지 계산하라. 문제 풀이 class Solution: def trap(self, height: List[int]) -> int: empty_size = [] if len(height)
[알고리즘] 배열-두 수의 합 - Jungyu Ko 문제 출처 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 문제 설명 덧셈하여 타겟을 만들 수 있는 배열의 두 숫자 인덱스를 리턴하라. 문제 풀이 class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: ans = [] for i in range(len(nums)-1): # 처음부터 마지막 바로 전까지 반북 for j in range(i+1, len(nums)): #..
[알고리즘] 문자열 조작-그룹 애너그램 - Jungyu Ko 문제 출처 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 문제 설명 문자열 배열을 받아 애너그램 단위로 그룹핑하라. '애너그램'이란? 일종의 언어유희로 문자를 재배열하여 다른 뜻을 가진 단어로 바꾸는 것을 말한다. '어구전철'이라고도 부르며, 과거 유럽에서는 근대까지 이러한 언어유희가 매우 유행했다고 한다. 애너그램의 우리말 예로는, '문전박대'를 '대박전문'으로 바꿔 부르는 단어 등을 들 수 있다. 문제 풀이 애너그램을 판단하는 ..
[알고리즘] 문자열 조작-가장 흔한 단어 - Jungyu Ko 문제 출처 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 문제 설명 금지된 단어를 제외한 가장 흔하게 등장하는 단어를 출력하라. 대소문자 구분을 하지 않으며, 구두점(마침표, 쉼표 등) 또한 무시한다. 문제 풀이 class Solution: def mostCommonWord(self, paragraph: str, banned: List[str]) -> str: lowers = paragraph.lower() # 입력 문자열을 소..
[알고리즘] 문자열조작-로그파일 재정렬 - Jungyu Ko 문제 출처 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 문제 설명 로그를 재정렬하라. 기준은 다음과 같다. 로그의 가장 앞 부분은 식별자다. 문자로 구성된 로그가 숫자 로그보다 앞에 온다. 식별자는 순서에 영향을 끼치지 않지만, 문자가 동일할 경우 식별자 순으로 한다. 숫자 로그는 입력 순서대로 한다. 문제 풀..
[알고리즘] 문자열조작-문자열 뒤집기 - Jungyu Ko 문제 출처 https://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 문제 설명 문자열을 뒤집는 함수를 작성하라. 입력값은 문자 배열이며, 리턴 없이 리스트 내부를 직접 조작하라. 문제 풀이 class Solution: def reverseString(self, s: List[str]) -> None: """ Do not return anything, modify..