본문 바로가기

⏳ 알고리즘

(103)
리트코드 - 53. Maximum Subarray 문제 https://leetcode.com/problems/maximum-subarray/ Maximum Subarray - 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 코드 1) 메모이제이션 class Solution: def maxSubArray(self, nums: List[int]) -> int: for i in range(1, len(nums)): nums[i] += nums[i-1] if nums[i-1]>0 else 0 return max(nums)
<DP> 관련 문제 모음 EASY 1. 피보나치 수 https://leetcode.com/problems/fibonacci-number/ Fibonacci Number - 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 풀이 > 리트코드 - 509. Fibonacci Number 2. 최대 서브 배열 https://leetcode.com/problems/maximum-subarray/submissions/ Maximum Subarray - LeetCode Level up your codi..
리트코드 - 509. Fibonacci Number 문제 https://leetcode.com/problems/fibonacci-number/ Fibonacci Number - 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 코드 1) Memoization (Top-Down, 하향식) 하향식(Top-Down) 경우 하위 문제에 대하여 정답을 계산했는지 확인해가며 문제를 자연스럽게 풀어나가는 방법이다. 흔히 이를 Memoization이라고 부른다. d = [0] * 100 def fib(x) if x==1 or x=..
프로그래머스 - LV3. 단어 변환 문제 https://programmers.co.kr/learn/courses/30/lessons/43163 코딩테스트 연습 - 단어 변환 두 개의 단어 begin, target과 단어의 집합 words가 있습니다. 아래와 같은 규칙을 이용하여 begin에서 target으로 변환하는 가장 짧은 변환 과정을 찾으려고 합니다. 1. 한 번에 한 개의 알파벳만 바꿀 수 programmers.co.kr 코드 1) BFS 이용 from collections import deque def solution(begin, target, words): answer = 0 if target not in words: return 0 visited = [0] * len(words) queue = deque([begin]) whi..
프로그래머스 - LV3. 네트워크 문제 https://programmers.co.kr/learn/courses/30/lessons/43162 코딩테스트 연습 - 네트워크 네트워크란 컴퓨터 상호 간에 정보를 교환할 수 있도록 연결된 형태를 의미합니다. 예를 들어, 컴퓨터 A와 컴퓨터 B가 직접적으로 연결되어있고, 컴퓨터 B와 컴퓨터 C가 직접적으로 연결되어 있 programmers.co.kr 내 풀이 1) BFS 이용 from collections import deque def solution(n, computers): answer = n gp = [[] for _ in range(n)] for i in range(len(computers)): for j in range(0, i): if computers[i][j] == 1: gp[i]...
프로그래머스 - [3차] 방금그곡 (2018 KAKAO BLIND RECRUITMENT) 문제 https://programmers.co.kr/learn/courses/30/lessons/17683 코딩테스트 연습 - [3차] 방금그곡 방금그곡 라디오를 자주 듣는 네오는 라디오에서 방금 나왔던 음악이 무슨 음악인지 궁금해질 때가 많다. 그럴 때 네오는 다음 포털의 '방금그곡' 서비스를 이용하곤 한다. 방금그곡에서는 TV, programmers.co.kr 코드 1) 제목과 멜로디를 딕셔너리에 저장 def write_melody(item): li = [] i = 0 while i < len(item)-1: if item[i+1].isalpha(): li.append(item[i]) i += 1 else: li.append(item[i:i+2]) i += 2 li.append(item[-1]) re..
프로그래머스 - 삼각 달팽이(월간 코드 챌린지 시즌1) 문제 https://programmers.co.kr/learn/courses/30/lessons/68645 코딩테스트 연습 - 삼각 달팽이 5 [1,2,12,3,13,11,4,14,15,10,5,6,7,8,9] 6 [1,2,15,3,16,14,4,17,21,13,5,18,19,20,12,6,7,8,9,10,11] programmers.co.kr 코드 def solution(n): answer = [] snail = [[0] * n for _ in range(n)] num = 0 direction = 1 x, y = 0, 0 for count in range(n, 0, -1): if direction == 1: #아래 방향으로 direction = 2 for _ in range(count): num += ..
프로그래머스 - LV2. 더 맵게 문제 https://programmers.co.kr/learn/courses/30/lessons/42626?language=python3 코딩테스트 연습 - 더 맵게 매운 것을 좋아하는 Leo는 모든 음식의 스코빌 지수를 K 이상으로 만들고 싶습니다. 모든 음식의 스코빌 지수를 K 이상으로 만들기 위해 Leo는 스코빌 지수가 가장 낮은 두 개의 음식을 아래와 같 programmers.co.kr 내 풀이 1) 힙 이용 import heapq as hq def solution(scoville, K): hq.heapify(scoville) answer = 0 while True: first = hq.heappop(scoville) if first >= K: return answer if len(scoville..
프로그래머스 - 숫자 문자열과 영단어( 2021 카카오 채용연계형 인턴십 ) 문제 https://programmers.co.kr/learn/courses/30/lessons/81301 코딩테스트 연습 - 숫자 문자열과 영단어 네오와 프로도가 숫자놀이를 하고 있습니다. 네오가 프로도에게 숫자를 건넬 때 일부 자릿수를 영단어로 바꾼 카드를 건네주면 프로도는 원래 숫자를 찾는 게임입니다. 다음은 숫자의 일부 자 programmers.co.kr 풀이 1) 딕셔너리 이용 def solution(s): answer = '' dict = { 'zero' : '0', 'one' : '1', 'two' : '2', 'three' : '3', 'four' : '4', 'five' : '5', 'six' : '6', 'seven' : '7', 'eight' : '8', 'nine' : '9' } i..
프로그래머스 - 로또의 최고 순위와 최저 순위( 2021 Dev-Matching ) 문제 https://programmers.co.kr/learn/courses/30/lessons/77484 코딩테스트 연습 - 로또의 최고 순위와 최저 순위 로또 6/45(이하 '로또'로 표기)는 1부터 45까지의 숫자 중 6개를 찍어서 맞히는 대표적인 복권입니다. 아래는 로또의 순위를 정하는 방식입니다. 1 순위 당첨 내용 1 6개 번호가 모두 일치 2 5개 번호 programmers.co.kr 풀이 1) 반복문 이용 def solution(lottos, win_nums): low, high = 0, 0 correct_count = 0 unknown_num = lottos.count(0) for n in win_nums: if n in lottos: correct_count += 1 if unknown..