공부하고 기록하는, 경제학과 출신 개발자의 노트

leetcode 10

[Python] LeetCode 56. merge intervals

leetcode.com/problems/merge-intervals/ Merge Intervals - 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 겹치는 구간을 병합하는 문제. 파이썬의 리스트 병합문법 중 하나인 " , " 를 배웠다. 이 코드는 아래 책을 참고하였다. 파이썬 알고리즘 인터뷰 국내도서 저자 : 박상길 출판 : 책만 2020.07.15 상세보기

[Python] LeetCode 743. Network Delay Time

leetcode.com/problems/network-delay-time/ Network Delay Time - 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 다엑스트라 알고리즘 구현이 주목적인 문제. defaultdict / heapq 자료구조를 활용해 다엑스트라 알고리즘 구현을 해볼 수 있다. 이 코드는 아래의 책을 참고해 작성했다. 파이썬 알고리즘 인터뷰 국내도서 저자 : 박상길 출판 : 책만 2020.07.15 상세보기

[Python] LeetCode 78. Subsets

leetcode.com/problems/subsets/ Subsets - 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 부분집합을 만들어야 하는 문제에서, permutation / combination 라이브러리는 모든 경우의 수를 iterate 형태로 반환하기 때문에 시간초과가 발생할 수 있었다. 부분집합을 dfs 방식으로 생성하는 기본 문제이자 코드. 아래 코드는 이 책을 참고해서 작성하였다. 파이썬 알고리즘 인터뷰 국내도서 저자 : 박상길 출판 : 책만 2..

[Python] LeetCode 3. Longest Substring without repeating characters

leetcode.com/problems/longest-substring-without-repeating-characters/ Longest Substring Without Repeating Characters - 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 중복 문자열 없는 가장 긴 부분문자열 구하기. 슬라이딩 윈도우 + 투 포인터로 해결이 가능하다. 아래 풀이는 이 책의 코드를 참고했다. 파이썬 알고리즘 인터뷰 국내도서 저자 : 박상길 출판 : 책만 2020..

[Python] LeetCode 739. Daily Temperature

leetcode.com/problems/daily-temperatures/ Daily Temperatures - 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 각각 현재 날짜를 기준으로, 현재보다 더 온도가 높은 날이 있기까지 며칠이 걸리는지를 구하는 문제. stack을 사용해서 풀 수 있는, 백준 '오큰수' 와 완전히 동일한 문제. [Python] 백준 17298. 오큰수 스택을 사용해 풀 수 있는 문제. _ = input() # len(arr) 값과 똑같아서..

[Python] LeetCode 234. Palindrome Linked List

leetcode.com/problems/palindrome-linked-list/ 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 링크드 리스트가 주어졌을 때, 값이 좌우대칭인 Palindrome인지 판별하는 문제. 사실 Deque를 쓰면 매우 간단한 문제다. Deque까지 쓰지 않더라도, 리스트로 값을 저장할 수만 있다면 리스트 파싱으로도 쉽게 해결할 수 있다. 링크드 리스트의 특징을 살린 방법인 Runner를 적용해..

[Python] LeetCode 121. Best time to buy and sell stock

leetcode.com/problems/best-time-to-buy-and-sell-stock/ 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 한 번의 거래로 낼 수 있는 최대 이익을 계산하는 문제. 저점에 사서 고점에 팔아야 최대 수익이 남는데, 저점에서 먼저 산 뒤, 이후 최고점에서의 차액이 가장 커야 한다. 선후관계가 있다는 소리. 카데인 알고리즘을 활용하면 O(n)에 풀어낼 수 있다고 한다. ..

[Python] LeetCode 238. Product of Array Except self

leetcode.com/problems/product-of-array-except-self/ Product of Array Except Self - 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 배열을 입력받아, output[i]가 num[i]를 제외한 나머지 모든 요소의 곱셈 결과가 되도록 output을 생성하는 문제. 나눗셈을 사용하지 말라는 제약조건 때문에, 원소를 전부 곱한 다음 num[i]로 나누는 식의 접근이 불가능하다. 아래 코드는 이 책을 참고하여..

[Python] LeetCode 15. 3Sum

leetcode.com/problems/3sum/submissions/ 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이 되는 세 숫자의 조합"을 리스트로 저장해 반환하는 문제. combinations로 모든 경우의 수를 조사할 경우 timeout이 발생한다. 투 포인터로 해결할 수 있는 문제. cf. 이 코드는 아래의 책을 참고했습니다. 파이썬 알고리즘 인터뷰 국내도서 저자 : 박상길 출판 : 책만 2020.07.15 상세보기

[Python] LeetCode 42. Trapping Rain Water

https://leetcode.com/problems/trapping-rain-water/ 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 왼쪽 끝 / 오른쪽 끝에서부터 각각 가운데로 이동하면서 "최대 높이 - 현재 높이" 순으로 값을 더하는 식으로 풀 수 있는 문제. 왼쪽의 최대높이와 오른쪽의 최대높이를 비교해서, 더 작은 쪽을 가운데로 이동시킨다. cf. 이 풀이는 아래 책에서 제공한 코드를 참고했습니다. 파이썬 알고리즘..