본문 바로가기
728x90
반응형

알고리즘 문제풀이25

프로그래머스 코딩테스트 고득점 kit * 계속 업데이트 중 해시 폰켓몬 from collections import Counter def solution(nums): count_nums = Counter(nums) return len(nums) // 2 if len(count_nums) >= len(nums) // 2 else len(count_nums) 완주하지 못한 선수 from collections import Counter def solution(participant, completion): partici = Counter(participant) comple = Counter(completion) partici_keys = partici.keys() for i in partici_keys: if partici[i] != comple[i.. 2023. 7. 14.
Codility Exercise4 First Unique 코드 from collections import Counter def solution(A): my_cnt = Counter(A) for i in my_cnt.keys(): if my_cnt[i] == 1: return i return -1 StrSymmetryPoint 코드 def solution(S): if len(S) == 1: return 0 if len(S) % 2 == 0: return -1 for i in range(len(S) // 2): if S[i] != S[-(i+1)]: return -1 return len(S) // 2 return cnt TreeHeight 코드 from extratypes import Tree # library with types used .. 2023. 6. 22.
나동빈 유튜브 예제 문제 *업데이트 중 그리디 알고리즘 1이 될 때까지 import sys a, b = map(int, sys.stdin.readline().split()) cnt = 0 while True: if a % b != 0: a = a - 1 cnt += 1 else: a = a // b cnt += 1 if a == 1: break print(cnt) 곱하기 혹은 더하기 import sys word = sys.stdin.readline() answer = int(word[0]) for i in range(1, len(word)-1): if int(word[i-1]) a or nx < 1 or ny < 1: continue x, y = nx, ny print(x, y) x, y축이 반대임. 2023. 6. 2.
백준 2559(시간 초과 나기 쉬운 문제) 처음 시도한 코드(시간 초과) a, b = map(int, input().split()) temp = list(map(int, input().split())) number = [] num = 0 for i in range(len(temp)): for j in range(i, i + b): if i + b > a: break num += temp[j] number.append(num) num = 0 number.pop(a-1) print(max(number)) 두 번째 시도한 코드(시간 초과) import sys a, b = map(int, sys.stdin.readline().split()) temp = list(map(int, sys.stdin.readline().rstrip().split())) ma.. 2023. 6. 2.
백준 12865 코드 n, m = map(int, input().split()) product = [list(map(int, input().split())) for _ in range(n)] # 가치 계산하기 values = [0] * n # 무게 계산하기 weights = [0] * n # 무게, 가치 값 초기화 for i in range(n): weights[i] = product[i][0] values[i] = product[i][1] # dp def dp(n, m, weights, values): dp = [[0] * (m+1) for _ in range(n+1)] for i in range(1, n+1): weight = weights[i-1] value = values[i-1] for j in range(1,.. 2023. 5. 24.
백준 11048 코드 n, m = map(int, input().split()) candies = [list(map(int, input().split())) for i in range(n)] # 1, 1부터 시작이므로 1 더 큰 2차원 배열 생성 result = [[0] * (m+1) for i in range(n+1)] for x in range(1, n+1): for y in range(1, m+1): result[x][y] = max(result[x-1][y], result[x][y-1], result[x-1][y-1]) + candies[x-1][y-1] print(result[n][m]) 2023. 5. 24.
728x90
반응형