전체 글 790

[백준] - 1463. 1로 만들기(python)

[top down 방식과 memoization] import sys sys.setrecursionlimit(10**6) def dp(n): #base case if n == 2 or n == 3: memo[n] = 1 return memo[n] elif n == 1: memo[n] = 0 return memo[n] #memoization if n not in memo: if n % 6 == 0: #점화식 memo[n] = 1+min(dp(n//3),dp(n//2)) elif n%3 == 0: #점화식 memo[n] = 1+min(dp(n//3), dp(n-1)) elif n%2 == 0: #점화식 memo[n] = 1+min(dp(n//2), dp(n-1)) else: #점화식 memo[n] = dp(n..

[백준] - 2839. 설탕 배달(python)

[dp - bottom up + dp table 문제풀이] n = int(input()) dp = [10000] * (n+5) dp[3] = dp[5] = 1 for now in range(6, n+1): dp[now] = 1 + min(dp[now-3], dp[now-5]) print(dp[n] if dp[n] < 10000 else -1) 10000 10000 1 10000 1 2 10001 2 3 2 1 2 3 / 3 4 5 6 7 8 9 10 6의 경우 6부터 살펴보는 이유는 간단하다 3과 5 이후의 숫자만 설탕을 나눌수 있기 때문이다. 3(6-3)의 경우엔 1개의 설탕이 저장되어 1을 더해주면 최종적으로 6kg의 설탕을 3kg 2개로 나눌 수 있게 된다. 1(6-5)의 경우엔 10000이라는 수..

[자료구조] - Linked List

Linked List(Head만 존재할 때) 자바로 구현한 linked list public class LinkedList { Node head; public LinkedList() { this.head = null; } public void append(int value){ Node newNode = new Node(value); if (head == null){ this.head = newNode; } else { Node currentNode = this.head; while (currentNode.next != null){ currentNode = currentNode.next; } currentNode.next= newNode; } } public int get(int index){ Node c..

[프로그래머스] - 카드 뭉치(파이썬)

def solution(c1, c2, goal): answer = "Yes" p1 = p2 = 0 for i in range(len(goal)): if goal[i] in c1 and goal[i] == c1[p1]: p1 += 1 elif goal[i] in c2 and goal[i] == c2[p2]: p2 += 1 else: answer = "No" return answer 각각의 포인터를 주고 카드1에 없으면 카드2를 살펴보도록 한다. 이때 포인터에 다음 값에 있더라도 이는 먼저 사용할 수 없기 때문에 자동으로 else문에서 no로 판명나게 된다.

[프로그래머스] - 연속된 부분 수열의 합

def solution(sequence, k): answer = [] start, end = 0,0 num = sequence[0] min_len = 2147483647 while start end - start + 1: min_len = end - start + 1 answer = [start, end] num -= sequence[start] start += 1 elif num > k: num -= sequence[start] start += 1 elif num < k: end+=1 if end < len(sequence): num += sequence[end] return answer 이 문제의 핵심은 이미 더해줬던 부분을 계속해서 더하는 것이 아니고 양 끝값을 더하고 빼는 식으로 진행해서 시간 초..

[프로그래머스] - 달리기 경주 (파이썬/자바)

def solution(players, callings): answer = [] index_player = {} player_index ={} for idx, val in enumerate(players, 1): index_player[idx]=val for idx, val in enumerate(players, 1): player_index[val]=idx for c in callings: current_player = c current_idx = player_index[c] before_idx = current_idx-1 before_player = index_player[before_idx] player_index[current_player], player_index[before_player] = ..

[프로그래머스] - 성격 유형 검사하기

python def solution(survey, choices): answer = '' dic= {"R" : 0,"T" : 0,"C" : 0,"F" : 0,"J" : 0,"M" : 0,"A" : 0,"N" : 0 } #4의 경우엔 점수 부여하지 않으니까 4를 기준으로 나눠주면 된다. for s,c in zip(survey, choices): if c > 4: dic[s[1]] += c-4 elif c < 4: dic[s[0]] += 4-c li = list(dic.items()) for i in range(0,8,2): if li[i][1] < li[i+1][1]: answer += li[i+1][0] else: answer += li[i][0] return answer 해당 딕셔너리를 list로 다시..