개발일기
하루에 한가지라도 배울점을 찾기
-
(LeetCode)42. 빗물 트래핑
(LeetCode)42. 빗물 트래핑 문제 Trapping Rain Water - LeetCode 아이디어 투포인터를 이용해 가운데 가장높은 장벽까지 왼쪽과 오른쪽 포인터중 값이 낮은 포인터를 이동시킨다(같은경우 오른쪽을 이동) 가장높은 장벽에서 두 포인터가 만나면 종료 코드 class Solution: def trap(self, height: List[int]) -> int: if not height: return 0 volume = 0 left, right =...
-
(LeetCode)1. 두 수의 합
(LeetCode)1. 두 수의 합 문제 Two Sum - LeetCode 아이디어 브루트 포스로 계산하면 쉽게 계산할수 있으나 시간복잡도가 O(N^2)으로 굉장히 느림 첫 번째 수를 뺀 결과 키조회 → 비교나 탐색 대신 한번에 정답을 찾는 방법 코드 def twoSum(self, nums: List[int], target: int) -> List[int]: nums_map = {} # 키와 값을 바꿔서...
-
(LeetCode)561. 배열 파티션 I
(LeetCode)561. 배열 파티션 I 문제 Array Partition I - LeetCode 아이디어 정렬 후 짝수번째항을 모두 더함 코드 class Solution: def arrayPairSum(self, nums: List[int]) -> int: return sum(sorted(nums[::2])) 배운점 강력한 파이썬의 문자열 슬라이싱을 사용하면 간단히 풀이 가능
-
(LeetCode)121. 주식을 사고팔기 가장 좋은 시점
(LeetCode)121. 주식을 사고팔기 가장 좋은 시점 문제 Best Time to Buy and Sell Stock - LeetCode 아이디어 카데인 알고리즘 적용 코드 class Solution: def maxProfit(self, prices: List[int]) -> int: profit = 0 min_price = sys.maxsize # 최솟값과 최댓값을 계속 갱신 for price in prices: min_price = min(min_price, price) profit =...