문제풀이 324

[프로그래머스][해시] - 전화번호 목록(Python)

def solution(phone_book): a = phone_book[0] for i in phone_book: if a in i: return False else: return True return answer # 전부 False로 때려박아서 실패 def solution(phone_book): phone_book = sorted(phone_book) for p1, p2 in zip(phone_book,phone_book[1:]): if p2.startswith(p1): #p2가 p1으로 시작되면? return False return True # startswith 함수를 사용해서 접두사를 찾아낸다.

[프로그래머스][해시] - 완주하지 못한 선수(Python)

def solution(participant, completion): answer = [] for i in participant: if completion not in participant: answer.append(i) return answer # 이렇게 진행하면 리스트형으로 출력을 해서 안 된다. def solution(participant, completion): answer = [] for i in participant: if completion not in participant: answer += i return answer # 이렇게 진행하면 중복값에 문제가 생기게 된다. 2명의 참가자 중 1명이 통과를 해도 1명이 통과해서 2명 전부 통과한 걸로 나오기 때문에..

[백준][분할 정복] 1629. 곱셈(파이썬/Python)

import sys input = sys.stdin.readline a,b,c = map(int,input().split()) print((a**b)%c) # 시간초과 당연하다 ^-^. 범위가 엄청 넓기 때문 이를 쪼개서 사용해야 한다. import sys input = sys.stdin.readline a,b,c = map(int,input().split()) def dc(a,b): # 지수가 1일 경우엔 n^1은 n이기 때문에 그대로 값 반환 if b == 1: return a % c else: temp = dc(a,b//2) # 지수가 홀수라면 n^2 * n^2 * n을 해주어야 하기에 이렇게 진행 if b % 2 == 1: return temp * temp * a % c else: return t..

[백준][분할 정복] 1992. 쿼드트리 (파이썬/Python)

n = int(input()) m = [list(map(int, input())) for _ in range(n)] def quadtree(x, y, n): if n == 1: return str(m[x][y]) result = [] for i in range(x, x + n): for j in range(y, y + n): # 색이 다르면, 다시 분할진행 if m[i][j] != m[x][y]: result.append('(') result.extend(quadtree(x, y, n//2)) result.extend(quadtree(x, y + n//2, n//2)) result.extend(quadtree(x + n//2, y, n//2)) result.extend(quadtree(x + n//2, ..

[백준][그리디 알고리즘] 1744. 수 묶기 (파이썬/Python)

import sys input = sys.stdin.readline n = int(input()) a = [] result = [] for _ in range(n): a.append(int(input())) a.sort() for i in range(1,len(a),2): if a[i-1] 0: a.append(c) else: b.append(c) a.sort()#pop은 뒤에서부터 꺼내기 때문에 오름차순으로 정렬해줘야 함. b.sort() while len(a) != 0 : if len(a) == 1: result += a.pop() else: result += a.pop()*a.pop() while len(b) != 0: result += b.pop() print(result) # for문을 돌면 b..