Algorithm

Algorithm/Python

[백준] 1918 후위 표기식

import sysfrom collections import dequearr = sys.stdin.readline().rstrip()stack =[]answer=""prior = { '+' : 1, '-':1, '*' : 2, '/': 2 }for x in arr: if x.isalpha(): answer+=x elif x in "+-*/": while stack and stack[-1] !='(' and prior[stack[-1]] >= prior[x]: answer+=stack.pop() stack.append(x) elif x=='(': stack.append(x) elif x=='..

Algorithm/Python

[백준] 10866

https://www.acmicpc.net/problem/10866문제에서 시킨 대로 구현하면 된다.import sysinput = sys.stdin.readlinewrite = sys.stdout.writen = int(input())deque = list()for _ in range(n): inst = list(input().split()) if inst[0]== "push_front": deque.insert(0,inst[1]) elif inst[0]=="push_back": deque.append(inst[1]) elif inst[0]=="pop_front": if len(deque)>0: write(str(deque.pop(0))+"\n..

Algorithm/Python

[백준] 13701 중복 제거

https://www.acmicpc.net/problem/13701틀린 답안import sysinput = sys.stdin.readlinenums = set()for x in map(int, input().split()): if x not in nums: sys.stdout.write(str(x) + ' ') nums.add(x) 비트마스킹으로 풀어야 한다!! 메모리가 작아서 set도 못 쓴다..물론 한 줄로 입력 받아버려도 메모리가 안 돼서 한 글자씩 처리해야 한다!아래는 통과한 코드이다...import sysvisited = bytearray(1 > 3 bit_offset = num & 7 mask = 1

Algorithm/Python

[백준] 1740 거듭제곱

https://www.acmicpc.net/problem/174000001 -> 1 00010 -> 3 00011 ->1+3 = 4한 개 이상의 서로 다른 3의 제곱수의 합으로 표현되는 수 -> 이진수처럼 계산하되, 제곱되는 수를 3으로 하면 됨 ( 각 자리당 0,1만 가능 - 같은 수 두 번 이상 더할 수 없기 때문!!) 따라서 n을 이진수로 변환한 뒤에, 각 자릿수를 더할 때 2의 제곱들을 더하는 게 아니라 3의 제곱을 각 자릿수만큼 계산해서 더하면 된다.import sysinput = sys.stdin.readlinen = int(input())ans=0idx=1while n>1: ans+=(n%2)*idx n//=2 idx*=3ans+=n*idxprint(ans)

Algorithm/Python

[백준] 1932 정수 삼각형

https://www.acmicpc.net/problem/1932선택된 수의 합이 최대가 되는 경로를 구하기 위해 DP를 사용한다.아래층에 있는 수는 현재 층에서 선택된 대각선의 왼쪽 혹은 오른쪽에 있는 것만 가능 -> 배열 상 위에가 k번째일때, 아래에서는 k나 k+1만 선택가능하다.위에서 아래로 내려가며 고려하는 것보다, 아래에서 위로 올라가며 최종 선택되어 도출된 하나의 값이 자연스레 최대의 값이 되도록 하는 것이 편하다!!따라서 위(k번째 값)에서는 아래 k, k+1중 큰 값을 현재에 더한다. 이를 해당 레벨의 반복을 돌린 후, 더 위의 레벨로 올라가 다시 아래에서 이동 가능한 두 값중 더 큰값을 현재 위치의 값에 더한다.이를 반복한다면, 가장 상위에 있는 하나의 값은 자연적으로 갈 수 있는 모든..

Algorithm/Python

[백준] 17626

https://www.acmicpc.net/problem/17626python3으로 내니까 안 돼서 찾아보니까 pypy3으로 내면 된다길래 내보니까 진짜됨...허무import sysimport mathinput = sys.stdin.readlinen = int(input())dp = [i for i in range(n+1)]for i in range(2, n+1): sqrt = int(math.sqrt(i)) for x in range(sqrt, 0, -1): if dp[i] > dp[i-x*x]+1 : dp[i] = dp[i-x*x]+1print(dp[n])

Algorithm/Python

[프로그래머스] 단어 변환

https://school.programmers.co.kr/learn/courses/30/lessons/43163 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr처음 DFS + 백트래킹으로 푼 답안 (맞음)- 최소 횟수를 구하는 것이므로 BFS가 논리적으로 더 좋은 건 알지만 구현 방법이 아리송해서 DFS로 우선 풀었다!!answer = 51def solution(begin, target, words): global answer visited = [False for _ in range(len(words))] for i in range(len(words)): if onecha..

Algorithm/Python

[프로그래머스] 코딩테스트 고득점 Kit #완전탐색

https://school.programmers.co.kr/learn/courses/30/lessons/86491?language=python3 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr정렬하는 방법 익히기 - 한 줄로 작성하는 for 문, 람다 등 사용!!!def solution(sizes): sizes = [sorted(size, reverse=True) for size in sizes] return max(x for x, _ in sizes)*max(y for _, y in sizes) https://school.programmers.co.kr/learn/courses/30/less..

Algorithm/Python

[프로그래머스] 가장 큰 수

https://school.programmers.co.kr/learn/courses/30/lessons/42746 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr배열 안에 있는 각 숫자는 1000 이하이기 때문에 글자 수 맞춰서 정렬해줌만약에 "30" vs "3"이렇게 비교하면 원래 문자열이었으면 30이 더 큰 걸로 여겨져서 30이 먼저나옴(내림차순 정렬 시)근데 사실 그러면 안 되고, 3이 먼저 나와야 함-> 해당 숫자를 3번 반복해서 붙였을 때 "333"과 "303030"이 비교되게 끔 함def solution(numbers): numtostr = list(map(str, numbers)) ..

Algorithm/Python

[백준] 2294 동전2

https://www.acmicpc.net/problem/2294당연히 dp로 풀어야된다import sysinput = sys.stdin.readlinen, k = map(int, input().split())coins = [0 for _ in range(n)]for i in range(len(coins)): coins[i] = int(input())coins.sort()dp = [0 for _ in range(k+1)]for i in range(1, k+1): for coin in coins: if coin>i: break if i-coin!=0 and dp[i-coin]==0: continue dp[i] = dp[i-coin]+1 if dp[i]==0 ..

yoursin
'Algorithm' 카테고리의 글 목록 (7 Page)