python3 27

[프로그래머스] LEVEL2 짝지어 제거하기, python3, 2017 팁스타운

def solution(s): if len(s) % 2: #홀수 경우는 불가능 return 0 list_s = list(s) stack = [] for i in range(len(s)): tmp = list_s.pop() if stack == []: stack.append(tmp) elif stack[-1] == tmp: stack.pop() elif stack[-1] != tmp: stack.append(tmp) if stack != []: return 0 return 1 위 코드는 타인의 코드를 보고 생각해보며 내 방식대로 적은 코드이다. 스택의 개념에 대해서는 알지만 어떻게 사용해야할지 잘 몰랐던 것 같다. 스택이 비었을때 추가하고, 스택에 값이 있을때 비교하는것... 앞으로는 제발 잊지 않았으면 ..

STUDY/Algorithm 2021.01.28

[프로그래머스] LEVEL2 타겟 넘버, python3, 깊이/너비 우선 탐색(DFS/BFS)

def solution(numbers, target): answer_list=[0] for i in numbers: temporary_list=[] #print("\nanswer_list:",answer_list) for j in answer_list: temporary_list.append(j+i) temporary_list.append(j-i) #print("tmp_list",temporary_list) answer_list=temporary_list #print("answer_list",answer_list) answer = answer_list.count(target) return answer 방법은 똑같았으나 구현하지 못해 내 코드는 아니지만 올린다.

STUDY/Algorithm 2019.11.20

[프로그래머스] LEVEL2 조이스틱,python3, 탐욕법(greedy)

1차시도 def solution(name): answer = 0 init="A"*len(name) for i in range(len(name)):# ascii: A=65 Z=90 if ord(name[i]) == 65: pass else: tmp=ord(name[i]) - 65 #print(tmp) if tmp>13: answer+=26-tmp else: answer+=tmp return answer 2차시도 def solution(name): answer = 0 len_name=len(name) init="A"*len_name chk=[] for i in range(len_name):# ascii: A=65 Z=90 if ord(name[i]) == 65: chk.append(1) pass else: ..

STUDY/Algorithm 2019.11.04

[프로그래머스] LEVEL2 다리를 지나는 트럭,python3, 스택/큐

def solution(bridge_length, weight, truck_weights): answer = 0 ing=[] # 다리를 건너는 트럭 cnt=[] # 트럭당 지난 거리 ed=[] # 다리를 지난 트력 while 1: if (truck_weights == []) and (ing == []): break answer+=1 # count total time for i in range(len(cnt)): # decrease the times of trucks above the bridge cnt[i]-=1 if (cnt!=[])and(cnt[0]==0): ed.append(ing[0]) del cnt[0] del ing[0] if (sum(ing) max_weight: bridge.append(0..

STUDY/Algorithm 2019.10.22