diff --git a/JustDevRae/Hash/make_B_with_A.js b/JustDevRae/Hash/make_B_with_A.js new file mode 100644 index 0000000..2ce90bb --- /dev/null +++ b/JustDevRae/Hash/make_B_with_A.js @@ -0,0 +1,21 @@ +function solution(before, after) { + var answer = 0; + const beforeCount = {}; + const afterCount = {}; + + for (const char of before) { + beforeCount[char] = (beforeCount[char] || 0) + 1; + } + + for (const char of after) { + afterCount[char] = (afterCount[char] || 0) + 1; + } + + for (const key in beforeCount) { + if (beforeCount[key] !== afterCount[key]) { + return (answer = 0); + } + } + + return (answer = 1); +} diff --git a/JustDevRae/Hash/morse_code.js b/JustDevRae/Hash/morse_code.js new file mode 100644 index 0000000..5d70273 --- /dev/null +++ b/JustDevRae/Hash/morse_code.js @@ -0,0 +1,37 @@ +function solution(letter) { + var answer = ""; + const morse = { + ".-": "a", + "-...": "b", + "-.-.": "c", + "-..": "d", + ".": "e", + "..-.": "f", + "--.": "g", + "....": "h", + "..": "i", + ".---": "j", + "-.-": "k", + ".-..": "l", + "--": "m", + "-.": "n", + "---": "o", + ".--.": "p", + "--.-": "q", + ".-.": "r", + "...": "s", + "-": "t", + "..-": "u", + "...-": "v", + ".--": "w", + "-..-": "x", + "-.--": "y", + "--..": "z", + }; + const str = letter.split(" "); + + for (const s of str) { + answer += morse[s]; + } + return answer; +} diff --git a/JustDevRae/Hash/not_finish_runner.js b/JustDevRae/Hash/not_finish_runner.js new file mode 100644 index 0000000..e8026cc --- /dev/null +++ b/JustDevRae/Hash/not_finish_runner.js @@ -0,0 +1,21 @@ +function solution(participant, completion) { + const obj = {}; + + for (const p of participant) { + if (obj[p]) { + obj[p] += 1; + } else { + obj[p] = 1; + } + } + + for (const c of completion) { + obj[c] -= 1; + } + + for (const key in obj) { + if (obj[key] > 0) { + return key; + } + } +} diff --git a/JustDevRae/Hash/rank_order.js b/JustDevRae/Hash/rank_order.js new file mode 100644 index 0000000..8d33d58 --- /dev/null +++ b/JustDevRae/Hash/rank_order.js @@ -0,0 +1,6 @@ +function solution(score) { + const averages = score.map(([english, math]) => (english + math) / 2); + const sortedAverages = [...averages].sort((a, b) => b - a); + + return averages.map((avg) => sortedAverages.indexOf(avg) + 1); +} diff --git a/JustDevRae/Hash/setting_order_care.js b/JustDevRae/Hash/setting_order_care.js new file mode 100644 index 0000000..c90c316 --- /dev/null +++ b/JustDevRae/Hash/setting_order_care.js @@ -0,0 +1,10 @@ +function solution(emergency) { + const sorted = [...emergency].sort((a, b) => b - a); + const rankMap = new Map(); + + sorted.forEach((value, index) => { + rankMap.set(value, index + 1); + }); + + return emergency.map((value) => rankMap.get(value)); +} diff --git a/JustDevRae/Queue/card_bundle.js b/JustDevRae/Queue/card_bundle.js new file mode 100644 index 0000000..1db6617 --- /dev/null +++ b/JustDevRae/Queue/card_bundle.js @@ -0,0 +1,15 @@ +function solution(cards1, cards2, goal) { + var answer = ""; + for (let word of goal) { + if (word == cards1[0]) { + cards1.shift(); + } else if (word == cards2[0]) { + cards2.shift(); + } else { + answer = "No"; + break; + } + } + + return (answer = "Yes"); +} diff --git a/JustDevRae/Queue/find_point_position.js b/JustDevRae/Queue/find_point_position.js new file mode 100644 index 0000000..4f16fad --- /dev/null +++ b/JustDevRae/Queue/find_point_position.js @@ -0,0 +1,10 @@ +function solution(dot) { + var answer = 0; + + if (dot[0] > 0 && dot[1] > 0) answer = 1; + if (dot[0] < 0 && dot[1] > 0) answer = 2; + if (dot[0] < 0 && dot[1] < 0) answer = 3; + if (dot[0] > 0 && dot[1] < 0) answer = 4; + + return answer; +} diff --git a/JustDevRae/Queue/login_success.js b/JustDevRae/Queue/login_success.js new file mode 100644 index 0000000..c3ccabc --- /dev/null +++ b/JustDevRae/Queue/login_success.js @@ -0,0 +1,16 @@ +function solution(id_pw, db) { + var answer = ""; + for (let i = 0; i < db.length; i++) { + if (id_pw[0] === db[i][0]) { + if (id_pw[1] === db[i][1]) { + answer = "login"; + break; + } + answer = "wrong pw"; + break; + } else { + answer = "fail"; + } + } + return answer; +} diff --git a/JustDevRae/Queue/make_hamburger.js b/JustDevRae/Queue/make_hamburger.js new file mode 100644 index 0000000..bda6c56 --- /dev/null +++ b/JustDevRae/Queue/make_hamburger.js @@ -0,0 +1,20 @@ +function solution(ingredient) { + const burger = []; + let answer = 0; + for (let i of ingredient) { + burger.push(i); + + if ( + burger.length >= 4 && + burger[burger.length - 4] === 1 && + burger[burger.length - 3] === 2 && + burger[burger.length - 2] === 3 && + burger[burger.length - 1] === 1 + ) { + burger.splice(burger.length - 4, 4); + answer++; + } + } + + return answer; +} diff --git a/JustDevRae/Queue/pair_count.js b/JustDevRae/Queue/pair_count.js new file mode 100644 index 0000000..19f39ac --- /dev/null +++ b/JustDevRae/Queue/pair_count.js @@ -0,0 +1,13 @@ +function solution(n) { + var answer = 0; + + // 1부터 n까지 숫자를 확인 + for (let i = 1; i <= n; i++) { + // i가 n의 약수인지 확인 + if (n % i === 0) { + // i가 n의 약수인지 확인 + answer++; + } + } + return answer; +} diff --git a/JustDevRae/Queue/process.js b/JustDevRae/Queue/process.js new file mode 100644 index 0000000..fcc601e --- /dev/null +++ b/JustDevRae/Queue/process.js @@ -0,0 +1,20 @@ +function solution(priorities, location) { + var answer = 0; + const queue = priorities.map((priority, index) => ({ priority, index })); + + while (queue.length > 0) { + const current = queue.shift(); + + const highPriority = queue.some((item) => item.priority > current.priority); + + if (highPriority) { + queue.push(current); + } else { + answer++; + + if (current.index === location) { + return answer; + } + } + } +} diff --git a/JustDevRae/Set/contains_duplicate.js b/JustDevRae/Set/contains_duplicate.js new file mode 100644 index 0000000..d5e8bc8 --- /dev/null +++ b/JustDevRae/Set/contains_duplicate.js @@ -0,0 +1,4 @@ +var containsDuplicate = function (nums) { + const numSet = new Set(nums); + return numSet.size !== nums.length; +}; diff --git a/JustDevRae/Set/pick_random_k_numbers.js b/JustDevRae/Set/pick_random_k_numbers.js new file mode 100644 index 0000000..e6f3923 --- /dev/null +++ b/JustDevRae/Set/pick_random_k_numbers.js @@ -0,0 +1,18 @@ +function solution(arr, k) { + const uniqueSet = new Set(); + const result = []; + + for (const num of arr) { + if (!uniqueSet.has(num)) { + uniqueSet.add(num); + result.push(num); + } + if (result.length === k) break; + } + + while (result.length < k) { + result.push(-1); + } + + return result; +} diff --git a/JustDevRae/Set/prime_factorization.js b/JustDevRae/Set/prime_factorization.js new file mode 100644 index 0000000..b25b010 --- /dev/null +++ b/JustDevRae/Set/prime_factorization.js @@ -0,0 +1,15 @@ +function solution(n) { + let result = []; + let divisor = 2; + + while (n > 1) { + if (n % divisor === 0) { + result.push(divisor); + n /= divisor; + } else { + divisor++; + } + } + + return [...new Set(result)]; +} diff --git a/JustDevRae/Set/remove_duplicate_chars.js b/JustDevRae/Set/remove_duplicate_chars.js new file mode 100644 index 0000000..fa33417 --- /dev/null +++ b/JustDevRae/Set/remove_duplicate_chars.js @@ -0,0 +1,4 @@ +function solution(my_string) { + var answer = [...new Set(my_string)].join(""); + return answer; +} diff --git a/JustDevRae/Set/unique_chars_only.js b/JustDevRae/Set/unique_chars_only.js new file mode 100644 index 0000000..18ddb6b --- /dev/null +++ b/JustDevRae/Set/unique_chars_only.js @@ -0,0 +1,16 @@ +function solution(s) { + const charSet = new Set(); + const duplicateSet = new Set(); + + for (const char of s) { + if (charSet.has(char)) { + duplicateSet.add(char); + } else { + charSet.add(char); + } + } + + const uniqueChars = [...charSet].filter((char) => !duplicateSet.has(char)); + + return uniqueChars.sort().join(""); +} diff --git a/JustDevRae/Stack/control_Z.js b/JustDevRae/Stack/control_Z.js new file mode 100644 index 0000000..44fba87 --- /dev/null +++ b/JustDevRae/Stack/control_Z.js @@ -0,0 +1,15 @@ +function solution(s) { + var answer = 0; + let Z = 0; + const controlArray = s.split(" "); + for (let i = 0; i < controlArray.length; i++) { + if (controlArray[i] == "Z") { + answer -= Z; + continue; + } + answer += Number(controlArray[i]); + Z = Number(controlArray[i]); + } + + return answer; +} diff --git a/JustDevRae/Stack/crane_claw_game.js b/JustDevRae/Stack/crane_claw_game.js new file mode 100644 index 0000000..ab28cd5 --- /dev/null +++ b/JustDevRae/Stack/crane_claw_game.js @@ -0,0 +1,23 @@ +function solution(board, moves) { + let answer = 0; + const stack = []; + + for (let move of moves) { + for (let i = 0; i < board.length; i++) { + if (board[i][move - 1] !== 0) { + let doll = board[i][move - 1]; + board[i][move - 1] = 0; + + if (stack.length > 0 && stack[stack.length - 1] === doll) { + stack.pop(); + answer += 2; + } else { + stack.push(doll); + } + break; + } + } + } + + return answer; +} diff --git a/JustDevRae/Stack/dart_game.js b/JustDevRae/Stack/dart_game.js new file mode 100644 index 0000000..7b67c10 --- /dev/null +++ b/JustDevRae/Stack/dart_game.js @@ -0,0 +1,41 @@ +function solution(dartResult) { + var answer = 0; + const dartResultArray = dartResult.match(/\d+|[SDT]|\#|\*/g); + const stack = []; + let value; + + for (str of dartResultArray) { + if (str === "S") { + value = stack.pop(); + stack.push(value); + } else if (str === "D") { + value = stack.pop(); + stack.push(value * value); + } else if (str === "T") { + value = stack.pop(); + stack.push(value * value * value); + } else if (str === "*") { + let firtPopValue = stack.pop(); + firtPopValue *= 2; + let secondPopValue = stack.pop(); + if (secondPopValue === undefined) { + stack.push(firtPopValue); + } else { + secondPopValue *= 2; + stack.push(secondPopValue); + stack.push(firtPopValue); + } + } else if (str === "#") { + value = stack.pop(); + stack.push(value * -1); + } else { + stack.push(Number(str)); + } + } + + for (element of stack) { + answer += element; + } + + return answer; +} diff --git a/JustDevRae/Stack/reverse_string.js b/JustDevRae/Stack/reverse_string.js new file mode 100644 index 0000000..003d086 --- /dev/null +++ b/JustDevRae/Stack/reverse_string.js @@ -0,0 +1,10 @@ +function solution(my_string) { + var answer = ""; + const string = [...my_string]; + + for (let i = 0; i < my_string.length; i++) { + answer += string.pop(); + } + + return answer; +} diff --git a/JustDevRae/Stack/valid_parentheses.js b/JustDevRae/Stack/valid_parentheses.js new file mode 100644 index 0000000..14767b2 --- /dev/null +++ b/JustDevRae/Stack/valid_parentheses.js @@ -0,0 +1,16 @@ +function solution(s) { + const stack = []; + for (const c of s) { + if (c === "(") { + stack.push(c); + } else if (c === ")") { + if (stack.length === 0) { + continue; + } else { + stack.pop(); + } + } + } + + return stack.length === 0; +} diff --git a/JustDevRae/Tree/binary_tree_inorder_traversal.js b/JustDevRae/Tree/binary_tree_inorder_traversal.js new file mode 100644 index 0000000..253bdea --- /dev/null +++ b/JustDevRae/Tree/binary_tree_inorder_traversal.js @@ -0,0 +1,18 @@ +function inorderTraversal(root) { + let result = []; + let stack = []; + let current = root; + + while (current || stack.length) { + while (current) { + stack.push(current); + current = current.left; + } + + current = stack.pop(); + result.push(current.val); + current = current.right; + } + + return result; +} diff --git a/JustDevRae/Tree/binary_tree_level_order_traversal.js b/JustDevRae/Tree/binary_tree_level_order_traversal.js new file mode 100644 index 0000000..04ab782 --- /dev/null +++ b/JustDevRae/Tree/binary_tree_level_order_traversal.js @@ -0,0 +1,23 @@ +function levelOrder(root) { + if (!root) return []; + + let result = []; + let queue = [root]; + + while (queue.length > 0) { + let level = []; + let size = queue.length; + + for (let i = 0; i < size; i++) { + let node = queue.shift(); + level.push(node.val); + + if (node.left) queue.push(node.left); + if (node.right) queue.push(node.right); + } + + result.push(level); + } + + return result; +} diff --git a/JustDevRae/Tree/invert_binary_tree.js b/JustDevRae/Tree/invert_binary_tree.js new file mode 100644 index 0000000..470896b --- /dev/null +++ b/JustDevRae/Tree/invert_binary_tree.js @@ -0,0 +1,10 @@ +function invertTree(root) { + if (!root) return null; + + [root.left, root.right] = [root.right, root.left]; + + invertTree(root.left); + invertTree(root.right); + + return root; +} diff --git a/JustDevRae/Tree/maximum_depth_of_binary.js b/JustDevRae/Tree/maximum_depth_of_binary.js new file mode 100644 index 0000000..900b87d --- /dev/null +++ b/JustDevRae/Tree/maximum_depth_of_binary.js @@ -0,0 +1,5 @@ +function maxDepth(root) { + if (!root) return 0; + + return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; +} diff --git a/JustDevRae/Tree/same_tree.js b/JustDevRae/Tree/same_tree.js new file mode 100644 index 0000000..6ad9cda --- /dev/null +++ b/JustDevRae/Tree/same_tree.js @@ -0,0 +1,10 @@ +function isSameTree(p, q) { + + if (!p && !q) return true; + + if (!p || !q) return false; + + if (p.val !== q.val) return false; + + return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); +}