diff --git "a/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Control_Z.js" "b/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Control_Z.js" new file mode 100644 index 0000000..4086d55 --- /dev/null +++ "b/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Control_Z.js" @@ -0,0 +1,14 @@ +function solution(s) { + const elements = s.split(" "); + let total = 0; + + for (let i = 0; i < elements.length; i++) { + if (elements[i] === "Z") { + total -= parseInt(elements[i - 1]); + } else { + total += parseInt(elements[i]); + } + } + + return total; +} diff --git "a/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Crane_Game.js" "b/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Crane_Game.js" new file mode 100644 index 0000000..1c4f3f1 --- /dev/null +++ "b/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Crane_Game.js" @@ -0,0 +1,25 @@ +function solution(board, moves) { + const basket = []; + let count = 0; + + for (let move of moves) { + const column = move - 1; + + for (let i = 0; i < board.length; i++) { + if (board[i][column] !== 0) { + const doll = board[i][column]; + board[i][column] = 0; + + if (basket[basket.length - 1] === doll) { + basket.pop(); + count += 2; + } else { + basket.push(doll); + } + break; + } + } + } + + return count; +} diff --git "a/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Dart_Game.js" "b/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Dart_Game.js" new file mode 100644 index 0000000..8b568f2 --- /dev/null +++ "b/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Dart_Game.js" @@ -0,0 +1,24 @@ +function solution(dartResult) { + const stack = []; + let i = 0; + + while (i < dartResult.length) { + const isTen = dartResult[i] === "1" && dartResult[i + 1] === "0"; + let score = isTen ? 10 : parseInt(dartResult[i]); + i += isTen ? 2 : 1; + + const bonus = dartResult[i++]; + score **= bonus === "S" ? 1 : bonus === "D" ? 2 : 3; + + if (dartResult[i] === "*" || dartResult[i] === "#") { + score *= dartResult[i] === "*" ? 2 : -1; + if (dartResult[i++] === "*" && stack.length) { + stack[stack.length - 1] *= 2; + } + } + + stack.push(score); + } + + return stack.reduce((sum, cur) => sum + cur, 0); +} diff --git "a/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Delivery_Box.js" "b/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Delivery_Box.js" new file mode 100644 index 0000000..3d13dd4 --- /dev/null +++ "b/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Delivery_Box.js" @@ -0,0 +1,18 @@ +function solution(order) { + let stack = []; + let current = 1; + let count = 0; + + for (let box of order) { + while (current <= box) { + stack.push(current++); + } + if (stack.pop() === box) { + count++; + } else { + break; + } + } + + return count; +} diff --git "a/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Hate_English.js" "b/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Hate_English.js" new file mode 100644 index 0000000..245d676 --- /dev/null +++ "b/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Hate_English.js" @@ -0,0 +1,22 @@ +function solution(numbers) { + return parseInt( + numbers.replace( + /zero|one|two|three|four|five|six|seven|eight|nine/g, + (match) => { + const numWords = { + zero: "0", + one: "1", + two: "2", + three: "3", + four: "4", + five: "5", + six: "6", + seven: "7", + eight: "8", + nine: "9", + }; + return numWords[match]; + } + ) + ); +} diff --git "a/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Rotate_Parentheses.js" "b/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Rotate_Parentheses.js" new file mode 100644 index 0000000..599ac03 --- /dev/null +++ "b/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Rotate_Parentheses.js" @@ -0,0 +1,25 @@ +function solution(s) { + let answer = 0; + for (let i = 0; i < s.length; i++) { + const rotated = s.slice(i) + s.slice(0, i); + if (isValid(rotated)) answer++; + } + return answer; +} + +function isValid(str) { + const stack = []; + for (let c of str) { + if ("([{".includes(c)) stack.push(c); + else if (!stack.length || !isPair(stack.pop(), c)) return false; + } + return stack.length === 0; +} + +function isPair(open, close) { + return ( + (open === "(" && close === ")") || + (open === "[" && close === "]") || + (open === "{" && close === "}") + ); +} diff --git "a/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Stock_Price.js" "b/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Stock_Price.js" new file mode 100644 index 0000000..f5d8d0c --- /dev/null +++ "b/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Stock_Price.js" @@ -0,0 +1,18 @@ +function solution(prices) { + const answer = Array(prices.length).fill(0); + const stack = []; + + prices.forEach((price, i) => { + while (stack.length && prices[stack[stack.length - 1]] > price) { + const idx = stack.pop(); + answer[idx] = i - idx; + } + stack.push(i); + }); + + stack.forEach((idx) => { + answer[idx] = prices.length - 1 - idx; + }); + + return answer; +} diff --git "a/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/String_Calculation.js" "b/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/String_Calculation.js" new file mode 100644 index 0000000..50bd5cc --- /dev/null +++ "b/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/String_Calculation.js" @@ -0,0 +1,3 @@ +function solution(my_string) { + return eval(my_string); +} diff --git "a/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/String_Reversal.js" "b/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/String_Reversal.js" new file mode 100644 index 0000000..cae1e0c --- /dev/null +++ "b/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/String_Reversal.js" @@ -0,0 +1,3 @@ +function solution(my_string) { + return my_string.split("").reverse().join(""); +} diff --git "a/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Valid_Parentheses.js" "b/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Valid_Parentheses.js" new file mode 100644 index 0000000..1aee364 --- /dev/null +++ "b/oh-chaeyeon/2\354\243\274\354\260\250_\354\212\244\355\203\235/Valid_Parentheses.js" @@ -0,0 +1,15 @@ +function solution(s) { + const stack = []; + + for (let char of s) { + if (char === "(") { + stack.push(char); + } else if (stack.length > 0) { + stack.pop(); + } else { + return false; + } + } + + return stack.length === 0; +}