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
7 changes: 7 additions & 0 deletions jiyeeeah/[week1]Array/array_length.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function solution(strlist) {
const answer = [];
for (const str of strlist) {
answer.push(str.length);
}
return answer;
}
9 changes: 9 additions & 0 deletions jiyeeeah/[week1]Array/divided_number_array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function solution(arr, divisor) {
const answer = [];
for (const val of arr) {
if (val % divisor === 0) answer.push(val);
}
if (answer.length <= 0) answer.push(-1);
answer.sort((a, b) => a - b);
return answer;
}
3 changes: 3 additions & 0 deletions jiyeeeah/[week1]Array/number_of_duplicate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function solution(array, n) {
return array.filter((value) => value === n).length;
}
18 changes: 18 additions & 0 deletions jiyeeeah/[week1]Array/procession_multiply.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function solution(arr1, arr2) {
const r1 = arr1.length;
const c1 = arr1[0].length;
const r2 = arr2.length;
const c2 = arr2[0].length;
// c1 === r2임

const answer = new Array(r1).fill(0).map(() => new Array(c2).fill(0));

for (let i = 0; i < r1; i++) {
for (let j = 0; j < c2; j++) {
for (let k = 0; k < c1; k++) {
answer[i][j] += arr1[i][k] * arr2[k][j];
}
}
}
return answer;
}
11 changes: 11 additions & 0 deletions jiyeeeah/[week1]Array/procession_sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function solution(arr1, arr2) {
const Xn = arr1.length;
const Yn = arr1[0].length;
const answer = new Array(Xn).fill(0).map(() => new Array(Yn).fill(0));
for (let i = 0; i < Xn; i++) {
for (let j = 0; j < Yn; j++) {
answer[i][j] = arr1[i][j] + arr2[i][j];
}
}
return answer;
}
9 changes: 9 additions & 0 deletions jiyeeeah/[week1]Array/remove_min_number.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function solution(arr) {
const answer = [...arr];

const minNum = arr.sort((a, b) => a - b)[0];
answer.splice(answer.indexOf(minNum), 1);
if (answer.length <= 0) answer.push(-1);

return answer;
}
11 changes: 11 additions & 0 deletions jiyeeeah/[week1]Array/rotate_array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function solution(numbers, direction) {
const answer = [...numbers];
if (direction === "right") {
const temp = answer.pop();
answer.unshift(temp);
} else if (direction === "left") {
const temp = answer.shift();
answer.push(temp);
}
return answer;
}
4 changes: 4 additions & 0 deletions jiyeeeah/[week1]Array/trim_array.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function solution(numbers, num1, num2) {
const answer = numbers;
return answer.slice(num1, num2 + 1);
}
24 changes: 24 additions & 0 deletions jiyeeeah/[week1]Array/배열.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
> 이번에는 문제를 풀며 몰랐거나 까먹었다가 이제 다시 알게 된 부분들 위주로 정리해봤습니다.

## 2차원 배열 만들기

```js
const arr = new Array(5).fill(0).map(() => new Array(4));
```

### 📄 REFERENCE

https://joonfluence.tistory.com/508

## 행렬의 곱셈

```js
const r1 = arr1.length;
const c1 = arr1[0].length;
const r2 = arr2.length;
const c2 = arr2[0].length;
// 여기서 c1 === r2이다.

// 결과 배열은 r1 * c2이다.
const answer = new Array(r1).fill(0).map(() => new Array(c2).fill(0));
```
17 changes: 17 additions & 0 deletions jiyeeeah/[week2]Stack/calculate_string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function solution(my_string) {
const expression = my_string.split(" ");
const stack = [Number(expression[0])];
for (let i = 1; i < expression.length; i++) {
if (expression[i] === "+" || expression[i] === "-") {
stack.push(expression[i]);
} else {
const operator = stack.pop();
const a = stack.pop();
let result;
if (operator === "+") result = a + Number(expression[i]);
else result = a - Number(expression[i]);
stack.push(result);
}
}
return stack[0];
}
13 changes: 13 additions & 0 deletions jiyeeeah/[week2]Stack/control_z.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function solution(s) {
const cmd = s.split(" ");
const stack = [];
for (const c of cmd) {
if (c === "Z") {
stack.pop();
} else {
stack.push(c);
}
}
const answer = stack.reduce((acc, value) => acc + Number(value), 0);
return answer;
}
12 changes: 12 additions & 0 deletions jiyeeeah/[week2]Stack/correct_parentheses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function solution(s) {
const stack = [];
for (const par of s) {
if (par === "(") {
stack.push(par);
} else {
if (stack.length === 0) return false;
else stack.pop();
}
}
return stack.length === 0;
}
22 changes: 22 additions & 0 deletions jiyeeeah/[week2]Stack/crane_doll_machine.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function solution(board, moves) {
let answer = 0;
const N = board.length;

const stack = [];
for (const move of moves) {
for (let i = 0; i < N; i++) {
if (board[i][move - 1] === 0) continue;

const item = board[i][move - 1];
if (stack.length > 0 && stack[stack.length - 1] === item) {
stack.pop();
answer += 2;
} else {
stack.push(item);
}
board[i][move - 1] = 0;
break;
}
}
return answer;
}
22 changes: 22 additions & 0 deletions jiyeeeah/[week2]Stack/dart_game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function solution(dartResult) {
const stack = [];
for (const c of dartResult) {
const topIndex = stack.length - 1;
if (c === "S") stack[topIndex] = stack[topIndex] ** 1;
else if (c === "D") stack[topIndex] = stack[topIndex] ** 2;
else if (c === "T") stack[topIndex] = stack[topIndex] ** 3;
else if (c === "*") {
stack[topIndex] *= 2;
if (topIndex - 1 >= 0) stack[topIndex - 1] *= 2;
} else if (c === "#") {
stack[topIndex] *= -1;
} else {
if (c === "0" && stack[topIndex] === 1) {
stack[topIndex] = 10;
} else {
stack.push(Number(c));
}
}
}
return stack.reduce((acc, val) => acc + val, 0);
}
39 changes: 39 additions & 0 deletions jiyeeeah/[week2]Stack/delivery_box.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function solution(order) {
let answer = 0;
const tempConv = [];
let boxIndex = 1;
for (let i = 0; i < order.length; i++) {
while (boxIndex <= order[i]) {
tempConv.push(boxIndex++);
}
const top = tempConv[tempConv.length - 1];
if (top !== order[i]) break;
tempConv.pop();
answer++;
}
return answer;
}

// 틀린 답변입니다.
function solution(order) {
const tempConv = [];
const truck = [];
for (let i = 0; i < order.length; i++) {
const topTempConv = tempConv[tempConv.length - 1];
if (tempConv.length > 0 && topTempConv === order[i]) {
tempConv.pop();
truck.push(order[i]);
} else if (!tempConv.includes(order[i])) {
const startNum = i - 1 >= 0 ? order[i - 1] + 1 : 1;
for (let j = startNum; j < order[i]; j++) {
tempConv.push(j);
}
truck.push(order[i]);
} else if (tempConv.length === 0 && i + 1 === order[i]) {
truck.push(order[i]);
} else {
return truck.length;
}
}
return truck.length;
}
8 changes: 8 additions & 0 deletions jiyeeeah/[week2]Stack/reverse_string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function solution(my_string) {
var answer = "";
const str = my_string.split("");
for (let i = 0; i < my_string.length; i++) {
answer += str.pop();
}
return answer;
}
48 changes: 48 additions & 0 deletions jiyeeeah/[week2]Stack/rotate_parentheses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
function isValidParentheses(pars) {
const stack = [];
for (const p of pars) {
if (p === "(" || p === "[" || p == "{") {
stack.push(p);
} else {
if (stack.length === 0) return false;

const top = stack[stack.length - 1];
if (p === ")" && top === "(") stack.pop();
else if (p === "]" && top === "[") stack.pop();
else if (p === "}" && top === "{") stack.pop();
}
}
return stack.length === 0;
}

function solution(s) {
let answer = 0;
const N = s.length;

for (let i = 0; i < N; i++) {
let isValid = true;
const stack = [];
for (let j = 0; j < N; j++) {
const p = s[(i + j) % N];
if (p === "(" || p === "[" || p == "{") {
stack.push(p);
} else {
if (stack.length === 0) {
isValid = false;
break;
}

const top = stack[stack.length - 1];
if (p === ")" && top === "(") stack.pop();
else if (p === "]" && top === "[") stack.pop();
else if (p === "}" && top === "{") stack.pop();
else {
isValid = false;
break;
}
}
}
if (isValid && stack.length === 0) answer++;
}
return answer;
}
22 changes: 22 additions & 0 deletions jiyeeeah/[week2]Stack/stock_price.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function solution(prices) {
const N = prices.length;
const answer = new Array(N).fill(0);

const stack = [0];
for (let i = 1; i < N; i++) {
for (let j = stack.length - 1; j >= 0; j--) {
if (prices[stack[j]] > prices[i]) {
answer[stack[j]] = i - stack[j];
stack.pop();
} else {
break;
}
}
stack.push(i);
}
const last = stack.pop();
for (const s of stack) {
answer[s] = last - s;
}
return answer;
}
49 changes: 49 additions & 0 deletions jiyeeeah/[week2]Stack/스택.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
## 스택의 ADT

> ADT는 우리말로 추상 자료형 (Abstract Data Type)
>
> 추상 자료형 : 인터페이스만 있고 실제로 구현은 되지 않은 자료형 (일종의 자료형의 설계도)

| 연산 | `boolean isFull()` | 스택에 들어 있는 테이터 개수가 maxsize인지 확인해 boolean 값을 반환 |
| ---- | -------------------------- | ------------------------------------------------------------------- |
| | `boolean isEmpty()` | 스택에 들어 있는 테이터가 하나도 없는지 확인해 boolean 값을 반환 |
| | `void push(ItemType item)` | 스택에 데이터를 푸시 |
| | `ItemType pop()` | 스택에서 최근에 푸시한 테이터를 팝하고, 그 데이터를 반환 |
| 상태 | `Int top` | 스택에서 최근에 푸시한 데이터의 위치를 기록 |
| | `ItemType data[maxsize]` | 스택의 데이터를 관리하는 배열. 최대 maxsize개의 데이터를 관리 |

- `data`배열의 최대 크기는 `maxsize` ⇒ 인덱스의 범위는 `0`부터 `maxsize-1`
- 아무 데이터도 없을 때 `top`은 `-1`
- `top`이 `0`이면 데이터가 `1`개 있다는 것

## 스택의 세부 동작

- `push(3)`
1. `isFull()`을 우선 수행 ⇒ `data` 배열에 데이터가 가득 찼는지 확인
2. 그렇지 않다면 `top`을 1만큼 증가시킨 후 (`top = -1` → `top = 0`)
3. `top`이 가리키는 위치 `data[0]`에 3을 추가
- `pop()`
1. `isEmpty()`를 우선 수행 ⇒ `data` 배열에 데이터가 없는건 아닌지 확인
2. 데이터가 있다면 `top`을 1만큼 감소시키고
3. 데이터 3을 반환

## 스택 구현하기

자바스크립트의 내장 메서드 사용

```tsx
const stack = []; // 스택 초기화

// 스택에 데이터 추가
stack.push(1);
stack.push(2);
stack.push(3);

//스택에서 데이터 꺼냄
const topElement = stack.pop(); // 3
const nextElement = stack.pop(); // 2

const stackSize = stack.length;
```

> 스택을 몰라서 풀지 못하는 것이 아니라 ‘이 문제는 스택을 활용해야 풀 수 있다’라는 생각 자체를 못해서 풀지 못하는 경우가 대부분입니다. **따라서 스택 관련 문제를 많이 풀어보며 ‘이 문제는 스택을 사용하는 게 좋겠다’라는 감을 익히기를 권합니다.**
Loading