[c++] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค :: ํƒ‘ (๋ฌธ์ œ ํ’€์ด, ์ฝ”๋“œ)
Algorithm ๋ฌธ์ œ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2020. 7. 26. 11:16

ํƒ‘ https://programmers.co.kr/learn/courses/30/lessons/42588?language=cpp ์ฝ”๋“œ #include #include #include using namespace std; vector solution(vector heights) { int heights_len = heights.size(); vector answer(heights_len); stack stk; // index, key for (int i = heights_len - 1; i>0; i--) { if (heights[i - 1] > heights[i]) { answer[i] = i; // stack while (stk.size() != 0) { auto tmp = stk.top(); if (h..

[c++] ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐœ๋…๊ณต๋ถ€ :: ์ž๋ฃŒ๊ตฌ์กฐ - ์Šคํƒ, ํ, ๋ฑ
์ปดํ“จํ„ฐ๊ณผํ•™ (CS)/Algorithm 2020. 7. 19. 11:39

์Šคํƒ ํŠน์ง• ์šฉ๋„ ์—ฐ์‚ฐ ์‚ฝ์ž… ์‚ญ์ œ ์ ‘๊ทผ ๊ตฌํ˜„ ํ ์šฉ๋„ ์ข…๋ฅ˜ ์—ฐ์‚ฐ ์‚ฝ์ž… ์‚ญ์ œ ์ ‘๊ทผ ๊ตฌํ˜„ ์šฐ์„ ์ˆœ์œ„ ํ ๋ฑ ์šฉ๋„ ๊ตฌํ˜„ ์Šคํƒ ์Šคํƒ์€ LIFO(Last-In First-Out), ์ฆ‰ ํ›„์ž…์„ ์ถœ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค. ๊ตฌ์กฐ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ์ด๋ฉฐ, ์ ‘์‹œ์ฒ˜๋Ÿผ ์Œ“์—ฌ์„œ ์ดํ›„์— ์‚ฝ์ž…๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋จผ์ € ๋น ์ ธ๋‚˜๊ฐ€๋„๋ก ๋˜์–ด์žˆ๋‹ค. top ๋ณ€์ˆ˜๋Š” ๊ฐ€์žฅ ์ตœ๊ทผ์— ๋“ค์–ด์˜จ ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ฉฐ ์Šคํƒ์ด ๋น„์–ด์žˆ์œผ๋ฉด -1์ด๋‹ค. ํŠน์ง• LIFO ๋‚ด๋ถ€ ์š”์†Œ๋ฅผ ํ™•์ธํ•  ๋ฐฉ๋ฒ•์ด pop ๋ง๊ณ ๋Š” ์—†๋‹ค. ์šฉ๋„ ์ž…๋ ฅ์˜ ์—ญ์ˆœ์ด ํ•„์š”ํ•  ๋•Œ ์Šคํƒ์„ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋ฉฐ, ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ์—์„œ ๋ณต๊ท€ ์ฃผ์†Œ๋ฅผ ๊ธฐ์–ตํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‹œ์Šคํ…œ ์Šคํƒ์ด ์‚ฌ์šฉ๋˜๊ธฐ๋„ ํ•œ๋‹ค. ๋˜ํ•œ ๊ด„ํ˜ธ ๊ฒ€์‚ฌ, ๊ณ„์‚ฐ๊ธฐ, ๋ฏธ๋กœ ํƒ์ƒ‰ ๋“ฑ์˜ ๊ฒฝ์šฐ์— ์Šคํƒ์ด ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋œ๋‹ค. ๊ณ„์‚ฐ๊ธฐ์—์„œ ์ค‘์œ„ ํ‘œ๊ธฐ์‹(infix)์„ ํ›„์œ„ ํ‘œ๊ธฐ์‹(postfix)์œผ๋กœ..

[c++] ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐœ๋…๊ณต๋ถ€ :: ์ž๋ฃŒ๊ตฌ์กฐ - ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ
์ปดํ“จํ„ฐ๊ณผํ•™ (CS)/Algorithm 2020. 7. 19. 11:09

์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ๋ฆฌ์ŠคํŠธ๋ž€? ์ •์˜ ๋ฐฐ์—ด๊ณผ์˜ ๋น„๊ต ์—ฐ์‚ฐ ์‚ฝ์ž… ์‚ญ์ œ ์ ‘๊ทผ ์ข…๋ฅ˜ / ๊ตฌํ˜„ ๋ฆฌ์ŠคํŠธ๋ž€? ๋ฆฌ์ŠคํŠธ๋Š” ์ˆœ์„œ๋ฅผ ๊ฐ€์ง„ ํ•ญ๋ชฉ๋“ค์˜ ๋ชจ์ž„์ด๋‹ค. ์ด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ฐฉ๋ฒ•์€ 2๊ฐ€์ง€๊ฐ€ ์žˆ๋Š”๋ฐ, ํ•˜๋‚˜๋Š” ๋ฐฐ์—ด์„ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๊ณ  ๋‚˜๋จธ์ง€ ํ•˜๋‚˜๋Š” ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ๋ฐฐ์—ด์„ ์ด์šฉํ•œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์„ ํ˜•๋ฆฌ์ŠคํŠธ๋ผ๊ณ  ํ•˜๋Š”๋ฐ ๊ตฌํ˜„ ์ž์ฒด๋Š” ๊ฐ„๋‹จํ•˜๊ณ  ์ž„์˜์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, ์‚ฝ์ž… / ์‚ญ์ œ ์—ฐ์‚ฐ ์‹œ ๋‹ค๋ฅธ ์š”์†Œ๋“ค์˜ ์ด๋™์ด ๋ถˆ๊ฐ€ํ”ผํ•˜๋ฏ€๋กœ ์˜ค๋ฒ„ํ—ค๋“œ๊ฐ€ ํฌ๋‹ค. ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋Š” ๊ตฌํ˜„์ด ๋ณต์žกํ•˜๊ณ  ์ˆœ์ฐจ์ ‘๊ทผ์ด ํ•„์š”ํ•˜์ง€๋งŒ ์‚ฝ์ž…, ์‚ญ์ œ๊ฐ€ ํšจ์œจ์ ์ด๋‹ค. ์ •์˜ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ž€ ์•„๋ž˜์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋ฐ์ดํ„ฐ์™€ ๋งํฌ๋กœ ์ด๋ฃจ์–ด์ง„ ๋…ธ๋“œ๋กœ ๊ตฌ์„ฑ๋˜์–ด์žˆ๋‹ค. ๋งํฌ๋Š” ๋‹ค์Œ ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ์ด๋‹ค. ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ์ฒ˜์Œ์—๋Š” ํ—ค๋“œ ํฌ์ธํ„ฐ๊ฐ€ ํ—ค๋“œ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์œผ๋ฉฐ, ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์˜ ๋งํฌ๋Š” ..

[c++] ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ฐœ๋…๊ณต๋ถ€ :: ์ž๋ฃŒ๊ตฌ์กฐ - ๋ฐฐ์—ด, ๋ฌธ์ž์—ด
์ปดํ“จํ„ฐ๊ณผํ•™ (CS)/Algorithm 2020. 7. 19. 11:05

๋ชฉ์ฐจ ๋ฐฐ์—ด ์„ ์–ธ ์ดˆ๊ธฐํ™” ์ ‘๊ทผ ๋‹ค์ฐจ์› ๋ฐฐ์—ด ํฌ์†Œ ํ–‰๋ ฌ ํ‘œํ˜„ ์ฃผ์†Œ์™€ ํฌ์ธํ„ฐ ํฌ์ธํ„ฐ ํŠน์ง• ์ œ์•ฝ ๋™์  ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น ๋™์  ๋ฐฐ์—ด(vector) ๋ฌธ์ž์—ด ์„ ์–ธ ์ดˆ๊ธฐํ™” ํ•จ์ˆ˜ ๋ฐฐ์—ด / ๋ฌธ์ž์—ด ๋ฐฐ์—ด ๋ฐฐ์—ด์ด๋ž€ ๋‹จ์ผ ์‹๋ณ„์ž๋กœ _๊ฐ™์€ ์ž๋ฃŒํ˜•_์˜ ์—ฌ๋Ÿฌ ๋ณ€์ˆ˜๋ฅผ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค. ์„ ์–ธ c++์—์„œ๋Š” int arr[3];์™€ ๊ฐ™์ด ์ž๋ฃŒํ˜• ๋ฐฐ์—ด์ด๋ฆ„[๋ฐฐ์—ด ๊ธธ์ด];๋กœ ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•œ๋‹ค. ์ด ๋•Œ, ๋Œ€๊ด„ํ˜ธ ๋‚ด์˜ ์š”์†Œ์˜ ๊ฐฏ์ˆ˜๋Š” '์ปดํŒŒ์ผ ํƒ€์ž„ ์ƒ์ˆ˜'์—ฌ์•ผํ•œ๋‹ค. ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผํ•  ๋•Œ, ๊ณ ์ • ๋ฐฐ์—ด์˜ ๊ธธ์ด๋ฅผ ์•Œ์•„์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๋”ฐ๋ผ์„œ ๋งคํฌ๋กœ ๊ธฐํ˜ธ ์ƒ์ˆ˜ (ex_ #define ARRAY_LEN 5)๋‚˜ ์—ด๊ฑฐ์ž(ex_enum A{ARRAY_LEN = 5})๋Š” ๋ฐฐ์—ด์˜ ๊ธธ์ด๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. const ๋˜ํ•œ ์ปดํŒŒ์ผ ์‹œ ๊ณ ์ •๋˜๋Š” ์ƒ์ˆ˜์ด๋ฏ€๋กœ ์‚ฌ์šฉ๊ฐ€..

[c++] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค :: ์นดํŽซ (brute force)
Algorithm ๋ฌธ์ œ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2020. 5. 28. 21:50

์นดํŽซ ์ฝ”๋“œ #include #include #include using namespace std; vector solution(int brown, int yellow) { vector answer; vector candidate; for (int i = 1; i ๋งž์œผ๋ฉด (๊ฐ€๋กœ, ์„ธ๋กœ) ๊ธธ์ด๋ฅผ ๊ฒฐ๊ณผ vector์— ๋„ฃ์–ด์„œ return brown ๊ฐฏ์ˆ˜ = 2*๊ฐ€๋กœyellow + 2*์„ธ๋กœyellow +4 4 : ์–‘์ชฝ ๋ชจ์„œ๋ฆฌ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์ฝ”๋“œ #include #include using namespace std; vector solution(int brown, int red) { int len = brown / 2 + 2; int w = len - 3; int h = 3; while(w >= h){ if(w * h ..

[c++] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค :: ์ˆซ์ž์•ผ๊ตฌ (brute force)
Algorithm ๋ฌธ์ œ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2020. 5. 28. 09:00

์ˆซ์ž์•ผ๊ตฌ ์ฝ”๋“œ #include #include using namespace std; bool check(int* arr, vector& bb) { int num = bb[0]; int s = bb[1]; int b = bb[2]; int n[3]; n[0] = num / 100; n[1] = (num - n[0] * 100) / 10; n[2] = (num - n[0] * 100 - n[1] * 10); //strike check int t_num = 0; for (int i = 0; i < 3; i++) { if (n[i] == arr[i]) t_num++; } if (t_num != s) return false; t_num = 0; for (int i = 0; i < 3; i++) { if (n[i]..

[c++] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค :: ์†Œ์ˆ˜ ์ฐพ๊ธฐ (brute force)
Algorithm ๋ฌธ์ œ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2020. 5. 24. 10:24

https://programmers.co.kr/learn/courses/30/lessons/42839 ์ฝ”๋“œ #include #include #include #include using namespace std; set res; void test(int num) { if (num num์ด ์ตœ๋Œ€ 9999999 (10^7) if (num%i == 0) return; } res.insert(num); return; } void makePrime(vector& memo,int num) { if (memo.size()==0) return; int size = memo.size(); for (int i = 0; i < size; i++) { // 7 int value = memo[i]; test(num * 10 + v..

[c++] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค :: ๋ชจ์˜๊ณ ์‚ฌ (brute force)
Algorithm ๋ฌธ์ œ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค 2020. 5. 24. 10:14

https://programmers.co.kr/learn/courses/30/lessons/42840 ๋Œ€์ถฉ ํ’€๋ฉด ์‰ฌ์šด ๋ฌธ์ œ. ์ฝ”๋“œ๋ฅผ ๊ฐ„๊ฒฐํ•˜๊ณ  ๋ช…ํ™•ํ•˜๊ฒŒ ์“ฐ๋Š” ๊ฑธ ์—ฐ์Šตํ•˜์ž. ์ฝ”๋“œ #include #include #include using namespace std; vector solution(vector answers) { vector answer; int size = answers.size(); int correct[3] = { 0,0,0 }; int secondList[4] = { 1,3,4,5 }; int thirdList[5] = { 3,1,2,4,5 }; for (int i = 0; icorrect[1]) { if (correct[1] >= correct[2]) { answer.push_back(1..