[프로그래머스/Python] 의상
·
Algorithm/Programmers
백준 '9375번, 패션왕 신해빈' 문제와 완전 똑같기 때문에 풀이도 똑같다. 해당 문제 풀이는 여기 참고! def solution(clothes): closet = {} for cloth, type in clothes: if type not in closet: closet[type] = 1 closet[type] += 1 answer = 1 for k, v in closet.items(): answer *= v return answer - 1
[프로그래머스/Python] 전화번호 목록
·
Algorithm/Programmers
처음 풀었던 방법인데 효율성 테스트 3,4 에서 시간초과가 뜬 코드다 def solution(phone_book): p = [] answer = True for num in sorted(phone_book, key = lambda x: len(x)): for prefix in p: if num[:len(prefix)] == prefix: answer = False else: p.append(num) if not answer: break return answer startswith 도 써보고, 길이로 정렬이 아닌 그냥 문자열 정렬을 해주기도 했지만 계속 시간 초과가 나서 풀이 과정에 뭔가 생각의 전환이 필요함을 느꼈다!! 참고로 문자열 숫자 정렬은 숫자 크기가 아닌, 앞의 글자 숫자 크기로 정렬을 한다(내..
[백준/Python] 1600번 - 말이 되고픈 원숭이
·
Algorithm/BaekJoon
import sys from collections import deque input = sys.stdin.readline # 상하좌우, 체스 나이트 이동 monkey = [[1,0],[0,1],[-1,0],[0,-1]] horse = [[-2,-1], [-2,1],[-1,-2],[-1,2],[2,-1],[2,1],[1,-2],[1,2]] K = int(input()) # 체스 나이트처럼 움직일 수 있는 횟수 W, H = map(int, input().split()) graph = [list(map(int, input().split())) for _ in range(H)] ## 특정 이동 방식에 횟수가 정해져 있을 때 3차원 배열 활용! ## 즉, 방문 처리 배열을 3차원으로 나타냄: visited[열][..
[백준/Python] 13549번-숨바꼭질 3
·
Algorithm/BaekJoon
import sys from collections import deque input = sys.stdin.readline MAX = 100000 + 1 n, k = map(int, input().split()) visited = [-1] * MAX # [위치][시간] 으로 하려고 했으나 메모리 초과로 [위치]=시간으로 바꿈(물론 dist 배열을 따로 둬도 됨) def bfs(): q = deque() q.append(n) visited[n] = 0 while q: loc = q.popleft() # 동생 위치를 찾음 if loc == k: return visited[loc] # 순간이동 if loc * 2 < MAX and visited[loc*2] == -1: visited[loc*2] = visite..
[백준/Python] 3184번 - 양
·
Algorithm/BaekJoon
import sys input = sys.stdin.readline r, c = map(int, input().split()) yard = [list(input().rstrip()) for _ in range(r)] visited = [[False for _ in range(c)] for _ in range(r)] # . 빈필드 / # 울타리 / o 양 / v 늑대 def bfs(a, b): stack = [(a,b)] sheep, wolf = 0, 0 while stack: x, y = stack.pop() visited[x][y] = True # 양, 늑대 개수 세기 if yard[x][y] == 'o': sheep += 1 elif yard[x][y] == 'v': wolf += 1 for dx, ..
[백준/Python] 9375번 - 패션왕 신해빈
·
Algorithm/BaekJoon
import sys input = sys.stdin.readline for _ in range(int(input())): clothes = {} # 옷 분류 for _ in range(int(input())): cloth, type = input().rstrip().split() if type in clothes: clothes[type] += 1 else: clothes[type] = 1 # 분류 대로 코디하기 # +1 해서 숫자를 모두 곱하고 -1(알몸) result = 1 for _, v in clothes.items(): result *= (v + 1) # 이 분류를 코디에 포함하지 않는 경우의 수 print(result - 1) 고등학교 때 배운 확률 과목이 계속 생각났다ㅋㅋㅋ 옷을 분류하고, ..