프로그래머스 69

[프로그래머스] 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(people, limit): answer = 0 people.sort() #print(people) while people!=[]: answer+=1 tmp1=people.pop() tmp2=limit-tmp1 #print(tmp1,tmp2,people) if people==[]: break elif tmp1==limit: continue elif tmp2tmp3: continue return answer 2차시도 def solution(people, limit): answer = 0 people.sort() while people!=[]: answer+=1 tmp=people.pop() remainder=limit-tmp if people==[]: break elif ..

STUDY/Algorithm 2019.11.14

[프로그래머스] LEVEL2 H-Index, python3, 정렬

문제가 이해가 안되었다. 질문하기에도 나랑 비슷한 사람들이 많아서 도움을 얻었는데 해당 테스트 케이스를 보니 이해가 갔다. test case: [22, 42] return value:2 알고리즘 공부할때 문제를 읽고 이해하는 능력도 많이 중요한것 같다. 1차 시도 def solution(citations): answer = 0 max_cit=max(citations) for n in range(max_cit): tmp=list(i for i in citations if i>=n) length=len(tmp) if n>length: answer=n-1 break return answer

STUDY/Algorithm 2019.11.14

[프로그래머스] LEVEL2 큰 수 만들기,python3, 탐욕법(greedy)

1차시도(여러번 했지만 ㅋㅋ) def solution(number, k): answer = '' front='' rear='' cnt=20 while k>0: max_num=max(number) max_ind=number.index(max_num) if max_ind==0: front+=number[0] number=number[1:] if k>=max_ind: number=number[max_ind:] k-=max_ind else: rear=number[max_ind:]+rear number=number[:max_ind] #print("front:{}\tnumber:{}\trear:{}\tk:{}".format(front,number,rear,k)) if number=='': if k==1: front..

STUDY/Algorithm 2019.11.11

[프로그래머스] LEVEL2 가장큰수, python3, 정렬

1차 시도 from itertools import permutations def solution(numbers): answer = '' ## permutation of elements of numbers numbers=list(map(str,numbers)) tmp=list(map(''.join, permutations(numbers))) answer=max(tmp) return answer 논리는 맞는거 같은데 permutation할때 시간이 오래 걸리는듯 하다. 2차시도 def solution(numbers): answer = '' tmp_nums=[] max_len=len(str(max(numbers))) for num in numbers: length=0 len_num=len(str(num)) t..

STUDY/Algorithm 2019.11.06

[프로그래머스] LEVEL2 더맵게, python3, 힙,Heap

1차 시도 def solution(scoville, K): answer = 0 while True: scoville.sort() if scoville[0]==0 and scoville[1]==0: return -1 min1=scoville.pop(0) min2=scoville.pop(1) mixed=min1+min2*2 scoville.insert(0,mixed) answer+=1 if min(scoville)>=K: break return answer Heap에 관한 설명 https://gmlwjd9405.github.io/2018/05/10/data-structure-heap.html [자료구조] 힙(heap)이란 - Heee's Development Blog Step by step goes a lo..

STUDY/Algorithm 2019.11.06

[프로그래머스] 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