728x90
๋ฐ์ํ
https://school.programmers.co.kr/learn/courses/30/lessons/76502
ํ๋ก๊ทธ๋๋จธ์ค
SW๊ฐ๋ฐ์๋ฅผ ์ํ ํ๊ฐ, ๊ต์ก, ์ฑ์ฉ๊น์ง Total Solution์ ์ ๊ณตํ๋ ๊ฐ๋ฐ์ ์ฑ์ฅ์ ์ํ ๋ฒ ์ด์ค์บ ํ
programmers.co.kr
ํ๋ก๊ทธ๋๋จธ์ค ๋ ๋ฒจ 2 - ์๊ฐ ์ฝ๋ ์ฑ๋ฆฐ์ง ์์ฆ2 ๊ธฐ์ถ
s ๊ธธ์ด๊ฐ 1000์ดํ๋ก ์ ํด์ ธ์์ด์, ์ด์ค ๋ฐ๋ณต๋ฌธ์ผ๋ก ์ฝ๊ฒ ํ ์ ์์๋ค
ํ, ์คํ ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ ์์์ผ ํ๋๊ฒ ํฌ์ธํธ์ธ ๋ฌธ์ ์ธ๋ฏ
๋์ ํ์ด
#include <string>
#include <vector>
#include <stack>
#include <queue>
using namespace std;
int solution(string s) {
int answer = 0;
queue<int> q;
queue<int> newq;
for(int i=0; i<s.length(); ++i){
q.push(s[i]);
newq.push(s[i]);
}
for(int i=0; i<s.length(); ++i){
stack<int> st;
// stack์ ๋ฃ์ผ๋ฉด์ ํ์ธ
for(int i=0; i<s.length(); ++i){
int a = newq.front();
newq.pop();
if(!st.empty() && st.top()=='(' && a==')') {
st.pop();
}
else if(!st.empty() && st.top()=='[' && a==']') {
st.pop();
}
else if(!st.empty() && st.top()=='{' && a=='}') {
st.pop();
}
else st.push(a);
}
// ์ ๊ณผ์ ํ, ์คํ์ด ๋น์ด์์ผ๋ฉด ์ ๋ต
if(st.empty()) answer++;
// ๊ธฐ์กด ํ๋ฅผ ํ์ ์ํด
int topNum = q.front();
q.pop();
q.push(topNum);
newq = q;
}
return answer;
}
728x90
๋ฐ์ํ
'๐ ์๊ณ ๋ฆฌ์ฆ > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++/PGS] Lv.2 : ๊ธฐ๋ฅ๊ฐ๋ฐ (0) | 2025.04.24 |
---|---|
[C++/PGS] Lv.3 : ๋์คํฌ ์ปจํธ๋กค๋ฌ (Heap/priority_queue) (0) | 2025.04.17 |
[C++/PGS] Lv.4 : ํธํ ๋ฐฉ ๋ฐฐ์ (์ฌ๊ท) (0) | 2025.04.16 |
[C++/PGS] Lv.3 : ์๊ณผ ๋๋ (DFS) (0) | 2025.04.15 |
[C++/PGS] Lv.3 : ์ฐ์ ํ์ค ๋ถ๋ถ ์์ด์ ํฉ (DP) (0) | 2025.04.15 |