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

๐Ÿš“ Self Study65

LeetCode (Two Sum) C++ class Solution { public: vector twoSum(vector& nums, int target) { vector answer; for(int i = 0 ; i < nums.size() - 1 ; i++) { for(int j = i+1 ; j < nums.size() ; j++) { if(nums[i] + nums[j] == target) { answer.push_back(i); answer.push_back(j); } } } return answer; } }; 2022. 1. 7.
LeetCode (Employee Importance) C++ BFS๋กœ ์ ‘๊ทผํ•ด์„œ ํ‘ผ ๋ฌธ์ œ. ๋จธ๋ฆฟ ์†์— ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์žˆ๋‹ค๋ฉด ๋‚ด๊ฐ€ ํ•ญ์ƒ ํ’€๋˜ ๋ฐฉ์‹์ด ์•„๋‹ˆ๋ผ ์ฃผ์–ด์ง„ Structure๋กœ ํ’€ ์ˆ˜ ์žˆ์–ด์•ผ ํ–ˆ๋˜ ๋ฌธ์ œ์˜€๋‹ค. BFS๊ฐ€ ๊ฐ€๋ฌผ๊ฐ€๋ฌผ ํ•ด์ง€๋ฉด ๋ณด๋Ÿฌ์˜ค์Ÿˆ! /* // Definition for Employee. class Employee { public: int id; int importance; vector subordinates; }; */ class Solution { public: int getImportance(vector employees, int id) { int answer = 0; int index = 0; queue q; q.push(id); while(!q.empty()) { int node = q.front(); // id์— ํ•ด๋‹นํ•˜๋Š” ๊ฒƒ์ด ๋ช‡ ๋ฒˆ์งธ index.. 2022. 1. 6.
LeetCode (Sum of Left Leaves) C++ left leave๋ฅผ ๋งŒ๋‚  ๋•Œ์˜ ์กฐ๊ฑด์€ ํ•ด๋‹น node์˜ left์™€ right๊ฐ€ ๋ชจ๋‘ null ์ธ ๊ฒฝ์šฐ ์ด๋ฏ€๋กœ ํ•ด๋‹น ์กฐ๊ฑด์ผ ๋•Œ return ํ•˜๊ณ  ์•„๋‹Œ ๊ฒฝ์šฐ๋Š” ๋ชจ๋‘ ํƒ์ƒ‰ํ•ด์„œ ๋“ค์–ด๊ฐ€๋ฉด ๋œ๋‹ค. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(le.. 2022. 1. 6.
๋ฐฑ์ค€ BaekJoon (DFS์™€ BFS, 1260) C++ ์™„์ „ ์ •์„ ์ค‘์˜ ์ •์„ ๋ฌธ์ œ. ํ’€๋ฉด DFS์™€ BFS์˜ ๊ฐœ๋…์— ๋Œ€ํ•ด์„œ ๋‹ค์‹œ ์žก์„ ์ˆ˜ ์žˆ๋‹ค. #include #include #include #include #include using namespace std; int main() { int vertex = 0; int edge = 0; int first = 0; cin >> vertex >> edge >> first; vector adj(vertex+1); vector visited(vertex+1, 0); for(int i = 0 ; i > one >> two; adj[one].push_back(two); adj[two].push_back(one); } for(int i = 1 ; i 2022. 1. 5.
๋ฐฑ์ค€ BaekJoon (๋ฐ”์ด๋Ÿฌ์Šค, 2606) C++ BFS ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์ดํด์— visitedํ•˜๊ฒŒ ๋˜๋Š” node๊ฐ€ ๋ช‡ ๊ฐœ์ธ์ง€ ์„ธ์–ด์ฃผ๋ฉด ๋œ๋‹ค. #include #include #include using namespace std; int main() { int computer = 0; int num = 0; cin >> computer; cin >> num; vector adj(computer+1); vector visited(computer, 0); queue q; for(int i = 0 ; i > one >> two; adj[one].push_back(two); adj[two].push_back(one); } // for(int i =1 ; i < adj.size() ; i++).. 2022. 1. 5.
ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค (์ด์ค‘์šฐ์„ ์ˆœ์œ„ํ, ํž™(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.
Deep Learning(Fully-connected Layers, CNN in PyTorch) Fully connected Layers Neural Network์˜ ๋งˆ์ง€๋ง‰์— Classification ๋˜๋Š” Regression์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ๋งŽ์ด ์‚ฌ์šฉ๋˜๋Š” Regression์ด Detection์ด๋‹ค. ์–ด๋–ค ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ์žˆ์„ ๋•Œ Bounding Box๋ฅผ ์ฐพ์•„๋‚ด๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•œ๋‹ค. ์ด๊ฒƒ์ด Regression ๋ฌธ์ œ๋กœ ํ’€๋ฆฐ๋‹ค. CNN in PyTorch ํŒŒ์ด์ฌ์—์„œ ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฌธ๋ฒ•. nn.Modulel์ด๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›์•„์„œ Net์„ ์ •์˜ํ•˜๊ฒ ๋‹ค. def๋Š” ๋ฉ”์†Œ๋“œ์— ํ•ด๋‹นํ•˜๋Š”๋ฐ, __init__ ์€ ์ปจ์ŠคํŠธ๋Ÿญํ„ฐ์— ํ•ด๋‹นํ•œ๋‹ค. Neural Network๋ฅผ ์ •์˜ํ–ˆ๋‹ค. ์ž…๋ ฅ Layer(RGB)๊ฐ€ ๋“ค์–ด์™€์„œ 6๊ฐœ์˜ Feature๋ฅผ ๋งŒ๋“ค์–ด๋‚ธ๋‹ค. ๊ทธ ๋‹ค์Œ Max-pooling์„ ํ†ตํ•ด์„œ ์ ˆ๋ฐ˜ ํฌ๊ธฐ๋กœ ์ค„์ธ๋‹ค. ๋‹ค์‹œ Convolu.. 2022. 1. 4.
ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค (๊ฐ€์žฅ ํฐ ์ˆ˜, ์ •๋ ฌ) 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.