[프로그래머스 Level2 스택] 주식가격 문제 입출력 문제풀이 #include #include #include #include using namespace std; vector solution(vector prices) { vector answer; queue time; int idx = 0; int count = 0; int size = prices.size()//이렇게 상수로 표현하고 for문 돌리면 시간 단축 for(int i=0;i programming language/Structure 2021.07.29
[프로그래머스 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... programming language/Structure 2021.07.29
std::stack, std::queue, std::priority_queue std::stack (LIFO) stack.empty() stack.size() stack.top() stack.push() stack.pop() stack.emplace() std::queue (FIFO) queue.push() queue.pop() std::priority_queue q.push() q.pop() 제일 앞에 있는 것이 최대값의 원소이다. 내림차순의 queue. priority_queue pq; priority_queue pq; 내림차순 디폴트 오름차순 , greater : #include programming language/Structure 2021.07.29
[프로그래머스 Level2 스택] 프린터 문제 입출력 문제풀이 #include #include #include using namespace std; //큐에 인쇄 대기목록을 모두 넣어주기 int solution(vector priorities, int location) { int answer = 0; // queue list; // for(int i =0;i programming language/Structure 2021.07.28
[프로그래머스 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일차때 이미 모든 일들은 계쏙 같이 진행 중이였던 .. programming language/Structure 2021.07.28
std::vector, std::deque std::vector std::array는 크기가 고정되어 있는 반면(항상 stack 사용), vector는 가변 크기 배열이다. std::vector vec; // 크기가 0인 벡터 선언 std::vector 함수 push_back(val) 맨 마지막에 원소 추가 insert(val) 삽입할 위치를 나타내는 반복자를 첫번째 인자로 받음으로써 원하는 위치에 원소 추가 pop_back(val) 맨 마지막 원소 제거 erase() 두가지 형태 vec.erase(vec.begin) //= vec.erase(vec.begin() + 1, vec.begin() + 4) : 두번째, 세번째만 삭제하는 것 clear() 벡터 초기화 sort(a.begin(), a.end(), 임의함수 bool형태) std::dequ.. programming language/Structure 2021.07.28
std::array std::array 원소의 타입과 배열 크기를 매개변수로 사용하는 클래스 템플릿 메모리를 자동으로 할당하고 해제한다. (클래스 템플릿 : 클래스의 일반화 선언, 배열출력하는 함수 만들때 다양한 배열을 출력하기위해 사용) std::array arr1; template class 클래스명{ 클래스 멤버; }; template void print(const std::array& arr>; auto a; //auto 는 타입추론형식. //처음 선언된 자료형을 추론한다. for (auto elements : arr) //배열 arr 원소들을 auto로 함 std::array.size() 배열의 크기 std::array.at(index) 원하는 배열 index 값 반환 std::array.begin() 첫번째 원소.. programming language/Structure 2021.07.26
연속된 자료구조 vs 연결된 자료구조 연속된 자료구조 연결된 자료구조 모든 데이터가 메모리에 연속적으로 저장된다. 데이터는 노드에 저장되고, 노드는 메모리 곳곳에 흩어져 있다. 임의 원소에 즉각적으로 접근할 수 있다. (시작주소 + 2 * sizeof(type) 식으로 수식을 통해 접근한다) 임의 원소에 접근하는 것은 산형 시간 복잡도를 가지며 느린 편입니다. 데이터가 연속적으로 저장되어 있고, 캐시 지역성 효과로 인해 모든 데이터를 순회하는 것이 매우 빠릅니다. 캐시 지역성 효과가 없으므로 모든 데이터를 순회하는 것이 느린 편입니다. 데이터 저장을 위해 정확하게 데이터 크기만큼의 메모리를 사용합니다. 각 노드에서 포인터 저장을 위해 여분의 메모리를 사용한다. 배열의 유형 1. 정적 배열 (stack) : 선언된 블록이 끝나면 소멸 int .. programming language/Structure 2021.07.26