Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions oh-chaeyeon/2주차_스택/Control_Z.js
Original file line number Diff line number Diff line change
@@ -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;
}
25 changes: 25 additions & 0 deletions oh-chaeyeon/2주차_스택/Crane_Game.js
Original file line number Diff line number Diff line change
@@ -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;
}
24 changes: 24 additions & 0 deletions oh-chaeyeon/2주차_스택/Dart_Game.js
Original file line number Diff line number Diff line change
@@ -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);
}
18 changes: 18 additions & 0 deletions oh-chaeyeon/2주차_스택/Delivery_Box.js
Original file line number Diff line number Diff line change
@@ -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;
}
22 changes: 22 additions & 0 deletions oh-chaeyeon/2주차_스택/Hate_English.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function solution(numbers) {
return parseInt(
numbers.replace(
/zero|one|two|three|four|five|six|seven|eight|nine/g,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

정규식을활용하여 코드가 직관적이라 좋았습니다!

(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];
}
)
);
}
25 changes: 25 additions & 0 deletions oh-chaeyeon/2주차_스택/Rotate_Parentheses.js
Original file line number Diff line number Diff line change
@@ -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 === "}")
);
}
18 changes: 18 additions & 0 deletions oh-chaeyeon/2주차_스택/Stock_Price.js
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 3 additions & 0 deletions oh-chaeyeon/2주차_스택/String_Calculation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function solution(my_string) {
return eval(my_string);
}
3 changes: 3 additions & 0 deletions oh-chaeyeon/2주차_스택/String_Reversal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function solution(my_string) {
return my_string.split("").reverse().join("");
}
15 changes: 15 additions & 0 deletions oh-chaeyeon/2주차_스택/Valid_Parentheses.js
Original file line number Diff line number Diff line change
@@ -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;
}
Loading