๐ Self Study/๐ Programmers26 ํ๋ก๊ทธ๋๋จธ์ค (์ด์ค์ฐ์ ์์ํ, ํ(Heap)) C++ priority_queue => Min Heap priority_queue => Max heap ์ฐ์ ์์ ํ๋ ์ต๋๊ฐ๊ณผ ์ต์๊ฐ์ ๊ฐ์ฅ ํจ์จ์ ์ผ๋ก ํ์ธํ ์ ์๋ ๋ฐฉ๋ฒ์ด๋ฏ๋ก ํด๋น STL์ ์ฌ์ฉํ๋ค๋ฉด ์์ฝ๊ฒ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๋ค. ์ฌ๊ธฐ์ ์ค์ํ ๊ฒ์ ๋ ๊ฐ์ Heap์ผ๋ก ํ๋์ ์ํฉ์ ์ฒ๋ฆฌํ๋ค๋ณด๋๊น heap์ ํฌ๊ธฐ๋ฅผ ๋์ผํ๊ฒ ์นด์ดํธ ํด์ค ์ ์๋ ๋ณ์๊ฐ ํ์ํ๋ค๋ ์ ์ด๋ค. ๋ง์ฝ pop์ ํ๋๋ฐ ํฌ๊ธฐ๊ฐ 0์ด ๋ ๊ฒฝ์ฐ ๋ ๊ฐ์ Heap์ ๋ชจ๋ ์ด๊ธฐํํด์ค ํ์๊ฐ ์์๋ค. (์ ์ผ ์ค์ํ ์ ) #include #include #include #include #include using namespace std; vector solution(vector operations) { vector answer; int hea.. 2022. 1. 5. ํ๋ก๊ทธ๋๋จธ์ค (๋์คํฌ ์ปจํธ๋กค๋ฌ, ํ(Heap)) C++ sort ์ ๋ ฌ์ ํ ๋ cmp๋ฅผ ์ฌ์ฉํด์ ์๊ณ ๋ฆฌ์ฆ์ ์์ฑํ์์ง๋ง ํ ์คํธ ์ผ์ด์ฌ ์ ๋ถ ํต๊ณผํ์ง ๋ชปํ๋ค. ์์ฒญํ ์์๋๋ก ์ถ๊ฐ๋ฅผ ํ๊ณ ์ถ๊ฐ๋ฅผ ํ ๋ ์ฐ์ ์์ ํ๋ฅผ ์ด์ฉํด์ operation์ ์ถ๊ฐํด์ ๋ฃ์ผ๋ฉด ํ ์คํธ ์ผ์ด์ฌ ๋ชจ๋ ํต๊ณผํ ์ ์๋ค. #include #include #include #include #include using namespace std; struct cmp { bool operator()(vectora, vector b) { return a[1] > b[1]; } }; int solution(vector jobs) { int answer = 0; int task = 0; int time = 0; // pq์ ๊ฐ์ ๋ฃ๊ฒ ๋๋ ๊ฒฝ์ฐ ์์ ์๊ฐ์ด ์งง์ ์์๋๋ก ์ ๋ ฌ์ด ๋๋ค. priority.. 2022. 1. 5. ํ๋ก๊ทธ๋๋จธ์ค (๊ฐ์ฅ ํฐ ์, ์ ๋ ฌ) C++ ์ผ๋จ ์์ด๋ก ์ ๊ทผ์ ํ๋๋ฐ ๋ช ๊ฐ์ Test case๊ฐ ๋์๊ฐ์ง ์์์ ์ข ๋ต๋ตํ๋ค. Sort๋ฅผ ํ ๋ ๋น๊ต ์ฐ์ ์์๋ฅผ ๋ถ์ฌํ๋๊น ๋ฐ๋ก ํ๋ ธ๋ ๋ฌธ์ #include #include #include #include using namespace std; bool cmp(const string& a, const string& b) { if(a + b > b + a) return true; else return false; } string solution(vector numbers) { string answer = ""; vector real; // ๋ฒกํฐ์ string์ผ๋ก ํ ๋น for(int i = 0 ; i < numbers.size() ; i++) { real.push_back(to_string(number.. 2022. 1. 4. ํ๋ก๊ทธ๋๋จธ์ค (ํ๋ฆฐํฐ, ์คํ/ํ) C++ map์ ๊ฐ๋ ์ผ๋ก index์ value๊ฐ ๋ชจ๋ ํ์ํ ๋ฌธ์ ์๋ค. Map์ ์ด์ฉํด์ Pair๋ฅผ ๋ง๋ค๊ณ ์ด๋ฅผ Queue์ ๋ฃ์ด์ฃผ๋ ๋ฐฉ์์ผ๋ก ์ ๊ทผ์ ํ๋ค๊ฐ ์ ๋์ง ์์๋ค. ๊ทธ๋์ queue ์์ฒด๋ฅผ ํ ์์ ๋ง๋ค์ด์ ํ ๋น์ ํ๋๋ ์ ํ๋ ธ๋ค. Queue๋ฅผ ์ฌ์ฉํด์ ํ ๋ฐฉ์ ๋ฌธ์ ๊ฐ ํ๋ ธ๋ค... ๋๋ฌด ๊ธฐ๋ถ์ด ์ข์๋ฐ...? #include #include #include #include #include using namespace std; int solution(vector priorities, int location) { int answer = 0; int index = 0; queue q; // key์ index ๊ฐ์ ๋ฃ๊ณ value์ ์ฐ์ ์์๋ฅผ ๋ฃ์ด์ค๋ค. for(int i =0 ; i < prioritie.. 2022. 1. 4. ํ๋ก๊ทธ๋๋จธ์ค (์ง์ง์ด ์ ๊ฑฐํ๊ธฐ, 2017 ํ์คํ์ด) C++ Stack์ ๊ฐ๋ ์ ์ฌ์ฉํ๊ฒ ๋๋ ๊ฒฝ์ฐ๋ฅผ ๋ฐฐ์ธ ์ ์์๋ ๋ฌธ์ ์ด๋ค. ํ์ฌ์ ๊ฒ๊ณผ ์ด์ ์ ๊ฒ์ ๋น๊ตํ๊ณ erase ํ๊ฒ ๋๋ ๊ฒฝ์ฐ stack์ ์ฌ์ฉํ๋ฉด ํจ์จ์ฑ ์ธก๋ฉด์์ ํจ์ฌ ๋น ๋ฅด๊ฒ ์ ๊ทผ์ด ๊ฐ๋ฅํ๋ค๋ ๊ฒ์ ๋ฐฐ์ ๋ค. #include #include #include using namespace std; int solution(string s) { int answer = -1; stack st; // ๋ฌธ์์ด์ ๊ธธ์ด๋งํผ ๊ฒ์ฌ๋ฅผ ํ๋ค. for(int i = 0 ; i < s.size() ; i++) { // stack์ด ๋น์ด์๊ฑฐ๋ ๋ฌธ์์ด์ ํ์ฌ ๋ฌธ์์ top์ด ๊ฐ์ง ์๋ค๋ฉด push๋ฅผ ํ๋ค. if(st.empty() || st.top() != s[i]) st.push(s[i]); // top๊ณผ ํ์ฌ ๋ฌธ์๊ฐ ๊ฐ๋ค๋ฉด ์ ๊ฑฐ.. 2022. 1. 4. ํ๋ก๊ทธ๋๋จธ์ค (๊ธฐ๋ฅ ๊ฐ๋ฐ, ์คํ/ํ) C++ Queue๋ฅผ ์ฌ์ฉํ์ง ์๊ณ ๋์ผํ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ์งํํ๋๋ฐ Time ์ด๊ณผ๋๋ ๊ฒฝ์ฐ๊ฐ ์๊ธด๋ค. ๋ค๋ฅธ ๊ฒฝ์ฐ๋ ๋ค ํต๊ณผํ๋๋ฐ Queue๋ Vector์ ์ฐจ์ด๊ฐ ์ด๋ ๊ฒ ์ฌํ๊ฐ... ์๊ฐ์ด ๋ ๋ค. #include #include #include using namespace std; vector solution(vector progresses, vector speeds) { vector answer; int front_index = 0; int count = 0; while(front_index != progresses.size()) { // ๊ฐ ์์ ์ ํ ๋ฒ์ฉ ์งํ์ํค๋ for๋ฌธ for(int i =0 ; i < progresses.size() ; i++) { progresses[i] += speeds[i]; } .. 2022. 1. 3. ํ๋ก๊ทธ๋๋จธ์ค (124 ๋๋ผ, ์ฐ์ต๋ฌธ์ ) C++ ์ฌ๊ทํจ์๋ก ํ์๋ค๊ฐ ์ฝ๋ ๋ญ๋น๊ฐ ๋ง์์ ธ์ ๋ฌดํ๋ฃจํ ์์์ ํด๊ฒฐํ๋ค. #include #include #include using namespace std; string solution(int n) { string answer = ""; int top = n; int bottom = 0; while(true) { bottom = top % 3; top = top / 3; switch(bottom) { case 0: answer = '4' + answer; top -= 1; break; case 1: answer = '1' + answer; break; case 2: answer = '2' + answer; break; } if(top == 0) break; } return answer; } 2022. 1. 3. ํ๋ก๊ทธ๋๋จธ์ค (์คํจ์จ, 2019 KAKAO BLIND RECRUITMENT) C++ ์นด์นด์ค ๋ฌธ์ ๋ก vector๊ฐ ์ฃผ์ด์ง๋ ๊ฒฝ์ฐ ๊ฑฐ์ ์ด์ค for๋ฌธ์ผ๋ก ์์ํด์ผ ํ๋ ๋ฌธ์ ๊ฐ ๋ง์ ๊ฒ ๊ฐ๋ค. ์ค๋๋ง์ ์ฃผ์์ ๋จผ์ ๋ฌ๊ณ ์ฝ๋๋ฅผ ์งฐ๋๋ฐ ์๊ฐ๋ณด๋ค ์ ์ง์ก๋ค. Map์ ํ์ฉํด์ value์ ๋ํด ์ค๋ฆ์ฐจ์/๋ด๋ฆผ์ฐจ์ ์ ๋ ฌํ๋ ๋ฒ์ ์ฒดํํ ๊ฒ ๊ฐ๋ค. #include #include #include #include #include using namespace std; bool cmp(const pair& a, const pair& b) { // value๊ฐ ๊ฐ๋ค๋ฉด key์ ํด๋นํ๋ ๊ฒ์ ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌํ๋ค. if(a.second == b.second) return a.first b.second; .. 2022. 1. 3. ํ๋ก๊ทธ๋๋จธ์ค (๋ฉ์ฉกํ ์ฌ๊ฐํ, Summer/Winter Coding(2019)) C++ ์ต์๊ณต๋ฐฐ์๋ฅผ ๊ตฌํ๋ ๋ฐฉ๋ฒ์ผ๋ก "์ ํด๋ฆฌ๋ ํธ์ ๋ฒ"์ด ๊ตฌ๊ธ์ ๊ฐ์ฅ ๋ง์ด ๋์์์ด์ ํด๋น ๋ฐฉ๋ฒ์ ์ตํ๋ณด์ ๋ค์๊ณผ ๊ฐ์ด ์ฌ์ฉ์ ํ๋ค. ๊ฐ๋ก์ ์ธ๋ก๋ฅผ ์ต๋๊ณต์ฝ์๋ก ๋๋์ด์ ๊ท์น์ ์ฐพ์๋ณด๋ ค๊ณ ํ์ง๋ง ์๊ฐ๋ณด๋ค ๊ท์น์ ์ฐพ๋๊ฒ ์ฝ์ง ์์์ ํ๋ค์๋ค. GCD ๊ตฌํ๋ ๋ฐฉ๋ฒ ํ๋ ์ ๋๋ ์ธ์ฐ๋ ๊ฒ๋ ๋์์ง ์์๋ณด์ธ๋ค. using namespace std; long long gcd(int x, int y) { long long temp =0 ; while(y != 0) { temp = x % y; x = y; y = temp; } return x; } long long solution(int w,int h) { long long answer = 1; long long W = w; long long H = h; long long.. 2022. 1. 3. ํ๋ก๊ทธ๋๋จธ์ค (ํฌ์ผ๋ชฌ, ์ฐพ์๋ผ ํ๋ก๊ทธ๋๋ฐ ๋ง์คํฐ) C++ ์ค๋ณต์ฑ์ ์ ๊ฑฐํด์ค์ผ ํ๋ค. -> Set ์ด์ฉ set set; ์๋ก์ด ์์ ์ถ๊ฐ set.insert(์์); #include #include #include using namespace std; int solution(vector nums) { int answer = 0; int number = nums.size()/2; set set; // nums ๋ฐฐ์ด์ ๋ด๊ธด ์ซ์์ ์ค๋ณต์ฑ์ ์์ ์ฃผ๊ธฐ ์ํด์ set์ ์ ์ธํ๊ณ ๋ฃ์ด์ค๋ค. for(int i = 0 ; i < nums.size() ; i++) { set.insert(nums[i]); } // ์ข ๋ฅ๊ฐ N/2 ๋ณด๋ค ์๋ค๋ฉด ๊ทธ๋๋ก answer์ ๋ฃ์ด์ฃผ๊ณ // ์๋๋ฉด N/2๋ฅผ answer๋ฅผ ๋ต์ ๋ฃ์ด์ค๋ค. if(number 2022. 1. 2. ์ด์ 1 2 3 ๋ค์