본문 바로가기

알고리즘

(28)
[파이썬 알고리즘 인터뷰] 역순 연결 리스트 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 재귀 구조로 뒤집기 def reverseList(self, head) -> ListNode: def reverse(node, prev): if not node: return prev next, node.next = node.next, prev return reverse(next, node) return reverse(head) 반복 구조로 뒤집기 def reverseList(s..
[파이썬 알고리즘 인터뷰] 두 정렬 리스트의 병합 Merge Two Sorted Lists - 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 재귀 구조로 연결 def mergeTwoLists(self, l1,l2) -> ListNode: if (not l1) or (l2 and l1.val > l2.val): l1, l2 = l2, l1 if l1: l1.next = self.mergeTwoLists(l1.next, l2) return l1 재귀를 이용해서 푸는 건데... 솔직히 이해가 잘 되지 않는다... 재..
[파이썬 알고리즘 인터뷰] 팰린드롬 연결리스트 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 나의코드 def isPalindrome(self, head: Optional[ListNode]) -> bool: l=[] # 리스트로 변환 while head: l.append(head.val) head=head.next left,right=0,len(l)-1 #팰린드롬 확인 while left bool: q=[] if not head: return True node=he..
[파이썬 알고리즘 인터뷰] 주식을 사고팔기 가장 좋은 시점 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 나의코드 def maxProfit(self, prices: List[int]) -> int: min_price=prices[0] profit=0 for price in prices: min_price=min(price, min_price) profit = max(profit, price-min_price) return profit 최저점과 profit을 계..
[파이썬 알고리즘 인터뷰] 배열파티션1 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 나의풀이 def arrayPairSum(self, nums: List[int]) -> int: nums.sort() sum=0 for i in range(0,len(nums),2): sum+=min(nums[i],nums[i+1]) return sum 오름차순으로 정렬후 쌍을 구하여 각 쌍의 최솟값을 구하면 항상 가장 큰 값이 나옴 짝수번째 값 계산 def arrayPairSum(..
[파이썬 알고리즘 인터뷰] 3수의 합 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 브루트포스로 계산 def threeSum(self, nums: List[int]) -> List[List[int]]: results=[] nums.sort() for i in range(len(nums)-2): if i>0 and nums[i]==nums[i-1]: continue for j in range(i+1,len(nums)-1): if j>i+1 and nums[j]==nums[j-1]: con..
[파이썬 알고리즘 인터뷰] 빗물 트래핑 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 나의풀이 def trap(self, height: List[int]) -> int: left,right=0,len(height)-1 left_max=height[left] right_max=height[right] trapped=0 while left!=right: if left_maxright_max: right-=1 right_max=max(height[right],righ..
[파이썬 알고리즘 인터뷰] 가장 긴 팰린드롬 부분 문자열 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 중앙을 중심으로 확장하는 풀이 def longestPalindrome(self, s: str) -> str: def expand(left,right): while left>=0 and right

728x90
반응형