전체 글

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..
2753번 year = int(input()) if year%400 == 0: print(1) elif year%4 == 0 and year%100 != 0: print(1) else: print(0) 조건문에서 여러 조건을 입력할 때 c언어와 같이 &&을 썼는데 and를 써야한다. 2884번 gHour, gMinute = map(int, input().split()) if gHour < 1 and gMinute < 45: pHour = 23 pMinute = 15 + gMinute else: res = 60*gHour + gMinute - 45 pHour = res//60 pMinute = res%60 print(str(pHour)+" "+str(pMinute)) 최종 출력문을 print(pHour+p..
내장함수 map(함수명, 리스트명) : 리스트값 하나하나를 함수명에 대입 def f(x): return x + 5 numList = [1, 2, 3, 4, 5] print(list(map(f, numList))) def f(x): return x + 5 numList = [1, 2, 3, 4, 5] tempList = [] for i in numList: tempList.append(f(i)) print(tempList) 위 map 함수를 이용한 코드는 아래의 반복문을 이용한 코드와 동일한 결과를 출력한다. 입력 값을 변수 두 개에 저장할 때는 아래와 같이 쓴다. var1, var2 = input().split() var1, var2 = input().split('기준문자열') var1, var2 = i..
210B
break