File tree Expand file tree Collapse file tree 10 files changed +252
-0
lines changed
Expand file tree Collapse file tree 10 files changed +252
-0
lines changed Original file line number Diff line number Diff line change 1+ function solution ( my_string ) {
2+ const expression = my_string . split ( " " ) ;
3+ const stack = [ Number ( expression [ 0 ] ) ] ;
4+ for ( let i = 1 ; i < expression . length ; i ++ ) {
5+ if ( expression [ i ] === "+" || expression [ i ] === "-" ) {
6+ stack . push ( expression [ i ] ) ;
7+ } else {
8+ const operator = stack . pop ( ) ;
9+ const a = stack . pop ( ) ;
10+ let result ;
11+ if ( operator === "+" ) result = a + Number ( expression [ i ] ) ;
12+ else result = a - Number ( expression [ i ] ) ;
13+ stack . push ( result ) ;
14+ }
15+ }
16+ return stack [ 0 ] ;
17+ }
Original file line number Diff line number Diff line change 1+ function solution ( s ) {
2+ const cmd = s . split ( " " ) ;
3+ const stack = [ ] ;
4+ for ( const c of cmd ) {
5+ if ( c === "Z" ) {
6+ stack . pop ( ) ;
7+ } else {
8+ stack . push ( c ) ;
9+ }
10+ }
11+ const answer = stack . reduce ( ( acc , value ) => acc + Number ( value ) , 0 ) ;
12+ return answer ;
13+ }
Original file line number Diff line number Diff line change 1+ function solution ( s ) {
2+ const stack = [ ] ;
3+ for ( const par of s ) {
4+ if ( par === "(" ) {
5+ stack . push ( par ) ;
6+ } else {
7+ if ( stack . length === 0 ) return false ;
8+ else stack . pop ( ) ;
9+ }
10+ }
11+ return stack . length === 0 ;
12+ }
Original file line number Diff line number Diff line change 1+ function solution ( board , moves ) {
2+ let answer = 0 ;
3+ const N = board . length ;
4+
5+ const stack = [ ] ;
6+ for ( const move of moves ) {
7+ for ( let i = 0 ; i < N ; i ++ ) {
8+ if ( board [ i ] [ move - 1 ] === 0 ) continue ;
9+
10+ const item = board [ i ] [ move - 1 ] ;
11+ if ( stack . length > 0 && stack [ stack . length - 1 ] === item ) {
12+ stack . pop ( ) ;
13+ answer += 2 ;
14+ } else {
15+ stack . push ( item ) ;
16+ }
17+ board [ i ] [ move - 1 ] = 0 ;
18+ break ;
19+ }
20+ }
21+ return answer ;
22+ }
Original file line number Diff line number Diff line change 1+ function solution ( dartResult ) {
2+ const stack = [ ] ;
3+ for ( const c of dartResult ) {
4+ const topIndex = stack . length - 1 ;
5+ if ( c === "S" ) stack [ topIndex ] = stack [ topIndex ] ** 1 ;
6+ else if ( c === "D" ) stack [ topIndex ] = stack [ topIndex ] ** 2 ;
7+ else if ( c === "T" ) stack [ topIndex ] = stack [ topIndex ] ** 3 ;
8+ else if ( c === "*" ) {
9+ stack [ topIndex ] *= 2 ;
10+ if ( topIndex - 1 >= 0 ) stack [ topIndex - 1 ] *= 2 ;
11+ } else if ( c === "#" ) {
12+ stack [ topIndex ] *= - 1 ;
13+ } else {
14+ if ( c === "0" && stack [ topIndex ] === 1 ) {
15+ stack [ topIndex ] = 10 ;
16+ } else {
17+ stack . push ( Number ( c ) ) ;
18+ }
19+ }
20+ }
21+ return stack . reduce ( ( acc , val ) => acc + val , 0 ) ;
22+ }
Original file line number Diff line number Diff line change 1+ function solution ( order ) {
2+ let answer = 0 ;
3+ const tempConv = [ ] ;
4+ let boxIndex = 1 ;
5+ for ( let i = 0 ; i < order . length ; i ++ ) {
6+ while ( boxIndex <= order [ i ] ) {
7+ tempConv . push ( boxIndex ++ ) ;
8+ }
9+ const top = tempConv [ tempConv . length - 1 ] ;
10+ if ( top !== order [ i ] ) break ;
11+ tempConv . pop ( ) ;
12+ answer ++ ;
13+ }
14+ return answer ;
15+ }
16+
17+ // 틀린 답변입니다.
18+ function solution ( order ) {
19+ const tempConv = [ ] ;
20+ const truck = [ ] ;
21+ for ( let i = 0 ; i < order . length ; i ++ ) {
22+ const topTempConv = tempConv [ tempConv . length - 1 ] ;
23+ if ( tempConv . length > 0 && topTempConv === order [ i ] ) {
24+ tempConv . pop ( ) ;
25+ truck . push ( order [ i ] ) ;
26+ } else if ( ! tempConv . includes ( order [ i ] ) ) {
27+ const startNum = i - 1 >= 0 ? order [ i - 1 ] + 1 : 1 ;
28+ for ( let j = startNum ; j < order [ i ] ; j ++ ) {
29+ tempConv . push ( j ) ;
30+ }
31+ truck . push ( order [ i ] ) ;
32+ } else if ( tempConv . length === 0 && i + 1 === order [ i ] ) {
33+ truck . push ( order [ i ] ) ;
34+ } else {
35+ return truck . length ;
36+ }
37+ }
38+ return truck . length ;
39+ }
Original file line number Diff line number Diff line change 1+ function solution ( my_string ) {
2+ var answer = "" ;
3+ const str = my_string . split ( "" ) ;
4+ for ( let i = 0 ; i < my_string . length ; i ++ ) {
5+ answer += str . pop ( ) ;
6+ }
7+ return answer ;
8+ }
Original file line number Diff line number Diff line change 1+ function isValidParentheses ( pars ) {
2+ const stack = [ ] ;
3+ for ( const p of pars ) {
4+ if ( p === "(" || p === "[" || p == "{" ) {
5+ stack . push ( p ) ;
6+ } else {
7+ if ( stack . length === 0 ) return false ;
8+
9+ const top = stack [ stack . length - 1 ] ;
10+ if ( p === ")" && top === "(" ) stack . pop ( ) ;
11+ else if ( p === "]" && top === "[" ) stack . pop ( ) ;
12+ else if ( p === "}" && top === "{" ) stack . pop ( ) ;
13+ }
14+ }
15+ return stack . length === 0 ;
16+ }
17+
18+ function solution ( s ) {
19+ let answer = 0 ;
20+ const N = s . length ;
21+
22+ for ( let i = 0 ; i < N ; i ++ ) {
23+ let isValid = true ;
24+ const stack = [ ] ;
25+ for ( let j = 0 ; j < N ; j ++ ) {
26+ const p = s [ ( i + j ) % N ] ;
27+ if ( p === "(" || p === "[" || p == "{" ) {
28+ stack . push ( p ) ;
29+ } else {
30+ if ( stack . length === 0 ) {
31+ isValid = false ;
32+ break ;
33+ }
34+
35+ const top = stack [ stack . length - 1 ] ;
36+ if ( p === ")" && top === "(" ) stack . pop ( ) ;
37+ else if ( p === "]" && top === "[" ) stack . pop ( ) ;
38+ else if ( p === "}" && top === "{" ) stack . pop ( ) ;
39+ else {
40+ isValid = false ;
41+ break ;
42+ }
43+ }
44+ }
45+ if ( isValid && stack . length === 0 ) answer ++ ;
46+ }
47+ return answer ;
48+ }
Original file line number Diff line number Diff line change 1+ function solution ( prices ) {
2+ const N = prices . length ;
3+ const answer = new Array ( N ) . fill ( 0 ) ;
4+
5+ const stack = [ 0 ] ;
6+ for ( let i = 1 ; i < N ; i ++ ) {
7+ for ( let j = stack . length - 1 ; j >= 0 ; j -- ) {
8+ if ( prices [ stack [ j ] ] > prices [ i ] ) {
9+ answer [ stack [ j ] ] = i - stack [ j ] ;
10+ stack . pop ( ) ;
11+ } else {
12+ break ;
13+ }
14+ }
15+ stack . push ( i ) ;
16+ }
17+ const last = stack . pop ( ) ;
18+ for ( const s of stack ) {
19+ answer [ s ] = last - s ;
20+ }
21+ return answer ;
22+ }
Original file line number Diff line number Diff line change 1+ ## 스택의 ADT
2+
3+ > ADT는 우리말로 추상 자료형 (Abstract Data Type)
4+ >
5+ > 추상 자료형 : 인터페이스만 있고 실제로 구현은 되지 않은 자료형 (일종의 자료형의 설계도)
6+
7+ | 연산 | ` boolean isFull() ` | 스택에 들어 있는 테이터 개수가 maxsize인지 확인해 boolean 값을 반환 |
8+ | ---- | -------------------------- | ------------------------------------------------------------------- |
9+ | | ` boolean isEmpty() ` | 스택에 들어 있는 테이터가 하나도 없는지 확인해 boolean 값을 반환 |
10+ | | ` void push(ItemType item) ` | 스택에 데이터를 푸시 |
11+ | | ` ItemType pop() ` | 스택에서 최근에 푸시한 테이터를 팝하고, 그 데이터를 반환 |
12+ | 상태 | ` Int top ` | 스택에서 최근에 푸시한 데이터의 위치를 기록 |
13+ | | ` ItemType data[maxsize] ` | 스택의 데이터를 관리하는 배열. 최대 maxsize개의 데이터를 관리 |
14+
15+ - ` data ` 배열의 최대 크기는 ` maxsize ` ⇒ 인덱스의 범위는 ` 0 ` 부터 ` maxsize-1 `
16+ - 아무 데이터도 없을 때 ` top ` 은 ` -1 `
17+ - ` top ` 이 ` 0 ` 이면 데이터가 ` 1 ` 개 있다는 것
18+
19+ ## 스택의 세부 동작
20+
21+ - ` push(3) `
22+ 1 . ` isFull() ` 을 우선 수행 ⇒ ` data ` 배열에 데이터가 가득 찼는지 확인
23+ 2 . 그렇지 않다면 ` top ` 을 1만큼 증가시킨 후 (` top = -1 ` → ` top = 0 ` )
24+ 3 . ` top ` 이 가리키는 위치 ` data[0] ` 에 3을 추가
25+ - ` pop() `
26+ 1 . ` isEmpty() ` 를 우선 수행 ⇒ ` data ` 배열에 데이터가 없는건 아닌지 확인
27+ 2 . 데이터가 있다면 ` top ` 을 1만큼 감소시키고
28+ 3 . 데이터 3을 반환
29+
30+ ## 스택 구현하기
31+
32+ 자바스크립트의 내장 메서드 사용
33+
34+ ``` tsx
35+ const stack = []; // 스택 초기화
36+
37+ // 스택에 데이터 추가
38+ stack .push (1 );
39+ stack .push (2 );
40+ stack .push (3 );
41+
42+ // 스택에서 데이터 꺼냄
43+ const topElement = stack .pop (); // 3
44+ const nextElement = stack .pop (); // 2
45+
46+ const stackSize = stack .length ;
47+ ```
48+
49+ > 스택을 몰라서 풀지 못하는 것이 아니라 ‘이 문제는 스택을 활용해야 풀 수 있다’라는 생각 자체를 못해서 풀지 못하는 경우가 대부분입니다. ** 따라서 스택 관련 문제를 많이 풀어보며 ‘이 문제는 스택을 사용하는 게 좋겠다’라는 감을 익히기를 권합니다.**
You can’t perform that action at this time.
0 commit comments