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
12 changes: 12 additions & 0 deletions unn04012/array/Array_of_divisible_numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function solution(arr, divisor) {
const answer = [];

for (const e of arr) {
if (e % divisor === 0) answer.push(e);
}
if (answer.length === 0) answer.push(-1);

return answer.sort((a, b) => a - b);
}

console.log(solution([5, 9, 7, 10], 5));
31 changes: 31 additions & 0 deletions unn04012/array/Matrix_multiplication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
function solution(arr1, arr2) {
const n = arr1.length;
const m = arr2[0].length;

const answer = Array.from({ length: n }, () => Array(m).fill(0));

for (let i = 0; i < n; i++) {
for (let j = 0; j < m; j++) {
const result = arr1[i].reduce((acc, cur, idx) => acc + cur * arr2[idx][j], 0);

answer[i][j] = result;
}
}

return answer;
}

console.log(
solution(
[
[2, 3, 2],
[4, 2, 4],
[3, 1, 4],
],
[
[5, 4, 3],
[2, 4, 1],
[3, 1, 1],
]
)
);
19 changes: 19 additions & 0 deletions unn04012/array/arr_rotate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function solution(numbers, direction) {
const answer = [];

if (direction === 'right') {
for (let i = 0; i < numbers.length - 1; i++) {
answer[i + 1] = numbers[i];
}
answer[0] = numbers[numbers.length - 1];
} else {
for (let i = numbers.length - 1; i >= 1; i--) {
answer[i - 1] = numbers[i];
}
answer[numbers.length - 1] = numbers[0];
}

return answer;
}

console.log(solution([4, 455, 6, 4, -1, 45, 6], 'left'));
9 changes: 9 additions & 0 deletions unn04012/array/cut_array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function solution(numbers, num1, num2) {
const answer = [];

for (let i = num1; i <= num2; i++) answer.push(numbers[i]);

return answer;
}

console.log(solution([1, 2, 3, 4, 5], 1, 3));
10 changes: 10 additions & 0 deletions unn04012/array/delete_min_num.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function solution(arr) {
const answer = [];
const minNum = Math.min(...arr);

for (const e of arr) {
if (e === minNum) continue;
answer.push(e);
}
return answer.length ? answer : [-1];
}
8 changes: 8 additions & 0 deletions unn04012/queue/Number_of_pairs.js
Copy link
Collaborator

Choose a reason for hiding this comment

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

λ‹¨μˆœνžˆ 길이λ₯Ό λ°˜ν™˜ν•˜λŠ” 거라면 const divisors = [];λ₯Ό count 둜 κ°€μ Έκ°€λŠ”κ²Œ λ©”λͺ¨λ¦¬μ μœΌλ‘œ 더 쒋을것 κ°™μ•„μš”

function solution(n) {
  let count = 0;

  for (let i = 1; i <= n; i++) if (n % i === 0) count++;
  return count;
}

console.log(solution(100));

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function solution(n) {
const divisors = [];

for (let i = 1; i <= n; i++) if (n % i === 0) divisors.push(i);
return divisors.length;
}

console.log(solution(100));
20 changes: 20 additions & 0 deletions unn04012/queue/card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function solution(cards1, cards2, goal) {
const reversedCard1 = cards1.reverse();
const reversedCard2 = cards2.reverse();
Comment on lines +2 to +3
Copy link
Collaborator

Choose a reason for hiding this comment

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

reverse & pop을 μ‚¬μš©ν•˜λ©΄ shiftλ₯Ό μ‚¬μš©ν•˜λŠ” 것보닀 μ‹œκ°„ λ³΅μž‘λ„ λ©΄μ—μ„œ 더 νš¨μœ¨μ μ΄κ΅°μš”. λ°°μ›Œκ°‘λ‹ˆλ‹€!


for (const word of goal) {
let flag = false;
if (reversedCard1[reversedCard1.length - 1] === word) {
reversedCard1.pop();
flag = true;
} else if (reversedCard2[reversedCard2.length - 1] === word) {
reversedCard2.pop();
flag = true;
}
if (!flag) return 'No';
}

return 'Yes';
}

console.log(solution(['i', 'drink', 'water'], ['want', 'to'], ['i', 'want', 'to', 'drink', 'water']));
27 changes: 27 additions & 0 deletions unn04012/queue/finding_the_position_of_a_point.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function solution(dot) {
let answer = 0;
const [x, y] = dot;
Copy link
Collaborator

Choose a reason for hiding this comment

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

이 μ½”λ“œλŠ” μ‚¬μš©μ•ˆν•˜λŠ”κ±°κ°™μ•„μš”!


const quadrant = [
[1, 1],
[-1, 1],
[-1, -1],
[1, -1],
];

for (let i = 0; i < quadrant.length; i++) {
const [x, y] = quadrant[i];

const newX = x * dot[0];
const newY = y * dot[1];

if (newX > 0 && newY > 0) {
answer = i + 1;
break;
}
}

return answer;
}

console.log(solution([-7, 9]));
20 changes: 20 additions & 0 deletions unn04012/queue/hambuger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
function solution(ingredients) {
let answer = 0;
const queue = [];

for (const ingredient of ingredients) {
queue.push(ingredient);
if (queue.length >= 4) {
const hamburger = queue.slice(-4).join('');

if (hamburger === '1231') {
answer++;
for (let i = 0; i < 4; i++) queue.pop();
}
}
}

return answer;
}

console.log(solution([2, 1, 1, 2, 3, 1, 2, 3, 1]));
24 changes: 24 additions & 0 deletions unn04012/queue/process.js
Copy link
Collaborator

Choose a reason for hiding this comment

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

이 μ½”λ“œμ—μ„œ queue 배열이 λ”±νžˆ μš°μ„ μˆœμœ„ 값을 λ‹΄λŠ” 것 말고 μ‚¬μš©μ΄ λ˜μ§€ μ•ŠλŠ” 것 κ°™μ•„μš”. ν˜Ήμ‹œ λ‹€λ₯Έ μ˜λ„κ°€ μžˆμ—ˆλŠ”μ§€ κΆκΈˆν•©λ‹ˆλ‹€!

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function solution(priorities, location) {
let answer = 1;
const queue = [];

const prioritiesWithIndex = priorities.map((priority, index) => ({ priority, index }));

while (prioritiesWithIndex.length) {
const { priority, index } = prioritiesWithIndex.shift();

const checkPriority = prioritiesWithIndex.findIndex((e) => e.priority > priority);
Copy link
Collaborator

Choose a reason for hiding this comment

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

some λ©”μ†Œλ“œλ₯Ό κ³ λ €ν•΄ λ³΄μ‹œλŠ”κ±΄ μ–΄λ–¨κΉŒμš”??
findIndexλŠ” 전체 배열을 μˆœνšŒν•˜μ§€λ§Œ, some은 쑰건을 λ§Œμ‘±ν•˜λŠ” μš”μ†Œλ₯Ό 찾으면 μ¦‰μ‹œ λ°˜ν™˜ν•©λ‹ˆλ‹€!!


// μš°μ„ μˆœμœ„κ°€ 더 큰 ν”„λ‘œμ„ΈμŠ€κ°€ μ—†μœΌλ©΄
if (checkPriority === -1) {
if (location === index) return queue.length + 1;
queue.push(priority);
continue;
}

prioritiesWithIndex.push({ priority, index });
}

return answer;
}
console.log(solution([1, 1, 9, 1, 1, 1], 0));
43 changes: 43 additions & 0 deletions unn04012/stack/Crane_Machine_Game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function solution(board, moves) {
let answer = 0;
const stack = [];
const width = board.length;

const pullCrane = (order) => {
const w = order - 1;
let h = 0;

while (width > h) {
const doll = board[h][w];
if (doll !== 0) {
if (stack[stack.length - 1] === doll) {
stack.pop();
answer += 2;
} else {
stack.push(board[h][w]);
}

board[h][w] = 0;
break;
}
h++;
}
};

moves.forEach((e) => pullCrane(e));

return answer;
}

console.log(
solution(
[
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 3],
[0, 2, 5, 0, 1],
[4, 2, 4, 4, 2],
[3, 5, 1, 3, 1],
],
[1, 5, 3, 5, 1, 2, 1, 4]
)
);
25 changes: 25 additions & 0 deletions unn04012/stack/I_hate_English.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function solution(numbers) {
let answer = numbers;
const englishObj = {
zero: 0,
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
nine: 9,
};

for (const [key, value] of Object.entries(englishObj)) {
if (numbers.includes(key)) {
answer = answer.replaceAll(key, value);
}
}

return Number(answer);
}

console.log(solution('onefourzerosixseven'));
19 changes: 19 additions & 0 deletions unn04012/stack/ctrl_z.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function solution(s) {
const stack = [];
let answer = 0;

s.split(' ').forEach((e) => {
// μˆ«μžκ°€ μ•„λ‹ˆλ©΄
if (Number.isNaN(Number(e))) {
answer -= stack.pop();
} else {
const num = Number(e);
answer += num;
stack.push(num);
}
});

return answer;
}

console.log(solution('10 Z 20 Z'));
24 changes: 24 additions & 0 deletions unn04012/stack/string-calculate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function solution(my_string) {
let answer = 0;

const expression = my_string.split(' ').reverse();

while (expression.length) {
const element = expression.pop();

if (element === '+') {
answer += Number(expression.pop());
expression.push(answer);
} else if (element === '-') {
answer -= Number(expression.pop());
expression.push(answer);
} else {
// 숫자인 경우
answer = Number(element);
}
}

return answer;
}

console.log(solution('3 + 4 - 2'));
5 changes: 5 additions & 0 deletions unn04012/stack/string_reversal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function solution(my_string) {
return my_string.split('').reverse().join('');
}

console.log(solution('jaron'));
Loading