File tree Expand file tree Collapse file tree 5 files changed +176
-0
lines changed
best-time-to-buy-and-sell-stock
encode-and-decode-strings
implement-trie-prefix-tree Expand file tree Collapse file tree 5 files changed +176
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 문제 유형
3
+ * - Array
4
+ *
5
+ * 문제 설명
6
+ * - 주식을 가장 싸게 사서 비싸게 팔수 있는 경우 찾기
7
+ *
8
+ * 아이디어
9
+ * 1) 최소값을 찾고 그 이후의 값 중 최대값을 찾는다.
10
+ *
11
+ */
12
+ function maxProfit ( prices : number [ ] ) : number {
13
+ let min = prices [ 0 ] ;
14
+ let maxProfit = 0 ;
15
+
16
+ for ( let i = 1 ; i < prices . length ; i ++ ) {
17
+ min = Math . min ( min , prices [ i ] ) ;
18
+ maxProfit = Math . max ( prices [ i ] - min , maxProfit ) ;
19
+ }
20
+ return maxProfit ;
21
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 문제 유형
3
+ * - String
4
+ *
5
+ * 문제 설명
6
+ * - 문자열 인코딩과 디코딩
7
+ *
8
+ * 아이디어
9
+ * 1) "길이 + # + 문자열" 형태로 인코딩
10
+ *
11
+ */
12
+ class Solution {
13
+ /**
14
+ * @param {string[] } strs
15
+ * @returns {string }
16
+ */
17
+ encode ( strs ) {
18
+ return strs . map ( ( str ) => `${ str . length } #${ str } ` ) . join ( "" ) ;
19
+ }
20
+
21
+ /**
22
+ * @param {string } str
23
+ * @returns {string[] }
24
+ */
25
+ decode ( str ) {
26
+ const result = [ ] ;
27
+ let tempStr = str ;
28
+ while ( tempStr . length ) {
29
+ let i = 0 ;
30
+
31
+ while ( tempStr [ i ] !== "#" ) {
32
+ i ++ ;
33
+ }
34
+
35
+ const length = Number ( tempStr . slice ( 0 , i ) ) ;
36
+ const currentStr = tempStr . slice ( i + 1 , i + 1 + length ) ;
37
+ result . push ( currentStr ) ;
38
+ tempStr = tempStr . slice ( length + i + 1 ) ;
39
+ }
40
+ return result ;
41
+ }
42
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 문제 유형
3
+ * - String
4
+ *
5
+ * 문제 설명
6
+ * - 애너그램 그룹화
7
+ *
8
+ * 아이디어
9
+ * 1) 각 문자열을 정렬하여 키로 사용하고 그 키에 해당하는 문자열 배열을 만들어 리턴
10
+ *
11
+ */
12
+ function groupAnagrams ( strs : string [ ] ) : string [ ] [ ] {
13
+ const map = new Map < string , string [ ] > ( ) ;
14
+
15
+ for ( let i = 0 ; i < strs . length ; i ++ ) {
16
+ const key = strs [ i ] . split ( "" ) . sort ( ) . join ( "" ) ;
17
+ map . set ( key , [ ...( map . get ( key ) ?? [ ] ) , strs [ i ] ] ) ;
18
+ }
19
+ return [ ...map . values ( ) ] ;
20
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 문제 유형
3
+ * - Trie 구현 (문자열 검색)
4
+ *
5
+ * 문제 설명
6
+ * - 문자열 검색/추천/자동완성에서 자주 사용하는 자료구조 Trie 구현
7
+ *
8
+ * 아이디어
9
+ * 1) 문자열의 각 문자를 TrieNode 클래스의 인스턴스로 표현s
10
+ *
11
+ */
12
+ class TrieNode {
13
+ children : Map < string , TrieNode > ;
14
+ isEnd : boolean ;
15
+
16
+ constructor ( ) {
17
+ this . children = new Map ( ) ;
18
+ this . isEnd = false ;
19
+ }
20
+ }
21
+
22
+ class Trie {
23
+ root : TrieNode ;
24
+ constructor ( ) {
25
+ this . root = new TrieNode ( ) ;
26
+ }
27
+
28
+ insert ( word : string ) : void {
29
+ let node = this . root ;
30
+ for ( const char of word ) {
31
+ if ( ! node . children . has ( char ) ) {
32
+ node . children . set ( char , new TrieNode ( ) ) ;
33
+ }
34
+ node = node . children . get ( char ) ! ;
35
+ }
36
+ node . isEnd = true ;
37
+ }
38
+
39
+ // isEnd 까지 확인이 필요
40
+ search ( word : string ) : boolean {
41
+ const node = this . _findNode ( word ) ;
42
+ return node !== null && node . isEnd ;
43
+ }
44
+
45
+ // isEnd까지 확인 필요 없고 존재 여부만 확인 필요
46
+ startsWith ( prefix : string ) : boolean {
47
+ return this . _findNode ( prefix ) !== null ;
48
+ }
49
+
50
+ private _findNode ( word : string ) : TrieNode | null {
51
+ let node = this . root ;
52
+ for ( const char of word ) {
53
+ if ( ! node . children . get ( char ) ) return null ;
54
+ node = node . children . get ( char ) ! ;
55
+ }
56
+
57
+ return node ;
58
+ }
59
+ }
60
+
61
+ /**
62
+ * Your Trie object will be instantiated and called as such:
63
+ * var obj = new Trie()
64
+ * obj.insert(word)
65
+ * var param_2 = obj.search(word)
66
+ * var param_3 = obj.startsWith(prefix)
67
+ */
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 유형
3
+ * - dp (뒤쪽 단어를 쪼갤 수 있어야 전체를 쪼갤 수 있다.)
4
+ *
5
+ * 문제 설명
6
+ * - 문자열 s를 주어진 wordDict에 있는 단어로 쪼갤 수 있는가?
7
+ *
8
+ * 아이디어
9
+ * - dp[i] = s[i]로 시작하는 문자열이 wordDict에 있는 단어로 쪼갤 수 있는지 여부
10
+ */
11
+
12
+ function wordBreak ( s : string , wordDict : string [ ] ) : boolean {
13
+ const dp = new Array ( s . length + 1 ) . fill ( false ) ;
14
+
15
+ dp [ s . length ] = true ;
16
+
17
+ for ( let i = s . length - 1 ; i >= 0 ; i -- ) {
18
+ for ( const word of wordDict ) {
19
+ if ( i + word . length <= s . length && s . slice ( i , i + word . length ) === word ) {
20
+ dp [ i ] = dp [ i + word . length ] ;
21
+ }
22
+ if ( dp [ i ] ) break ;
23
+ }
24
+ }
25
+ return dp [ 0 ] ;
26
+ }
You can’t perform that action at this time.
0 commit comments