Algorithm

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(); ..

Algorithm/Java

[백준] 25515 트리 노드 합의 최댓값

https://www.acmicpc.net/problem/25515https://beomseok37.tistory.com/94 백준 25515번 트리 노드 합의 최대값문제 설명 이번 문제는 트리가 주어지고 노드마다 하나의 정수가 적혀있을 때, 루트 노드에서 시작해 이웃한 노드를 방문하여 방문한 노드에 적혀있는 정수 합의 최대값을 출력하는 문제입니다beomseok37.tistory.com해당 링크를 참고하여 풀었다!기본적으로 자식 -> 부모로 이동이 가능하기 때문에(재방문 가능, 다시 더하지는 않음) 일반적인 트리 문제의 경로 중 최대값을 구하는 것과는 다르다! 예시 값에서도 볼 수 있듯이, 단순히 트리의 왼쪽 자식쪽에서만 값을 더하는 것이 아니라 더한 후 오른쪽 자식으로 넘어가는 듯한 모습을 볼 수 있다..

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