[프로그래머스 lv 2] 구명보트 (탐욕법(Greedy))
·
✨ 공부 기록/알고리즘
아이디어 : 작은 순으로 정렬한 후에 2명씩 태워보면 되지 않을까 싶다. # 첫 번째 시도def solution(people, limit): answer = 0 people.sort() people.append(0) flag = 0 for i in range(0,len(people)-1,2): if people[i]+people[i+1]  그런데 고려하지 못했던 부분은, 작은 순대로 차례로 넣는게 아니라 큰수와 작은 수를 잘 조합하면 limit을 넘기지 않을 수 있다는 것이다. 예를 들면 [10, 20, 30, 40, 50, 60, 70, 80], limit=90 -> return 4와 같은 테스트케이스를 생각해볼 수 있다. # 두 번째 시도 구명보트의 무게..
[프로그래머스 lv 1] 체육복 (탐욕법(Greedy))
·
✨ 공부 기록/알고리즘
아이디어 : 바로 앞뒤만 빌려줄 수 있음.reserve와 lost에 겹치는 사람있는지를 먼저 체크.일단 한쪽 방향으로만 체크하는걸 생각했다. # 첫 번째 시도def solution(n, lost, reserve): for i in range(len(lost)-1,-1,-1): if lost[i] in reserve: reserve.remove(lost[i]) del lost[i] for j in range(len(reserve)): if reserve[j]-1 in lost: lost.remove(reserve[j]-1) elif reserve[j]+1 in lost: lost...
[프로그래머스 lv 2] 주식가격 (스택/큐)
·
✨ 공부 기록/알고리즘
아이디어 : 특정 가격을 기준으로 각각 수익권인지 아닌지를 묻는거라고 이해하면 쉬울 것 같다. 가격이 떨어지지 않은 기간은 그 다음 가격이 바로 떨어지더라도 1초는 기본으로 준다.(마지막 가격의 경우 무조건 0이 되는 것 같다) # 첫 번째 시도def solution(prices): times = [0] * len(prices) idx_list = [] for i in range(len(prices)): times[i]+=1 idx_list.append(i) for j in idx_list: try: if prices[j] 테스트 케이스만 통과할뿐 나머지는 줄줄이 실패라서 좀 더 잘 생각해봐야 할 것 같다..
[프로그래머스 lv 2] 프로세스 (스택/큐)
·
✨ 공부 기록/알고리즘
아이디어 : deque를 사용하면 편할 것 같다.주의할 점 : 프로세스가 '실행'된 값이 answer이다.(큐를 옮긴 횟수가 아님) # 첫 번째 시도from collections import dequedef solution(priorities, location): answer = 0 queue = deque(priorities) while True: if location==0: if queue[location]==max(priorities): answer+=1 return answer else: target = queue.popleft() ..
[프로그래머스 lv 2] 올바른 괄호 (스택/큐)
·
✨ 공부 기록/알고리즘
아이디어 : 어...? 이 문제는 학교 전공 시험 문제에 나왔던 것과 매우 유사하다. 큐 돌리면서 (이냐 )이냐에 따라 if문으로 큐에 넣고 빼면 될 것 같다. # 첫 번째 시도from collections import dequedef solution(s): answer = True queue = deque() for i in range(len(s)): if not queue: queue.append(s[i]) elif queue[0]!=s[i]: queue.popleft() queue.append(s[i]) if queue[0]=='(': answer=False return..
[프로그래머스 lv 2] 기능개발 (스택/큐)
·
✨ 공부 기록/알고리즘
아이디어 : 동시에 일단 개발을 시작한 후에 배포 순서대로 차례로 고려하면서 일수를 계산하는 것이므로, 각각 개발에 걸리는 시간을 먼저 파악해야 할 것 같다. import mathdef solution(progresses, speeds): dates = [] answer = [] for i in range(len(progresses)): date = (100-progresses[i])/speeds[i] dates.append(math.ceil(date)) completed = [False]*len(dates) flag = 0 count = 0 for i in range(len(dates)): if flag ==0: ..
[프로그래머스 lv 3] 단어 변환 (깊이/너비 우선 탐색(DFS/BFS))
·
✨ 공부 기록/알고리즘
아이디어 : 관건은 어떻게 한글자씩만 다른 단어를 찾을 것인가 + 최소의 단계만 거치도록 할 것인가. 인것 같다. BFS 유형인 것으로 추측해본다. 1) 한글자만 다른 단어의 pool을 찾고,2) pool안의 각 단어에 대해서 동일한 작업을 반복하면...? # 첫 번째 시도from collections import dequedef is_substring_in_word(substring, word): i = 0 # substring 인덱스 j = 0 # word 인덱스 while i  오랫동안 고민을 해보았지만 여기서 answer 카운트를 어떻게 해야 할지 모르겠다. 분명... 한글자씩 바꿔서 타겟 단어까지 만드는 과정은 찾은 거 같은데, 타겟 단어로 가는 길이 아닌 경우의 단어들도 함..
[프로그래머스 lv 2] 게임 맵 최단거리(깊이/너비 우선 탐색(DFS/BFS))
·
✨ 공부 기록/알고리즘
아이디어 : 이코테 책에서 본 유형과 비슷하다. BFS방식으로 지나간 칸에 숫자를 1씩 더하면서 최종 거리를 구하면 될 것 같다. # 첫 번째 시도from collections import dequedef solution(maps): queue = deque([(0,0)]) n = len(maps) m = len(maps[0]) # 동서남북 dx = [1,-1,0,0] dy = [0,0,-1,1] while queue: x,y = queue.popleft() for i in range(4): nx = x+dx[i] ny = y+dy[i] if nx >= 0 and ny >= 0 and..
[프로그래머스 lv 3] 네트워크(깊이/너비 우선 탐색(DFS/BFS))
·
✨ 공부 기록/알고리즘
아이디어 : DFS로 풀면 될 것 같다.def dfs(visited, computers, i): if visited[i] == True: return 0 else: visited[i] = True for node in range(len(computers[i])): if computers[i][node]==1: dfs(visited, computers, node) return 1 def solution(n, computers): answer = 0 visited = [False] * n for i in range(len(computers)): answer+= dfs(..