본문 바로가기

알고리즘

(86)
Python) 프로그래머스 - 롤케이크 자르기 https://school.programmers.co.kr/learn/courses/30/lessons/132265 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 실행코드 from collections import Counter def solution(topping): dic = Counter(topping) set_dic = set() res = 0 for i in topping: dic[i] -= 1 set_dic.add(i) if dic[i] == 0: dic.pop(i) print('setdic',set_dic, 'dic',dic) if len(d..
Python) 프로그래머스 - 게임 맵 최단거리 https://school.programmers.co.kr/learn/courses/30/lessons/1844 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 실행코드: from collections import deque def solution(maps): answer = -1 row = len(maps) col = len(maps[0]) visited = [[False] * col for _ in range(row)] delta = [(1,0), (-1,0),(0,1),(0,-1)] queue = deque() queue.append((0,0,1)) ..
Maximum Depth of Binary Tree https://leetcode.com/problems/maximum-depth-of-binary-tree/ Maximum Depth of Binary Tree - LeetCode Can you solve this real interview question? Maximum Depth of Binary Tree - Given the root of a binary tree, return its maximum depth. A binary tree's maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf leetcode.com Input: root = [3,9,20,null,nul..
LIFO - example 2 https://leetcode.com/problems/daily-temperatures/ Daily Temperatures - LeetCode Can you solve this real interview question? Daily Temperatures - Given an array of integers temperatures represents the daily temperatures, return an array answer such that answer[i] is the number of days you have to wait after the ith day to get a warmer leetcode.com 문제: 정수 온도 배열이 일교차를 나타내는 경우, [i]라는 대답이 더 따뜻한 온도를 얻..
LIFO - example 1 def isValid(s): stack = [] for p in s: if p == "(": stack.append(")") elif p == "{": stack.append("}") elif p == "[": stack.append("]") elif not stack or stack.pop() != p: return False return not stack 이 문제는 문자의 괄호의 상태가 올바른지에 대한 함수 입니다. stack을 사용하여 LIFO문제를 풀었습니다. 올바른 괄호가 되기 위해서는 ( { [ 여는 괄호가 들어왔을때 stack의 가장 윗부분이 마지막에 들어온 괄호와 맞아야 합니다. 혹은 닫는괄호 ) } ]가 먼저 들어온 경우에도 맞지 않기 때문에 False를 return 합니다. for문을 ..
Linked List - leetcode 예시로 이해 https://leetcode.com/problems/design-browser-history/ Design Browser History - LeetCode Can you solve this real interview question? Design Browser History - You have a browser of one tab where you start on the homepage and you can visit another url, get back in the history number of steps or move forward in the history number of steps. Implem leetcode.com class ListNOde(object): def __init__(sel..
Python) 프로그래머스 - 자연수 뒤집어 배열로 만들기 https://school.programmers.co.kr/learn/courses/30/lessons/12932 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 실행코드 def solution(n): if not isinstance(n, int) or n < 1: raise ValueError("Input must be a natural number.") return [int(char) for char in str(n)[::-1]] 풀이 먼저 입력 n이 자연수인지 확인하고 그렇지 않은 경우 오류 메시지가 있는 'ValueError'을 raise 해 줍니다..
Python) 프로그래머스 - 약수의 합 https://school.programmers.co.kr/learn/courses/30/lessons/12928 프로그래머스 코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요. programmers.co.kr 실행코드 def solution(n): sum = 0 for i in range(1, n+1): if n % i == 0: sum += i return sum 풀이: 1부터 n까지 for문으로 탐색. 만약 i가 n으로 나누어지면 약수. 약수를 sum에 더해서 약수들의 합을 구해준다.