File tree Expand file tree Collapse file tree 3 files changed +120
-0
lines changed
container-with-most-water
design-add-and-search-words-data-structure Expand file tree Collapse file tree 3 files changed +120
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ 시간복잡도: O(n)
3+ 공간복잡도: O(1)
4+ 포인터까지는 생각했으나, 그이후로는 해결법이 떠오르지 않아, 알고달레 풀이를 보고 해결.
5+
6+ */
7+ function maxArea ( height : number [ ] ) : number {
8+ let maxArea = 0 ;
9+ let left = 0 ;
10+ let right = height . length - 1 ;
11+
12+ while ( left < right ) {
13+ let area = ( right - left ) * Math . min ( height [ left ] , height [ right ] ) ;
14+
15+ if ( maxArea < area ) maxArea = area ;
16+
17+ if ( height [ left ] > height [ right ] ) {
18+ right -= 1 ;
19+ } else {
20+ left += 1 ;
21+ }
22+ }
23+ return maxArea ;
24+ }
Original file line number Diff line number Diff line change 1+ /*
2+ 시간복잡도: O(L)
3+ 공간복잡도: O(L)
4+ Trie는 검색을 빠르게 해주지만,
5+ . 와일드카드는 DFS 분기를 유발해 최악의 경우 시간 복잡도가 커질 수 있다.
6+
7+ DFS가 필요한 검색 문제에 대한 공부 필요.
8+ */
9+ class TrieNode {
10+ children : Map < string , TrieNode > ;
11+ isEnd : boolean ;
12+
13+ constructor ( ) {
14+ this . children = new Map ( ) ;
15+ this . isEnd = false ;
16+ }
17+ }
18+
19+ class WordDictionary {
20+ private root : TrieNode ;
21+
22+ constructor ( ) {
23+ this . root = new TrieNode ( ) ;
24+ }
25+
26+ addWord ( word : string ) : void {
27+ let node = this . root ;
28+
29+ for ( const ch of word ) {
30+ if ( ! node . children . has ( ch ) ) {
31+ node . children . set ( ch , new TrieNode ( ) ) ;
32+ }
33+ node = node . children . get ( ch ) ! ;
34+ }
35+ node . isEnd = true ;
36+ }
37+
38+ search ( word : string ) : boolean {
39+ const dfs = ( index : number , node : TrieNode ) : boolean => {
40+ if ( index === word . length ) {
41+ return node . isEnd ;
42+ }
43+
44+ const ch = word [ index ] ;
45+
46+ if ( ch === '.' ) {
47+ for ( const child of node . children . values ( ) ) {
48+ if ( dfs ( index + 1 , child ) ) return true ;
49+ }
50+ return false ;
51+ }
52+
53+ const next = node . children . get ( ch ) ;
54+ if ( ! next ) return false ;
55+
56+ return dfs ( index + 1 , next ) ;
57+ } ;
58+
59+ return dfs ( 0 , this . root ) ;
60+ }
61+ }
Original file line number Diff line number Diff line change 1+ /*
2+ 시간복잡도: O(n)
3+ 공간복잡도: O(n)
4+ */
5+ function isValid ( s : string ) : boolean {
6+ if ( s . length % 2 !== 0 ) {
7+ return false ;
8+ }
9+
10+ const stack : string [ ] = [ ] ;
11+ const pair : Map < string , string > = new Map < string , string > ( [
12+ [ ')' , '(' ] ,
13+ [ '}' , '{' ] ,
14+ [ ']' , '[' ] ,
15+ ] ) ;
16+
17+ for ( const char of s ) {
18+ // close brackts인지 확인
19+ if ( pair . has ( char ) ) {
20+ let topItem = stack . pop ( ) ;
21+ // pair 확인
22+ if ( topItem !== pair . get ( char ) ) {
23+ return false ;
24+ }
25+ } else {
26+ stack . push ( char ) ;
27+ }
28+ }
29+
30+ if ( stack . length === 0 ) {
31+ return true ;
32+ } else {
33+ return false ;
34+ }
35+ }
You can’t perform that action at this time.
0 commit comments