본문 바로가기

Algorithm

(28)
백준 최솟값 찾기(11003) C++ 풀이 알고리즘 분류 자료 구조 우선순위 큐 덱 문제 해결 아이디어 항상 deque의 front의 최솟값이 있도록 하고, 이렇게 하기 위해서 deque의 back보다 더 작은 수가 들어오면 pop_back()을 반복하여 back()이 현재 들어오는 숫자보다 큰 경우가 없도록 합니다. 또, 구한 i - L + 1이 현재 front()의 idx(deque의 들어온 숫자)랑 같다면 pop_front()를 하여 최솟값을 갱신시킵니다. 코드 #include #include using namespace std; struct num { public: int val; int idx; }; deque d; int n, l; int main() { ios_base::sync_with_stdio(false); cin.tie(NUL..
백준 AC(5430) C++ 풀이 알고리즘 분류 구현 자료구조 문자열 파싱 덱 문제 해결 아이디어 배열을 뒤집는 경우, 시간을 오래 잡아 먹을 수 있기 때문에 deque를 통해 뒤집으면 front를 탐색하는 것을 back으로 back으로 탐색을 하는 것을 front로 탐색을 하도록 바꾸면 된다. 코드 #include #include using namespace std; int t; int main() { cin >> t; while (t--) { string fuc; int n; deque d; bool isError = false; // false: front true: back bool findDir = false; cin >> fuc; cin >> n; string nums; cin >> nums; string num = ""; f..
백준 회전하는 큐(1021) C++ 풀이 알고리즘 영역 자료 구조 덱 문제 해결 아이디어 앞으로 찾기와 뒤부터 찾기를 비교하여 deque를 이용하면 pop_front() 또는 pop_back()을 하여 진행하면 된다. 코드 #include #include #include using namespace std; deque d; int n, m; int arr[51]; int result = 0; int main() { cin >> n >> m; for (int i = 0; i > arr[i]; } for (int i = 1; i
백준 덱(10866) C++ 풀이 알고리즘 영역 자료 구조 덱 문제 해결 아이디어 양방향으로 빠르게 확장시킬 수 있는 자료구조라는 점을 알고 구현을 하면 된다. 코드 #include using namespace std; class deque { private: int arr[10001]; int backIdx = 0; public: void push_front(int x) { for (int i = backIdx; i > 0; i--) { arr[i] = arr[i - 1]; } arr[0] = x; backIdx++; } void push_back(int x) { arr[backIdx] = x; backIdx++; } int pop_front() { if (empty()) return -1; int temp = arr[0]; for (..
백준 카드2(2164) C++ 풀이 알고리즘 분류 자료구조 큐 문제 해결 아이디어 큐를 이용하여 큐의 사이즈가 1이 될 때 까지 pop() 한 후 top()을 임시 저장한 후 pop()하여 임시 저장한 값을 다시 큐에넣는 과정을 반복하면 되는 문제이다. 코드 #include #include using namespace std; int n; queue q; int main() { cin >> n; for (int i = 1; i 1) { q.pop(); int top = q.front(); q.pop(); q.push(top); } cout
백준 큐 2(18258) C++ 풀이 알고리즘 분류 자료 구조 큐 문제 해결 아이디어 vector를 이용하여 큐를 구현하면서 빠른 시간 초과를 요구하기에, 시간을 잡아먹는 empty()부분을 직접 구현해야 한다. 코드 #include #include using namespace std; #define EMPTY 111111 int n; vector queue; int check = 0; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n; string s; int num; while (n--) { cin >> s; if (s == "push") { cin >> num; queue.push_back(num); } else if (s == "..
백준 큐(10845) C++ 풀이 알고리즘 분류 자료 구조 큐 문제 해결 아이디어 vector를 이용하여 queue의 구조인 FIFO(첫번째로 들어온 것이 가장 먼저 나간다)를 구현하였다, 코드 #include #include using namespace std; vector v; int main() { int n; cin >> n; string s; int num; for (int i = 0; i > s; if (s == "push") { cin >> num; v.push_back(num); } else { if (s == "pop") { if (v.size() == 0) cout
백준 히스토그램에서 가장 큰 직사각형(6549) C++ 풀이 알고리즘 분류 자료 구조 세그먼트 트리 분할 정복 스택 문제 해결 아이디어 스택에 Index를 넣어 arr[top())보다 체크하고 있는 값이 더 작다면 직사각형 범위를 계산해야 하기 때문에 인덱스를 이용하여 계산하여 max값인지 비교하면 된다. 코드 #include #include using namespace std; int main() { while(true) { int n; int arr[100001]; stack st; long long result = 0; cin >> n; if(n == 0) return 0; for(int i = 0; i > arr[i]; } for(int i = 0; i < n; i++) { while(!st.empty()) { if(arr[i..