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

프로그래밍/코딩테스트 문제풀이 189

[Python] 프로그래머스. 내적 (Level 1)

programmers.co.kr/learn/courses/30/lessons/70128 코딩테스트 연습 - 내적 길이가 같은 두 1차원 정수 배열 a, b가 매개변수로 주어집니다. a와 b의 내적을 return 하도록 solution 함수를 완성해주세요. 이때, a와 b의 내적은 a[0]*b[0] + a[1]*b[1] + ... + a[n-1]*b[n-1] 입니다. (n은 a, b의 programmers.co.kr def solution(a, b): return sum([i*j for i, j in zip(a,b)])

[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] 프로그래머스. 예상 대진표 (Level 2)

programmers.co.kr/learn/courses/30/lessons/12985# 코딩테스트 연습 - 예상 대진표 △△ 게임대회가 개최되었습니다. 이 대회는 N명이 참가하고, 토너먼트 형식으로 진행됩니다. N명의 참가자는 각각 1부터 N번을 차례대로 배정받습니다. 그리고, 1번↔2번, 3번↔4번, ... , N-1번↔N programmers.co.kr A < B인 A, B를 전제하면, 문제 조건에서 A와 B는 서로 만나기 전까지 반드시 승리하므로 A와 B는 대전을 거쳐갈 때마다 (자신의 위치 // 2 + 자신의 위치 % 1) 이 된다. ex) A. B = 4, 7인 경우 1회 대전 후 A와 B의 위치는 A는 3과 대전해 승리하므로 (4 // 2 + 4 % 2) = 2번째. B는 8과 대전해 승리하..

[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)에 풀어낼 수 있다고 한다. ..