Algorithm

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 ..

Algorithm/Python

[프로그래머스] 게임 맵 최단거리, 네트워크 (BFS)

https://school.programmers.co.kr/learn/courses/30/lessons/1844 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr# 최단거리 -> BFS 사용from collections import dequedef solution(maps): queue = deque() n = len(maps) m = len(maps[0]) # 방향 이동 mv = [[1,0],[0,1],[-1,0],[0,-1]] queue.append((0,0)) # 시작점 while queue: x,y = queue.popleft() ..

Algorithm/Python

[백준] 2096 내려가기

https://www.acmicpc.net/problem/2096DP 문제지만 메모리 제한이 빡센 편이라 최소의 공간을 사용하게 배열을 설계해야 한다!이번 문제에서는 DP 배열에서 딱 직전의 값과, 입력 들어오는 숫자 값만 고려하면 되기 때문에 한 행짜리 배열로 DP를 풀었다.import sysinput = sys.stdin.readlinen = int(input()) # linesnums = list(map(int, input().split()))prev_min = nums[:]prev_max = nums[:]for i in range(1,n): nums = list(map(int, input().split())) # min curr_min = [ min(prev_min[0..

Algorithm/Python

[프로그래머스] 코딩테스트 고득점 Kit #해시

https://school.programmers.co.kr/learn/courses/30/lessons/1845 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.krdef solution(nums): # 최대 종류 -> n/2 개 return min(len(nums)/2, len(set(nums)))https://school.programmers.co.kr/learn/courses/30/lessons/42577 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.kr def solution(phone_boo..

Algorithm/Python

[프로그래머스] 코딩테스트 고득점 Kit #스택/큐

https://school.programmers.co.kr/learn/courses/30/lessons/12906 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프programmers.co.krdef solution(arr): answer = [] answer.append(arr[0]) for x in arr: if answer[-1] != x: answer.append(x) return answerhttps://school.programmers.co.kr/learn/courses/30/lessons/42586 프로그래머스SW개발자를 위한 평가, 교육, 채용까지 Total Solut..

Algorithm/Python

[백준] 9465 스티커

DP로 풀며 위쪽 스티커 선택할것인지 아래쪽 스티커 선택할 것인지 직전과 그 전 줄을 보며 선택import sysinput = sys.stdin.readlinet = int(input())for _ in range(t): n = int(input()) value = [[0]*(n+1) for _ in range(2)] value[0][1:] = list(map(int, input().split())) value[1][1:] = list(map(int, input().split())) dp = [[0]*(n+1) for _ in range(2)] dp[0][1] = value[0][1] dp[1][1] = value[1][1] for i in range(2, n+..

Algorithm/Python

[백준] 30805 사전 순 최대 공통 부분

import sysinput = sys.stdin.readlinen = int(input())a = list(map(int, input().split()))m = int(input())b = list(map(int, input().split()))arr=[]while a and b: common = set(a) & set(b) if not common: break else: start = max(common) arr.append(start) a = a[a.index(start)+1::] b = b[b.index(start)+1::]print(len(arr))for x in arr: print(x, end=" ")가장 큰 값이 앞 숫자가 되는..

Algorithm/Python

[프로그래머스] 완주하지 못한 선수

https://school.programmers.co.kr/learn/courses/30/lessons/42576?language=python3from collections import defaultdictdef solution(participant, completion): answer = '' part_cnt = defaultdict(int) for ppl in participant: part_cnt[ppl]+=1 for ppl in completion: if part_cnt[ppl]==1: part_cnt.pop(ppl) else : part_cnt[ppl]-=1 for ppl in part_cnt.keys(): ..

Algorithm/Python

[백준] 16953 A ->B

https://www.acmicpc.net/problem/16953import sysinput = sys.stdin.readlinea,b = map(int, input().split())cnt = 1while b>a: if b%2==0: cnt+=1 b/=2 elif b%10==1: cnt+=1 b//=10 else: breakif a!=b: print(-1)else : print(cnt)python의 경우 sys.stdin.readline 함수를 input 에 저장해두고 쓰면 편하다!split이 아니라 python의 map 함수를 사용해서, 형태 변환 및 변수 할당을 같이 할 수 있다

Algorithm/Java

[프로그래머스] 베스트 앨범

https://school.programmers.co.kr/learn/courses/30/lessons/42579?language=java 각 용도에 맞게 사용할 배열의 형태와, 이들을 정렬하는 방법을 찾는게 중요한 문제다.정렬과 배열의 형태를 쓰임에 맞게 바꾸는 게 약해서, map들의 정렬과 list의 정렬 방법을 암기해야겠다!import java.util.*;class Solution { public int[] solution(String[] genres, int[] plays) { List answer = new ArrayList(); Map playCntMap = new HashMap(); Map> genreMap = new HashMap(); ..

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