๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿš“ 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.