본문 바로가기

전체 글

(46)
[프로그래머스] [Python] 더 맵게 문제 풀이 def solution(scoville, K): answer = 0 # 리스트를 heapq 형식으로 변경 heapq.heapify(scoville) # heapq 형식이기 때문에 가장 작은 값이 [0]위치에 온다. 이 값이 K보다 작으면 while scoville[0] < K: try: # 스코빌 지수 공식 대입 heapq.heappush(scoville, heapq.heappop(scoville) + heapq.heappop(scoville) * 2) answer += 1 # 인덱스 에러가 났을 경우. 즉 끝까지 돌았음에도 여전히 scoville[0] < 인 경우에는 -1을 리턴한다 except IndexError: return -1 return answer 문제 링크 programmers...
[프로그래머스] [C++] 더 맵게 문제 풀이 #include #include #include using namespace std; int solution(vector scoville, int K) { int answer = 0; // 우선순위 큐를 사용. 오름차순으로 사용하기 위해 greater 추가 priority_queue pq; // 우선순위 큐에 scovile 값을 넣는다. auto는 파이썬의 for i in array와 같은 표현이라고 생각하면 된다. for (auto it : scoville) pq.push(it); while (1) { // 첫 번째 원소를 가져온 후 pop한다. int first = pq.top(); pq.pop(); // pq를 다 돌았음에도 스코빌 지수가 충족되지 않는다면 -1을 리턴한다. if (pq...
[프로그래머스] [Python] 프린터 문제 풀이 def solution(priorities, location): answer = 0 # enumerate를 사용하여 고유번호를 부여. 이후 찾고자 하는 location과 대응된다. priorities = [i for i in enumerate(priorities)] while True: # 맨 앞을 가져온다. 맨 앞이 가장 큰 값일 때는 그냥 빠지기만 한다. current_top = priorities.pop(0) # 현재 맨 앞의 값이 다른 값보다 작다면 if any(current_top[1] < p[1] for p in priorities): # 다시 배열에 넣어주도록 하자 priorities.append(current_top) # 해당 값이 가장 큰 값이라면 else: # 맨 앞은 whi..
[프로그래머스] [C++] 프린터 문제 풀이 #include #include #include using namespace std; int solution(vector priorities, int location) { int answer = 0; // 고유번호와 우선순위 값을 받기 위해 pair로 설정 queue q; // 우선순위 큐. 내림차순으로 자동 정렬된다. priority_queue pq; // q를 만들어 고유번호와 우선순위 값을 넣고 우선순위 큐인 pq에 넣는다. for (int i = 0; i < priorities.size(); i++) { q.push(make_pair(i, priorities[i])); pq.push(priorities[i]); } // q가 빌 때까지 while (!q.empty()) { // q의 맨..
[프로그래머스] [Python] 다리를 지나는 트럭 문제 풀이 def solution(bridge_length, weight, truck_weights): # answer += bridge_length를 할 것이기 때문에 그냥 처음부터 다리 길이를 answer에 넣었다. answer = bridge_length # 현재 다리 위에 있는 트럭의 무게 current_weight = 0 # 현재 다리 위에 있는 트럭의 배열 truck_on_bridge = list() # 각 트럭을 반복문으로 돌면서 for i in range(len(truck_weights)): truck = truck_weights[i] while True: # 다리에 트럭이 한 대도 없으면 if not truck_on_bridge: # 트럭이 다리위에 올라가고 해당 트럭의 무게를 추가. 시..
[프로그래머스] [C++] 다리를 지나는 트럭 문제 풀이 #include #include #include using namespace std; int solution(int bridge_length, int weight, vector truck_weights) { int answer = 0; // 다리를 지나고 있는 트럭의 배열 queue across_truck; // 다리 위에 있는 트럭의 무게 int max_size = 0; // 트럭의 무게 int size = 0; // 각 트럭을 반복문으로 돌면서 for(int i = 0; i < truck_weights.size(); i++){ size = truck_weights[i]; while(1){ // 다리에 트럭이 한 대도 없으면 if(across_truck.empty()){ // 트럭이 다리 위..
[프로그래머스] [Python] 기능 개발 문제 풀이 def solution(progresses, speeds): answer = [] # 진도율을 day로 표현 day = 1; # progresses가 빌 때까지(progresses에 값이 있다면) while progresses: # 완료된 작업의 개수 complete = 0 for i in range(len(progresses)): # 하단 설명 참조 if (progresses[0] + (speeds[0] * day)) >= 100: complete += 1 progresses.pop(0) speeds.pop(0) # 완료된 작업이 있다면 if complete != 0: answer.append(complete) # 진도율 증가 day += 1 return answer 우선 progresses..
[프로그래머스] [C++] 기능개발 문제 풀이 #include #include #include using namespace std; vector solution(vector progresses, vector speeds) { vector answer; queue pr, sp; // queue를 만들어 값을 넣는다. for (int i = 0; i < progresses.size(); i++) { pr.push(progresses[i]); sp.push(speeds[i]); } // 진도의 증가를 day로 표현한다. 곱셈을 사용하기 때문에 초기값은 1. int day = 1; // 큐가 빌 때까지 while (!pr.empty()) { //완료된 작업의 개수 int finished = 0; for (int j = 0; j < pr.size(..