Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- ๋ฐฑ์ค
- ๋ฏธ๊ตญ์ฌํ #๋ฏธ๊ตญ์ ๊ตญ์ฌํ
- HTML์ ๋ฌธ
- 2579
- ๊ฐ๋ฐ์์ค๋น #์ปดํจํฐ๊ณตํ๊ณผ
- C++
- GitHub
- html #css์ ๋ฌธ #visual studio
- ๋์ ํ ๋น๋ฒ
Archives
- Today
- Total
๐๊ฐ๋ฐ๊ณผ ์ผ์ (โง∇โฆ)๏พ
[ํ๋ก๊ทธ๋๋จธ์ค/C++] ํ๋ก์ธ์ค (์คํ,ํ - L2) ๋ณธ๋ฌธ
์๋ฃ๊ตฌ์กฐ๋ฅผ ํ์ฉํ ์์ ํ์ ๋ฌธ์ ๋ค.
๋จ, ์ฐ์ ์์๋ฅผ ๊ณ ๋ คํด์ผํ๋ ๋ฌธ์ ๊ฐ ์์ด์ ์ฒ์์๋ ์ฐ์ ์์ ํ ์๋ฃ๊ตฌ์กฐ๋ฅผ ํ์ฉํด์ผํ๋? ์ถ์๋๋ฐ,
๋ง์ ์ํ๋ location์ ๊ฐ์ด ์ธ์ ๋์ค๋์ง๋ฅผ ํ์ธํ๊ธฐ ์ํด์๋ ์๋ ์์น๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ์์ ํ ์ ๋ฐ์ ์์๋ค.
๊ทธ๋์ location์์ ์ง์ ํด๋ index๋ฅผ ๋ฐ๋ผ๊ฐ๋ฉด์ ์์๋ฅผ ์ฐพ๋ ๋ฐฉ์์ผ๋ก ๊ฐ๋ฐํ๋ค.
๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก๋ ํ ์ ์์ ๊ฒ ๊ฐ์ง๋ง ๊ฐ์ฅ ์ง๊ด์ ์ด๊ฒ ๋ค๊ฐ์จ ๋ฐฉ๋ฒ์ ์ด ๋ฐฉ๋ฒ์ด์๋ค.
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
using namespace std;
int solution(vector<int> priorities, int location) {
int answer = 0;
queue<int> q;
// queue ์๋ฃ๊ตฌ์กฐ
for(int i = 0; i < priorities.size(); i++) q.push(priorities[i]);
//์ฐ์ ์์๋ฅผ ์ํด์ ์ ๋ ฌ ํ, ํด๋น ์ซ์๊ฐ popํ ์ ์๋๋ก ์์
sort(priorities.begin(), priorities.end());
int Aidx = location;
while(true){
//๋งจ ์ ์น๊ตฌ๊ฐ priority์ ๋ง์ถฐ ๋์ฌ ์ ์๋์ง
//ํ์ฌ ์ฐ์ ์์์ ๋์ผํ์ง, ๋์ผ ํ๋ฉด pop
if(q.front() == priorities.back()){
answer++;
//๊ทผ๋ฐ ์ด๋ฒ์ ๋์ค๋ ์ธ๋ฑ์ค๊ฐ ์ฒซ๋ฒ์งธ ์ธ๋ฑ์ค์์ ๊ฒฝ์ฐ
//(ํฌ์ธํฐ ๊ฐ๋
์ผ๋ก ๊ณ์ ๋ฐ๋ผ๊ฐ๊ฒ๋ ํ ๊ฑฐ์)
if(Aidx == 0) break;
priorities.pop_back(); //์ฐ์ ์์ ํจ์ pop
q.pop(); //queue ์๋ pop
// ์๋ ๊ฒฝ์ฐ์๋ pop๋ง ์งํํจ <- ํ์ ์์ ์๋ ์์
Aidx--; if(Aidx < 0) Aidx = q.size()-1;
}
//์ฐ์ ์์๊ฐ ๋์ผํ์ง ์๋๋ค๋ฉด ๋ค๋ก ๋ค์ ๋ฃ๊ธฐ
else{
int temp = q.front();
q.pop();
q.push(temp);
// ์๋ ๊ฒฝ์ฐ์๋ pop๋ง ์งํํจ <- ํ์ ์์ ์๋ ์์
Aidx--; if(Aidx < 0) Aidx = q.size()-1;
}
}
return answer;
}
์๊ฐ ๊ฒฐ๊ณผ
'CS > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค/C++] ๋ ํ ํฉ ๊ฐ๊ฒ ๋ง๋ค๊ธฐ (queue - L2) (1) | 2023.11.21 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค/C++] ๋คํธ์ํฌ (DFS,BFS - L3) (0) | 2023.11.17 |
[๋ฐฑ์ค c++] 1937 ์์ฌ์์ด ํ๋ค (๊ณจ3) (0) | 2023.11.15 |
[๋ฐฑ์ค c++] 1030 ํ๋ ํํ๋ฉด (๊ณจ3) (0) | 2023.10.02 |
[๋ฐฑ์ค c++] 1149 RGB๊ฑฐ๋ฆฌ (์ค1) (0) | 2023.09.25 |