24. Swap Nodes in Pairs Medium 파이썬 알고리즘 인터뷰 229p Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.) ♬idea while 문, list.next 사용해서 값 바꾸기 1. 그냥 값만 바꾸기 # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val ..
234. Palindrome Linked List Easy 파이썬 알고리즘 인터뷰 201p Given the head of a singly linked list, return true if it is a palindrome or false otherwise. ♬idea 빗물 트래핑에서 썼던 투 포인터를 활용할 수 있을 것 같다. 런너를 사용한 역순 연결 리스트 작성을 통해 해결할 수 있을 것 같다. 1. 리스트와 투 포인터 사용 # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def isPal..
206. Reverse Linked List Easy 파이썬 알고리즘 인터뷰 219p Given the head of a singly linked list, reverse the list, and return the reversed list. ♬idea iteratively recursively 1. iteratively # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:..
876. Middle of the Linked List Easy Given the head of a singly linked list, return the middle node of the linked list. If there are two middle nodes, return the second middle node. ♬idea 런너를 이용해 fast 런너가 끝에 도착했을 때 slow 런너가 도달한 노드를 반환한다. 배열을 활용한다. 1. 런너를 이용한 풀이 # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class ..
빗물 트래핑 Hard 파이썬 알고리즘 인터뷰 180p Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it can trap after raining. ♬idea class Solution: def trap(self, height: List[int]) -> int: if not height: #리스트가 empty인지 확인 return 0 # 변수 선언 volume = 0 left, right = 0, len(height) - 1 left_max, right_max = height[left], height[right] while left < righ..
121. Best Time to Buy and Sell Stock Easy 파이썬 알고리즘 인터뷰 195p You are given an array prices where prices[i] is the price of a given stock on the ith day. You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock. Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0...
561. Array Partition Easy 파이썬 알고리즘 인터뷰 190p Given an integer array nums of 2n integers, group these integers into n pairs (a1, b1), (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) for all i is maximized. Return the maximized sum. ♬idea min 의 합이 커지려면 작은 수는 작은 수끼리, 큰 수는 큰 수 끼리 묶여야 낭비하는 큰 수가 없을 것 sort 함수로 정렬, for 문과 append 함수로 페어링 1. 배열 오름차순 정렬 후 페어링, min 합 구하기 class Solution: def arrayPai..