programming language 103

[프로그래머스 Level3 힙] 디스크 컨트롤러

문제 문제풀이 #include #include #include #include using namespace std; struct cmp { bool operator()(vector a, vector b) { return a.at(1) > b.at(1); } }; int solution(vector jobs) { int answer = 0; priority_queue pq; //우선순위 큐 min heap int size = jobs.size(); int time=0; //이차원배열 정렬하기 sort(jobs.begin(), jobs.end()); int idx = 0;int j=0; while(idx < size || !pq.empty()){ //현재 시간보다 이하에 들어온 작업들을 모두 넣는다. if(..

[프로그래머스 Level3 그래프] 가장 먼 노드

문제 입출력 문제풀이 #include #include #include #include #include using namespace std; int solution(int n, vector edge) { int answer = 0; //n개의 노드가 있는 그래프가 있습니다. //1번 노드에서 가장 멀리 떨어진 노드의 개수 구하기 //최단경로로 이동했을 때 간선의 개수가 가장 많은 노드들 //이차원 배열이 주어지고 1번 노드에서 출발. 노드의 개수 주어짐. //[[3, 6], [4, 3], [3, 2], [1, 3], [1, 2], [2, 4], [5, 2]] //1번에서 2번으로 가는 최단 경로는? //coninfo : 각 연결되어 있는 시작 노드와 끝 노드들을 넣어 연결 정보 저장 // vector co..

[프로그래머스 Level2 힙] 더 맵게

문제 입출력 문제풀이 #include #include #include using namespace std; int solution(vector scoville, int K) { int answer = 0; int i = 0; while(1){ sort(scoville.begin(), scoville.end());//정렬 if(scoville[i] >= K){ break; } else { answer++; int new_input = scoville[i] + (scoville[i+1] * 2); scoville.erase(scoville.begin(),scoville.begin()+2);// //scoville.erase(scoville.begin()+1); scoville.push_back(new_inp..

트리, 힙, 그래프

1. 비선형 문제 계층적 문제 순환 종속성 2. 트리 : 상하 반전된 형태 트리 순회 : 루트 노드를 기준으로 지어진 용어 A B C 1. preorder A B C 2. inorder B A C 3. postorder B C A 3. 다양한 트리 구조 이진 검색 트리 ( Binary search tree) (1) 원소 검색 (2) 새 원소 삽입하기 (3) 원소 삭제하기 (4) 시간복잡도 검색의 경우 매번 검색 범위가 절반으로 줄어든다. T(N) = T(N/2) + 1 => T(N) = O(log N) 균형 트리 N 항 트리 4. 힙 완전 이진 트리 : 마지막 레벨의 노드를 제외하고는 모두 두 개의 자식 노드가 존재 최대 힙 (max heap) : 부모 노드가 두 자식노드보다 항상 커야 한다는 불변성 유..

[프로그래머스 Level2 스택] 다리를 지나는 트럭

문제 입출력 문제 풀이 #include #include #include using namespace std; int solution(int bridge_length, int weight, vector truck_weights) { int answer = 0; queue q; int sum = 0;//weight int idx = 0; while(1){ if(idx == truck_weights.size()){ answer += bridge_length; break;//? } answer++;//반복문이 한번 돌면 초가 지난다고 한다. int tmp = truck_weights[idx]; if(q.size()==bridge_length) {//넣기 전에 q의 크기 따지고 꽉 차있으면 빼기 sum -= q...

[프로그래머스 Level2 스택] 기능개발

문제 입출력 문제풀이 #include #include using namespace std; vector solution(vector progresses, vector speeds) { vector answer; vector days; int day = 0; //진도가 100일때 서비스 반영이 가능하다. //배포되어야 하는 순서대로 작업진도 배열과 작업개발속도가 적인 배열 주어짐. //배포는 하루에 한번만 할수 있고, 하루의 끝에. 95%인데 4라면 배포는 2일 뒤에 이뤄진다. for(int i =0;i= 100){ days.push_back(day);day = 0; } } int first = 1; //[7,3,9] //[5,10,1,1,20,1] //7일차때 이미 모든 일들은 계쏙 같이 진행 중이였던 ..